reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofVec3f.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofVec2f.h"
4#include "ofVec4f.h"
5#include "ofMathConstants.h"
6#include "ofConstants.h"
7#include "glm/vec3.hpp"
8#include <cmath>
9#include <iostream>
10
79class ofVec3f {
80public:
82 static const int DIM = 3;
84
86 float x;
87
89 float y;
90
92 float z;
93
94 //---------------------
97
111
113 ofVec3f( float x, float y, float z=0 );
114
116 explicit ofVec3f( float scalar );
117
118 ofVec3f( const ofVec2f& vec );
119
127 ofVec3f( const ofVec4f& vec );
128
129 ofVec3f( const glm::vec2 & vec );
130 ofVec3f( const glm::vec3 & vec );
131 ofVec3f( const glm::vec4 & vec );
132 operator glm::vec3() const;
133
135
136 //---------------------
139
140
156 float * getPtr() {
157 return (float*)&x;
158 }
159 const float * getPtr() const {
160 return (const float *)&x;
161 }
162
163
177 float& operator[]( int n ){
178 return getPtr()[n];
179 }
180
181 float operator[]( int n ) const {
182 return getPtr()[n];
183 }
184
185
193 void set( float x, float y, float z = 0 );
194
195
204 void set( const ofVec3f& vec );
205 void set( float _scalar );
206
208
209 //---------------------
212
226 bool operator==( const ofVec3f& vec ) const;
227
239 bool operator!=( const ofVec3f& vec ) const;
240
251 bool match( const ofVec3f& vec, float tolerance = 0.0001f ) const;
255 bool isAligned( const ofVec3f& vec, float tolerance = 0.0001f ) const;
256 bool isAlignedRad( const ofVec3f& vec, float tolerance = 0.0001f ) const;
257
267 bool align( const ofVec3f& vec, float tolerance = 0.0001f ) const;
268
278 bool alignRad( const ofVec3f& vec, float tolerance = 0.0001f ) const;
279
280
282
283 //---------------------
286
295 ofVec3f operator+( const ofVec3f& pnt ) const;
296
304
305 ofVec3f operator+( const float f ) const;
306
315 ofVec3f& operator+=( const ofVec3f& pnt );
316
323 ofVec3f& operator+=( const float f );
324
333 ofVec3f operator-( const ofVec3f& vec ) const;
334
335
336
344 ofVec3f operator-( const float f ) const;
345
355
364 ofVec3f& operator-=( const ofVec3f& vec );
365
372 ofVec3f& operator-=( const float f );
373
384 ofVec3f operator*( const ofVec3f& vec ) const;
385
393 ofVec3f operator*( const float f ) const;
394
405 ofVec3f& operator*=( const ofVec3f& vec );
406
413 ofVec3f& operator*=( const float f );
414
424 ofVec3f operator/( const ofVec3f& vec ) const;
425
433 ofVec3f operator/( const float f ) const;
434
444 ofVec3f& operator/=( const ofVec3f& vec );
445
452 ofVec3f& operator/=( const float f );
453
455 friend std::ostream& operator<<(std::ostream& os, const ofVec3f& vec);
456 friend std::istream& operator>>(std::istream& is, ofVec3f& vec);
458
460
461 //---------------------
464
473 ofVec3f getScaled( const float length ) const;
474
475
482 ofVec3f& scale( const float length );
483
484
494 ofVec3f getRotated( float angle, const ofVec3f& axis ) const;
495
508 ofVec3f getRotated(float ax, float ay, float az) const;
509
513 ofVec3f getRotated( float angle, const ofVec3f& pivot, const ofVec3f& axis ) const;
514
515
524 ofVec3f getRotatedRad( float angle, const ofVec3f& axis ) const;
525
538 ofVec3f getRotatedRad(float ax, float ay, float az) const;
539
543 ofVec3f getRotatedRad( float angle, const ofVec3f& pivot, const ofVec3f& axis ) const;
544
553 ofVec3f& rotate( float angle, const ofVec3f& axis );
554
567 ofVec3f& rotate(float ax, float ay, float az);
568
571 ofVec3f& rotate( float angle, const ofVec3f& pivot, const ofVec3f& axis );
572
573
582 ofVec3f& rotateRad( float angle, const ofVec3f& axis );
583
595 ofVec3f& rotateRad(float ax, float ay, float az);
596
599 ofVec3f& rotateRad( float angle, const ofVec3f& pivot, const ofVec3f& axis );
600
601
602
603
615 ofVec3f getMapped( const ofVec3f& origin,
616 const ofVec3f& vx,
617 const ofVec3f& vy,
618 const ofVec3f& vz ) const;
619
630 ofVec3f& map( const ofVec3f& origin,
631 const ofVec3f& vx,
632 const ofVec3f& vy,
633 const ofVec3f& vz );
634
635
636
638
639
640 //---------------------
643
644
660 float distance( const ofVec3f& pnt) const;
661
678 float squareDistance( const ofVec3f& pnt ) const;
679
680
682
683 //---------------------
686
701 ofVec3f getInterpolated( const ofVec3f& pnt, float p ) const;
702
719 ofVec3f& interpolate( const ofVec3f& pnt, float p );
720
728 ofVec3f getMiddle( const ofVec3f& pnt ) const;
729
741 ofVec3f& middle( const ofVec3f& pnt );
742
757 ofVec3f& average( const ofVec3f* points, int num );
758
760
761 //---------------------
764
779
795
796
808 ofVec3f getLimited(float max) const;
809
821 ofVec3f& limit(float max);
822
823
825
826 //---------------------
829
830
845 float length() const;
846
860 float lengthSquared() const;
861
870 float angle( const ofVec3f& vec ) const;
871
881 float angleRad( const ofVec3f& vec ) const;
882
883
885
886 //---------------------
889
890
907 ofVec3f getPerpendicular( const ofVec3f& vec ) const;
908
909
927
928
929
940 ofVec3f getCrossed( const ofVec3f& vec ) const;
941
950 ofVec3f& cross( const ofVec3f& vec );
951
952
953
954
985 float dot( const ofVec3f& vec ) const;
986
988
989
990
991 //-----------------------------------------------
992 // this methods are deprecated in 006 please use:
994
995 // getScaled
996 OF_DEPRECATED_MSG("Use member method getScaled() instead.", ofVec3f rescaled( const float length ) const);
997
998 // scale
999 OF_DEPRECATED_MSG("Use member method scale() instead.", ofVec3f& rescale( const float length ));
1000
1001 // getRotated
1002 OF_DEPRECATED_MSG("Use member method getRotated() instead.", ofVec3f rotated( float angle, const ofVec3f& axis ) const);
1003
1004 // getRotated should this be const???
1005 OF_DEPRECATED_MSG("Use member method getRotated() instead.", ofVec3f rotated(float ax, float ay, float az));
1006
1007 // getNormalized
1008 OF_DEPRECATED_MSG("Use member method getNormalized() instead.", ofVec3f normalized() const);
1009
1010 // getLimited
1011 OF_DEPRECATED_MSG("Use member method getLimited() instead.", ofVec3f limited(float max) const);
1012
1013 // getCrossed
1014 OF_DEPRECATED_MSG("Use member method getCrossed() instead.", ofVec3f crossed( const ofVec3f& vec ) const);
1015
1016 // getPerpendicular
1017 OF_DEPRECATED_MSG("Use member method getPerpendicular() instead.", ofVec3f perpendiculared( const ofVec3f& vec ) const);
1018
1019 // use getMapped
1020 OF_DEPRECATED_MSG("Use member method getMapped() instead.", ofVec3f mapped( const ofVec3f& origin,
1021 const ofVec3f& vx,
1022 const ofVec3f& vy,
1023 const ofVec3f& vz ) const);
1024
1025 // use squareDistance
1026 OF_DEPRECATED_MSG("Use member method squareDistance() instead.", float distanceSquared( const ofVec3f& pnt ) const);
1027
1028 // use getInterpolated
1029 OF_DEPRECATED_MSG("Use member method getInterpolated() instead.", ofVec3f interpolated( const ofVec3f& pnt, float p ) const);
1030
1031 // use getMiddle
1032 OF_DEPRECATED_MSG("Use member method getMiddle() instead.", ofVec3f middled( const ofVec3f& pnt ) const);
1033
1034 // use getRotated
1035 OF_DEPRECATED_MSG("Use member method getRotated() instead.", ofVec3f rotated( float angle,
1036 const ofVec3f& pivot,
1037 const ofVec3f& axis ) const);
1038
1039 // return all zero vector
1040 static ofVec3f zero() { return ofVec3f(0, 0, 0); }
1041
1042 // return all one vector
1043 static ofVec3f one() { return ofVec3f(1, 1, 1); }
1044
1046
1047};
1048
1049
1051
1052
1053// Non-Member operators
1054//
1055//
1056ofVec3f operator+( float f, const ofVec3f& vec );
1057ofVec3f operator-( float f, const ofVec3f& vec );
1058ofVec3f operator*( float f, const ofVec3f& vec );
1059ofVec3f operator/( float f, const ofVec3f& vec );
1060
1061
1063// Implementation
1065
1066
1067inline ofVec3f::ofVec3f( const ofVec2f& vec ):x(vec.x), y(vec.y), z(0) {}
1068inline ofVec3f::ofVec3f( const ofVec4f& vec ):x(vec.x), y(vec.y), z(vec.z) {}
1069inline ofVec3f::ofVec3f(): x(0), y(0), z(0) {}
1070inline ofVec3f::ofVec3f( float _all ): x(_all), y(_all), z(_all) {}
1071inline ofVec3f::ofVec3f( float _x, float _y, float _z ):x(_x), y(_y), z(_z) {}
1072
1073inline ofVec3f::ofVec3f( const glm::vec2 & vec ):x(vec.x), y(vec.y), z(0.f){}
1074inline ofVec3f::ofVec3f( const glm::vec3 & vec ):x(vec.x), y(vec.y), z(vec.z){}
1075inline ofVec3f::ofVec3f( const glm::vec4 & vec ):x(vec.x), y(vec.y), z(vec.z){}
1076
1077inline ofVec3f::operator glm::vec3() const{
1078 return glm::vec3(x,y,z);
1079}
1080
1081
1082// Getters and Setters.
1083//
1084//
1085inline void ofVec3f::set( float _scalar ) {
1086 x = _scalar;
1087 y = _scalar;
1088 z = _scalar;
1089}
1090
1091inline void ofVec3f::set( float _x, float _y, float _z ) {
1092 x = _x;
1093 y = _y;
1094 z = _z;
1095}
1096
1097inline void ofVec3f::set( const ofVec3f& vec ) {
1098 x = vec.x;
1099 y = vec.y;
1100 z = vec.z;
1101}
1102
1103
1104// Check similarity/equality.
1105//
1106//
1107inline bool ofVec3f::operator==( const ofVec3f& vec ) const {
1108 return (x == vec.x) && (y == vec.y) && (z == vec.z);
1109}
1110
1111inline bool ofVec3f::operator!=( const ofVec3f& vec ) const {
1112 return (x != vec.x) || (y != vec.y) || (z != vec.z);
1113}
1114
1115inline bool ofVec3f::match( const ofVec3f& vec, float tolerance ) const{
1116 return (fabs(x - vec.x) < tolerance)
1117 && (fabs(y - vec.y) < tolerance)
1118 && (fabs(z - vec.z) < tolerance);
1119}
1120
1124inline bool ofVec3f::isAligned( const ofVec3f& vec, float tolerance ) const {
1125 float angle = this->angle( vec );
1126 return angle < tolerance;
1127}
1128inline bool ofVec3f::align( const ofVec3f& vec, float tolerance ) const {
1129 return isAligned( vec, tolerance );
1130}
1131
1132inline bool ofVec3f::isAlignedRad( const ofVec3f& vec, float tolerance ) const {
1133 float angle = this->angleRad( vec );
1134 return angle < tolerance;
1135}
1136inline bool ofVec3f::alignRad( const ofVec3f& vec, float tolerance ) const {
1137 return isAlignedRad( vec, tolerance );
1138}
1139
1140
1141// Operator overloading for ofVec3f
1142//
1143//
1144
1145inline std::ostream& operator<<(std::ostream& os, const ofVec3f& vec) {
1146 os << vec.x << ", " << vec.y << ", " << vec.z;
1147 return os;
1148}
1149
1150inline std::istream& operator>>(std::istream& is, ofVec3f& vec) {
1151 is >> vec.x;
1152 is.ignore(2);
1153 is >> vec.y;
1154 is.ignore(2);
1155 is >> vec.z;
1156 return is;
1157}
1158
1159inline ofVec3f ofVec3f::operator+( const ofVec3f& pnt ) const {
1160 return ofVec3f( x+pnt.x, y+pnt.y, z+pnt.z );
1161}
1162
1163inline ofVec3f& ofVec3f::operator+=( const ofVec3f& pnt ) {
1164 x+=pnt.x;
1165 y+=pnt.y;
1166 z+=pnt.z;
1167 return *this;
1168}
1169
1170inline ofVec3f ofVec3f::operator-( const ofVec3f& vec ) const {
1171 return ofVec3f( x-vec.x, y-vec.y, z-vec.z );
1172}
1173
1174inline ofVec3f& ofVec3f::operator-=( const ofVec3f& vec ) {
1175 x -= vec.x;
1176 y -= vec.y;
1177 z -= vec.z;
1178 return *this;
1179}
1180
1181inline ofVec3f ofVec3f::operator*( const ofVec3f& vec ) const {
1182 return ofVec3f( x*vec.x, y*vec.y, z*vec.z );
1183}
1184
1185inline ofVec3f& ofVec3f::operator*=( const ofVec3f& vec ) {
1186 x*=vec.x;
1187 y*=vec.y;
1188 z*=vec.z;
1189 return *this;
1190}
1191
1192inline ofVec3f ofVec3f::operator/( const ofVec3f& vec ) const {
1193 return ofVec3f( vec.x!=0 ? x/vec.x : x , vec.y!=0 ? y/vec.y : y, vec.z!=0 ? z/vec.z : z );
1194}
1195
1196inline ofVec3f& ofVec3f::operator/=( const ofVec3f& vec ) {
1197 vec.x!=0 ? x/=vec.x : x;
1198 vec.y!=0 ? y/=vec.y : y;
1199 vec.z!=0 ? z/=vec.z : z;
1200 return *this;
1201}
1202
1203inline ofVec3f ofVec3f::operator-() const {
1204 return ofVec3f( -x, -y, -z );
1205}
1206
1207
1208//operator overloading for float
1209//
1210//
1211//inline void ofVec3f::operator=( const float f){
1212// x = f;
1213// y = f;
1214// z = f;
1215//}
1216
1217inline ofVec3f ofVec3f::operator+( const float f ) const {
1218 return ofVec3f( x+f, y+f, z+f);
1219}
1220
1221inline ofVec3f& ofVec3f::operator+=( const float f ) {
1222 x += f;
1223 y += f;
1224 z += f;
1225 return *this;
1226}
1227
1228inline ofVec3f ofVec3f::operator-( const float f ) const {
1229 return ofVec3f( x-f, y-f, z-f);
1230}
1231
1232inline ofVec3f& ofVec3f::operator-=( const float f ) {
1233 x -= f;
1234 y -= f;
1235 z -= f;
1236 return *this;
1237}
1238
1239inline ofVec3f ofVec3f::operator*( const float f ) const {
1240 return ofVec3f( x*f, y*f, z*f );
1241}
1242
1243inline ofVec3f& ofVec3f::operator*=( const float f ) {
1244 x*=f;
1245 y*=f;
1246 z*=f;
1247 return *this;
1248}
1249
1250inline ofVec3f ofVec3f::operator/( const float f ) const {
1251 if(f == 0) return ofVec3f( x, y, z);
1252
1253 return ofVec3f( x/f, y/f, z/f );
1254}
1255
1256inline ofVec3f& ofVec3f::operator/=( const float f ) {
1257 if(f == 0) return *this;
1258
1259 x/=f;
1260 y/=f;
1261 z/=f;
1262 return *this;
1263}
1264
1265
1266//Scale
1267//
1268//
1269inline ofVec3f ofVec3f::rescaled( const float length ) const {
1270 return getScaled(length);
1271}
1272inline ofVec3f ofVec3f::getScaled( const float length ) const {
1273 float l = (float)sqrt(x*x + y*y + z*z);
1274 if( l > 0 )
1275 return ofVec3f( (x/l)*length, (y/l)*length, (z/l)*length );
1276 else
1277 return ofVec3f();
1278}
1279inline ofVec3f& ofVec3f::rescale( const float length ) {
1280 return scale(length);
1281}
1282inline ofVec3f& ofVec3f::scale( const float length ) {
1283 float l = (float)sqrt(x*x + y*y + z*z);
1284 if (l > 0) {
1285 x = (x/l)*length;
1286 y = (y/l)*length;
1287 z = (z/l)*length;
1288 }
1289 return *this;
1290}
1291
1292
1293
1294// Rotation
1295//
1296//
1297inline ofVec3f ofVec3f::rotated( float angle, const ofVec3f& axis ) const {
1298 return getRotated(angle, axis);
1299}
1300inline ofVec3f ofVec3f::getRotated( float angle, const ofVec3f& axis ) const {
1301 ofVec3f ax = axis.getNormalized();
1302 float a = (float)(ofDegToRad(angle));
1303 float sina = sin( a );
1304 float cosa = cos( a );
1305 float cosb = 1.0f - cosa;
1306
1307 return ofVec3f( x*(ax.x*ax.x*cosb + cosa)
1308 + y*(ax.x*ax.y*cosb - ax.z*sina)
1309 + z*(ax.x*ax.z*cosb + ax.y*sina),
1310 x*(ax.y*ax.x*cosb + ax.z*sina)
1311 + y*(ax.y*ax.y*cosb + cosa)
1312 + z*(ax.y*ax.z*cosb - ax.x*sina),
1313 x*(ax.z*ax.x*cosb - ax.y*sina)
1314 + y*(ax.z*ax.y*cosb + ax.x*sina)
1315 + z*(ax.z*ax.z*cosb + cosa) );
1316}
1317
1318inline ofVec3f ofVec3f::getRotatedRad( float angle, const ofVec3f& axis ) const {
1319 ofVec3f ax = axis.getNormalized();
1320 float a = angle;
1321 float sina = sin( a );
1322 float cosa = cos( a );
1323 float cosb = 1.0f - cosa;
1324
1325 return ofVec3f( x*(ax.x*ax.x*cosb + cosa)
1326 + y*(ax.x*ax.y*cosb - ax.z*sina)
1327 + z*(ax.x*ax.z*cosb + ax.y*sina),
1328 x*(ax.y*ax.x*cosb + ax.z*sina)
1329 + y*(ax.y*ax.y*cosb + cosa)
1330 + z*(ax.y*ax.z*cosb - ax.x*sina),
1331 x*(ax.z*ax.x*cosb - ax.y*sina)
1332 + y*(ax.z*ax.y*cosb + ax.x*sina)
1333 + z*(ax.z*ax.z*cosb + cosa) );
1334}
1335
1336inline ofVec3f& ofVec3f::rotate( float angle, const ofVec3f& axis ) {
1337 ofVec3f ax = axis.getNormalized();
1338 float a = (float)(ofDegToRad(angle));
1339 float sina = sin( a );
1340 float cosa = cos( a );
1341 float cosb = 1.0f - cosa;
1342
1343 float nx = x*(ax.x*ax.x*cosb + cosa)
1344 + y*(ax.x*ax.y*cosb - ax.z*sina)
1345 + z*(ax.x*ax.z*cosb + ax.y*sina);
1346 float ny = x*(ax.y*ax.x*cosb + ax.z*sina)
1347 + y*(ax.y*ax.y*cosb + cosa)
1348 + z*(ax.y*ax.z*cosb - ax.x*sina);
1349 float nz = x*(ax.z*ax.x*cosb - ax.y*sina)
1350 + y*(ax.z*ax.y*cosb + ax.x*sina)
1351 + z*(ax.z*ax.z*cosb + cosa);
1352 x = nx; y = ny; z = nz;
1353 return *this;
1354}
1355
1356
1357inline ofVec3f& ofVec3f::rotateRad(float angle, const ofVec3f& axis ) {
1358 ofVec3f ax = axis.getNormalized();
1359 float a = angle;
1360 float sina = sin( a );
1361 float cosa = cos( a );
1362 float cosb = 1.0f - cosa;
1363
1364 float nx = x*(ax.x*ax.x*cosb + cosa)
1365 + y*(ax.x*ax.y*cosb - ax.z*sina)
1366 + z*(ax.x*ax.z*cosb + ax.y*sina);
1367 float ny = x*(ax.y*ax.x*cosb + ax.z*sina)
1368 + y*(ax.y*ax.y*cosb + cosa)
1369 + z*(ax.y*ax.z*cosb - ax.x*sina);
1370 float nz = x*(ax.z*ax.x*cosb - ax.y*sina)
1371 + y*(ax.z*ax.y*cosb + ax.x*sina)
1372 + z*(ax.z*ax.z*cosb + cosa);
1373 x = nx; y = ny; z = nz;
1374 return *this;
1375}
1376
1377// const???
1378inline ofVec3f ofVec3f::rotated(float ax, float ay, float az) {
1379 return getRotated(ax,ay,az);
1380}
1381
1382inline ofVec3f ofVec3f::getRotated(float ax, float ay, float az) const {
1383 float a = (float)cos(ofDegToRad(ax));
1384 float b = (float)sin(ofDegToRad(ax));
1385 float c = (float)cos(ofDegToRad(ay));
1386 float d = (float)sin(ofDegToRad(ay));
1387 float e = (float)cos(ofDegToRad(az));
1388 float f = (float)sin(ofDegToRad(az));
1389
1390 float nx = c * e * x - c * f * y + d * z;
1391 float ny = (a * f + b * d * e) * x + (a * e - b * d * f) * y - b * c * z;
1392 float nz = (b * f - a * d * e) * x + (a * d * f + b * e) * y + a * c * z;
1393
1394 return ofVec3f( nx, ny, nz );
1395}
1396
1397inline ofVec3f ofVec3f::getRotatedRad(float ax, float ay, float az) const {
1398 float a = cos(ax);
1399 float b = sin(ax);
1400 float c = cos(ay);
1401 float d = sin(ay);
1402 float e = cos(az);
1403 float f = sin(az);
1404
1405 float nx = c * e * x - c * f * y + d * z;
1406 float ny = (a * f + b * d * e) * x + (a * e - b * d * f) * y - b * c * z;
1407 float nz = (b * f - a * d * e) * x + (a * d * f + b * e) * y + a * c * z;
1408
1409 return ofVec3f( nx, ny, nz );
1410}
1411
1412
1413inline ofVec3f& ofVec3f::rotate(float ax, float ay, float az) {
1414 float a = (float)cos(ofDegToRad(ax));
1415 float b = (float)sin(ofDegToRad(ax));
1416 float c = (float)cos(ofDegToRad(ay));
1417 float d = (float)sin(ofDegToRad(ay));
1418 float e = (float)cos(ofDegToRad(az));
1419 float f = (float)sin(ofDegToRad(az));
1420
1421 float nx = c * e * x - c * f * y + d * z;
1422 float ny = (a * f + b * d * e) * x + (a * e - b * d * f) * y - b * c * z;
1423 float nz = (b * f - a * d * e) * x + (a * d * f + b * e) * y + a * c * z;
1424
1425 x = nx; y = ny; z = nz;
1426 return *this;
1427}
1428
1429
1430inline ofVec3f& ofVec3f::rotateRad(float ax, float ay, float az) {
1431 float a = cos(ax);
1432 float b = sin(ax);
1433 float c = cos(ay);
1434 float d = sin(ay);
1435 float e = cos(az);
1436 float f = sin(az);
1437
1438 float nx = c * e * x - c * f * y + d * z;
1439 float ny = (a * f + b * d * e) * x + (a * e - b * d * f) * y - b * c * z;
1440 float nz = (b * f - a * d * e) * x + (a * d * f + b * e) * y + a * c * z;
1441
1442 x = nx; y = ny; z = nz;
1443 return *this;
1444}
1445
1446
1447// Rotate point by angle (deg) around line defined by pivot and axis.
1448//
1449//
1450inline ofVec3f ofVec3f::rotated( float angle,
1451 const ofVec3f& pivot,
1452 const ofVec3f& axis ) const{
1453 return getRotated(angle, pivot, axis);
1454}
1455
1456inline ofVec3f ofVec3f::getRotated( float angle,
1457 const ofVec3f& pivot,
1458 const ofVec3f& axis ) const
1459{
1460 ofVec3f ax = axis.getNormalized();
1461 float tx = x - pivot.x;
1462 float ty = y - pivot.y;
1463 float tz = z - pivot.z;
1464
1465 float a = ofDegToRad(angle);
1466 float sina = sin( a );
1467 float cosa = cos( a );
1468 float cosb = 1.0f - cosa;
1469
1470 float xrot = tx*(ax.x*ax.x*cosb + cosa)
1471 + ty*(ax.x*ax.y*cosb - ax.z*sina)
1472 + tz*(ax.x*ax.z*cosb + ax.y*sina);
1473 float yrot = tx*(ax.y*ax.x*cosb + ax.z*sina)
1474 + ty*(ax.y*ax.y*cosb + cosa)
1475 + tz*(ax.y*ax.z*cosb - ax.x*sina);
1476 float zrot = tx*(ax.z*ax.x*cosb - ax.y*sina)
1477 + ty*(ax.z*ax.y*cosb + ax.x*sina)
1478 + tz*(ax.z*ax.z*cosb + cosa);
1479
1480
1481 return ofVec3f( xrot+pivot.x, yrot+pivot.y, zrot+pivot.z );
1482}
1483
1484
1485inline ofVec3f ofVec3f::getRotatedRad( float angle,
1486 const ofVec3f& pivot,
1487 const ofVec3f& axis ) const
1488{
1489 ofVec3f ax = axis.getNormalized();
1490 float tx = x - pivot.x;
1491 float ty = y - pivot.y;
1492 float tz = z - pivot.z;
1493
1494 float a = angle;
1495 float sina = sin( a );
1496 float cosa = cos( a );
1497 float cosb = 1.0f - cosa;
1498
1499 float xrot = tx*(ax.x*ax.x*cosb + cosa)
1500 + ty*(ax.x*ax.y*cosb - ax.z*sina)
1501 + tz*(ax.x*ax.z*cosb + ax.y*sina);
1502 float yrot = tx*(ax.y*ax.x*cosb + ax.z*sina)
1503 + ty*(ax.y*ax.y*cosb + cosa)
1504 + tz*(ax.y*ax.z*cosb - ax.x*sina);
1505 float zrot = tx*(ax.z*ax.x*cosb - ax.y*sina)
1506 + ty*(ax.z*ax.y*cosb + ax.x*sina)
1507 + tz*(ax.z*ax.z*cosb + cosa);
1508
1509
1510 return ofVec3f( xrot+pivot.x, yrot+pivot.y, zrot+pivot.z );
1511}
1512
1513
1514inline ofVec3f& ofVec3f::rotate( float angle,
1515 const ofVec3f& pivot,
1516 const ofVec3f& axis )
1517{
1518 ofVec3f ax = axis.getNormalized();
1519 x -= pivot.x;
1520 y -= pivot.y;
1521 z -= pivot.z;
1522
1523 float a = ofDegToRad(angle);
1524 float sina = sin( a );
1525 float cosa = cos( a );
1526 float cosb = 1.0f - cosa;
1527
1528 float xrot = x*(ax.x*ax.x*cosb + cosa)
1529 + y*(ax.x*ax.y*cosb - ax.z*sina)
1530 + z*(ax.x*ax.z*cosb + ax.y*sina);
1531 float yrot = x*(ax.y*ax.x*cosb + ax.z*sina)
1532 + y*(ax.y*ax.y*cosb + cosa)
1533 + z*(ax.y*ax.z*cosb - ax.x*sina);
1534 float zrot = x*(ax.z*ax.x*cosb - ax.y*sina)
1535 + y*(ax.z*ax.y*cosb + ax.x*sina)
1536 + z*(ax.z*ax.z*cosb + cosa);
1537
1538 x = xrot + pivot.x;
1539 y = yrot + pivot.y;
1540 z = zrot + pivot.z;
1541
1542 return *this;
1543}
1544
1545
1546inline ofVec3f& ofVec3f::rotateRad( float angle,
1547 const ofVec3f& pivot,
1548 const ofVec3f& axis )
1549{
1550 ofVec3f ax = axis.getNormalized();
1551 x -= pivot.x;
1552 y -= pivot.y;
1553 z -= pivot.z;
1554
1555 float a = angle;
1556 float sina = sin( a );
1557 float cosa = cos( a );
1558 float cosb = 1.0f - cosa;
1559
1560 float xrot = x*(ax.x*ax.x*cosb + cosa)
1561 + y*(ax.x*ax.y*cosb - ax.z*sina)
1562 + z*(ax.x*ax.z*cosb + ax.y*sina);
1563 float yrot = x*(ax.y*ax.x*cosb + ax.z*sina)
1564 + y*(ax.y*ax.y*cosb + cosa)
1565 + z*(ax.y*ax.z*cosb - ax.x*sina);
1566 float zrot = x*(ax.z*ax.x*cosb - ax.y*sina)
1567 + y*(ax.z*ax.y*cosb + ax.x*sina)
1568 + z*(ax.z*ax.z*cosb + cosa);
1569
1570 x = xrot + pivot.x;
1571 y = yrot + pivot.y;
1572 z = zrot + pivot.z;
1573
1574 return *this;
1575}
1576
1577
1578
1579
1580// Map point to coordinate system defined by origin, vx, vy, and vz.
1581//
1582//
1583inline ofVec3f ofVec3f::mapped( const ofVec3f& origin,
1584 const ofVec3f& vx,
1585 const ofVec3f& vy,
1586 const ofVec3f& vz ) const{
1587 return getMapped(origin, vx, vy, vz);
1588}
1589
1590inline ofVec3f ofVec3f::getMapped( const ofVec3f& origin,
1591 const ofVec3f& vx,
1592 const ofVec3f& vy,
1593 const ofVec3f& vz ) const
1594{
1595 return ofVec3f( origin.x + x*vx.x + y*vy.x + z*vz.x,
1596 origin.y + x*vx.y + y*vy.y + z*vz.y,
1597 origin.z + x*vx.z + y*vy.z + z*vz.z );
1598}
1599
1600inline ofVec3f& ofVec3f::map( const ofVec3f& origin,
1601 const ofVec3f& vx,
1602 const ofVec3f& vy,
1603 const ofVec3f& vz )
1604{
1605 float xmap = origin.x + x*vx.x + y*vy.x + z*vz.x;
1606 float ymap = origin.y + x*vx.y + y*vy.y + z*vz.y;
1607 z = origin.z + x*vx.z + y*vy.z + z*vz.z;
1608 x = xmap;
1609 y = ymap;
1610 return *this;
1611}
1612
1613
1614// Distance between two points.
1615//
1616//
1617inline float ofVec3f::distance( const ofVec3f& pnt) const {
1618 float vx = x-pnt.x;
1619 float vy = y-pnt.y;
1620 float vz = z-pnt.z;
1621 return (float)sqrt(vx*vx + vy*vy + vz*vz);
1622}
1623
1624inline float ofVec3f::distanceSquared( const ofVec3f& pnt ) const{
1625 return squareDistance(pnt);
1626}
1627
1628inline float ofVec3f::squareDistance( const ofVec3f& pnt ) const {
1629 float vx = x-pnt.x;
1630 float vy = y-pnt.y;
1631 float vz = z-pnt.z;
1632 return vx*vx + vy*vy + vz*vz;
1633}
1634
1635
1636
1637// Linear interpolation.
1638//
1639//
1645inline ofVec3f ofVec3f::interpolated( const ofVec3f& pnt, float p ) const {
1646 return getInterpolated(pnt,p);
1647}
1648
1649inline ofVec3f ofVec3f::getInterpolated( const ofVec3f& pnt, float p ) const {
1650 return ofVec3f( x*(1-p) + pnt.x*p,
1651 y*(1-p) + pnt.y*p,
1652 z*(1-p) + pnt.z*p );
1653}
1654
1655inline ofVec3f& ofVec3f::interpolate( const ofVec3f& pnt, float p ) {
1656 x = x*(1-p) + pnt.x*p;
1657 y = y*(1-p) + pnt.y*p;
1658 z = z*(1-p) + pnt.z*p;
1659 return *this;
1660}
1661
1662
1663inline ofVec3f ofVec3f::middled( const ofVec3f& pnt ) const {
1664 return getMiddle(pnt);
1665}
1666
1667inline ofVec3f ofVec3f::getMiddle( const ofVec3f& pnt ) const {
1668 return ofVec3f( (x+pnt.x)/2.0f, (y+pnt.y)/2.0f, (z+pnt.z)/2.0f );
1669}
1670
1671inline ofVec3f& ofVec3f::middle( const ofVec3f& pnt ) {
1672 x = (x+pnt.x)/2.0f;
1673 y = (y+pnt.y)/2.0f;
1674 z = (z+pnt.z)/2.0f;
1675 return *this;
1676}
1677
1678
1679// Average (centroid) among points.
1680// Addition is sometimes useful for calculating averages too.
1681//
1682//
1683inline ofVec3f& ofVec3f::average( const ofVec3f* points, int num ) {
1684 x = 0.f;
1685 y = 0.f;
1686 z = 0.f;
1687 for( int i=0; i<num; i++) {
1688 x += points[i].x;
1689 y += points[i].y;
1690 z += points[i].z;
1691 }
1692 x /= num;
1693 y /= num;
1694 z /= num;
1695 return *this;
1696}
1697
1698
1699
1700// Normalization
1701//
1702//
1703inline ofVec3f ofVec3f::normalized() const {
1704 return getNormalized();
1705}
1706
1707inline ofVec3f ofVec3f::getNormalized() const {
1708 float length = (float)sqrt(x*x + y*y + z*z);
1709 if( length > 0 ) {
1710 return ofVec3f( x/length, y/length, z/length );
1711 } else {
1712 return ofVec3f();
1713 }
1714}
1715
1716inline ofVec3f& ofVec3f::normalize() {
1717 float length = (float)sqrt(x*x + y*y + z*z);
1718 if( length > 0 ) {
1719 x /= length;
1720 y /= length;
1721 z /= length;
1722 }
1723 return *this;
1724}
1725
1726
1727
1728// Limit length.
1729//
1730//
1731
1732inline ofVec3f ofVec3f::limited(float max) const {
1733 return getLimited(max);
1734}
1735
1736inline ofVec3f ofVec3f::getLimited(float max) const {
1737 ofVec3f limited;
1738 float lengthSquared = (x*x + y*y + z*z);
1739 if( lengthSquared > max*max && lengthSquared > 0 ) {
1740 float ratio = max/(float)sqrt(lengthSquared);
1741 limited.set( x*ratio, y*ratio, z*ratio);
1742 } else {
1743 limited.set(x,y,z);
1744 }
1745 return limited;
1746}
1747
1748inline ofVec3f& ofVec3f::limit(float max) {
1749 float lengthSquared = (x*x + y*y + z*z);
1750 if( lengthSquared > max*max && lengthSquared > 0 ) {
1751 float ratio = max/(float)sqrt(lengthSquared);
1752 x *= ratio;
1753 y *= ratio;
1754 z *= ratio;
1755 }
1756 return *this;
1757}
1758
1759
1760// Perpendicular vector.
1761//
1762//
1763inline ofVec3f ofVec3f::crossed( const ofVec3f& vec ) const {
1764 return getCrossed(vec);
1765}
1766inline ofVec3f ofVec3f::getCrossed( const ofVec3f& vec ) const {
1767 return ofVec3f( y*vec.z - z*vec.y,
1768 z*vec.x - x*vec.z,
1769 x*vec.y - y*vec.x );
1770}
1771
1772inline ofVec3f& ofVec3f::cross( const ofVec3f& vec ) {
1773 float _x = y*vec.z - z*vec.y;
1774 float _y = z*vec.x - x*vec.z;
1775 z = x*vec.y - y*vec.x;
1776 x = _x;
1777 y = _y;
1778 return *this;
1779}
1780
1784inline ofVec3f ofVec3f::perpendiculared( const ofVec3f& vec ) const {
1785 return getPerpendicular(vec);
1786}
1787
1788inline ofVec3f ofVec3f::getPerpendicular( const ofVec3f& vec ) const {
1789 float crossX = y*vec.z - z*vec.y;
1790 float crossY = z*vec.x - x*vec.z;
1791 float crossZ = x*vec.y - y*vec.x;
1792
1793 float length = (float)sqrt(crossX*crossX +
1794 crossY*crossY +
1795 crossZ*crossZ);
1796
1797 if( length > 0 )
1798 return ofVec3f( crossX/length, crossY/length, crossZ/length );
1799 else
1800 return ofVec3f();
1801}
1802
1803inline ofVec3f& ofVec3f::perpendicular( const ofVec3f& vec ) {
1804 float crossX = y*vec.z - z*vec.y;
1805 float crossY = z*vec.x - x*vec.z;
1806 float crossZ = x*vec.y - y*vec.x;
1807
1808 float length = (float)sqrt(crossX*crossX +
1809 crossY*crossY +
1810 crossZ*crossZ);
1811
1812 if( length > 0 ) {
1813 x = crossX/length;
1814 y = crossY/length;
1815 z = crossZ/length;
1816 } else {
1817 x = 0.f;
1818 y = 0.f;
1819 z = 0.f;
1820 }
1821
1822 return *this;
1823}
1824
1825
1826// Length
1827//
1828//
1829inline float ofVec3f::length() const {
1830 return (float)sqrt( x*x + y*y + z*z );
1831}
1832
1833inline float ofVec3f::lengthSquared() const {
1834 return (float)(x*x + y*y + z*z);
1835}
1836
1837
1838
1844inline float ofVec3f::angle( const ofVec3f& vec ) const {
1845 ofVec3f n1 = this->getNormalized();
1846 ofVec3f n2 = vec.getNormalized();
1847 return ofRadToDeg(acos( n1.dot(n2) ));
1848}
1849
1850inline float ofVec3f::angleRad( const ofVec3f& vec ) const {
1851 ofVec3f n1 = this->getNormalized();
1852 ofVec3f n2 = vec.getNormalized();
1853 return (float)acos( n1.dot(n2) );
1854}
1855
1856
1857
1861inline float ofVec3f::dot( const ofVec3f& vec ) const {
1862 return x*vec.x + y*vec.y + z*vec.z;
1863}
1864
1865
1866
1867
1868
1869// Non-Member operators
1870//
1871//
1872inline ofVec3f operator+( float f, const ofVec3f& vec ) {
1873 return ofVec3f( f+vec.x, f+vec.y, f+vec.z );
1874}
1875
1876inline ofVec3f operator-( float f, const ofVec3f& vec ) {
1877 return ofVec3f( f-vec.x, f-vec.y, f-vec.z );
1878}
1879
1880inline ofVec3f operator*( float f, const ofVec3f& vec ) {
1881 return ofVec3f( f*vec.x, f*vec.y, f*vec.z );
1882}
1883
1884inline ofVec3f operator/( float f, const ofVec3f& vec ) {
1885 return ofVec3f( f/vec.x, f/vec.y, f/vec.z);
1886}
1887
ofVec2f is a class for storing a two dimensional vector.
Definition ofVec2f.h:72
ofVec3f is a class for storing a three dimensional vector.
Definition ofVec3f.h:79
ofVec3f & operator*=(const float f)
ofVec3f getNormalized() const
Return a normalized copy of this vector.
ofVec3f(const ofVec2f &vec)
ofVec3f operator-(const ofVec3f &vec) const
ofVec3f getMiddle(const ofVec3f &pnt) const
Calculate and return the midpoint between this vector and 'pnt'.
bool isAlignedRad(const ofVec3f &vec, float tolerance=0.0001f) const
ofVec3f(float x, float y, float z=0)
Construt a 3D vector with x, y and z specified.
ofVec3f operator/(const ofVec3f &vec) const
ofVec3f(const glm::vec3 &vec)
ofVec3f getRotated(float angle, const ofVec3f &axis) const
Returns a new vector that is the result of rotating this vector by 'angle' degrees about the given ax...
ofVec3f & rotate(float ax, float ay, float az)
Performs an Euler rotation of this vector by 'ax' degrees about the x axis, 'ay' degrees about the y ...
void set(float x, float y, float z=0)
Set 'x', 'y' and 'z' components of this vector with just one function call. 'z' is optional,...
ofVec3f & operator-=(const float f)
ofVec3f & operator/=(const float f)
float angleRad(const ofVec3f &vec) const
Calculate and return the coplanar angle in radians between this vector and 'vec'.
ofVec3f operator-(const float f) const
ofVec3f & scale(const float length)
Scales this vector up or down so that it has the requested length.
float lengthSquared() const
Return the squared length (squared magnitude) of this vector.
void set(float _scalar)
ofVec3f & operator+=(const ofVec3f &pnt)
bool match(const ofVec3f &vec, float tolerance=0.0001f) const
Let you check if two vectors are similar given a tolerance threshold 'tolerance' (default = 0....
ofVec3f getRotated(float angle, const ofVec3f &pivot, const ofVec3f &axis) const
Returns a new vector that is the result of rotating this vector by 'angle' degrees about the axis spe...
bool operator!=(const ofVec3f &vec) const
Returns 'true' if any component is different to its corresponding component in 'vec',...
ofVec3f operator-() const
ofVec3f getLimited(float max) const
Return a copy of this vector with its length (magnitude) restricted to a maximum of 'max' units by sc...
ofVec3f & operator*=(const ofVec3f &vec)
ofVec3f operator+(const ofVec3f &pnt) const
ofVec3f(float scalar)
Construct a 3D vector with x, y and z set to scalar
ofVec3f()
Construct a 3D vector. Defaults to (0,0,0).
ofVec3f & rotate(float angle, const ofVec3f &pivot, const ofVec3f &axis)
Rotates this vector by 'angle' degrees about the axis specified by 'axis', using 'pivot' as the origi...
ofVec3f(const glm::vec2 &vec)
ofVec3f & map(const ofVec3f &origin, const ofVec3f &vx, const ofVec3f &vy, const ofVec3f &vz)
Map this vector from its default coordinate system – origin (0,0,0), X direction (1,...
float squareDistance(const ofVec3f &pnt) const
Treats both this vector and 'pnt' as points in 3D space, and calculates and returns the squared dista...
ofVec3f getRotatedRad(float ax, float ay, float az) const
Returns a new vector that is the result of performing an Euler rotation of this vector by 'ax' radian...
float length() const
ofVec3f(const glm::vec4 &vec)
ofVec3f & rotate(float angle, const ofVec3f &axis)
Rotates this vector by 'angle' degrees around the given axis.
ofVec3f getRotatedRad(float angle, const ofVec3f &pivot, const ofVec3f &axis) const
Returns a new vector that is the result of rotating this vector by 'angle' radians about the axis spe...
bool align(const ofVec3f &vec, float tolerance=0.0001f) const
Returns 'true' if this vector is pointing in the same direction as 'vec', with an angle error thresho...
ofVec3f getRotatedRad(float angle, const ofVec3f &axis) const
Returns a new vector that is the result of rotating this vector by 'angle' radians about the given ax...
bool isAligned(const ofVec3f &vec, float tolerance=0.0001f) const
float & operator[](int n)
Allows to access the individual components of an 'ofVec3f' as though it is an array.
Definition ofVec3f.h:177
bool alignRad(const ofVec3f &vec, float tolerance=0.0001f) const
Returns 'true' if this vector is pointing in the same direction as 'vec', with an angle error thresho...
ofVec3f & operator/=(const ofVec3f &vec)
ofVec3f operator*(const ofVec3f &vec) const
ofVec3f & middle(const ofVec3f &pnt)
float angle(const ofVec3f &vec) const
Calculate and return the coplanar angle in degrees between this vector and 'vec'.
float operator[](int n) const
Definition ofVec3f.h:181
ofVec3f & normalize()
Normalize the vector.
ofVec3f & rotateRad(float angle, const ofVec3f &pivot, const ofVec3f &axis)
Rotates this vector by 'angle' radians around the axis specified by 'axis', using 'pivot' as the orig...
float distance(const ofVec3f &pnt) const
Treats both this vector and 'pnt' as points in 3D space, and calculates and returns the distance betw...
ofVec3f getInterpolated(const ofVec3f &pnt, float p) const
Perform a linear interpolation of this vector's position towards 'pnt' and return the interpolated ve...
void set(const ofVec3f &vec)
Setting the values by using other 3 dimension vector ofVec3f.
const float * getPtr() const
Definition ofVec3f.h:159
ofVec3f & cross(const ofVec3f &vec)
ofVec3f getScaled(const float length) const
Return a new 'ofVec3f' that is the result of scaling this vector up or down so that it has the reques...
float dot(const ofVec3f &vec) const
Returns the dot product of this vector with 'vec'.
bool operator==(const ofVec3f &vec) const
Check for equality between two ofVec3f.
ofVec3f operator/(const float f) const
ofVec3f getCrossed(const ofVec3f &vec) const
Returns the cross product (vector product) of this vector and 'vec'. This is a binary operation on tw...
ofVec3f operator+(const float f) const
ofVec3f & limit(float max)
Restrict the length (magnitude) of this vector to a maximum of 'max' units by scaling down if necessa...
float x
Stores the X component of this vector.
Definition ofVec3f.h:86
float y
Stores the Y component of this vector.
Definition ofVec3f.h:89
float z
Stores the Z component of this vector.
Definition ofVec3f.h:92
ofVec3f operator*(const float f) const
ofVec3f getPerpendicular(const ofVec3f &vec) const
Construct a plane using this vector and 'vec' (by finding the plane that both lectors lie on),...
ofVec3f & rotateRad(float angle, const ofVec3f &axis)
Rotates this vector by 'angle' radians about the given axis.
ofVec3f & operator-=(const ofVec3f &vec)
ofVec3f(const ofVec4f &vec)
Construct a new 3D vector from a 4D vector by throwing away the 'w' component.
ofVec3f & interpolate(const ofVec3f &pnt, float p)
Perform a linear interpolation of this vector's position towards 'pnt'. 'p' controls the amount to mo...
ofVec3f getRotated(float ax, float ay, float az) const
Returns a new vector that is the result of performing an Euler rotation of this vector by 'ax' degree...
float * getPtr()
Returns a pointer to the memory position of the first element of the vector ('x'); the other elements...
Definition ofVec3f.h:156
ofVec3f & perpendicular(const ofVec3f &vec)
Construct a plane using this vector and 'vec' (by finding the plane that both lie on),...
ofVec3f & rotateRad(float ax, float ay, float az)
Performs an Euler rotation of this vector by 'ax' radians about the x axis, 'ay' radians about the y ...
ofVec3f & average(const ofVec3f *points, int num)
Sets this vector to be the average (centre of gravity or centroid) of a given array of 'ofVec3f's....
ofVec3f getMapped(const ofVec3f &origin, const ofVec3f &vx, const ofVec3f &vy, const ofVec3f &vz) const
Return a new 'ofVec3f' calculated by copying this vector and then mapping from its default coordinate...
ofVec3f & operator+=(const float f)
Definition ofVec4f.h:11
#define OF_DEPRECATED_MSG(message,...)
Definition ofConstants.h:78
float ofDegToRad(float degrees)
Convert degrees to radians.
Definition ofMath.cpp:142
float ofRadToDeg(float radians)
Convert radians to degrees.
Definition ofMath.cpp:137
std::ostream & operator<<(std::ostream &os, const ofMatrix3x3 &M)
Definition ofMatrix3x3.cpp:304
std::istream & operator>>(std::istream &is, ofMatrix3x3 &M)
Definition ofMatrix3x3.cpp:323
#define d
#define tx
#define a
#define ty
#define tz
#define c
#define b
glm::vec3 operator+(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:292
glm::vec3 operator/(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:307
glm::vec3 operator*(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:302
glm::vec3 operator-(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:297