 Irrlicht 3D Engine vector3d.h
Go to the documentation of this file.
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
5 #ifndef __IRR_POINT_3D_H_INCLUDED__
6 #define __IRR_POINT_3D_H_INCLUDED__
7
8 #include "irrMath.h"
9
10 namespace irr
11 {
12 namespace core
13 {
14
16
21  template <class T>
22  class vector3d
23  {
24  public:
26  vector3d() : X(0), Y(0), Z(0) {}
28  vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
30  explicit vector3d(T n) : X(n), Y(n), Z(n) {}
32  vector3d(const vector3d<T>& other) : X(other.X), Y(other.Y), Z(other.Z) {}
33
34  // operators
35
36  vector3d<T> operator-() const { return vector3d<T>(-X, -Y, -Z); }
37
38  vector3d<T>& operator=(const vector3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; }
39
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 val) const { return vector3d<T>(X + val, Y + val, Z + val); }
43  vector3d<T>& operator+=(const T val) { X+=val; Y+=val; Z+=val; return *this; }
44
45  vector3d<T> operator-(const vector3d<T>& other) const { return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z); }
46  vector3d<T>& operator-=(const vector3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; }
47  vector3d<T> operator-(const T val) const { return vector3d<T>(X - val, Y - val, Z - val); }
48  vector3d<T>& operator-=(const T val) { X-=val; Y-=val; Z-=val; return *this; }
49
50  vector3d<T> operator*(const vector3d<T>& other) const { return vector3d<T>(X * other.X, Y * other.Y, Z * other.Z); }
51  vector3d<T>& operator*=(const vector3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; }
52  vector3d<T> operator*(const T v) const { return vector3d<T>(X * v, Y * v, Z * v); }
53  vector3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; }
54
55  vector3d<T> operator/(const vector3d<T>& other) const { return vector3d<T>(X / other.X, Y / other.Y, Z / other.Z); }
56  vector3d<T>& operator/=(const vector3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; }
57  vector3d<T> operator/(const T v) const { T i=(T)1.0/v; return vector3d<T>(X * i, Y * i, Z * i); }
58  vector3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; }
59
61  bool operator<=(const vector3d<T>&other) const
62  {
63  return (X<other.X || core::equals(X, other.X)) ||
64  (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y))) ||
65  (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z<other.Z || core::equals(Z, other.Z)));
66  }
67
69  bool operator>=(const vector3d<T>&other) const
70  {
71  return (X>other.X || core::equals(X, other.X)) ||
72  (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))) ||
73  (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z>other.Z || core::equals(Z, other.Z)));
74  }
75
77  bool operator<(const vector3d<T>&other) const
78  {
79  return (X<other.X && !core::equals(X, other.X)) ||
80  (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y)) ||
81  (core::equals(X, other.X) && core::equals(Y, other.Y) && Z<other.Z && !core::equals(Z, other.Z));
82  }
83
85  bool operator>(const vector3d<T>&other) const
86  {
87  return (X>other.X && !core::equals(X, other.X)) ||
88  (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)) ||
89  (core::equals(X, other.X) && core::equals(Y, other.Y) && Z>other.Z && !core::equals(Z, other.Z));
90  }
91
93  bool operator==(const vector3d<T>& other) const
94  {
95  return this->equals(other);
96  }
97
98  bool operator!=(const vector3d<T>& other) const
99  {
100  return !this->equals(other);
101  }
102
103  // functions
104
106  bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
107  {
108  return core::equals(X, other.X, tolerance) &&
109  core::equals(Y, other.Y, tolerance) &&
110  core::equals(Z, other.Z, tolerance);
111  }
112
113  vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
114  vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
115
117  T getLength() const { return core::squareroot( X*X + Y*Y + Z*Z ); }
118
120
122  T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
123
125  T dotProduct(const vector3d<T>& other) const
126  {
127  return X*other.X + Y*other.Y + Z*other.Z;
128  }
129
131
132  T getDistanceFrom(const vector3d<T>& other) const
133  {
134  return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
135  }
136
138
139  T getDistanceFromSQ(const vector3d<T>& other) const
140  {
141  return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
142  }
143
145
148  {
149  return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
150  }
151
153
157  bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
158  {
159  const T f = (end - begin).getLengthSQ();
160  return getDistanceFromSQ(begin) <= f &&
161  getDistanceFromSQ(end) <= f;
162  }
163
165
169  {
170  f64 length = X*X + Y*Y + Z*Z;
171  if (length == 0 ) // this check isn't an optimization but prevents getting NAN in the sqrt.
172  return *this;
173  length = core::reciprocal_squareroot(length);
174
175  X = (T)(X * length);
176  Y = (T)(Y * length);
177  Z = (T)(Z * length);
178  return *this;
179  }
180
182  vector3d<T>& setLength(T newlength)
183  {
184  normalize();
185  return (*this *= newlength);
186  }
187
190  {
191  X *= -1;
192  Y *= -1;
193  Z *= -1;
194  return *this;
195  }
196
198
200  void rotateXZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
201  {
203  f64 cs = cos(degrees);
204  f64 sn = sin(degrees);
205  X -= center.X;
206  Z -= center.Z;
207  set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
208  X += center.X;
209  Z += center.Z;
210  }
211
213
215  void rotateXYBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
216  {
218  f64 cs = cos(degrees);
219  f64 sn = sin(degrees);
220  X -= center.X;
221  Y -= center.Y;
222  set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
223  X += center.X;
224  Y += center.Y;
225  }
226
228
230  void rotateYZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
231  {
233  f64 cs = cos(degrees);
234  f64 sn = sin(degrees);
235  Z -= center.Z;
236  Y -= center.Y;
237  set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
238  Z += center.Z;
239  Y += center.Y;
240  }
241
243
248  {
249  const f64 inv = 1.0 - d;
250  return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
251  }
252
254
260  {
261  // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
262  const f64 inv = (T) 1.0 - d;
263  const f64 mul0 = inv * inv;
264  const f64 mul1 = (T) 2.0 * d * inv;
265  const f64 mul2 = d * d;
266
267  return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
268  (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
269  (T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
270  }
271
273
279  {
280  X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
281  Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
282  Z = (T)((f64)b.Z + ( ( a.Z - b.Z ) * d ));
283  return *this;
284  }
285
286
288
302  {
303  vector3d<T> angle;
304
305  // tmp avoids some precision troubles on some compilers when working with T=s32
306  f64 tmp = (atan2((f64)X, (f64)Z) * RADTODEG64);
307  angle.Y = (T)tmp;
308
309  if (angle.Y < 0)
310  angle.Y += 360;
311  if (angle.Y >= 360)
312  angle.Y -= 360;
313
314  const f64 z1 = core::squareroot(X*X + Z*Z);
315
316  tmp = (atan2((f64)z1, (f64)Y) * RADTODEG64 - 90.0);
317  angle.X = (T)tmp;
318
319  if (angle.X < 0)
320  angle.X += 360;
321  if (angle.X >= 360)
322  angle.X -= 360;
323
324  return angle;
325  }
326
328
333  {
334  vector3d<T> angle;
335  const f64 length = X*X + Y*Y + Z*Z;
336
337  if (length)
338  {
339  if (X!=0)
340  {
341  angle.Y = (T)(atan2((f64)Z,(f64)X) * RADTODEG64);
342  }
343  else if (Z<0)
344  angle.Y=180;
345
346  angle.X = (T)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64);
347  }
348  return angle;
349  }
350
352
359  vector3d<T> rotationToDirection(const vector3d<T> & forwards = vector3d<T>(0, 0, 1)) const
360  {
361  const f64 cr = cos( core::DEGTORAD64 * X );
362  const f64 sr = sin( core::DEGTORAD64 * X );
363  const f64 cp = cos( core::DEGTORAD64 * Y );
364  const f64 sp = sin( core::DEGTORAD64 * Y );
365  const f64 cy = cos( core::DEGTORAD64 * Z );
366  const f64 sy = sin( core::DEGTORAD64 * Z );
367
368  const f64 srsp = sr*sp;
369  const f64 crsp = cr*sp;
370
371  const f64 pseudoMatrix[] = {
372  ( cp*cy ), ( cp*sy ), ( -sp ),
373  ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
374  ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
375
376  return vector3d<T>(
377  (T)(forwards.X * pseudoMatrix +
378  forwards.Y * pseudoMatrix +
379  forwards.Z * pseudoMatrix),
380  (T)(forwards.X * pseudoMatrix +
381  forwards.Y * pseudoMatrix +
382  forwards.Z * pseudoMatrix),
383  (T)(forwards.X * pseudoMatrix +
384  forwards.Y * pseudoMatrix +
385  forwards.Z * pseudoMatrix));
386  }
387
389
391  void getAs4Values(T* array) const
392  {
393  array = X;
394  array = Y;
395  array = Z;
396  array = 0;
397  }
398
400
401  void getAs3Values(T* array) const
402  {
403  array = X;
404  array = Y;
405  array = Z;
406  }
407
408
410  T X;
411
413  T Y;
414
416  T Z;
417  };
418
420  // Implementer note: inline keyword needed due to template specialization for s32. Otherwise put specialization into a .cpp
421  template <>
422  inline vector3d<s32> vector3d<s32>::operator /(s32 val) const {return core::vector3d<s32>(X/val,Y/val,Z/val);}
423  template <>
424  inline vector3d<s32>& vector3d<s32>::operator /=(s32 val) {X/=val;Y/=val;Z/=val; return *this;}
425
426  template <>
428  {
429  vector3d<s32> angle;
430  const f64 length = X*X + Y*Y + Z*Z;
431
432  if (length)
433  {
434  if (X!=0)
435  {
436  angle.Y = round32((f32)(atan2((f64)Z,(f64)X) * RADTODEG64));
437  }
438  else if (Z<0)
439  angle.Y=180;
440
441  angle.X = round32((f32)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64));
442  }
443  return angle;
444  }
445
448
451
453  template<class S, class T>
454  vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
455
456 } // end namespace core
457 } // end namespace irr
458
459 #endif
460
vector3d< T > & invert()
Inverts the vector.
Definition: vector3d.h:189
vector3d< T > & operator-=(const T val)
Definition: vector3d.h:48
64bit constant for converting from radians to degrees
Definition: irrMath.h:84
vector3d< T > crossProduct(const vector3d< T > &p) const
Calculates the cross product with another vector.
Definition: vector3d.h:147
vector3d< T > & operator=(const vector3d< T > &other)
Definition: vector3d.h:38
REALINLINE s32 round32(f32 x)
Definition: irrMath.h:718
T Y
Y coordinate of the vector.
Definition: vector3d.h:413
bool equals(const vector3d< T > &other, const T tolerance=(T) ROUNDING_ERROR_f32) const
returns if this vector equals the other one, taking floating point rounding errors into account
Definition: vector3d.h:106
T getDistanceFromSQ(const vector3d< T > &other) const
Returns squared distance from another point.
Definition: vector3d.h:139
vector3d< T > & operator-=(const vector3d< T > &other)
Definition: vector3d.h:46
void getAs4Values(T *array) const
Fills an array of 4 values with the vector data (usually floats).
Definition: vector3d.h:391
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
vector3d< T > getInterpolated(const vector3d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition: vector3d.h:247
vector3d(T nx, T ny, T nz)
Constructor with three different values.
Definition: vector3d.h:28
bool operator!=(const vector3d< T > &other) const
Definition: vector3d.h:98
REALINLINE f32 squareroot(const f32 f)
Definition: irrMath.h:506
bool operator<(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition: vector3d.h:77
void rotateXZBy(f64 degrees, const vector3d< T > &center=vector3d< T >())
Rotates the vector by a specified number of degrees around the Y axis and the specified center.
Definition: vector3d.h:200
vector3d< T > operator-(const T val) const
Definition: vector3d.h:47
T X
X coordinate of the vector.
Definition: vector3d.h:410
vector3d< T > rotationToDirection(const vector3d< T > &forwards=vector3d< T >(0, 0, 1)) const
Builds a direction vector from (this) rotation vector.
Definition: vector3d.h:359
vector3d< T > & set(const T nx, const T ny, const T nz)
Definition: vector3d.h:113
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
3d vector template class with lots of operators and methods.
Definition: vector3d.h:22
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
bool operator==(const vector3d< T > &other) const
use weak float compare
Definition: vector3d.h:93
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition: vector3d.h:447
vector3d(const vector3d< T > &other)
Copy constructor.
Definition: vector3d.h:32
vector3d< T > & operator/=(const vector3d< T > &other)
Definition: vector3d.h:56
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition: irrMath.h:81
const f32 ROUNDING_ERROR_f32
Definition: irrMath.h:50
CMatrix4< T > operator *(const T scalar, const CMatrix4< T > &mat)
Definition: matrix4.h:2370
vector3d< T > operator/(const vector3d< T > &other) const
Definition: vector3d.h:55
vector3d< T > & setLength(T newlength)
Sets the length of the vector to a new value.
Definition: vector3d.h:182
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
Definition: irrMath.h:246
bool operator>=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition: vector3d.h:69
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
vector3d< T > & operator+=(const vector3d< T > &other)
Definition: vector3d.h:41
vector3d< s32 > vector3di
Typedef for an integer 3d vector.
Definition: vector3d.h:450
vector3d< T > & operator+=(const T val)
Definition: vector3d.h:43
vector3d< T > & operator/=(const T v)
Definition: vector3d.h:58
vector3d< T > & normalize()
Normalizes the vector.
Definition: vector3d.h:168
vector3d< T > operator/(const T v) const
Definition: vector3d.h:57
vector3d(T n)
Constructor with the same value for all elements.
Definition: vector3d.h:30
T dotProduct(const vector3d< T > &other) const
Get the dot product with another vector.
Definition: vector3d.h:125
bool operator>(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition: vector3d.h:85
vector3d< T > operator+(const T val) const
Definition: vector3d.h:42
Self reallocating template array (like stl vector) with additional features.
Definition: irrArray.h:22
vector3d< T > operator-() const
Definition: vector3d.h:36
vector3d< T > & operator *=(const vector3d< T > &other)
Definition: vector3d.h:51
vector3d< T > & interpolate(const vector3d< T > &a, const vector3d< T > &b, f64 d)
Sets this vector to the linearly interpolated vector between a and b.
Definition: vector3d.h:278
vector3d< T > getInterpolated_quadratic(const vector3d< T > &v2, const vector3d< T > &v3, f64 d) const
Creates a quadratically interpolated vector between this and two other vectors.
Definition: vector3d.h:259
T Z
Z coordinate of the vector.
Definition: vector3d.h:416
bool isBetweenPoints(const vector3d< T > &begin, const vector3d< T > &end) const
Returns if this vector interpreted as a point is on a line between two other points.
Definition: vector3d.h:157
void getAs3Values(T *array) const
Fills an array of 3 values with the vector data (usually floats).
Definition: vector3d.h:401
T getDistanceFrom(const vector3d< T > &other) const
Get distance from another point.
Definition: vector3d.h:132
T getLength() const
Get length of the vector.
Definition: vector3d.h:117
vector3d< T > getSphericalCoordinateAngles() const
Get the spherical coordinate angles.
Definition: vector3d.h:332
T getLengthSQ() const
Get squared length of the vector.
Definition: vector3d.h:122
vector3d< T > operator+(const vector3d< T > &other) const
Definition: vector3d.h:40
vector3d< T > getHorizontalAngle() const
Get the rotations that would make a (0,0,1) direction vector point in the same direction as this dire...
Definition: vector3d.h:301
bool operator<=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition: vector3d.h:61
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition: irrMath.h:532
void rotateYZBy(f64 degrees, const vector3d< T > &center=vector3d< T >())
Rotates the vector by a specified number of degrees around the X axis and the specified center.
Definition: vector3d.h:230
void rotateXYBy(f64 degrees, const vector3d< T > &center=vector3d< T >())
Rotates the vector by a specified number of degrees around the Z axis and the specified center.
Definition: vector3d.h:215
vector3d< T > & set(const vector3d< T > &p)
Definition: vector3d.h:114
vector3d< T > operator-(const vector3d< T > &other) const
Definition: vector3d.h:45
vector3d()
Default constructor (null vector).
Definition: vector3d.h:26
vector3d< T > operator *(const vector3d< T > &other) const
Definition: vector3d.h:50