Duckcpp 2.1.0
Duckcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
vector3d.hpp
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 duckcpp/duckcpp.hpp
4
5#ifndef DCPP_POINT_3D_HPP_INCLUDED
6#define DCPP_POINT_3D_HPP_INCLUDED
7
8#include <duckcpp/core/engine/irrMath.hpp>
9
10namespace dcpp
11{
12namespace nub
13{
14
15 template <class T>
16 class vector3d;
17
20
23
25
30 template <class T>
32 {
33 public:
35 vector3d() : X(0), Y(0), Z(0) {}
37 vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
39 explicit vector3d(T n) : X(n), Y(n), Z(n) {}
40
41 // operators
42
43 vector3d<T> operator-() const { return vector3d<T>(-X, -Y, -Z); }
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 val) const { return vector3d<T>(X - val, Y - val, Z - val); }
53 vector3d<T>& operator-=(const T val) { X-=val; Y-=val; Z-=val; 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 { return vector3d<T>(X * v, Y * v, Z * v); }
58 vector3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; }
59
60 vector3d<T> operator/(const vector3d<T>& other) const { return vector3d<T>(X / other.X, Y / other.Y, Z / other.Z); }
61 vector3d<T>& operator/=(const vector3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; }
62 vector3d<T> operator/(const T v) const { return vector3d<T>(X/v, Y/v, Z/v); }
63 vector3d<T>& operator/=(const T v) { X/=v; Y/=v; Z/=v; return *this; }
64
65 T& operator [](dcpp::uint32_kt index)
66 {
67 DCPP_DEBUG_BREAK_IF(index>2) // access violation
68
69 return *(&X+index);
70 }
71
72 const T& operator [](dcpp::uint32_kt index) const
73 {
74 DCPP_DEBUG_BREAK_IF(index>2) // access violation
75
76 return *(&X+index);
77 }
78
80 bool operator<=(const vector3d<T>&other) const
81 {
82 return (X<other.X || dcpp::nub::equals(X, other.X)) ||
85 }
86
88 bool operator>=(const vector3d<T>&other) const
89 {
90 return (X>other.X || dcpp::nub::equals(X, other.X)) ||
93 }
94
96 bool operator<(const vector3d<T>&other) const
97 {
98 return (X<other.X && !dcpp::nub::equals(X, other.X)) ||
101 }
102
104 bool operator>(const vector3d<T>&other) const
105 {
106 return (X>other.X && !dcpp::nub::equals(X, other.X)) ||
109 }
110
112 bool operator==(const vector3d<T>& other) const
113 {
114 return this->equals(other);
115 }
116
117 bool operator!=(const vector3d<T>& other) const
118 {
119 return !this->equals(other);
120 }
121
122 // functions
123
125 bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_Float32 ) const
126 {
127 return dcpp::nub::equals(X, other.X, tolerance) &&
130 }
131
132 vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
133 vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
134
136 T getLength() const { return dcpp::nub::squareroot( X*X + Y*Y + Z*Z ); }
137
139
141 T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
142
145 {
146 return X*other.X + Y*other.Y + Z*other.Z;
147 }
148
150
152 {
153 return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
154 }
155
157
159 {
160 return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
161 }
162
164
167 {
168 return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
169 }
170
172
176 bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
177 {
178 const T f = (end - begin).getLengthSQ();
179 return getDistanceFromSQ(begin) <= f &&
180 getDistanceFromSQ(end) <= f;
181 }
182
184
188 {
189 dcpp::float64_kt length = X*X + Y*Y + Z*Z;
190 if (length == 0 ) // this check isn't an optimization but prevents getting NAN in the sqrt.
191 return *this;
192 length = dcpp::nub::reciprocal_squareroot(length);
193
194 X = (T)(X * length);
195 Y = (T)(Y * length);
196 Z = (T)(Z * length);
197 return *this;
198 }
199
202 {
203 normalize();
204 return (*this *= newlength);
205 }
206
207#if defined(_DCPP_COMPILE_WITH_90_DEGREE_CAMERA)
209 {
211 if (::fabs(l) < 0.000000001)
212 {
213 X = def.X;
214 Y = def.Y;
215 Z = def.Z;
216 }
217 else
218 {
219 l = 1.0 / ::sqrt(l);
221 v = X * l; X = ::fabs(v) < 0.00000001 ? (T)0 : (T)v;
222 v = Y * l; Y = ::fabs(v) < 0.00000001 ? (T)0 : (T)v;
223 v = Z * l; Z = ::fabs(v) < 0.00000001 ? (T)0 : (T)v;
224 }
225 return *this;
226 }
227#define normalize_x() normalize_camera_direction(dcpp::nub::vector3df(1.f, 0.f, 0.f))
228#define normalize_z() normalize_camera_direction(dcpp::nub::vector3df(0.f, 0.f, 1.f))
229#define normalize_y(v) dcpp::nub::vector3df(v).normalize_camera_direction(dcpp::nub::vector3df(0.f, 1.f, 0.f))
230#else
231#define normalize_x() normalize()
232#define normalize_z() normalize()
233#define normalize_y(v) v
234#endif
235
236
239 {
240 X *= -1;
241 Y *= -1;
242 Z *= -1;
243 return *this;
244 }
245
247
250 {
254 X -= center.X;
255 Z -= center.Z;
256 set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
257 X += center.X;
258 Z += center.Z;
259 }
260
262
265 {
269 X -= center.X;
270 Y -= center.Y;
271 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
272 X += center.X;
273 Y += center.Y;
274 }
275
277
280 {
284 Z -= center.Z;
285 Y -= center.Y;
286 set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
287 Z += center.Z;
288 Y += center.Y;
289 }
290
292
297 {
298 const dcpp::float64_kt inv = 1.0 - d;
299 return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
300 }
301
303
309 {
310 // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
311 const dcpp::float64_kt inv = (T) 1.0 - d;
312 const dcpp::float64_kt mul0 = inv * inv;
313 const dcpp::float64_kt mul1 = (T) 2.0 * d * inv;
314 const dcpp::float64_kt mul2 = d * d;
315
316 return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
317 (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
318 (T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
319 }
320
322
328 {
329 X = (T)((dcpp::float64_kt)b.X + ( ( a.X - b.X ) * d ));
330 Y = (T)((dcpp::float64_kt)b.Y + ( ( a.Y - b.Y ) * d ));
331 Z = (T)((dcpp::float64_kt)b.Z + ( ( a.Z - b.Z ) * d ));
332 return *this;
333 }
334
335
337
351 {
353
354 // tmp avoids some precision troubles on some compilers when working with T=dcpp::int32_kt
356 angle.Y = (T)tmp;
357
358 if (angle.Y < 0)
359 angle.Y += 360;
360 if (angle.Y >= 360)
361 angle.Y -= 360;
362
363 const dcpp::float64_kt z1 = dcpp::nub::squareroot(X*X + Z*Z);
364
366 angle.X = (T)tmp;
367
368 if (angle.X < 0)
369 angle.X += 360;
370 if (angle.X >= 360)
371 angle.X -= 360;
372
373 return angle;
374 }
375
377
382 {
384 const dcpp::float64_kt length = X*X + Y*Y + Z*Z;
385
386 if (length)
387 {
388 if (X!=0)
389 {
391 }
392 else if (Z<0)
393 angle.Y=180;
394
395 angle.X = (T)(acos(Y * dcpp::nub::reciprocal_squareroot(length)) * RADTODEG64);
396 }
397 return angle;
398 }
399
401
409 {
416
417 const dcpp::float64_kt srsp = sr*sp;
418 const dcpp::float64_kt crsp = cr*sp;
419
421 ( cp*cy ), ( cp*sy ), ( -sp ),
422 ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
423 ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
424
425 return vector3d<T>(
426 (T)(forwards.X * pseudoMatrix[0] +
427 forwards.Y * pseudoMatrix[3] +
428 forwards.Z * pseudoMatrix[6]),
429 (T)(forwards.X * pseudoMatrix[1] +
430 forwards.Y * pseudoMatrix[4] +
431 forwards.Z * pseudoMatrix[7]),
432 (T)(forwards.X * pseudoMatrix[2] +
433 forwards.Y * pseudoMatrix[5] +
434 forwards.Z * pseudoMatrix[8]));
435 }
436
438
440 void getAs4Values(T* array) const
441 {
442 array[0] = X;
443 array[1] = Y;
444 array[2] = Z;
445 array[3] = 0;
446 }
447
449
450 void getAs3Values(T* array) const
451 {
452 array[0] = X;
453 array[1] = Y;
454 array[2] = Z;
455 }
456
457
460
463
466 };
467
469 // Implementer note: inline keyword needed due to template specialization for dcpp::int32_kt. Otherwise put specialization into a .cpp
470 template <>
471 inline vector3di vector3di::operator /(dcpp::int32_kt val) const {return dcpp::nub::vector3di(X/val,Y/val,Z/val);}
472 template <>
473 inline vector3di& vector3di::operator /=(dcpp::int32_kt val) {X/=val;Y/=val;Z/=val; return *this;}
474
475 template <>
477 {
478 vector3di angle;
479 const dcpp::float64_kt length = X*X + Y*Y + Z*Z;
480
481 if (length)
482 {
483 if (X!=0)
484 {
485 angle.Y = round32((dcpp::float32_kt)(atan2((dcpp::float64_kt)Z,(dcpp::float64_kt)X) * RADTODEG64));
486 }
487 else if (Z<0)
488 angle.Y=180;
489
490 angle.X = round32((dcpp::float32_kt)(acos(Y * dcpp::nub::reciprocal_squareroot(length)) * RADTODEG64));
491 }
492 return angle;
493 }
494
496 template<class S, class T>
497 vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
498
499} // end namespace nub
500} // end namespace dcpp
501
502#endif
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.hpp:23
3d vector template class with lots of operators and methods.
Definition vector3d.hpp:32
void rotateYZBy(dcpp::float64_kt 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.hpp:279
void rotateXZBy(dcpp::float64_kt 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.hpp:249
T X
X coordinate of the vector.
Definition vector3d.hpp:459
T getLengthSQ() const
Get squared length of the vector.
Definition vector3d.hpp:141
void rotateXYBy(dcpp::float64_kt 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.hpp:264
void getAs3Values(T *array) const
Fills an array of 3 values with the vector data (usually floats).
Definition vector3d.hpp:450
T getDistanceFromSQ(const vector3d< T > &other) const
Returns squared distance from another point.
Definition vector3d.hpp:158
bool operator>(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.hpp:104
vector3d< T > getSphericalCoordinateAngles() const
Get the spherical coordinate angles.
Definition vector3d.hpp:381
vector3d< T > & setLength(T newlength)
Sets the length of the vector to a new value.
Definition vector3d.hpp:201
vector3d< T > & invert()
Inverts the vector.
Definition vector3d.hpp:238
T getLength() const
Get length of the vector.
Definition vector3d.hpp:136
vector3d< T > getInterpolated_quadratic(const vector3d< T > &v2, const vector3d< T > &v3, dcpp::float64_kt d) const
Creates a quadratically interpolated vector between this and two other vectors.
Definition vector3d.hpp:308
vector3d< T > rotationToDirection(const vector3d< T > &forwards=vector3d< T >(0, 0, 1)) const
Builds a direction vector from (this) rotation vector.
Definition vector3d.hpp:408
bool operator<(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.hpp:96
vector3d(T n)
Constructor with the same value for all elements.
Definition vector3d.hpp:39
T Y
Y coordinate of the vector.
Definition vector3d.hpp:462
bool operator>=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition vector3d.hpp:88
vector3d< T > & interpolate(const vector3d< T > &a, const vector3d< T > &b, dcpp::float64_kt d)
Sets this vector to the linearly interpolated vector between a and b.
Definition vector3d.hpp:327
T Z
Z coordinate of the vector.
Definition vector3d.hpp:465
T dotProduct(const vector3d< T > &other) const
Get the dot product with another vector.
Definition vector3d.hpp:144
vector3d< T > & normalize()
Normalizes the vector.
Definition vector3d.hpp:187
bool equals(const vector3d< T > &other, const T tolerance=(T) ROUNDING_ERROR_Float32) const
returns if this vector equals the other one, taking floating point rounding errors into account
Definition vector3d.hpp:125
vector3d< T > crossProduct(const vector3d< T > &p) const
Calculates the cross product with another vector.
Definition vector3d.hpp:166
T getDistanceFrom(const vector3d< T > &other) const
Get distance from another point.
Definition vector3d.hpp:151
bool operator==(const vector3d< T > &other) const
use weak float compare
Definition vector3d.hpp:112
vector3d()
Default constructor (null vector).
Definition vector3d.hpp:35
vector3d< T > getInterpolated(const vector3d< T > &other, dcpp::float64_kt d) const
Creates an interpolated vector between this vector and another vector.
Definition vector3d.hpp:296
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.hpp:176
void getAs4Values(T *array) const
Fills an array of 4 values with the vector data (usually floats).
Definition vector3d.hpp:440
vector3d(T nx, T ny, T nz)
Constructor with three different values.
Definition vector3d.hpp:37
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.hpp:350
vector3d< dcpp::int32_kt > vector3di
using type alias for an integer 3d vector.
Definition vector3d.hpp:22
const dcpp::float64_kt DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.hpp:78
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.hpp:243
const dcpp::float64_kt RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.hpp:81
As of Duckcpp 1.6, position2d is a synonym for vector2d.
Definition shared_device.hpp:34
double float64_kt
64 bit floating point variable.
Definition irrTypes.hpp:112
unsigned int uint32_kt
32 bit unsigned variable.
Definition irrTypes.hpp:64
float float32_kt
32 bit floating point variable.
Definition irrTypes.hpp:108
signed int int32_kt
32 bit signed variable.
Definition irrTypes.hpp:72

Duckcpp    @cppfx.xyz