arsa  2.7
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  {
62  _IRR_DEBUG_BREAK_IF(index>2) // access violation
63 
64  return *(&X+index);
65  }
66 
67  const T& operator [](u32 index) const
68  {
69  _IRR_DEBUG_BREAK_IF(index>2) // access violation
70 
71  return *(&X+index);
72  }
73 
75  bool operator<=(const vector3d<T>&other) const
76  {
77  return (X<other.X || core::equals(X, other.X)) ||
78  (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y))) ||
79  (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z<other.Z || core::equals(Z, other.Z)));
80  }
81 
83  bool operator>=(const vector3d<T>&other) const
84  {
85  return (X>other.X || core::equals(X, other.X)) ||
86  (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))) ||
87  (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z>other.Z || core::equals(Z, other.Z)));
88  }
89 
91  bool operator<(const vector3d<T>&other) const
92  {
93  return (X<other.X && !core::equals(X, other.X)) ||
94  (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y)) ||
95  (core::equals(X, other.X) && core::equals(Y, other.Y) && Z<other.Z && !core::equals(Z, other.Z));
96  }
97 
99  bool operator>(const vector3d<T>&other) const
100  {
101  return (X>other.X && !core::equals(X, other.X)) ||
102  (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)) ||
103  (core::equals(X, other.X) && core::equals(Y, other.Y) && Z>other.Z && !core::equals(Z, other.Z));
104  }
105 
107  bool operator==(const vector3d<T>& other) const
108  {
109  return this->equals(other);
110  }
111 
112  bool operator!=(const vector3d<T>& other) const
113  {
114  return !this->equals(other);
115  }
116 
117  // functions
118 
120  bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
121  {
122  return core::equals(X, other.X, tolerance) &&
123  core::equals(Y, other.Y, tolerance) &&
124  core::equals(Z, other.Z, tolerance);
125  }
126 
127  vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
128  vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
129 
131  T getLength() const { return core::squareroot( X*X + Y*Y + Z*Z ); }
132 
134 
136  T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
137 
139  T dotProduct(const vector3d<T>& other) const
140  {
141  return X*other.X + Y*other.Y + Z*other.Z;
142  }
143 
145 
146  T getDistanceFrom(const vector3d<T>& other) const
147  {
148  return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
149  }
150 
152 
153  T getDistanceFromSQ(const vector3d<T>& other) const
154  {
155  return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
156  }
157 
159 
162  {
163  return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
164  }
165 
167 
171  bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
172  {
173  const T f = (end - begin).getLengthSQ();
174  return getDistanceFromSQ(begin) <= f &&
175  getDistanceFromSQ(end) <= f;
176  }
177 
179 
183  {
184  f64 length = X*X + Y*Y + Z*Z;
185  if (length == 0 ) // this check isn't an optimization but prevents getting NAN in the sqrt.
186  return *this;
188 
189  X = (T)(X * length);
190  Y = (T)(Y * length);
191  Z = (T)(Z * length);
192  return *this;
193  }
194 
196  vector3d<T>& setLength(T newlength)
197  {
198  normalize();
199  return (*this *= newlength);
200  }
201 
204  {
205  X *= -1;
206  Y *= -1;
207  Z *= -1;
208  return *this;
209  }
210 
212 
214  void rotateXZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
215  {
216  degrees *= DEGTORAD64;
217  f64 cs = cos(degrees);
218  f64 sn = sin(degrees);
219  X -= center.X;
220  Z -= center.Z;
221  set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
222  X += center.X;
223  Z += center.Z;
224  }
225 
227 
229  void rotateXYBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
230  {
231  degrees *= DEGTORAD64;
232  f64 cs = cos(degrees);
233  f64 sn = sin(degrees);
234  X -= center.X;
235  Y -= center.Y;
236  set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
237  X += center.X;
238  Y += center.Y;
239  }
240 
242 
244  void rotateYZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
245  {
246  degrees *= DEGTORAD64;
247  f64 cs = cos(degrees);
248  f64 sn = sin(degrees);
249  Z -= center.Z;
250  Y -= center.Y;
251  set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
252  Z += center.Z;
253  Y += center.Y;
254  }
255 
257 
262  {
263  const f64 inv = 1.0 - d;
264  return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
265  }
266 
268 
274  {
275  // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
276  const f64 inv = (T) 1.0 - d;
277  const f64 mul0 = inv * inv;
278  const f64 mul1 = (T) 2.0 * d * inv;
279  const f64 mul2 = d * d;
280 
281  return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
282  (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
283  (T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
284  }
285 
287 
293  {
294  X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
295  Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
296  Z = (T)((f64)b.Z + ( ( a.Z - b.Z ) * d ));
297  return *this;
298  }
299 
300 
302 
316  {
318 
319  // tmp avoids some precision troubles on some compilers when working with T=s32
320  f64 tmp = (atan2((f64)X, (f64)Z) * RADTODEG64);
321  angle.Y = (T)tmp;
322 
323  if (angle.Y < 0)
324  angle.Y += 360;
325  if (angle.Y >= 360)
326  angle.Y -= 360;
327 
328  const f64 z1 = core::squareroot(X*X + Z*Z);
329 
330  tmp = (atan2((f64)z1, (f64)Y) * RADTODEG64 - 90.0);
331  angle.X = (T)tmp;
332 
333  if (angle.X < 0)
334  angle.X += 360;
335  if (angle.X >= 360)
336  angle.X -= 360;
337 
338  return angle;
339  }
340 
342 
347  {
349  const f64 length = X*X + Y*Y + Z*Z;
350 
351  if (length)
352  {
353  if (X!=0)
354  {
355  angle.Y = (T)(atan2((f64)Z,(f64)X) * RADTODEG64);
356  }
357  else if (Z<0)
358  angle.Y=180;
359 
361  }
362  return angle;
363  }
364 
366 
373  vector3d<T> rotationToDirection(const vector3d<T> & forwards = vector3d<T>(0, 0, 1)) const
374  {
375  const f64 cr = cos( core::DEGTORAD64 * X );
376  const f64 sr = sin( core::DEGTORAD64 * X );
377  const f64 cp = cos( core::DEGTORAD64 * Y );
378  const f64 sp = sin( core::DEGTORAD64 * Y );
379  const f64 cy = cos( core::DEGTORAD64 * Z );
380  const f64 sy = sin( core::DEGTORAD64 * Z );
381 
382  const f64 srsp = sr*sp;
383  const f64 crsp = cr*sp;
384 
385  const f64 pseudoMatrix[] = {
386  ( cp*cy ), ( cp*sy ), ( -sp ),
387  ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
388  ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
389 
390  return vector3d<T>(
391  (T)(forwards.X * pseudoMatrix[0] +
392  forwards.Y * pseudoMatrix[3] +
393  forwards.Z * pseudoMatrix[6]),
394  (T)(forwards.X * pseudoMatrix[1] +
395  forwards.Y * pseudoMatrix[4] +
396  forwards.Z * pseudoMatrix[7]),
397  (T)(forwards.X * pseudoMatrix[2] +
398  forwards.Y * pseudoMatrix[5] +
399  forwards.Z * pseudoMatrix[8]));
400  }
401 
403 
405  void getAs4Values(T* array) const
406  {
407  array[0] = X;
408  array[1] = Y;
409  array[2] = Z;
410  array[3] = 0;
411  }
412 
414 
415  void getAs3Values(T* array) const
416  {
417  array[0] = X;
418  array[1] = Y;
419  array[2] = Z;
420  }
421 
422 
424  T X;
425 
427  T Y;
428 
430  T Z;
431  };
432 
434  // Implementer note: inline keyword needed due to template specialization for s32. Otherwise put specialization into a .cpp
435  template <>
437  template <>
438  inline vector3d<s32>& vector3d<s32>::operator /=(s32 val) {X/=val;Y/=val;Z/=val; return *this;}
439 
440  template <>
442  {
444  const f64 length = X*X + Y*Y + Z*Z;
445 
446  if (length)
447  {
448  if (X!=0)
449  {
450  angle.Y = round32((f32)(atan2((f64)Z,(f64)X) * RADTODEG64));
451  }
452  else if (Z<0)
453  angle.Y=180;
454 
456  }
457  return angle;
458  }
459 
462 
465 
467  template<class S, class T>
468  vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
469 
470 } // end namespace core
471 } // end namespace irr
472 
473 #endif
474 
vector3d< T > & invert()
Inverts the vector.
Definition: vector3d.h:203
vector3d< T > & operator-=(const T val)
Definition: vector3d.h:48
const f64 RADTODEG64
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:161
vector3d< T > & operator=(const vector3d< T > &other)
Definition: vector3d.h:38
REALINLINE s32 round32(f32 x)
Definition: irrMath.h:657
T Y
Y coordinate of the vector.
Definition: vector3d.h:427
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:120
GLdouble n
T getDistanceFromSQ(const vector3d< T > &other) const
Returns squared distance from another point.
Definition: vector3d.h:153
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:405
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:261
vector3d(T nx, T ny, T nz)
Constructor with three different values.
Definition: vector3d.h:28
GLuint GLuint end
Definition: SDL_opengl.h:1571
bool operator!=(const vector3d< T > &other) const
Definition: vector3d.h:112
GLfloat GLfloat p
REALINLINE f32 squareroot(const f32 f)
Definition: irrMath.h:495
bool operator<(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition: vector3d.h:91
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:214
vector3d< T > operator-(const T val) const
Definition: vector3d.h:47
T X
X coordinate of the vector.
Definition: vector3d.h:424
vector3d< T > rotationToDirection(const vector3d< T > &forwards=vector3d< T >(0, 0, 1)) const
Builds a direction vector from (this) rotation vector.
Definition: vector3d.h:373
vector3d< T > & set(const T nx, const T ny, const T nz)
Definition: vector3d.h:127
Everything in the Irrlicht Engine can be found in this namespace.
Definition: CARSADPad.h:6
3d vector template class with lots of operators and methods.
Definition: vector3d.h:22
GLfloat GLfloat GLfloat v2
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:107
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition: vector3d.h:461
vector3d(const vector3d< T > &other)
Copy constructor.
Definition: vector3d.h:32
const GLdouble * v
Definition: SDL_opengl.h:2064
vector3d< T > & operator/=(const vector3d< T > &other)
Definition: vector3d.h:56
const f64 DEGTORAD64
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
GLfloat GLfloat GLfloat GLfloat v3
vector3d< T > & setLength(T newlength)
Sets the length of the vector to a new value.
Definition: vector3d.h:196
GLfixed ny
const T & operator [](u32 index) const
Definition: vector3d.h:67
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
GLuint GLfloat * val
bool operator>=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition: vector3d.h:83
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
GLfloat f
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
GLbyte nx
vector3d< T > & operator+=(const vector3d< T > &other)
Definition: vector3d.h:41
vector3d< s32 > vector3di
Typedef for an integer 3d vector.
Definition: vector3d.h:464
vector3d< T > & operator+=(const T val)
Definition: vector3d.h:43
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185
vector3d< T > & operator/=(const T v)
Definition: vector3d.h:58
GLuint index
vector3d< T > & normalize()
Normalizes the vector.
Definition: vector3d.h:182
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
GLfixed GLfixed nz
T dotProduct(const vector3d< T > &other) const
Get the dot product with another vector.
Definition: vector3d.h:139
T & operator [](u32 index)
Definition: vector3d.h:60
bool operator>(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition: vector3d.h:99
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
GLboolean GLboolean GLboolean b
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:292
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:273
T Z
Z coordinate of the vector.
Definition: vector3d.h:430
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:171
void getAs3Values(T *array) const
Fills an array of 3 values with the vector data (usually floats).
Definition: vector3d.h:415
T getDistanceFrom(const vector3d< T > &other) const
Get distance from another point.
Definition: vector3d.h:146
GLfloat angle
GLboolean GLboolean GLboolean GLboolean a
T getLength() const
Get length of the vector.
Definition: vector3d.h:131
vector3d< T > getSphericalCoordinateAngles() const
Get the spherical coordinate angles.
Definition: vector3d.h:346
T getLengthSQ() const
Get squared length of the vector.
Definition: vector3d.h:136
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:315
GLuint GLsizei GLsizei * length
bool operator<=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition: vector3d.h:75
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition: irrMath.h:521
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:244
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:229
vector3d< T > & set(const vector3d< T > &p)
Definition: vector3d.h:128
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