Irrlicht 3D Engine
 
Loading...
Searching...
No Matches
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
10namespace irr
11{
12namespace core
13{
14
16
21 template <class T>
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) {}
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) &&
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
126 {
127 return X*other.X + Y*other.Y + Z*other.Z;
128 }
129
131
133 {
134 return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
135 }
136
138
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;
174
175 X = (T)(X * length);
176 Y = (T)(Y * length);
177 Z = (T)(Z * length);
178 return *this;
179 }
180
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
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
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
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 {
304
305 const f64 tmp = (atan2((f64)X, (f64)Z) * RADTODEG64);
306 angle.Y = (T)tmp;
307
308 if (angle.Y < 0)
309 angle.Y += 360;
310 if (angle.Y >= 360)
311 angle.Y -= 360;
312
313 const f64 z1 = core::squareroot(X*X + Z*Z);
314
315 angle.X = (T)(atan2((f64)z1, (f64)Y) * RADTODEG64 - 90.0);
316
317 if (angle.X < 0)
318 angle.X += 360;
319 if (angle.X >= 360)
320 angle.X -= 360;
321
322 return angle;
323 }
324
326
331 {
333 const f64 length = X*X + Y*Y + Z*Z;
334
335 if (length)
336 {
337 if (X!=0)
338 {
339 angle.Y = (T)(atan2((f64)Z,(f64)X) * RADTODEG64);
340 }
341 else if (Z<0)
342 angle.Y=180;
343
345 }
346 return angle;
347 }
348
350
358 {
359 const f64 cr = cos( core::DEGTORAD64 * X );
360 const f64 sr = sin( core::DEGTORAD64 * X );
361 const f64 cp = cos( core::DEGTORAD64 * Y );
362 const f64 sp = sin( core::DEGTORAD64 * Y );
363 const f64 cy = cos( core::DEGTORAD64 * Z );
364 const f64 sy = sin( core::DEGTORAD64 * Z );
365
366 const f64 srsp = sr*sp;
367 const f64 crsp = cr*sp;
368
369 const f64 pseudoMatrix[] = {
370 ( cp*cy ), ( cp*sy ), ( -sp ),
371 ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
372 ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
373
374 return vector3d<T>(
375 (T)(forwards.X * pseudoMatrix[0] +
376 forwards.Y * pseudoMatrix[3] +
377 forwards.Z * pseudoMatrix[6]),
378 (T)(forwards.X * pseudoMatrix[1] +
379 forwards.Y * pseudoMatrix[4] +
380 forwards.Z * pseudoMatrix[7]),
381 (T)(forwards.X * pseudoMatrix[2] +
382 forwards.Y * pseudoMatrix[5] +
383 forwards.Z * pseudoMatrix[8]));
384 }
385
387
389 void getAs4Values(T* array) const
390 {
391 array[0] = X;
392 array[1] = Y;
393 array[2] = Z;
394 array[3] = 0;
395 }
396
398
399 void getAs3Values(T* array) const
400 {
401 array[0] = X;
402 array[1] = Y;
403 array[2] = Z;
404 }
405
406
409
412
415 };
416
418 // Implementor note: inline keyword needed due to template specialization for s32. Otherwise put specialization into a .cpp
419 template <>
421 template <>
422 inline vector3d<s32>& vector3d<s32>::operator /=(s32 val) {X/=val;Y/=val;Z/=val; return *this;}
423
424 template <>
426 {
428 const f64 length = X*X + Y*Y + Z*Z;
429
430 if (length)
431 {
432 if (X!=0)
433 {
434 angle.Y = round32((f32)(atan2((f64)Z,(f64)X) * RADTODEG64));
435 }
436 else if (Z<0)
437 angle.Y=180;
438
440 }
441 return angle;
442 }
443
446
449
451 template<class S, class T>
452 vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
453
454} // end namespace core
455} // end namespace irr
456
457#endif
458
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.h:22
aabbox3d()
Default Constructor.
Definition aabbox3d.h:26
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
3d vector template class with lots of operators and methods.
Definition vector3d.h:23
vector3d< T > operator/(const T v) const
Definition vector3d.h:57
T getDistanceFromSQ(const vector3d< T > &other) const
Returns squared distance from another point.
Definition vector3d.h:139
vector3d(const vector3d< T > &other)
Copy constructor.
Definition vector3d.h:32
vector3d(T n)
Constructor with the same value for all elements.
Definition vector3d.h:30
vector3d(T nx, T ny, T nz)
Constructor with three different values.
Definition vector3d.h:28
vector3d< T > & setLength(T newlength)
Sets the length of the vector to a new value.
Definition vector3d.h:182
vector3d< T > & set(const vector3d< T > &p)
Definition vector3d.h:114
vector3d< T > operator/(const vector3d< T > &other) const
Definition vector3d.h:55
vector3d< T > & operator-=(const T val)
Definition vector3d.h:48
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
bool operator<(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.h:77
vector3d< T > operator-(const vector3d< T > &other) const
Definition vector3d.h:45
T getLengthSQ() const
Get squared length of the vector.
Definition vector3d.h:122
vector3d< T > & operator*=(const vector3d< T > &other)
Definition vector3d.h:51
vector3d< T > operator*(const T v) const
Definition vector3d.h:52
bool operator!=(const vector3d< T > &other) const
Definition vector3d.h:98
vector3d< T > & operator=(const vector3d< T > &other)
Definition vector3d.h:38
bool operator<=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition vector3d.h:61
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 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
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:42
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) const
Definition vector3d.h:50
vector3d< T > & operator/=(const T v)
Definition vector3d.h:58
T getLength() const
Get length of the vector.
Definition vector3d.h:117
vector3d< T > & operator/=(const vector3d< T > &other)
Definition vector3d.h:56
vector3d< T > & operator-=(const vector3d< T > &other)
Definition vector3d.h:46
vector3d< T > getInterpolated(const vector3d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition vector3d.h:247
bool operator>=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition vector3d.h:69
T X
X coordinate of the vector.
Definition vector3d.h:408
vector3d< T > operator-(const T val) const
Definition vector3d.h:47
vector3d< T > & operator+=(const T val)
Definition vector3d.h:43
vector3d< T > & normalize()
Normalizes the vector.
Definition vector3d.h:168
vector3d< T > & operator*=(const T v)
Definition vector3d.h:53
void getAs4Values(T *array) const
Fills an array of 4 values with the vector data (usually floats).
Definition vector3d.h:389
vector3d< T > & operator+=(const vector3d< T > &other)
Definition vector3d.h:41
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
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
vector3d< T > & set(const T nx, const T ny, const T nz)
Definition vector3d.h:113
T getDistanceFrom(const vector3d< T > &other) const
Get distance from another point.
Definition vector3d.h:132
vector3d< T > rotationToDirection(const vector3d< T > &forwards=vector3d< T >(0, 0, 1)) const
Builds a direction vector from (this) rotation vector.
Definition vector3d.h:357
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
T Z
Z coordinate of the vector.
Definition vector3d.h:414
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 > & invert()
Inverts the vector.
Definition vector3d.h:189
bool operator>(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.h:85
T dotProduct(const vector3d< T > &other) const
Get the dot product with another vector.
Definition vector3d.h:125
vector3d()
Default constructor (null vector).
Definition vector3d.h:26
vector3d< T > getSphericalCoordinateAngles() const
Get the spherical coordinate angles.
Definition vector3d.h:330
T Y
Y coordinate of the vector.
Definition vector3d.h:411
vector3d< T > operator+(const vector3d< T > &other) const
Definition vector3d.h:40
void getAs3Values(T *array) const
Fills an array of 3 values with the vector data (usually floats).
Definition vector3d.h:399
vector3d< T > operator-() const
Definition vector3d.h:36
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:445
CMatrix4< T > operator*(const T scalar, const CMatrix4< T > &mat)
Definition matrix4.h:2228
vector3d< s32 > vector3di
Typedef for an integer 3d vector.
Definition vector3d.h:448
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.h:80
const f32 ROUNDING_ERROR_f32
Definition irrMath.h:49
bool equals(const f64 a, const f64 b, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals b, taking possible rounding errors into account
Definition irrMath.h:185
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition irrMath.h:497
REALINLINE s32 round32(f32 x)
Definition irrMath.h:680
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.h:83
REALINLINE f32 squareroot(const f32 f)
Definition irrMath.h:471
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
float f32
32 bit floating point variable.
Definition irrTypes.h:104
double f64
64 bit floating point variable.
Definition irrTypes.h:108
signed int s32
32 bit signed variable.
Definition irrTypes.h:66