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

Loading...
Searching...
No Matches
Vec3.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_VEC3_H_
23#define _TGX_VEC3_H_
24
25// only C++, no plain C
26#ifdef __cplusplus
27
28
29#include <stdint.h>
30
31#include "Misc.h"
32#include "Vec2.h"
33
34
35 namespace tgx
36{
37
38
39 // forward declarations
40
41 template<typename T> struct Vec2;
42
43 template<typename T> struct Vec3;
44
45 template<typename T> struct Vec4;
46
47
48 // Specializations
49
50 typedef Vec3<int> iVec3;
51
52 typedef Vec3<float> fVec3;
53
54 typedef Vec3<double> dVec3;
55
56
57
58
59
69 template<typename T> struct Vec3 : public Vec2<T>
70 {
71
72 // mtools extension (if available).
73 #if (MTOOLS_TGX_EXTENSIONS)
74 #include <mtools/extensions/tgx/tgx_ext_Vec3.inl>
75 #endif
76
77
78 // first two coordinates from the base class
79 using Vec2<T>::x;
80 using Vec2<T>::y;
81
82 // and the new third coordinates
83 T z;
84
85
89 Vec3() {}
90
91
95 constexpr Vec3(T X, T Y, T Z) : Vec2<T>(X,Y), z(Z)
96 {
97 }
98
99
103 Vec3(const Vec3 & V) = default;
104
105
109 constexpr Vec3(Vec2<T> V, T Z) : Vec2<T>(V), z(Z)
110 {
111 }
112
113
117 Vec3 & operator=(const Vec3 & V) = default;
118
119
126 template<typename U>
127 explicit operator Vec3<U>() const { return Vec3<U>((U)x, (U)y, (U)z); }
128
129
133 operator Vec3<typename DefaultFPType<T>::fptype>() const { return Vec3<typename DefaultFPType<T>::fptype>((typename DefaultFPType<T>::fptype)x, (typename DefaultFPType<T>::fptype)y, (typename DefaultFPType<T>::fptype)z); }
134
135
136
140 inline bool operator==(const Vec3 & V) const
141 {
142 return ((x == V.x) && (y == V.y) && (z == V.z));
143 }
144
145
149 inline bool operator!=(const Vec3 & V) const
150 {
151 return(!(operator==(V)));
152 }
153
154
158 inline bool operator<(const Vec3 & V) const
159 {
160 if (x < V.x) return true;
161 if (x == V.x)
162 {
163 if (y < V.y) return true;
164 if (y == V.y)
165 {
166 if (z < V.z) return true;
167 }
168 }
169 return false;
170 }
171
172
176 inline bool operator<=(const Vec3 & V) const
177 {
178 if (x < V.x) return true;
179 if (x == V.x)
180 {
181 if (y < V.y) return true;
182 if (y == V.y)
183 {
184 if (z <= V.z) return true;
185 }
186 }
187 return false;
188 }
189
190
194 inline bool operator>(const Vec3 & V) const
195 {
196 return(!(operator<=(V)));
197 }
198
199
203 inline bool operator>=(const Vec3 & V) const
204 {
205 return(!(operator<(V)));
206 }
207
208
212 TGX_INLINE inline void operator+=(const Vec3 & V)
213 {
214 x += V.x;
215 y += V.y;
216 z += V.z;
217 }
218
219
223 TGX_INLINE inline void operator-=(const Vec3 & V)
224 {
225 x -= V.x;
226 y -= V.y;
227 z -= V.z;
228 }
229
230
234 TGX_INLINE inline void operator*=(const Vec3 & V)
235 {
236 x *= V.x;
237 y *= V.y;
238 z *= V.z;
239 }
240
241
245 TGX_INLINE inline void operator/=(const Vec3 & V)
246 {
247 x /= V.x;
248 y /= V.y;
249 z /= V.z;
250 }
251
252
256 TGX_INLINE inline void operator+=(T v)
257 {
258 x += v;
259 y += v;
260 z += v;
261 }
262
263
267 TGX_INLINE inline void operator-=(T v)
268 {
269 x -= v;
270 y -= v;
271 z -= v;
272 }
273
277 TGX_INLINE inline void operator*=(T v)
278 {
279 x *= v;
280 y *= v;
281 z *= v;
282 }
283
287 TGX_INLINE inline void operator/=(T v)
288 {
289 x /= v;
290 y /= v;
291 z /= v;
292 }
293
294
298 TGX_INLINE inline Vec3 operator-() const
299 {
300 return Vec3{ -x, -y, -z };
301 }
302
303
304
310 TGX_INLINE inline T norm2() const
311 {
312 return x*x + y*y +z*z;
313 }
314
315
324 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat norm() const
325 {
326 return (Tfloat)tgx::precise_sqrt((Tfloat)(x*x + y*y +z*z));
327 }
328
329
339 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat norm_fast() const
340 {
341 return (Tfloat)tgx::fast_sqrt((Tfloat)(x*x + y*y +z*z));
342 }
343
344
353 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat invnorm() const
354 {
355 return (Tfloat)tgx::precise_invsqrt((Tfloat)(x*x + y*y +z*z));
356 }
357
358
368 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat invnorm_fast() const
369 {
370 return (Tfloat)tgx::fast_invsqrt((Tfloat)(x*x + y*y +z*z));
371 }
372
373
382 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline void normalize()
383 {
384 Tfloat a = invnorm<Tfloat>();
385 x = (T)(x * a);
386 y = (T)(y * a);
387 z = (T)(z * a);
388 }
389
390
400 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline void normalize_fast()
401 {
402 Tfloat a = invnorm_fast<Tfloat>();
403 x = (T)(x * a);
404 y = (T)(y * a);
405 z = (T)(z * a);
406 }
407
408
417 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec3<T> getNormalize() const
418 {
419 Vec3<T> V(*this);
420 V.normalize();
421 return V;
422 }
423
424
434 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec3<T> getNormalize_fast() const
435 {
436 Vec3<T> V(*this);
437 V.normalize_fast();
438 return V;
439 }
440
441
442
443#ifdef TGX_ON_ARDUINO
444
450 inline void print(Stream & outputStream = Serial) const
451 {
452 outputStream.printf("[%.3f \t %.3f \t %.3f]\n", x, y, z);
453 }
454#endif
455
456
457 };
458
459
460
467 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec3<T> normalize(Vec3<T> V)
468 {
469 V.template normalize<Tfloat>();
470 return V;
471 }
472
473
481 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec3<T> normalize_fast(Vec3<T> V)
482 {
483 V.template normalize_fast<Tfloat>();
484 return V;
485 }
486
487
491 template<typename T> TGX_INLINE inline T dist2(const Vec3<T> & V1, const Vec3<T> & V2)
492 {
493 const T xx = V1.x - V2.x;
494 const T yy = V1.y - V2.y;
495 const T zz = V1.z - V2.z;
496 return xx * xx + yy * yy + zz * zz;
497 }
498
499
506 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat dist(const Vec3<T> & V1, const Vec3<T> & V2)
507 {
508 const T xx = V1.x - V2.x;
509 const T yy = V1.y - V2.y;
510 const T zz = V1.z - V2.z;
511 return (Tfloat)tgx::precise_sqrt((Tfloat)(xx * xx + yy * yy + zz * zz));
512 }
513
514
522 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat dist_fast(const Vec3<T> & V1, const Vec3<T> & V2)
523 {
524 const T xx = V1.x - V2.x;
525 const T yy = V1.y - V2.y;
526 const T zz = V1.z - V2.z;
527 return (Tfloat)tgx::fast_sqrt((Tfloat)(xx * xx + yy * yy + zz * zz));
528 }
529
530
532 template<typename T> TGX_INLINE inline Vec3<T> operator+(Vec3<T> V1, const Vec3<T> & V2) { V1 += V2; return V1; }
533
535 template<typename T> TGX_INLINE inline Vec3<T> operator-(Vec3<T> V1, const Vec3<T> & V2) { V1 -= V2; return V1; }
536
538 template<typename T> TGX_INLINE inline Vec3<T> operator*(Vec3<T> V1, const Vec3<T> & V2) { V1 *= V2; return V1; }
539
541 template<typename T> TGX_INLINE inline Vec3<T> operator/(Vec3<T> V1, const Vec3<T> & V2) { V1 /= V2; return V1; }
542
544 template<typename T> TGX_INLINE inline Vec3<T> operator+(const T a, Vec3<T> V) { V += a; return V; }
545
547 template<typename T> TGX_INLINE inline Vec3<T> operator+(Vec3<T> V, const T a) { V += a; return V; }
548
550 template<typename T> TGX_INLINE inline Vec3<T> operator-(Vec3<T> V, const T a) { V -= a; return V; }
551
553 template<typename T> TGX_INLINE inline Vec3<T> operator*(const T a, Vec3<T> V) { V *= a; return V; }
554
556 template<typename T> TGX_INLINE inline Vec3<T> operator*(Vec3<T> V, const T a) { V *= a; return V; }
557
559 template<typename T> TGX_INLINE inline Vec3<T> operator/(Vec3<T> V, const T a) { V /= a; return V; }
560
561
565 template<typename T> TGX_INLINE inline T dotProduct(const Vec3<T> & U, const Vec3<T> & V)
566 {
567 return U.x * V.x + U.y * V.y + U.z * V.z;
568 }
569
570#if TGX_USE_FMA_MATH
575 TGX_INLINE inline float dotProduct(const Vec3<float>& U, const Vec3<float>& V)
576 {
577 return fmaf(U.x, V.x, fmaf(U.y, V.y, U.z * V.z));
578 }
579#endif
580
581
585 template<typename T> TGX_INLINE inline Vec3<T> crossProduct(const Vec3<T>& U, const Vec3<T>& V)
586 {
587 return Vec3<T>{ U.y* V.z - U.z * V.y,
588 U.z* V.x - U.x * V.z,
589 U.x* V.y - U.y * V.x };
590 }
591
592
596 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec3<T> lerp(const Tfloat alpha, const Vec3<T> & V1, const Vec3<T> & V2)
597 {
598 return Vec3<T>{ (T)(V1.x + alpha * (V2.x - V1.x)),
599 (T)(V1.y + alpha * (V2.y - V1.y)),
600 (T)(V1.z + alpha * (V2.z - V1.z)) };
601 }
602
603
604}
605
606#endif
607
608#endif
609
tgx::operator*
Mat4< T > operator*(const Mat4< T > &A, const Mat4< T > &B)
Matrix-matrix multiplication.
Definition: Mat4.h:689
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:390
tgx::precise_sqrt
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:291
tgx::precise_invsqrt
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition: Misc.h:343
tgx::fast_sqrt
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition: Misc.h:311
Vec2.h
2D vector.
tgx::dist
TGX_INLINE Tfloat dist(const Vec2< T > &V1, const Vec2< T > &V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:497
tgx::operator+
TGX_INLINE Vec2< T > operator+(Vec2< T > V1, const Vec2< T > &V2)
Addition operator.
Definition: Vec2.h:521
tgx::dist2
TGX_INLINE T dist2(const Vec2< T > &V1, const Vec2< T > &V2)
Compute the squared euclidian distance between two vectors.
Definition: Vec2.h:483
tgx::normalize
TGX_INLINE 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::operator-
TGX_INLINE Vec2< T > operator-(Vec2< T > V1, const Vec2< T > &V2)
Substraction operator.
Definition: Vec2.h:524
tgx::dotProduct
TGX_INLINE T dotProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the dot product U.V between two vectors.
Definition: Vec2.h:554
tgx::normalize_fast
TGX_INLINE 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
tgx::operator/
TGX_INLINE Vec2< T > operator/(Vec2< T > V1, const Vec2< T > &V2)
Division operator.
Definition: Vec2.h:530
tgx::dist_fast
TGX_INLINE Tfloat dist_fast(const Vec2< T > &V1, const Vec2< T > &V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:512
tgx::lerp
TGX_INLINE Vec2< T > lerp(Tfloat alpha, const Vec2< T > &V1, const Vec2< T > &V2)
Return the linear interpolation: V1 + alpha(V2 - V1).
Definition: Vec2.h:583
tgx::crossProduct
TGX_INLINE T crossProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the cross product UxV (i.e.
Definition: Vec2.h:574
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::invnorm_fast
TGX_INLINE Tfloat invnorm_fast() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec3.h:368
tgx::Vec3::getNormalize_fast
TGX_INLINE Vec3< T > getNormalize_fast() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec3.h:434
tgx::Vec3::operator-=
TGX_INLINE void operator-=(const Vec3 &V)
Substract another vector from this one.
Definition: Vec3.h:223
tgx::Vec3::operator-=
TGX_INLINE void operator-=(T v)
scalar substraction.
Definition: Vec3.h:267
tgx::Vec3::operator<
bool operator<(const Vec3 &V) const
Less-than comparison operator.
Definition: Vec3.h:158
tgx::Vec3::getNormalize
Vec3< T > getNormalize() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec3.h:417
tgx::Vec3::operator*=
TGX_INLINE void operator*=(T v)
scalar multiplication.
Definition: Vec3.h:277
tgx::Vec3::operator>=
bool operator>=(const Vec3 &V) const
Greater-than-or-equal comparison operator.
Definition: Vec3.h:203
tgx::Vec3::operator<=
bool operator<=(const Vec3 &V) const
Less-than-or-equal comparison operator.
Definition: Vec3.h:176
tgx::Vec3::z
T z
'z' coordinate (third dimension)
Definition: Vec3.h:83
tgx::Vec3::norm2
TGX_INLINE T norm2() const
Compute the squared euclidian norm of the vector.
Definition: Vec3.h:310
tgx::Vec3::Vec3
constexpr Vec3(Vec2< T > V, T Z)
Constructor from a Vec2.
Definition: Vec3.h:109
tgx::Vec3::operator!=
bool operator!=(const Vec3 &V) const
Inequality operator.
Definition: Vec3.h:149
tgx::Vec3::Vec3
Vec3(const Vec3 &V)=default
Default copy constructor.
tgx::Vec3::operator/=
TGX_INLINE void operator/=(T v)
scalar division.
Definition: Vec3.h:287
tgx::Vec3::operator=
Vec3 & operator=(const Vec3 &V)=default
Default assignment operator.
tgx::Vec3::operator>
bool operator>(const Vec3 &V) const
Greater-than comparison operator.
Definition: Vec3.h:194
tgx::Vec3::operator+=
TGX_INLINE void operator+=(const Vec3 &V)
Add another vector to this one.
Definition: Vec3.h:212
tgx::Vec3::operator*=
TGX_INLINE void operator*=(const Vec3 &V)
Multiply this vector by another one (coordinate by coordinate multiplication).
Definition: Vec3.h:234
tgx::Vec3::operator==
bool operator==(const Vec3 &V) const
Equality comparator.
Definition: Vec3.h:140
tgx::Vec3::operator-
TGX_INLINE Vec3 operator-() const
unary negation operator
Definition: Vec3.h:298
tgx::Vec3::Vec3
Vec3()
default constructor: the vector content is undefined.
Definition: Vec3.h:89
tgx::Vec3::invnorm
Tfloat invnorm() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec3.h:353
tgx::Vec3::normalize_fast
TGX_INLINE void normalize_fast()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec3.h:400
tgx::Vec3::operator/=
TGX_INLINE void operator/=(const Vec3 &V)
Divide this vector by another one (coordinate by coordinate division).
Definition: Vec3.h:245
tgx::Vec3::normalize
void normalize()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec3.h:382
tgx::Vec3::operator+=
TGX_INLINE void operator+=(T v)
scalar addition.
Definition: Vec3.h:256
tgx::Vec3::norm
Tfloat norm() const
Compute the euclidian norm of the vector.
Definition: Vec3.h:324
tgx::Vec3::norm_fast
TGX_INLINE Tfloat norm_fast() const
Compute the euclidian norm of the vector.
Definition: Vec3.h:339
tgx::Vec3::Vec3
constexpr Vec3(T X, T Y, T Z)
Constructor with explicit initialization.
Definition: Vec3.h:95