Jimcpp 2.1.0
Jimcpp 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 jimcpp/jimcpp.hpp
4
5#ifndef JPP_POINT_3D_HPP_INCLUDED
6#define JPP_POINT_3D_HPP_INCLUDED
7
8#include <jimcpp/core/engine/irrMath.hpp>
9
10namespace jpp
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) {}
31
32 // operators
33
34 vector3d<T> operator-() const { return vector3d<T>(-X, -Y, -Z); }
35
36 vector3d<T> operator+(const vector3d<T>& other) const { return vector3d<T>(X + other.X, Y + other.Y, Z + other.Z); }
37 vector3d<T>& operator+=(const vector3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; }
38 vector3d<T> operator+(const T val) const { return vector3d<T>(X + val, Y + val, Z + val); }
39 vector3d<T>& operator+=(const T val) { X+=val; Y+=val; Z+=val; return *this; }
40
41 vector3d<T> operator-(const vector3d<T>& other) const { return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z); }
42 vector3d<T>& operator-=(const vector3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; }
43 vector3d<T> operator-(const T val) const { return vector3d<T>(X - val, Y - val, Z - val); }
44 vector3d<T>& operator-=(const T val) { X-=val; Y-=val; Z-=val; return *this; }
45
46 vector3d<T> operator*(const vector3d<T>& other) const { return vector3d<T>(X * other.X, Y * other.Y, Z * other.Z); }
47 vector3d<T>& operator*=(const vector3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; }
48 vector3d<T> operator*(const T v) const { return vector3d<T>(X * v, Y * v, Z * v); }
49 vector3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; }
50
51 vector3d<T> operator/(const vector3d<T>& other) const { return vector3d<T>(X / other.X, Y / other.Y, Z / other.Z); }
52 vector3d<T>& operator/=(const vector3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; }
53 vector3d<T> operator/(const T v) const { return vector3d<T>(X/v, Y/v, Z/v); }
54 vector3d<T>& operator/=(const T v) { X/=v; Y/=v; Z/=v; return *this; }
55
56 T& operator [](u32 index)
57 {
58 JPP_DEBUG_BREAK_IF(index>2) // access violation
59
60 return *(&X+index);
61 }
62
63 const T& operator [](u32 index) const
64 {
65 JPP_DEBUG_BREAK_IF(index>2) // access violation
66
67 return *(&X+index);
68 }
69
71 bool operator<=(const vector3d<T>&other) const
72 {
73 return (X<other.X || core::equals(X, other.X)) ||
74 (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y))) ||
75 (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z<other.Z || core::equals(Z, other.Z)));
76 }
77
79 bool operator>=(const vector3d<T>&other) const
80 {
81 return (X>other.X || core::equals(X, other.X)) ||
82 (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))) ||
83 (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z>other.Z || core::equals(Z, other.Z)));
84 }
85
87 bool operator<(const vector3d<T>&other) const
88 {
89 return (X<other.X && !core::equals(X, other.X)) ||
90 (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y)) ||
91 (core::equals(X, other.X) && core::equals(Y, other.Y) && Z<other.Z && !core::equals(Z, other.Z));
92 }
93
95 bool operator>(const vector3d<T>&other) const
96 {
97 return (X>other.X && !core::equals(X, other.X)) ||
98 (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)) ||
99 (core::equals(X, other.X) && core::equals(Y, other.Y) && Z>other.Z && !core::equals(Z, other.Z));
100 }
101
103 bool operator==(const vector3d<T>& other) const
104 {
105 return this->equals(other);
106 }
107
108 bool operator!=(const vector3d<T>& other) const
109 {
110 return !this->equals(other);
111 }
112
113 // functions
114
116 bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
117 {
118 return core::equals(X, other.X, tolerance) &&
121 }
122
123 vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
124 vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
125
127 T getLength() const { return core::squareroot( X*X + Y*Y + Z*Z ); }
128
130
132 T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
133
136 {
137 return X*other.X + Y*other.Y + Z*other.Z;
138 }
139
141
143 {
144 return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
145 }
146
148
150 {
151 return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
152 }
153
155
158 {
159 return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
160 }
161
163
167 bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
168 {
169 const T f = (end - begin).getLengthSQ();
170 return getDistanceFromSQ(begin) <= f &&
171 getDistanceFromSQ(end) <= f;
172 }
173
175
179 {
180 f64 length = X*X + Y*Y + Z*Z;
181 if (length == 0 ) // this check isn't an optimization but prevents getting NAN in the sqrt.
182 return *this;
183 length = core::reciprocal_squareroot(length);
184
185 X = (T)(X * length);
186 Y = (T)(Y * length);
187 Z = (T)(Z * length);
188 return *this;
189 }
190
193 {
194 normalize();
195 return (*this *= newlength);
196 }
197
198#if defined(_JPP_COMPILE_WITH_90_DEGREE_CAMERA)
200 {
201 f64 l = (f64)X * X + (f64)Y * Y + (f64)Z * Z;
202 if (::fabs(l) < 0.000000001)
203 {
204 X = def.X;
205 Y = def.Y;
206 Z = def.Z;
207 }
208 else
209 {
210 l = 1.0 / ::sqrt(l);
211 f64 v;
212 v = X * l; X = ::fabs(v) < 0.00000001 ? (T)0 : (T)v;
213 v = Y * l; Y = ::fabs(v) < 0.00000001 ? (T)0 : (T)v;
214 v = Z * l; Z = ::fabs(v) < 0.00000001 ? (T)0 : (T)v;
215 }
216 return *this;
217 }
218#define normalize_x() normalize_camera_direction(core::vector3df(1.f, 0.f, 0.f))
219#define normalize_z() normalize_camera_direction(core::vector3df(0.f, 0.f, 1.f))
220#define normalize_y(v) core::vector3df(v).normalize_camera_direction(core::vector3df(0.f, 1.f, 0.f))
221#else
222#define normalize_x() normalize()
223#define normalize_z() normalize()
224#define normalize_y(v) v
225#endif
226
227
230 {
231 X *= -1;
232 Y *= -1;
233 Z *= -1;
234 return *this;
235 }
236
238
241 {
243 f64 cs = cos(degrees);
244 f64 sn = sin(degrees);
245 X -= center.X;
246 Z -= center.Z;
247 set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
248 X += center.X;
249 Z += center.Z;
250 }
251
253
256 {
258 f64 cs = cos(degrees);
259 f64 sn = sin(degrees);
260 X -= center.X;
261 Y -= center.Y;
262 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
263 X += center.X;
264 Y += center.Y;
265 }
266
268
271 {
273 f64 cs = cos(degrees);
274 f64 sn = sin(degrees);
275 Z -= center.Z;
276 Y -= center.Y;
277 set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
278 Z += center.Z;
279 Y += center.Y;
280 }
281
283
288 {
289 const f64 inv = 1.0 - d;
290 return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
291 }
292
294
300 {
301 // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
302 const f64 inv = (T) 1.0 - d;
303 const f64 mul0 = inv * inv;
304 const f64 mul1 = (T) 2.0 * d * inv;
305 const f64 mul2 = d * d;
306
307 return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
308 (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
309 (T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
310 }
311
313
319 {
320 X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
321 Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
322 Z = (T)((f64)b.Z + ( ( a.Z - b.Z ) * d ));
323 return *this;
324 }
325
326
328
342 {
344
345 // tmp avoids some precision troubles on some compilers when working with T=s32
346 f64 tmp = (atan2((f64)X, (f64)Z) * RADTODEG64);
347 angle.Y = (T)tmp;
348
349 if (angle.Y < 0)
350 angle.Y += 360;
351 if (angle.Y >= 360)
352 angle.Y -= 360;
353
354 const f64 z1 = core::squareroot(X*X + Z*Z);
355
356 tmp = (atan2((f64)z1, (f64)Y) * RADTODEG64 - 90.0);
357 angle.X = (T)tmp;
358
359 if (angle.X < 0)
360 angle.X += 360;
361 if (angle.X >= 360)
362 angle.X -= 360;
363
364 return angle;
365 }
366
368
373 {
375 const f64 length = X*X + Y*Y + Z*Z;
376
377 if (length)
378 {
379 if (X!=0)
380 {
381 angle.Y = (T)(atan2((f64)Z,(f64)X) * RADTODEG64);
382 }
383 else if (Z<0)
384 angle.Y=180;
385
386 angle.X = (T)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64);
387 }
388 return angle;
389 }
390
392
400 {
401 const f64 cr = cos( core::DEGTORAD64 * X );
402 const f64 sr = sin( core::DEGTORAD64 * X );
403 const f64 cp = cos( core::DEGTORAD64 * Y );
404 const f64 sp = sin( core::DEGTORAD64 * Y );
405 const f64 cy = cos( core::DEGTORAD64 * Z );
406 const f64 sy = sin( core::DEGTORAD64 * Z );
407
408 const f64 srsp = sr*sp;
409 const f64 crsp = cr*sp;
410
411 const f64 pseudoMatrix[] = {
412 ( cp*cy ), ( cp*sy ), ( -sp ),
413 ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
414 ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
415
416 return vector3d<T>(
417 (T)(forwards.X * pseudoMatrix[0] +
418 forwards.Y * pseudoMatrix[3] +
419 forwards.Z * pseudoMatrix[6]),
420 (T)(forwards.X * pseudoMatrix[1] +
421 forwards.Y * pseudoMatrix[4] +
422 forwards.Z * pseudoMatrix[7]),
423 (T)(forwards.X * pseudoMatrix[2] +
424 forwards.Y * pseudoMatrix[5] +
425 forwards.Z * pseudoMatrix[8]));
426 }
427
429
431 void getAs4Values(T* array) const
432 {
433 array[0] = X;
434 array[1] = Y;
435 array[2] = Z;
436 array[3] = 0;
437 }
438
440
441 void getAs3Values(T* array) const
442 {
443 array[0] = X;
444 array[1] = Y;
445 array[2] = Z;
446 }
447
448
451
454
457 };
458
460 // Implementer note: inline keyword needed due to template specialization for s32. Otherwise put specialization into a .cpp
461 template <>
463 template <>
464 inline vector3d<s32>& vector3d<s32>::operator /=(s32 val) {X/=val;Y/=val;Z/=val; return *this;}
465
466 template <>
467 inline vector3d<s32> vector3d<s32>::getSphericalCoordinateAngles() const
468 {
469 vector3d<s32> angle;
470 const f64 length = X*X + Y*Y + Z*Z;
471
472 if (length)
473 {
474 if (X!=0)
475 {
476 angle.Y = round32((f32)(atan2((f64)Z,(f64)X) * RADTODEG64));
477 }
478 else if (Z<0)
479 angle.Y=180;
480
481 angle.X = round32((f32)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64));
482 }
483 return angle;
484 }
485
488
491
493 template<class S, class T>
494 vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
495
496} // end namespace core
497} // end namespace jpp
498
499#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:23
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.hpp:270
vector3d(T n)
Constructor with the same value for all elements.
Definition vector3d.hpp:30
T X
X coordinate of the vector.
Definition vector3d.hpp:450
vector3d(T nx, T ny, T nz)
Constructor with three different values.
Definition vector3d.hpp:28
void getAs4Values(T *array) const
Fills an array of 4 values with the vector data (usually floats).
Definition vector3d.hpp:431
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.hpp:299
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.hpp:318
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.hpp:255
vector3d< T > rotationToDirection(const vector3d< T > &forwards=vector3d< T >(0, 0, 1)) const
Builds a direction vector from (this) rotation vector.
Definition vector3d.hpp:399
bool operator==(const vector3d< T > &other) const
use weak float compare
Definition vector3d.hpp:103
vector3d< T > & invert()
Inverts the vector.
Definition vector3d.hpp:229
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:167
T getDistanceFromSQ(const vector3d< T > &other) const
Returns squared distance from another point.
Definition vector3d.hpp:149
vector3d< T > & setLength(T newlength)
Sets the length of the vector to a new value.
Definition vector3d.hpp:192
T Z
Z coordinate of the vector.
Definition vector3d.hpp:456
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.hpp:240
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.hpp:116
vector3d< T > getInterpolated(const vector3d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition vector3d.hpp:287
T getDistanceFrom(const vector3d< T > &other) const
Get distance from another point.
Definition vector3d.hpp:142
T getLength() const
Get length of the vector.
Definition vector3d.hpp:127
T getLengthSQ() const
Get squared length of the vector.
Definition vector3d.hpp:132
vector3d< T > crossProduct(const vector3d< T > &p) const
Calculates the cross product with another vector.
Definition vector3d.hpp:157
vector3d< T > & normalize()
Normalizes the vector.
Definition vector3d.hpp:178
bool operator>=(const vector3d< T > &other) const
sort in order X, Y, Z. Equality with rounding tolerance.
Definition vector3d.hpp:79
T dotProduct(const vector3d< T > &other) const
Get the dot product with another vector.
Definition vector3d.hpp:135
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:341
bool operator<(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.hpp:87
T Y
Y coordinate of the vector.
Definition vector3d.hpp:453
vector3d< T > getSphericalCoordinateAngles() const
Get the spherical coordinate angles.
Definition vector3d.hpp:372
void getAs3Values(T *array) const
Fills an array of 3 values with the vector data (usually floats).
Definition vector3d.hpp:441
bool operator>(const vector3d< T > &other) const
sort in order X, Y, Z. Difference must be above rounding tolerance.
Definition vector3d.hpp:95
vector3d()
Default constructor (null vector).
Definition vector3d.hpp:26
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.hpp:81
const f64 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
As of Jimcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
signed int s32
32 bit signed variable.
Definition irrTypes.hpp:72
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64
double f64
64 bit floating point variable.
Definition irrTypes.hpp:114
float f32
32 bit floating point variable.
Definition irrTypes.hpp:110

Jimcpp    @cppfx.xyz

K