TDME2  1.9.200
Vector3.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/math/fwd-tdme.h>
7 #include <tdme/math/Math.h>
8 #include <tdme/utilities/Float.h>
9 
10 using std::array;
11 
12 using tdme::math::Math;
14 
15 /**
16  * Vector3 class representing vector3 mathematical structure and operations with x, y, z components
17  * @author andreas.drewke
18  */
20 {
21  friend class Matrix4x4;
22  friend class Quaternion;
23  friend class Vector4;
24 
25 private:
26  array<float, 3> data { 0.0f, 0.0f, 0.0f };
27 
28 public:
29  /**
30  * Public constructor
31  */
32  inline Vector3() {
33  }
34 
35  /**
36  * Public constructor
37  * @param x x component
38  * @param y y component
39  * @param z z component
40  */
41  inline Vector3(float x, float y, float z) {
42  data[0] = x;
43  data[1] = y;
44  data[2] = z;
45  }
46 
47  /**
48  * Public constructor
49  * @param vector3 vector3 as array
50  */
51  inline Vector3(const array<float,3>& vector3) {
52  data = vector3;
53  }
54 
55  /**
56  * Public constructor
57  * @param vector3 vector3
58  */
59  inline Vector3(const Vector3& vector3) {
60  data = vector3.data;
61  }
62 
63  /**
64  * Sets this vector3 by its components
65  * @param x x component
66  * @param y y component
67  * @param z z component
68  * @return this vector3
69  */
70  inline Vector3& set(float x, float y, float z) {
71  data[0] = x;
72  data[1] = y;
73  data[2] = z;
74  return *this;
75  }
76 
77  /**
78  * Sets this vector3 by array
79  * @param vector3 vector3 as array
80  * @return this vector3
81  */
82  inline Vector3& set(const array<float, 3>& vector3) {
83  data = vector3;
84  return *this;
85  }
86 
87  /**
88  * Sets this vector3 by given vector3
89  * @param vector3 vector3
90  * @return this vector3
91  */
92  inline Vector3& set(const Vector3& vector3) {
93  data = vector3.data;
94  return *this;
95  }
96 
97  /**
98  * @return x component
99  */
100  inline float getX() const {
101  return data[0];
102  }
103 
104  /**
105  * Sets x component
106  * @param x x component
107  * @return this vector3
108  */
109  inline Vector3& setX(float x) {
110  data[0] = x;
111  return *this;
112  }
113 
114  /**
115  * @return y component
116  */
117  inline float getY() const {
118  return data[1];
119  }
120 
121  /**
122  * Sets y component
123  * @param y y component
124  * @return this vector3
125  */
126  inline Vector3& setY(float y) {
127  data[1] = y;
128  return *this;
129  }
130 
131  /**
132  * @return z component
133  */
134  inline float getZ() const {
135  return data[2];
136  }
137 
138  /**
139  * Sets z component
140  * @param z z component
141  * @return this vector3
142  */
143  inline Vector3& setZ(float z) {
144  data[2] = z;
145  return *this;
146  }
147 
148  /**
149  * Adds a scalar
150  * @param scalar scalar
151  * @return this vector3
152  */
153  inline Vector3& add(float scalar) {
154  data[0] += scalar;
155  data[1] += scalar;
156  data[2] += scalar;
157  return *this;
158  }
159 
160  /**
161  * Adds a vector3
162  * @param vector3 vector3
163  * @return this vector3
164  */
165  inline Vector3& add(const Vector3& vector3) {
166  data[0] += vector3.data[0];
167  data[1] += vector3.data[1];
168  data[2] += vector3.data[2];
169  return *this;
170  }
171 
172  /**
173  * Subtracts a scalar
174  * @param scalar scalar
175  * @return this vector3
176  */
177  inline Vector3& sub(float scalar) {
178  data[0] -= scalar;
179  data[1] -= scalar;
180  data[2] -= scalar;
181  return *this;
182  }
183 
184  /**
185  * Subtracts a vector3
186  * @param vector3 vector3
187  * @return this vector3
188  */
189  inline Vector3& sub(const Vector3& vector3) {
190  data[0] -= vector3.data[0];
191  data[1] -= vector3.data[1];
192  data[2] -= vector3.data[2];
193  return *this;
194  }
195 
196  /**
197  * Scales by scalar
198  * @param scalar scalar
199  * @return this vector3
200  */
201  inline Vector3& scale(float scalar) {
202  data[0] *= scalar;
203  data[1] *= scalar;
204  data[2] *= scalar;
205  return *this;
206  }
207 
208  /**
209  * Scales by vector3
210  * @param vector3 vector3
211  * @return this vector3
212  */
213  inline Vector3& scale(const Vector3& vector3) {
214  data[0] *= vector3.data[0];
215  data[1] *= vector3.data[1];
216  data[2] *= vector3.data[2];
217  return *this;
218  }
219 
220  /**
221  * Compares this vector3 with given vector3
222  * @param vector3 vector3
223  * @param tolerance tolerance per vector3 component
224  * @return equality
225  */
226  inline bool equals(const Vector3& vector3, float tolerance = Math::EPSILON) const {
227  return (this == &vector3) ||
228  (
229  Math::abs(data[0] - vector3.data[0]) < tolerance &&
230  Math::abs(data[1] - vector3.data[1]) < tolerance &&
231  Math::abs(data[2] - vector3.data[2]) < tolerance
232  );
233  }
234 
235  /**
236  * Normalizes this vector3
237  * @return this vector3
238  */
239  inline Vector3& normalize() {
240  auto length = computeLength();
241  data[0] /= length;
242  data[1] /= length;
243  data[2] /= length;
244  return *this;
245  }
246 
247  /**
248  * Computes the dot product of a and b
249  * @param a vector3 a
250  * @param b vector3 b
251  * @return dot product
252  */
253  inline static float computeDotProduct(const Vector3& a, const Vector3& b) {
254  return (a.data[0] * b.data[0]) + (a.data[1] * b.data[1]) + (a.data[2] * b.data[2]);
255  }
256 
257  /**
258  * Computes the cross product of a and b
259  * @param a vector3 a
260  * @param b vector3 b
261  * @return cross product
262  */
263  inline static Vector3 computeCrossProduct(const Vector3& a, const Vector3& b) {
264  return Vector3(
265  (a.data[1] * b.data[2]) - (a.data[2] * b.data[1]),
266  (a.data[2] * b.data[0]) - (a.data[0] * b.data[2]),
267  (a.data[0] * b.data[1]) - (a.data[1] * b.data[0])
268  );
269  }
270 
271  /**
272  * @return the vectors length
273  */
274  inline float computeLength() const {
275  return Math::sqrt((data[0] * data[0]) + (data[1] * data[1]) + (data[2] * data[2]));
276  }
277 
278  /**
279  * @return the vectors length squared
280  */
281  inline float computeLengthSquared() const {
282  return (data[0] * data[0]) + (data[1] * data[1]) + (data[2] * data[2]);
283  }
284 
285  /**
286  * Computes angle between a and b from 0.0 <= angle < 180.0
287  * @param a vector3 a, vector3 to test, must be normalized
288  * @param b vector3 b, vector3 to test against, must be normalized
289  * @return angle
290  */
291  inline static float computeAngle(const Vector3& a, const Vector3& b) {
292  auto result = 180.0 / Math::PI * Math::acos(Math::clamp(Vector3::computeDotProduct(a, b), -1.0f, 1.0f));
293  return result;
294  }
295 
296  /**
297  * Computes angle between a and b from, where 0.0 <= angle < 360.0
298  * @param a vector3 a, vector3 to test, must be normalized
299  * @param b vector3 b, vector3 to test against, must be normalized
300  * @param n plane normal n where a and b live in, must be normalized
301  * @return angle
302  */
303  inline static float computeAngle(const Vector3& a, const Vector3& b, const Vector3& n) {
304  auto angle = Vector3::computeAngle(a, b);
306  if (Float::isNaN(sign) == true) sign = 1.0f;
307  return Math::mod(((angle * sign) + 360.0f), 360.0f);
308  }
309 
310  /**
311  * Computes Euler angles
312  * @return Euler angles
313  */
314  inline Vector3 computeEulerAngles() const {
316  //
317  // TODO: check me, improve me
318  //
319  Vector3 euler;
320  //
321  Vector3 a(*this);
322  a.normalize();
323  // test around x axis
324  {
325  auto b = Vector3(0.0f, 1.0f, 0.0f);
326  auto n = Vector3(1.0f, 0.0f, 0.0f);
327  auto angle = Vector3::computeAngle(a, b, n);
328  euler.setX(angle);
329  }
330  // test around y axis
331  {
332  auto b = Vector3(0.0f, 0.0f, -1.0f);
333  auto n = Vector3(0.0f, 1.0f, 0.0f);
334  auto angle = Vector3::computeAngle(a, b, n);
335  euler.setY(angle);
336  }
337  // test around z axis
338  {
339  auto b = Vector3(0.0f, 1.0f, 0.0f);
340  auto n = Vector3(0.0f, 0.0f, 1.0f);
341  auto angle = Vector3::computeAngle(a, b, n);
342  euler.setZ(angle);
343  }
344  //
345  return euler;
346  }
347 
348  /**
349  * Interpolates between a and b by 0f<=t<=1f linearly
350  * @param a vector3 b
351  * @param b vector3 b
352  * @param t t
353  * @return interpolated vector3
354  */
355  inline static Vector3 interpolateLinear(const Vector3& a, const Vector3& b, float t) {
356  return Vector3(
357  (b.data[0] * t) + ((1.0f - t) * a.data[0]),
358  (b.data[1] * t) + ((1.0f - t) * a.data[1]),
359  (b.data[2] * t) + ((1.0f - t) * a.data[2])
360  );
361  }
362 
363  /**
364  * @return vector3 as array
365  */
366  inline const array<float,3>& getArray() const {
367  return (array<float,3>&)data;
368  }
369 
370  /**
371  * Clones this vector3
372  * @return cloned vector3
373  */
374  inline Vector3 clone() const {
375  return Vector3(data);
376  }
377 
378  /**
379  * Array access operator
380  * @param i index
381  * @return vector3 component
382  */
383  inline float& operator[](int i) {
384  return data[i];
385  }
386 
387  /**
388  * Const array access operator
389  * @param i index
390  * @return vector3 component
391  */
392  inline const float& operator[](int i) const {
393  return data[i];
394  }
395 
396  /**
397  * Operator + scalar
398  * @param scalar scalar
399  * @return new vector3 (this + scalar)
400  */
401  inline Vector3 operator +(const float scalar) const {
402  auto r = this->clone().add(scalar);
403  return r;
404  }
405 
406  /**
407  * Operator + vector3
408  * @param vector3 vector3
409  * @return new vector3 (this + vector3)
410  */
411  inline Vector3 operator +(const Vector3& vector3) const {
412  auto r = this->clone().add(vector3);
413  return r;
414  }
415 
416  /**
417  * Operator - scalar
418  * @param scalar scalar
419  * @return new vector3 (this - scalar)
420  */
421  inline Vector3 operator -(const float scalar) const {
422  auto r = this->clone().sub(scalar);
423  return r;
424  }
425 
426  /**
427  * Operator - vector3
428  * @param vector3 vector3
429  * @return new vector3 (this - vector3)
430  */
431  inline Vector3 operator -(const Vector3& vector3) const {
432  auto r = this->clone().sub(vector3);
433  return r;
434  }
435 
436  /**
437  * Operator * scalar
438  * @param scalar scalar
439  * @return new vector3 (this * scalar)
440  */
441  inline Vector3 operator *(const float scalar) const {
442  auto r = this->clone().scale(scalar);
443  return r;
444  }
445 
446  /**
447  * Operator * vector3
448  * @param vector3 vector3
449  * @return new vector3 (this * vector3)
450  */
451  inline Vector3 operator *(const Vector3& vector3) const {
452  auto r = this->clone().scale(vector3);
453  return r;
454  }
455 
456  /**
457  * Operator / scalar
458  * @param scalar scalar
459  * @return new vector3 (this / scalar)
460  */
461  inline Vector3 operator /(const float scalar) const {
462  auto r = this->clone().scale(1.0f / scalar);
463  return r;
464  }
465 
466  /**
467  * Operator / vector3
468  * @param vector3 vector3
469  * @return new vector3 (this / vector3)
470  */
471  inline Vector3 operator /(const Vector3& vector3) const {
472  auto vInverted = Vector3(1.0f / vector3[0], 1.0f / vector3[1], 1.0f / vector3[2]);
473  auto r = this->clone().scale(vInverted);
474  return r;
475  }
476 
477  /**
478  * Operator += scalar
479  * @param scalar scalar
480  * @return this vector3
481  */
482  inline Vector3& operator +=(const float scalar) {
483  return this->add(scalar);
484  }
485 
486  /**
487  * Operator += vector3
488  * @param vector3 vector3
489  * @return this vector3
490  */
491  inline Vector3& operator +=(const Vector3& vector3) {
492  return this->add(vector3);
493  }
494 
495  /**
496  * Operator -= scalar
497  * @param scalar scalar
498  * @return this vector3
499  */
500  inline Vector3& operator -=(const float scalar) {
501  return this->sub(scalar);
502  }
503 
504  /**
505  * Operator -= vector3
506  * @param vector3 vector3
507  * @return this vector3
508  */
509  inline Vector3& operator -=(const Vector3& vector3) {
510  return this->sub(vector3);
511  }
512 
513  /**
514  * Operator *= scalar
515  * @param scalar scalar
516  * @return this vector3
517  */
518  inline Vector3& operator *=(const float scalar) {
519  return this->scale(scalar);
520  }
521 
522  /**
523  * Operator *= vector3
524  * @param vector3 vector3
525  * @return this vector3
526  */
527  inline Vector3& operator *=(const Vector3& vector3) {
528  return this->scale(vector3);
529  }
530 
531  /**
532  * Operator /= scalar
533  * @param scalar scalar
534  * @return this vector3
535  */
536  inline Vector3& operator /=(const float scalar) {
537  return this->scale(1.0f / scalar);
538  }
539 
540  /**
541  * Operator /= vector3
542  * @param vector3 vector3
543  * @return this vector3
544  */
545  inline Vector3& operator /=(const Vector3& vector3) {
546  auto vInverted = Vector3(1.0f / vector3[0], 1.0f / vector3[1], 1.0f / vector3[2]);
547  return this->scale(vInverted);
548  }
549 
550  /**
551  * Equality comparison operator
552  * @param vector3 vector3
553  * @return equality
554  */
555  inline bool operator ==(const Vector3& vector3) const {
556  return this->equals(vector3);
557  }
558 
559  /**
560  * Non equality comparison operator
561  * @param vector3 vector3
562  * @return non equality
563  */
564  inline bool operator !=(const Vector3& vector3) const {
565  return this->equals(vector3) == false;
566  }
567 
568 };
Standard math functions.
Definition: Math.h:19
static float sqrt(float value)
Returns the square root of given value.
Definition: Math.h:192
static auto mod(auto value, auto range)
Returns modulo of value, so that return value is -range < value < range.
Definition: Math.h:229
static constexpr float EPSILON
Definition: Math.h:22
static auto abs(auto value)
Returns absolute value.
Definition: Math.h:63
static float acos(float x)
Returns the arc cosine of x.
Definition: Math.h:72
static constexpr float PI
Definition: Math.h:21
static auto sign(auto value)
Returns sign of value.
Definition: Math.h:44
static auto clamp(auto value, auto min, auto max)
Clamps a value to min or max value.
Definition: Math.h:33
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Quaternion class representing quaternion mathematical structure and operations with x,...
Definition: Quaternion.h:24
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
static float computeDotProduct(const Vector3 &a, const Vector3 &b)
Computes the dot product of a and b.
Definition: Vector3.h:253
float getY() const
Definition: Vector3.h:117
Vector3 & setX(float x)
Sets x component.
Definition: Vector3.h:109
const float & operator[](int i) const
Const array access operator.
Definition: Vector3.h:392
Vector3 operator/(const float scalar) const
Operator / scalar.
Definition: Vector3.h:461
Vector3(float x, float y, float z)
Public constructor.
Definition: Vector3.h:41
float getX() const
Definition: Vector3.h:100
float & operator[](int i)
Array access operator.
Definition: Vector3.h:383
static Vector3 computeCrossProduct(const Vector3 &a, const Vector3 &b)
Computes the cross product of a and b.
Definition: Vector3.h:263
float computeLength() const
Definition: Vector3.h:274
Vector3 operator+(const float scalar) const
Operator + scalar.
Definition: Vector3.h:401
float getZ() const
Definition: Vector3.h:134
Vector3 & scale(const Vector3 &vector3)
Scales by vector3.
Definition: Vector3.h:213
Vector3 computeEulerAngles() const
Computes Euler angles.
Definition: Vector3.h:314
Vector3 & add(float scalar)
Adds a scalar.
Definition: Vector3.h:153
bool operator!=(const Vector3 &vector3) const
Non equality comparison operator.
Definition: Vector3.h:564
Vector3 & setY(float y)
Sets y component.
Definition: Vector3.h:126
Vector3 & sub(const Vector3 &vector3)
Subtracts a vector3.
Definition: Vector3.h:189
Vector3 & operator+=(const float scalar)
Operator += scalar.
Definition: Vector3.h:482
bool operator==(const Vector3 &vector3) const
Equality comparison operator.
Definition: Vector3.h:555
static Vector3 interpolateLinear(const Vector3 &a, const Vector3 &b, float t)
Interpolates between a and b by 0f<=t<=1f linearly.
Definition: Vector3.h:355
const array< float, 3 > & getArray() const
Definition: Vector3.h:366
Vector3 operator-(const float scalar) const
Operator - scalar.
Definition: Vector3.h:421
Vector3 & add(const Vector3 &vector3)
Adds a vector3.
Definition: Vector3.h:165
float computeLengthSquared() const
Definition: Vector3.h:281
Vector3(const array< float, 3 > &vector3)
Public constructor.
Definition: Vector3.h:51
Vector3 clone() const
Clones this vector3.
Definition: Vector3.h:374
Vector3 & operator*=(const float scalar)
Operator *= scalar.
Definition: Vector3.h:518
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & scale(float scalar)
Scales by scalar.
Definition: Vector3.h:201
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Vector3 & operator/=(const float scalar)
Operator /= scalar.
Definition: Vector3.h:536
Vector3 & setZ(float z)
Sets z component.
Definition: Vector3.h:143
Vector3()
Public constructor.
Definition: Vector3.h:32
Vector3 & set(const array< float, 3 > &vector3)
Sets this vector3 by array.
Definition: Vector3.h:82
Vector3(const Vector3 &vector3)
Public constructor.
Definition: Vector3.h:59
array< float, 3 > data
Definition: Vector3.h:26
Vector3 operator*(const float scalar) const
Operator * scalar.
Definition: Vector3.h:441
Vector3 & set(const Vector3 &vector3)
Sets this vector3 by given vector3.
Definition: Vector3.h:92
static float computeAngle(const Vector3 &a, const Vector3 &b, const Vector3 &n)
Computes angle between a and b from, where 0.0 <= angle < 360.0.
Definition: Vector3.h:303
static float computeAngle(const Vector3 &a, const Vector3 &b)
Computes angle between a and b from 0.0 <= angle < 180.0.
Definition: Vector3.h:291
bool equals(const Vector3 &vector3, float tolerance=Math::EPSILON) const
Compares this vector3 with given vector3.
Definition: Vector3.h:226
Vector3 & operator-=(const float scalar)
Operator -= scalar.
Definition: Vector3.h:500
Vector3 & normalize()
Normalizes this vector3.
Definition: Vector3.h:239
Vector4 class representing vector4 mathematical structure and operations with x, y,...
Definition: Vector4.h:22
Float class.
Definition: Float.h:27