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