| TGX 1.0.8 A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.

Loading...
Searching...
No Matches
Vec4.h
Go to the documentation of this file.
1
5//
6// Copyright 2020 Arvind Singh
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11//version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; If not, see <http://www.gnu.org/licenses/>.
20
21
22#ifndef _TGX_VEC4_H_
23#define _TGX_VEC4_H_
24
25// only C++, no plain C
26#ifdef __cplusplus
27
28
29
30#include <stdint.h>
31
32#include "Misc.h"
33#include "Vec2.h"
34#include "Vec3.h"
35
36
37 namespace tgx
38{
39
40
41 // forward declarations
42
43 template<typename T> struct Vec2;
44
45 template<typename T> struct Vec3;
46
47 template<typename T> struct Vec4;
48
49
50 // Specializations
51
52 typedef Vec4<int> iVec4;
53
54 typedef Vec4<float> fVec4;
55
56 typedef Vec4<double> dVec4;
57
58
59
60
70 template<typename T> struct Vec4 : public Vec3<T>
71 {
72
73 // mtools extension (if available).
74 #if (MTOOLS_TGX_EXTENSIONS)
75 #include <mtools/extensions/tgx/tgx_ext_Vec4.inl>
76 #endif
77
78
79 // first three coordinates from base classes
80 using Vec2<T>::x;
81 using Vec2<T>::y;
82 using Vec3<T>::z;
83
84 // and the new fourth coordinates
85 T w;
86
87
91 Vec4() {}
92
93
97 constexpr Vec4(T X, T Y, T Z, T W) : Vec3<T>(X,Y,Z), w(W)
98 {
99 }
100
101
105 Vec4(const Vec4 & V) = default;
106
107
111 constexpr Vec4(Vec2<T> V, T Z, T W) : Vec3<T>(V, Z), w(W)
112 {
113 }
114
115
119 constexpr Vec4(Vec3<T> V, T W) : Vec3<T>(V), w(W)
120 {
121 }
122
123
127 Vec4 & operator=(const Vec4 & V) = default;
128
129
136 template<typename U>
137 explicit operator Vec4<U>() const { return Vec4<U>((U)x, (U)y, (U)z, (U)w); }
138
139
143 operator Vec4<typename DefaultFPType<T>::fptype>() const { return Vec4<typename DefaultFPType<T>::fptype>((typename DefaultFPType<T>::fptype)x, (typename DefaultFPType<T>::fptype)y, (typename DefaultFPType<T>::fptype)z, (typename DefaultFPType<T>::fptype)w); }
144
145
149 inline bool operator==(const Vec4 V) const
150 {
151 return ((x == V.x) && (y == V.y) && (z == V.z) && (w == V.w));
152 }
153
154
158 inline bool operator!=(const Vec4 V) const
159 {
160 return(!(operator==(V)));
161 }
162
163
167 inline bool operator<(const Vec4 V) const
168 {
169 if (x < V.x) return true;
170 if (x == V.x)
171 {
172 if (y < V.y) return true;
173 if (y == V.y)
174 {
175 if (z < V.z) return true;
176 if (z == V.z)
177 {
178 if (w < V.w) return true;
179 }
180 }
181 }
182 return false;
183 }
184
185
189 inline bool operator<=(const Vec4 V) const
190 {
191 if (x < V.x) return true;
192 if (x == V.x)
193 {
194 if (y < V.y) return true;
195 if (y == V.y)
196 {
197 if (z < V.z) return true;
198 if (z == V.z) return true;
199 {
200 if (w <= V.w) return true;
201 }
202 }
203 }
204 return false;
205 }
206
207
211 inline bool operator>(const Vec4 V) const
212 {
213 return(!(operator<=(V)));
214 }
215
216
220 inline bool operator>=(const Vec4 V) const
221 {
222 return(!(operator<(V)));
223 }
224
225
229 inline void operator+=(const Vec4 V)
230 {
231 x += V.x;
232 y += V.y;
233 z += V.z;
234 w += V.w;
235 }
236
237
241 inline void operator-=(const Vec4 V)
242 {
243 x -= V.x;
244 y -= V.y;
245 z -= V.z;
246 w -= V.w;
247 }
248
249
253 inline void operator*=(const Vec4 V)
254 {
255 x *= V.x;
256 y *= V.y;
257 z *= V.z;
258 w *= V.w;
259 }
260
261
265 inline void operator/=(const Vec4 V)
266 {
267 x /= V.x;
268 y /= V.y;
269 z /= V.z;
270 w /= V.w;
271 }
272
273
277 inline void operator+=(const T v)
278 {
279 x += v;
280 y += v;
281 z += v;
282 w += v;
283 }
284
285
289 inline void operator-=(const T v)
290 {
291 x -= v;
292 y -= v;
293 z -= v;
294 w -= v;
295 }
296
300 inline void operator*=(const T v)
301 {
302 x *= v;
303 y *= v;
304 z *= v;
305 w *= v;
306 }
307
311 inline void operator/=(const T v)
312 {
313 x /= v;
314 y /= v;
315 z /= v;
316 w /= v;
317 }
318
319
320
321 // DO NOT DEFINE FOR PROJECTIVE VECTORS
322 // TO PREVENT SPUPID MISTAKES WHEN CASTING...
323 // inline Vec4 operator-() const
324 // {
325 // return Vec4{ -x, -y, -z, w };
326 // }
327
328
334 inline T norm2() const
335 {
336 return x*x + y*y +z*z +w*w;
337 }
338
339
348 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat norm() const
349 {
350 return (Tfloat)tgx::precise_sqrt((Tfloat)(x*x + y*y + z*z + w*w));
351 }
352
353
363 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat norm_fast() const
364 {
365 return (Tfloat)tgx::fast_sqrt((Tfloat)(x*x + y*y + z*z + w*w));
366 }
367
368
377 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat invnorm() const
378 {
379 return (Tfloat)tgx::precise_invsqrt((Tfloat)(x*x + y*y + z*z + w*w));
380 }
381
382
392 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat invnorm_fast() const
393 {
394 return (Tfloat)tgx::fast_invsqrt((Tfloat)(x*x + y*y + z*z + w*w));
395 }
396
397
406 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline void normalize()
407 {
408 Tfloat a = invnorm<Tfloat>();
409 x = (T)(x * a);
410 y = (T)(y * a);
411 z = (T)(z * a);
412 w = (T)(w * a);
413 }
414
415
425 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline void normalize_fast()
426 {
427 Tfloat a = invnorm_fast<Tfloat>();
428 x = (T)(x * a);
429 y = (T)(y * a);
430 z = (T)(z * a);
431 w = (T)(w * a);
432 }
433
434
443 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec4<T> getNormalize() const
444 {
445 Vec4<T> V(*this);
446 V.normalize();
447 return V;
448 }
449
450
460 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec4<T> getNormalize_fast() const
461 {
462 Vec4<T> V(*this);
463 V.normalize_fast();
464 return V;
465 }
466
467
478 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline void zdivide()
479 {
480 const Tfloat iw = tgx::fast_inv((Tfloat)w);
481 x = iw*x;
482 y = iw*y;
483 z = iw*z;
484 w = iw;
485 }
486
487
488#ifdef TGX_ON_ARDUINO
489
495 inline void print(Stream & outputStream = Serial) const
496 {
497 outputStream.printf("[%.3f \t %.3f \t %.3f \t %.3f]\n", x, y, z, w);
498 }
499
500#endif
501
502 };
503
504
505
512 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec4<T> normalize(Vec4<T> V)
513 {
514 V.template normalize<Tfloat>();
515 return V;
516 }
517
518
526 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec4<T> normalize_fast(Vec4<T> V)
527 {
528 V.template normalize_fast<Tfloat>();
529 return V;
530 }
531
532
536 template<typename T> inline T dist2(const Vec4<T> V1, const Vec4<T> V2)
537 {
538 const T xx = V1.x - V2.x;
539 const T yy = V1.y - V2.y;
540 const T zz = V1.z - V2.z;
541 const T ww = V1.w - V2.w;
542 return xx * xx + yy * yy + zz * zz + ww * ww;
543 }
544
545
552 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > Tfloat dist(Vec4<T> V1, const Vec4<T> V2)
553 {
554 const T xx = V1.x - V2.x;
555 const T yy = V1.y - V2.y;
556 const T zz = V1.z - V2.z;
557 const T ww = V1.w - V2.w;
558 return (Tfloat)tgx::precise_sqrt((Tfloat)(xx * xx + yy * yy + zz * zz + ww * ww));
559 }
560
561
569 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > Tfloat dist_fast(Vec4<T> V1, const Vec4<T> V2)
570 {
571 const T xx = V1.x - V2.x;
572 const T yy = V1.y - V2.y;
573 const T zz = V1.z - V2.z;
574 const T ww = V1.w - V2.w;
575 return (Tfloat)tgx::fast_sqrt((Tfloat)(xx * xx + yy * yy + zz * zz + ww * ww));
576 }
577
578
580 template<typename T> inline Vec4<T> operator+(Vec4<T> V1, const Vec4<T> V2) { V1 += V2; return V1; }
581
583 template<typename T> inline Vec4<T> operator-(Vec4<T> V1, const Vec4<T> V2) { V1 -= V2; return V1; }
584
586 template<typename T> inline Vec4<T> operator*(Vec4<T> V1, const Vec4<T> V2) { V1 *= V2; return V1; }
587
589 template<typename T> inline Vec4<T> operator/(Vec4<T> V1, const Vec4<T> V2) { V1 /= V2; return V1; }
590
592 template<typename T> inline Vec4<T> operator+(const T a, Vec4<T> V) { V += a; return V; }
593
595 template<typename T> inline Vec4<T> operator+(Vec4<T> V, const T a) { V += a; return V; }
596
598 //template<typename T> inline Vec4<T> operator-(const T a, Vec4<T> V) { V -= a; return V; }
599
601 template<typename T> inline Vec4<T> operator-(Vec4<T> V, const T a) { V -= a; return V; }
602
604 template<typename T> inline Vec4<T> operator*(const T a, Vec4<T> V) { V *= a; return V; }
605
607 template<typename T> inline Vec4<T> operator*(Vec4<T> V, const T a) { V *= a; return V; }
608
610 //template<typename T> inline Vec4<T> operator/(const T a, Vec4<T> V) { V /= a; return V; }
611
613 template<typename T> inline Vec4<T> operator/(Vec4<T> V, const T a) { V /= a; return V; }
614
615
619 template<typename T> inline T dotProduct(const Vec4<T> U, const Vec4<T> V)
620 {
621 return U.x * V.x + U.y * V.y + U.z * V.z + U.w * V.w;
622 }
623
624
628 template<typename T> inline Vec4<T> crossProduct(const Vec4<T>& U, const Vec4<T>& V)
629 {
630 return Vec4<T>{ U.y* V.z - U.z * V.y,
631 U.z* V.x - U.x * V.z,
632 U.x* V.y - U.y * V.x,
633 0 };
634 }
635
636
637
641 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec4<T> lerp(Tfloat alpha, Vec4<T> V1, Vec4<T> V2)
642 {
643 return Vec4<T>{ (T)(V1.x + alpha * (V2.x - V1.x)),
644 (T)(V1.y + alpha * (V2.y - V1.y)),
645 (T)(V1.z + alpha * (V2.z - V1.z)),
646 (T)(V1.w + alpha * (V2.w - V1.w)) };
647 }
648
649
650}
651
652#endif
653
654#endif
655
tgx::operator*
Mat4< T > operator*(const Mat4< T > &A, const Mat4< T > &B)
Matrix-matrix multiplication.
Definition: Mat4.h:633
Misc.h
Utility/miscellaneous functions used throughout the library.
tgx::fast_invsqrt
TGX_INLINE float fast_invsqrt(float x)
Compute a fast approximation of the inverse square root of a float.
Definition: Misc.h:327
tgx::precise_sqrt
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:231
tgx::precise_invsqrt
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition: Misc.h:279
tgx::fast_sqrt
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition: Misc.h:251
tgx::fast_inv
TGX_INLINE float fast_inv(float x)
Fast (approximate) computation of 1/x.
Definition: Misc.h:180
Vec2.h
2D vector.
tgx::crossProduct
T crossProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the cross product UxV (i.e.
Definition: Vec2.h:569
tgx::dotProduct
T dotProduct(const Vec2< T > U, const Vec2< T > V)
Return the dot product U.V between two vectors.
Definition: Vec2.h:560
tgx::dist_fast
Tfloat dist_fast(Vec2< T > V1, const Vec2< T > V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:512
tgx::normalize
Vec2< T > normalize(Vec2< T > V)
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:459
tgx::lerp
Vec2< T > lerp(Tfloat alpha, Vec2< T > V1, Vec2< T > V2)
Return the linear interpolation: V1 + alpha(V2 - V1).
Definition: Vec2.h:578
tgx::operator+
Vec2< T > operator+(Vec2< T > V1, const Vec2< T > V2)
Addition operator.
Definition: Vec2.h:521
tgx::operator/
Vec2< T > operator/(Vec2< T > V1, const Vec2< T > V2)
Division operator.
Definition: Vec2.h:530
tgx::dist
Tfloat dist(Vec2< T > V1, const Vec2< T > V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:497
tgx::dist2
T dist2(const Vec2< T > V1, const Vec2< T > V2)
Compute the squared euclidian distance between two vectors.
Definition: Vec2.h:483
tgx::operator-
Vec2< T > operator-(Vec2< T > V1, const Vec2< T > V2)
Substraction operator.
Definition: Vec2.h:524
tgx::normalize_fast
Vec2< T > normalize_fast(Vec2< T > V)
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:473
Vec3.h
3D vector.
tgx::Vec2
Generic 2D vector [specializations iVec2, fVec2, dVec2].
Definition: Vec2.h:64
tgx::Vec2::x
T x
'x' coordinate (first dimension)
Definition: Vec2.h:72
tgx::Vec2::y
T y
'y' coordinate (second dimension)
Definition: Vec2.h:73
tgx::Vec3
Generic 3D vector [specializations iVec3, fVec3, dVec3].
Definition: Vec3.h:70
tgx::Vec3::z
T z
'z' coordinate (third dimension)
Definition: Vec3.h:83
tgx::Vec4
Geenric 4D vector [specializations iVec4, fVec4, dVec4].
Definition: Vec4.h:71
tgx::Vec4::operator*=
void operator*=(const T v)
Scalar multiplication.
Definition: Vec4.h:300
tgx::Vec4::operator!=
bool operator!=(const Vec4 V) const
Inequality operator.
Definition: Vec4.h:158
tgx::Vec4::operator*=
void operator*=(const Vec4 V)
Multiply this vector by another one (coordinate by coordinate multiplication).
Definition: Vec4.h:253
tgx::Vec4::w
T w
'w' coordinate (fourth dimension)
Definition: Vec4.h:85
tgx::Vec4::Vec4
Vec4(const Vec4 &V)=default
Default copy constructor.
tgx::Vec4::operator-=
void operator-=(const T v)
Scalar substraction.
Definition: Vec4.h:289
tgx::Vec4::normalize_fast
void normalize_fast()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec4.h:425
tgx::Vec4::operator<
bool operator<(const Vec4 V) const
Less-than comparison operator.
Definition: Vec4.h:167
tgx::Vec4::invnorm_fast
Tfloat invnorm_fast() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec4.h:392
tgx::Vec4::operator>=
bool operator>=(const Vec4 V) const
Greater-than-or-equal comparison operator.
Definition: Vec4.h:220
tgx::Vec4::operator/=
void operator/=(const T v)
Scalar division.
Definition: Vec4.h:311
tgx::Vec4::Vec4
constexpr Vec4(Vec3< T > V, T W)
Constructor from a Vec3.
Definition: Vec4.h:119
tgx::Vec4::operator>
bool operator>(const Vec4 V) const
Greater-than comparison operator.
Definition: Vec4.h:211
tgx::Vec4::operator/=
void operator/=(const Vec4 V)
Divide this vector by another one (coordinate by coordinate division).
Definition: Vec4.h:265
tgx::Vec4::getNormalize
Vec4< T > getNormalize() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec4.h:443
tgx::Vec4::operator==
bool operator==(const Vec4 V) const
Equality comparator.
Definition: Vec4.h:149
tgx::Vec4::Vec4
constexpr Vec4(Vec2< T > V, T Z, T W)
Constructor from a Vec2.
Definition: Vec4.h:111
tgx::Vec4::operator+=
void operator+=(const T v)
Scalar addition.
Definition: Vec4.h:277
tgx::Vec4::operator-=
void operator-=(const Vec4 V)
Substract another vector from this one.
Definition: Vec4.h:241
tgx::Vec4::operator<=
bool operator<=(const Vec4 V) const
Less-than-or-equal comparison operator.
Definition: Vec4.h:189
tgx::Vec4::Vec4
constexpr Vec4(T X, T Y, T Z, T W)
Constructor with explicit initialization.
Definition: Vec4.h:97
tgx::Vec4::operator+=
void operator+=(const Vec4 V)
Add another vector to this one.
Definition: Vec4.h:229
tgx::Vec4::zdivide
void zdivide()
Performs the 'z-divide' operation.
Definition: Vec4.h:478
tgx::Vec4::operator=
Vec4 & operator=(const Vec4 &V)=default
Default assignment operator.
tgx::Vec4::normalize
void normalize()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec4.h:406
tgx::Vec4::norm_fast
Tfloat norm_fast() const
Compute the euclidian norm of the vector.
Definition: Vec4.h:363
tgx::Vec4::invnorm
Tfloat invnorm() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec4.h:377
tgx::Vec4::norm
Tfloat norm() const
Compute the euclidian norm of the vector.
Definition: Vec4.h:348
tgx::Vec4::getNormalize_fast
Vec4< T > getNormalize_fast() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec4.h:460
tgx::Vec4::Vec4
Vec4()
default constructor: the vector content is undefined.
Definition: Vec4.h:91
tgx::Vec4::norm2
T norm2() const
Compute the squared euclidian norm of the vector.
Definition: Vec4.h:334