vector3d.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __IRR_POINT_3D_H_INCLUDED__
  5. #define __IRR_POINT_3D_H_INCLUDED__
  6. #include "irrMath.h"
  7. namespace irr
  8. {
  9. namespace core
  10. {
  11. //! 3d vector template class with lots of operators and methods.
  12. /** The vector3d class is used in Irrlicht for three main purposes:
  13. 1) As a direction vector (most of the methods assume this).
  14. 2) As a position in 3d space (which is synonymous with a direction vector from the origin to this position).
  15. 3) To hold three Euler rotations, where X is pitch, Y is yaw and Z is roll.
  16. */
  17. template <class T>
  18. class vector3d
  19. {
  20. public:
  21. //! Default constructor (null vector).
  22. vector3d() : X(0), Y(0), Z(0) {}
  23. //! Constructor with three different values
  24. vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
  25. //! Constructor with the same value for all elements
  26. explicit vector3d(T n) : X(n), Y(n), Z(n) {}
  27. //! Copy constructor
  28. vector3d(const vector3d<T>& other) : X(other.X), Y(other.Y), Z(other.Z) {}
  29. // operators
  30. vector3d<T> operator-() const { return vector3d<T>(-X, -Y, -Z); }
  31. vector3d<T>& operator=(const vector3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; }
  32. vector3d<T> operator+(const vector3d<T>& other) const { return vector3d<T>(X + other.X, Y + other.Y, Z + other.Z); }
  33. vector3d<T>& operator+=(const vector3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; }
  34. vector3d<T> operator+(const T val) const { return vector3d<T>(X + val, Y + val, Z + val); }
  35. vector3d<T>& operator+=(const T val) { X+=val; Y+=val; Z+=val; return *this; }
  36. vector3d<T> operator-(const vector3d<T>& other) const { return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z); }
  37. vector3d<T>& operator-=(const vector3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; }
  38. vector3d<T> operator-(const T val) const { return vector3d<T>(X - val, Y - val, Z - val); }
  39. vector3d<T>& operator-=(const T val) { X-=val; Y-=val; Z-=val; return *this; }
  40. vector3d<T> operator*(const vector3d<T>& other) const { return vector3d<T>(X * other.X, Y * other.Y, Z * other.Z); }
  41. vector3d<T>& operator*=(const vector3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; }
  42. vector3d<T> operator*(const T v) const { return vector3d<T>(X * v, Y * v, Z * v); }
  43. vector3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; }
  44. vector3d<T> operator/(const vector3d<T>& other) const { return vector3d<T>(X / other.X, Y / other.Y, Z / other.Z); }
  45. vector3d<T>& operator/=(const vector3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; }
  46. vector3d<T> operator/(const T v) const { T i=(T)1.0/v; return vector3d<T>(X * i, Y * i, Z * i); }
  47. vector3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; }
  48. T& operator [](u32 index)
  49. {
  50. _IRR_DEBUG_BREAK_IF(index>2) // access violation
  51. return *(&X+index);
  52. }
  53. const T& operator [](u32 index) const
  54. {
  55. _IRR_DEBUG_BREAK_IF(index>2) // access violation
  56. return *(&X+index);
  57. }
  58. //! sort in order X, Y, Z. Equality with rounding tolerance.
  59. bool operator<=(const vector3d<T>&other) const
  60. {
  61. return (X<other.X || core::equals(X, other.X)) ||
  62. (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y))) ||
  63. (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z<other.Z || core::equals(Z, other.Z)));
  64. }
  65. //! sort in order X, Y, Z. Equality with rounding tolerance.
  66. bool operator>=(const vector3d<T>&other) const
  67. {
  68. return (X>other.X || core::equals(X, other.X)) ||
  69. (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))) ||
  70. (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z>other.Z || core::equals(Z, other.Z)));
  71. }
  72. //! sort in order X, Y, Z. Difference must be above rounding tolerance.
  73. bool operator<(const vector3d<T>&other) const
  74. {
  75. return (X<other.X && !core::equals(X, other.X)) ||
  76. (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y)) ||
  77. (core::equals(X, other.X) && core::equals(Y, other.Y) && Z<other.Z && !core::equals(Z, other.Z));
  78. }
  79. //! sort in order X, Y, Z. Difference must be above rounding tolerance.
  80. bool operator>(const vector3d<T>&other) const
  81. {
  82. return (X>other.X && !core::equals(X, other.X)) ||
  83. (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)) ||
  84. (core::equals(X, other.X) && core::equals(Y, other.Y) && Z>other.Z && !core::equals(Z, other.Z));
  85. }
  86. //! use weak float compare
  87. bool operator==(const vector3d<T>& other) const
  88. {
  89. return this->equals(other);
  90. }
  91. bool operator!=(const vector3d<T>& other) const
  92. {
  93. return !this->equals(other);
  94. }
  95. // functions
  96. //! returns if this vector equals the other one, taking floating point rounding errors into account
  97. bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
  98. {
  99. return core::equals(X, other.X, tolerance) &&
  100. core::equals(Y, other.Y, tolerance) &&
  101. core::equals(Z, other.Z, tolerance);
  102. }
  103. vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
  104. vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
  105. //! Get length of the vector.
  106. T getLength() const { return core::squareroot( X*X + Y*Y + Z*Z ); }
  107. //! Get squared length of the vector.
  108. /** This is useful because it is much faster than getLength().
  109. \return Squared length of the vector. */
  110. T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
  111. //! Get the dot product with another vector.
  112. T dotProduct(const vector3d<T>& other) const
  113. {
  114. return X*other.X + Y*other.Y + Z*other.Z;
  115. }
  116. //! Get distance from another point.
  117. /** Here, the vector is interpreted as point in 3 dimensional space. */
  118. T getDistanceFrom(const vector3d<T>& other) const
  119. {
  120. return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
  121. }
  122. //! Returns squared distance from another point.
  123. /** Here, the vector is interpreted as point in 3 dimensional space. */
  124. T getDistanceFromSQ(const vector3d<T>& other) const
  125. {
  126. return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
  127. }
  128. //! Calculates the cross product with another vector.
  129. /** \param p Vector to multiply with.
  130. \return Crossproduct of this vector with p. */
  131. vector3d<T> crossProduct(const vector3d<T>& p) const
  132. {
  133. return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
  134. }
  135. //! Returns if this vector interpreted as a point is on a line between two other points.
  136. /** It is assumed that the point is on the line.
  137. \param begin Beginning vector to compare between.
  138. \param end Ending vector to compare between.
  139. \return True if this vector is between begin and end, false if not. */
  140. bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
  141. {
  142. const T f = (end - begin).getLengthSQ();
  143. return getDistanceFromSQ(begin) <= f &&
  144. getDistanceFromSQ(end) <= f;
  145. }
  146. //! Normalizes the vector.
  147. /** In case of the 0 vector the result is still 0, otherwise
  148. the length of the vector will be 1.
  149. \return Reference to this vector after normalization. */
  150. vector3d<T>& normalize()
  151. {
  152. f64 length = X*X + Y*Y + Z*Z;
  153. if (length == 0 ) // this check isn't an optimization but prevents getting NAN in the sqrt.
  154. return *this;
  155. length = core::reciprocal_squareroot(length);
  156. X = (T)(X * length);
  157. Y = (T)(Y * length);
  158. Z = (T)(Z * length);
  159. return *this;
  160. }
  161. //! Sets the length of the vector to a new value
  162. vector3d<T>& setLength(T newlength)
  163. {
  164. normalize();
  165. return (*this *= newlength);
  166. }
  167. //! Inverts the vector.
  168. vector3d<T>& invert()
  169. {
  170. X *= -1;
  171. Y *= -1;
  172. Z *= -1;
  173. return *this;
  174. }
  175. //! Rotates the vector by a specified number of degrees around the Y axis and the specified center.
  176. /** \param degrees Number of degrees to rotate around the Y axis.
  177. \param center The center of the rotation. */
  178. void rotateXZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
  179. {
  180. degrees *= DEGTORAD64;
  181. f64 cs = cos(degrees);
  182. f64 sn = sin(degrees);
  183. X -= center.X;
  184. Z -= center.Z;
  185. set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
  186. X += center.X;
  187. Z += center.Z;
  188. }
  189. //! Rotates the vector by a specified number of degrees around the Z axis and the specified center.
  190. /** \param degrees: Number of degrees to rotate around the Z axis.
  191. \param center: The center of the rotation. */
  192. void rotateXYBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
  193. {
  194. degrees *= DEGTORAD64;
  195. f64 cs = cos(degrees);
  196. f64 sn = sin(degrees);
  197. X -= center.X;
  198. Y -= center.Y;
  199. set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
  200. X += center.X;
  201. Y += center.Y;
  202. }
  203. //! Rotates the vector by a specified number of degrees around the X axis and the specified center.
  204. /** \param degrees: Number of degrees to rotate around the X axis.
  205. \param center: The center of the rotation. */
  206. void rotateYZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
  207. {
  208. degrees *= DEGTORAD64;
  209. f64 cs = cos(degrees);
  210. f64 sn = sin(degrees);
  211. Z -= center.Z;
  212. Y -= center.Y;
  213. set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
  214. Z += center.Z;
  215. Y += center.Y;
  216. }
  217. //! Creates an interpolated vector between this vector and another vector.
  218. /** \param other The other vector to interpolate with.
  219. \param d Interpolation value between 0.0f (all the other vector) and 1.0f (all this vector).
  220. Note that this is the opposite direction of interpolation to getInterpolated_quadratic()
  221. \return An interpolated vector. This vector is not modified. */
  222. vector3d<T> getInterpolated(const vector3d<T>& other, f64 d) const
  223. {
  224. const f64 inv = 1.0 - d;
  225. return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
  226. }
  227. //! Creates a quadratically interpolated vector between this and two other vectors.
  228. /** \param v2 Second vector to interpolate with.
  229. \param v3 Third vector to interpolate with (maximum at 1.0f)
  230. \param d Interpolation value between 0.0f (all this vector) and 1.0f (all the 3rd vector).
  231. Note that this is the opposite direction of interpolation to getInterpolated() and interpolate()
  232. \return An interpolated vector. This vector is not modified. */
  233. vector3d<T> getInterpolated_quadratic(const vector3d<T>& v2, const vector3d<T>& v3, f64 d) const
  234. {
  235. // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
  236. const f64 inv = (T) 1.0 - d;
  237. const f64 mul0 = inv * inv;
  238. const f64 mul1 = (T) 2.0 * d * inv;
  239. const f64 mul2 = d * d;
  240. return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
  241. (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
  242. (T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
  243. }
  244. //! Sets this vector to the linearly interpolated vector between a and b.
  245. /** \param a first vector to interpolate with, maximum at 1.0f
  246. \param b second vector to interpolate with, maximum at 0.0f
  247. \param d Interpolation value between 0.0f (all vector b) and 1.0f (all vector a)
  248. Note that this is the opposite direction of interpolation to getInterpolated_quadratic()
  249. */
  250. vector3d<T>& interpolate(const vector3d<T>& a, const vector3d<T>& b, f64 d)
  251. {
  252. X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
  253. Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
  254. Z = (T)((f64)b.Z + ( ( a.Z - b.Z ) * d ));
  255. return *this;
  256. }
  257. //! Get the rotations that would make a (0,0,1) direction vector point in the same direction as this direction vector.
  258. /** Thanks to Arras on the Irrlicht forums for this method. This utility method is very useful for
  259. orienting scene nodes towards specific targets. For example, if this vector represents the difference
  260. between two scene nodes, then applying the result of getHorizontalAngle() to one scene node will point
  261. it at the other one.
  262. Example code:
  263. // Where target and seeker are of type ISceneNode*
  264. const vector3df toTarget(target->getAbsolutePosition() - seeker->getAbsolutePosition());
  265. const vector3df requiredRotation = toTarget.getHorizontalAngle();
  266. seeker->setRotation(requiredRotation);
  267. \return A rotation vector containing the X (pitch) and Y (raw) rotations (in degrees) that when applied to a
  268. +Z (e.g. 0, 0, 1) direction vector would make it point in the same direction as this vector. The Z (roll) rotation
  269. is always 0, since two Euler rotations are sufficient to point in any given direction. */
  270. vector3d<T> getHorizontalAngle() const
  271. {
  272. vector3d<T> angle;
  273. // tmp avoids some precision troubles on some compilers when working with T=s32
  274. f64 tmp = (atan2((f64)X, (f64)Z) * RADTODEG64);
  275. angle.Y = (T)tmp;
  276. if (angle.Y < 0)
  277. angle.Y += 360;
  278. if (angle.Y >= 360)
  279. angle.Y -= 360;
  280. const f64 z1 = core::squareroot(X*X + Z*Z);
  281. tmp = (atan2((f64)z1, (f64)Y) * RADTODEG64 - 90.0);
  282. angle.X = (T)tmp;
  283. if (angle.X < 0)
  284. angle.X += 360;
  285. if (angle.X >= 360)
  286. angle.X -= 360;
  287. return angle;
  288. }
  289. //! Get the spherical coordinate angles
  290. /** This returns Euler degrees for the point represented by
  291. this vector. The calculation assumes the pole at (0,1,0) and
  292. returns the angles in X and Y.
  293. */
  294. vector3d<T> getSphericalCoordinateAngles() const
  295. {
  296. vector3d<T> angle;
  297. const f64 length = X*X + Y*Y + Z*Z;
  298. if (length)
  299. {
  300. if (X!=0)
  301. {
  302. angle.Y = (T)(atan2((f64)Z,(f64)X) * RADTODEG64);
  303. }
  304. else if (Z<0)
  305. angle.Y=180;
  306. angle.X = (T)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64);
  307. }
  308. return angle;
  309. }
  310. //! Builds a direction vector from (this) rotation vector.
  311. /** This vector is assumed to be a rotation vector composed of 3 Euler angle rotations, in degrees.
  312. The implementation performs the same calculations as using a matrix to do the rotation.
  313. \param[in] forwards The direction representing "forwards" which will be rotated by this vector.
  314. If you do not provide a direction, then the +Z axis (0, 0, 1) will be assumed to be forwards.
  315. \return A direction vector calculated by rotating the forwards direction by the 3 Euler angles
  316. (in degrees) represented by this vector. */
  317. vector3d<T> rotationToDirection(const vector3d<T> & forwards = vector3d<T>(0, 0, 1)) const
  318. {
  319. const f64 cr = cos( core::DEGTORAD64 * X );
  320. const f64 sr = sin( core::DEGTORAD64 * X );
  321. const f64 cp = cos( core::DEGTORAD64 * Y );
  322. const f64 sp = sin( core::DEGTORAD64 * Y );
  323. const f64 cy = cos( core::DEGTORAD64 * Z );
  324. const f64 sy = sin( core::DEGTORAD64 * Z );
  325. const f64 srsp = sr*sp;
  326. const f64 crsp = cr*sp;
  327. const f64 pseudoMatrix[] = {
  328. ( cp*cy ), ( cp*sy ), ( -sp ),
  329. ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
  330. ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
  331. return vector3d<T>(
  332. (T)(forwards.X * pseudoMatrix[0] +
  333. forwards.Y * pseudoMatrix[3] +
  334. forwards.Z * pseudoMatrix[6]),
  335. (T)(forwards.X * pseudoMatrix[1] +
  336. forwards.Y * pseudoMatrix[4] +
  337. forwards.Z * pseudoMatrix[7]),
  338. (T)(forwards.X * pseudoMatrix[2] +
  339. forwards.Y * pseudoMatrix[5] +
  340. forwards.Z * pseudoMatrix[8]));
  341. }
  342. //! Fills an array of 4 values with the vector data (usually floats).
  343. /** Useful for setting in shader constants for example. The fourth value
  344. will always be 0. */
  345. void getAs4Values(T* array) const
  346. {
  347. array[0] = X;
  348. array[1] = Y;
  349. array[2] = Z;
  350. array[3] = 0;
  351. }
  352. //! Fills an array of 3 values with the vector data (usually floats).
  353. /** Useful for setting in shader constants for example.*/
  354. void getAs3Values(T* array) const
  355. {
  356. array[0] = X;
  357. array[1] = Y;
  358. array[2] = Z;
  359. }
  360. //! X coordinate of the vector
  361. T X;
  362. //! Y coordinate of the vector
  363. T Y;
  364. //! Z coordinate of the vector
  365. T Z;
  366. };
  367. //! partial specialization for integer vectors
  368. // Implementer note: inline keyword needed due to template specialization for s32. Otherwise put specialization into a .cpp
  369. template <>
  370. inline vector3d<s32> vector3d<s32>::operator /(s32 val) const {return core::vector3d<s32>(X/val,Y/val,Z/val);}
  371. template <>
  372. inline vector3d<s32>& vector3d<s32>::operator /=(s32 val) {X/=val;Y/=val;Z/=val; return *this;}
  373. template <>
  374. inline vector3d<s32> vector3d<s32>::getSphericalCoordinateAngles() const
  375. {
  376. vector3d<s32> angle;
  377. const f64 length = X*X + Y*Y + Z*Z;
  378. if (length)
  379. {
  380. if (X!=0)
  381. {
  382. angle.Y = round32((f32)(atan2((f64)Z,(f64)X) * RADTODEG64));
  383. }
  384. else if (Z<0)
  385. angle.Y=180;
  386. angle.X = round32((f32)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64));
  387. }
  388. return angle;
  389. }
  390. //! Typedef for a f32 3d vector.
  391. typedef vector3d<f32> vector3df;
  392. //! Typedef for an integer 3d vector.
  393. typedef vector3d<s32> vector3di;
  394. //! Function multiplying a scalar and a vector component-wise.
  395. template<class S, class T>
  396. vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
  397. } // end namespace core
  398. } // end namespace irr
  399. #endif