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