TDME2 1.9.121
Vector4.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4
5#include <tdme/tdme.h>
7#include <tdme/math/Vector3.h>
8
9using std::array;
10
12
13/**
14 * 3D vector 4 class
15 * @author Andreas Drewke
16 * @version $Id$
17 */
19{
20 friend class Matrix4x4;
21 friend class Vector3;
22
23private:
24 array<float, 4> data;
25
26public:
27 /**
28 * Public constructor
29 */
30 inline Vector4() {
31 data.fill(0.0f);
32 }
33
34 /**
35 * Public constructor
36 * @param v vector
37 */
38 inline Vector4(const Vector4& v) {
39 data = v.data;
40 }
41
42 /**
43 * Public constructor
44 * @param v vector
45 * @param w w
46 */
47 inline Vector4(const Vector3& v, float w) {
48 data[0] = v.data[0];
49 data[1] = v.data[1];
50 data[2] = v.data[2];
51 data[3] = w;
52 }
53
54 /**
55 * Public constructor
56 * @param x x
57 * @param y y
58 * @param z z
59 * @param w w
60 */
61 inline Vector4(float x, float y, float z, float w) {
62 data[0] = x;
63 data[1] = y;
64 data[2] = z;
65 data[3] = w;
66 }
67
68 /**
69 * Public constructor
70 * @param v v
71 */
72 inline Vector4(const array<float, 4>& v) {
73 data = v;
74 }
75
76 /**
77 * Set up vector
78 * @param x x
79 * @param y y
80 * @param z z
81 * @param w w
82 * @return this vector
83 */
84 inline Vector4& set(float x, float y, float z, float w) {
85 data[0] = x;
86 data[1] = y;
87 data[2] = z;
88 data[3] = w;
89 return *this;
90 }
91
92 /**
93 * Set up vector
94 * @param v float array containing x,y,z,w values
95 * @return this vector
96 */
97 inline Vector4& set(const array<float, 4>& v) {
98 data = v;
99 return *this;
100 }
101
102 /**
103 * Set up vector
104 * @param v v
105 * @return this vector
106 */
107 inline Vector4& set(const Vector4& v) {
108 data[1] = v.data[1];
109 data[0] = v.data[0];
110 data[2] = v.data[2];
111 data[3] = v.data[3];
112 return *this;
113 }
114
115 /**
116 * Set up vector
117 * @param v vector 3
118 * @param w w
119 * @return this vector
120 */
121 inline Vector4& set(const Vector3& v, float w) {
122 data[0] = v.data[0];
123 data[1] = v.data[1];
124 data[2] = v.data[2];
125 data[3] = w;
126 return *this;
127 }
128
129 /**
130 * @return x
131 */
132 inline float getX() const {
133 return data[0];
134 }
135
136 /**
137 * set X
138 * @param x x
139 * @return this vector
140 */
141 inline Vector4& setX(float x) {
142 data[0] = x;
143 return *this;
144 }
145
146 /**
147 * @return y
148 */
149 inline float getY() const {
150 return data[1];
151 }
152
153 /**
154 * set Y
155 * @param y y
156 * @return this vector
157 */
158 inline Vector4& setY(float y) {
159 data[1] = y;
160 return *this;
161 }
162
163 /**
164 * @return z
165 */
166 inline float getZ() const {
167 return data[2];
168 }
169
170 /**
171 * Set Z
172 * @param z z
173 * @return this vector
174 */
175 inline Vector4& setZ(float z) {
176 data[2] = z;
177 return *this;
178 }
179
180 /**
181 * @return w
182 */
183 inline float getW() const {
184 return data[3];
185 }
186
187 /**
188 * Set W
189 * @param w w
190 * @return this vector
191 */
192 inline Vector4& setW(float w) {
193 data[3] = w;
194 return *this;
195 }
196
197 /**
198 * Adds a vector
199 * @param v v
200 * @return this vector
201 */
202 inline Vector4& add(const Vector4& v) {
203 data[0] += v.data[0];
204 data[1] += v.data[1];
205 data[2] += v.data[2];
206 data[3] += v.data[3];
207 return *this;
208 }
209
210 /**
211 * Subtracts a vector
212 * @param v v
213 * @return this vector
214 */
215 inline Vector4& sub(const Vector4& v) {
216 data[0] -= v.data[0];
217 data[1] -= v.data[1];
218 data[2] -= v.data[2];
219 data[3] -= v.data[3];
220 return *this;
221 }
222
223
224 /**
225 * Scale this vector
226 * @param scale scale
227 * @return this vector
228 */
229 inline Vector4& scale(float scale) {
230 data[0] *= scale;
231 data[1] *= scale;
232 data[2] *= scale;
233 data[3] *= scale;
234 return *this;
235 }
236
237 /**
238 * Scale this vector
239 * @param scale scale
240 * @return this vector
241 */
242 inline Vector4& scale(const Vector4& scale) {
243 data[0] *= scale.data[0];
244 data[1] *= scale.data[1];
245 data[2] *= scale.data[2];
246 data[3] *= scale.data[3];
247 return *this;
248 }
249
250 /**
251 * Array access operator
252 * @param i index
253 * @return vector3 component
254 */
255 inline float& operator[](int i) {
256 return data[i];
257 }
258
259 /**
260 * Const array access operator
261 * @param i index
262 * @return vector3 component
263 */
264 inline const float& operator[](int i) const {
265 return data[i];
266 }
267
268 /**
269 * Operator +
270 * @param v vector to add
271 * @return new vector (this + v)
272 */
273 inline Vector4 operator +(const Vector4& v) const {
274 auto r = this->clone().add(v);
275 return r;
276 }
277
278 /**
279 * Operator -
280 * @param v vector to subtract
281 * @return new vector (this - v)
282 */
283 inline Vector4 operator -(const Vector4& v) const {
284 auto r = this->clone().sub(v);
285 return r;
286 }
287
288 /**
289 * Operator * (float)
290 * @param f value to multiply by
291 * @return new vector (this * f)
292 */
293 inline Vector4 operator *(const float f) const {
294 auto r = this->clone().scale(f);
295 return r;
296 }
297
298 /**
299 * Operator * (Vector4&)
300 * @param v vector to multiply by
301 * @return new vector (this * v)
302 */
303 inline Vector4 operator *(const Vector4& v) const {
304 auto r = this->clone().scale(v);
305 return r;
306 }
307
308 /**
309 * Operator / (f)
310 * @param v value to divide by
311 * @return new vector (this / f)
312 */
313 inline Vector4 operator /(const float f) const {
314 auto r = this->clone().scale(1.0f / f);
315 return r;
316 }
317
318 /**
319 * Operator / (Vector4&)
320 * @param v vector to divide by
321 * @return new vector (this / v)
322 */
323 inline Vector4 operator /(const Vector4& v) const {
324 auto vInverted = Vector4(1.0f / v[0], 1.0f / v[1], 1.0f / v[2], 1.0f / v[3]);
325 auto r = this->clone().scale(vInverted);
326 return r;
327 }
328
329 /**
330 * Operator +=
331 * @param v vector to add
332 * @return this vector added by v
333 */
334 inline Vector4& operator +=(const Vector4& v) {
335 return this->add(v);
336 }
337
338 /**
339 * Operator -=
340 * @param v vector to substract
341 * @return this vector substracted by v
342 */
344 return this->sub(v);
345 }
346
347 /**
348 * Operator *=
349 * @param v vector to multiply by
350 * @return this vector multiplied by v
351 */
353 return this->scale(v);
354 }
355
356 /**
357 * Operator /=
358 * @param v vector to devide by
359 * @return this vector devided by v
360 */
362 auto vInverted = Vector4(1.0f / v[0], 1.0f / v[1], 1.0f / v[2], 1.0f / v[3]);
363 return this->scale(vInverted);
364 }
365
366 /**
367 * Operator *=
368 * @param f float to multiply by
369 * @return this vector multiplied by f
370 */
371 inline Vector4& operator *=(const float f) {
372 return this->scale(f);
373 }
374
375 /**
376 * Operator /=
377 * @param f float to divide by
378 * @return this vector divided by f
379 */
380 inline Vector4& operator /=(const float f) {
381 return this->scale(1.0f / f);
382 }
383
384 /**
385 * Equality comparison operator
386 * @param v vector to compare to
387 * @return equality
388 */
389
390 inline bool operator ==(const Vector4& v) const {
391 return this->equals(v);
392 }
393
394 /**
395 * Non equality comparison operator
396 * @param v vector to compare to
397 * @return non equality
398 */
399
400 inline bool operator !=(const Vector4& v) const {
401 return this->equals(v) == false;
402 }
403
404 /**
405 * @return vector as array
406 */
407 inline array<float, 4>& getArray() const {
408 return (array<float, 4>&)data;
409 }
410
411 /**
412 * Clones the vector
413 * @return new cloned vector
414 */
415 inline Vector4 clone() const {
416 return Vector4(*this);
417 }
418
419 /**
420 * Compares this vector with given vector
421 * @param v vector v
422 * @return equality
423 */
424 inline bool equals(const Vector4& v) const {
425 return equals(v, Math::EPSILON);
426 }
427
428 /**
429 * Compares this vector with given vector
430 * @param v vector v
431 * @param tolerance tolerance per component(x, y, z)
432 * @return equality
433 */
434 inline bool equals(const Vector4& v, float tolerance) const {
435 return (this == &v) ||
436 (
437 Math::abs(data[0] - v.data[0]) < tolerance &&
438 Math::abs(data[1] - v.data[1]) < tolerance &&
439 Math::abs(data[2] - v.data[2]) < tolerance &&
440 Math::abs(data[3] - v.data[3]) < tolerance
441 );
442 }
443
444
445};
static constexpr float EPSILON
Definition: Math.h:25
static int32_t abs(int32_t value)
Returns absolute value.
Definition: Math.h:91
4x4 3D Matrix class
Definition: Matrix4x4.h:24
3D vector 3 class
Definition: Vector3.h:22
array< float, 3 > data
Definition: Vector3.h:28
3D vector 4 class
Definition: Vector4.h:19
Vector4 & operator*=(Vector4 &v)
Operator *=.
Definition: Vector4.h:352
Vector4 & setZ(float z)
Set Z.
Definition: Vector4.h:175
bool equals(const Vector4 &v) const
Compares this vector with given vector.
Definition: Vector4.h:424
float getY() const
Definition: Vector4.h:149
Vector4 operator/(const float f) const
Operator / (f)
Definition: Vector4.h:313
array< float, 4 > & getArray() const
Definition: Vector4.h:407
float getX() const
Definition: Vector4.h:132
Vector4 clone() const
Clones the vector.
Definition: Vector4.h:415
Vector4 & set(float x, float y, float z, float w)
Set up vector.
Definition: Vector4.h:84
float getZ() const
Definition: Vector4.h:166
Vector4 & setY(float y)
set Y
Definition: Vector4.h:158
Vector4 operator-(const Vector4 &v) const
Operator -.
Definition: Vector4.h:283
Vector4 & setW(float w)
Set W.
Definition: Vector4.h:192
Vector4 & operator-=(Vector4 &v)
Operator -=.
Definition: Vector4.h:343
Vector4(const Vector4 &v)
Public constructor.
Definition: Vector4.h:38
Vector4 & setX(float x)
set X
Definition: Vector4.h:141
Vector4 & sub(const Vector4 &v)
Subtracts a vector.
Definition: Vector4.h:215
Vector4 & set(const Vector3 &v, float w)
Set up vector.
Definition: Vector4.h:121
Vector4()
Public constructor.
Definition: Vector4.h:30
Vector4(float x, float y, float z, float w)
Public constructor.
Definition: Vector4.h:61
bool equals(const Vector4 &v, float tolerance) const
Compares this vector with given vector.
Definition: Vector4.h:434
Vector4 operator*(const float f) const
Operator * (float)
Definition: Vector4.h:293
Vector4 & operator/=(Vector4 &v)
Operator /=.
Definition: Vector4.h:361
float getW() const
Definition: Vector4.h:183
Vector4 & set(const array< float, 4 > &v)
Set up vector.
Definition: Vector4.h:97
bool operator==(const Vector4 &v) const
Equality comparison operator.
Definition: Vector4.h:390
const float & operator[](int i) const
Const array access operator.
Definition: Vector4.h:264
Vector4 & operator+=(const Vector4 &v)
Operator +=.
Definition: Vector4.h:334
Vector4(const Vector3 &v, float w)
Public constructor.
Definition: Vector4.h:47
float & operator[](int i)
Array access operator.
Definition: Vector4.h:255
Vector4 & set(const Vector4 &v)
Set up vector.
Definition: Vector4.h:107
bool operator!=(const Vector4 &v) const
Non equality comparison operator.
Definition: Vector4.h:400
array< float, 4 > data
Definition: Vector4.h:24
Vector4 operator+(const Vector4 &v) const
Operator +.
Definition: Vector4.h:273
Vector4(const array< float, 4 > &v)
Public constructor.
Definition: Vector4.h:72
Vector4 & add(const Vector4 &v)
Adds a vector.
Definition: Vector4.h:202
Vector4 & scale(float scale)
Scale this vector.
Definition: Vector4.h:229
Vector4 & scale(const Vector4 &scale)
Scale this vector.
Definition: Vector4.h:242