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