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