Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Vector3.hpp
1// INFINITY_API_PUBLIC
2
3#pragma once
4
5#include <Infinity/Types/Core/Value.hpp>
6#include <Infinity/Types/Containers/Array.hpp>
7
8#include <cstdint>
9#include <cmath>
10
12{
80 template<typename T>
81 struct INFINITY_API_TEMPLATE t_Vector3
82 {
92 union
93 {
94 T value[3];
95 struct
96 {
97 T x, y, z;
98 };
99 struct
100 {
101 T r, g, b;
102 };
103 struct
104 {
105 T s, t, p;
106 };
107 };
108
114 constexpr t_Vector3() : value{} {}
115
121 constexpr t_Vector3(const t_Vector3& v) : value{v.x, v.y, v.z} {}
122
126 constexpr t_Vector3& operator=(const t_Vector3&) = default;
127
141 constexpr t_Vector3(T x, T y, T z) : value{x, y, z} {}
142
157 template<typename R>
158 constexpr explicit t_Vector3(const t_Vector3<R>& v) :
159 value{static_cast<T>(v.x), static_cast<T>(v.y), static_cast<T>(v.z)} {}
160
167 T& operator[](size_t i) { return value[i]; }
168
175 T operator[](size_t i) const { return value[i]; }
176
184 void set(T in_x, T in_y, T in_z)
185 {
186 x = in_x;
187 y = in_y;
188 z = in_z;
189 }
190
202 T* data() { return value; }
203
209 const T* data() const { return value; }
210
221 template<typename R>
223 {
224 value[0] = static_cast<T>(rhs[0]);
225 value[1] = static_cast<T>(rhs[1]);
226 value[2] = static_cast<T>(rhs[2]);
227 return *this;
228 }
229
236 inline t_Vector3& operator+=(const t_Vector3& rhs)
237 {
238 value[0] += rhs.value[0];
239 value[1] += rhs.value[1];
240 value[2] += rhs.value[2];
241 return *this;
242 }
243
250 inline t_Vector3& operator-=(const t_Vector3& rhs)
251 {
252 value[0] -= rhs.value[0];
253 value[1] -= rhs.value[1];
254 value[2] -= rhs.value[2];
255 return *this;
256 }
257
264 inline t_Vector3& operator*=(T rhs)
265 {
266 value[0] *= rhs;
267 value[1] *= rhs;
268 value[2] *= rhs;
269 return *this;
270 }
271
278 inline t_Vector3& operator*=(const t_Vector3& rhs)
279 {
280 value[0] *= rhs.value[0];
281 value[1] *= rhs.value[1];
282 value[2] *= rhs.value[2];
283 return *this;
284 }
285
297 inline t_Vector3& operator/=(T rhs)
298 {
299 if constexpr (std::is_floating_point_v<T>)
300 {
301 T inv = static_cast<T>(1.0) / rhs;
302 value[0] *= inv;
303 value[1] *= inv;
304 value[2] *= inv;
305 }
306 else
307 {
308 value[0] /= rhs;
309 value[1] /= rhs;
310 value[2] /= rhs;
311 }
312 return *this;
313 }
314
329 explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0; }
330
345 {
346 float len = length();
347 if (len == 0.0f) return t_Vector3<T>(0, 0, 0);
348 return t_Vector3<T>(x / len, y / len, z / len);
349 }
350
370 t_Vector3<T> cross(const t_Vector3<T>& other) const
371 {
372 return t_Vector3<T>(
373 y * other.z - z * other.y,
374 z * other.x - x * other.z,
375 x * other.y - y * other.x
376 );
377 }
378
389 float length() const
390 {
391 return std::sqrt(squaredMagnitude());
392 }
393
401 float magnitude() const
402 {
403 return std::sqrt(squaredMagnitude());
404 }
405
421 float squaredMagnitude() const
422 {
423 return x * x + y * y + z * z;
424 }
425
438 float distance(const t_Vector3<T>& other) const
439 {
440 return (*this - other).magnitude();
441 }
442
464 float dot(const t_Vector3<T>& other) const
465 {
466 return x * other.x + y * other.y + z * other.z;
467 }
468
485 float angle(const t_Vector3<T>& other) const
486 {
487 float dotProduct = dot(other);
488 float lengths = length() * other.length();
489 if (lengths == 0.0f) return 0.0f; // Avoid division by zero
490 return std::acos(dotProduct / lengths);
491 }
492
509 {
510 if constexpr (std::is_unsigned<T>::value) {
511 return *this; // No change for unsigned types
512 } else {
513 return t_Vector3<T>(std::abs(x), std::abs(y), std::abs(z));
514 }
515 }
516
522 static constexpr t_Vector3<T> zero() { return t_Vector3<T>((T)0.0f, (T)0.0f, (T)0.0f); }
523
531 static constexpr t_Vector3<T> identity() { return t_Vector3<T>((T)0.0f, (T)0.0f, (T)0.0f); }
532
538 static constexpr t_Vector3<T> left() { return t_Vector3<T>((T)-1.0f, (T)0.0f, (T)0.0f); }
539
545 static constexpr t_Vector3<T> right() { return t_Vector3<T>((T)1.0f, (T)0.0f, (T)0.0f); }
546
552 static constexpr t_Vector3<T> up() { return t_Vector3<T>((T)0.0f, (T)1.0f, (T)0.0f); }
553
559 static constexpr t_Vector3<T> down() { return t_Vector3<T>((T)0.0f, (T)-1.0f, (T)0.0f); }
560
566 static constexpr t_Vector3<T> front() { return t_Vector3<T>((T)0.0f, (T)0.0f, (T)1.0f); }
567
573 static constexpr t_Vector3<T> back() { return t_Vector3<T>((T)0.0f, (T)0.0f, (T)-1.0f); }
574 };
575
581
587
593
599
605
611
617
623
629
635
641
650 template<typename T>
651 constexpr bool operator==(const t_Vector3<T>& lhs, const t_Vector3<T>& rhs)
652 {
653 return lhs[0] == rhs[0] && lhs[1] == rhs[1] && lhs[2] == rhs[2];
654 }
655
664 template<typename T>
665 constexpr bool operator!=(const t_Vector3<T>& lhs, const t_Vector3<T>& rhs)
666 {
667 return lhs[0] != rhs[0] || lhs[1] != rhs[1] || lhs[2] != rhs[2];
668 }
669
681 template<typename T>
682 constexpr bool operator<(const t_Vector3<T>& lhs, const t_Vector3<T>& rhs)
683 {
684 if (lhs[0] < rhs[0]) return true;
685 if (lhs[0] > rhs[0]) return false;
686 if (lhs[1] < rhs[1]) return true;
687 if (lhs[1] > rhs[1]) return false;
688 return lhs[2] < rhs[2];
689 }
690
699 template<typename T>
700 constexpr t_Vector3<T> operator-(const t_Vector3<T>& lhs, const t_Vector3<T>& rhs)
701 {
702 return t_Vector3<T>(lhs[0] - rhs[0], lhs[1] - rhs[1], lhs[2] - rhs[2]);
703 }
704
712 template<typename T>
714 {
715 return t_Vector3<T>(-v[0], -v[1], -v[2]);
716 }
717
726 template<typename T>
727 constexpr t_Vector3<T> operator+(const t_Vector3<T>& lhs, const t_Vector3<T>& rhs)
728 {
729 return t_Vector3<T>(lhs[0] + rhs[0], lhs[1] + rhs[1], lhs[2] + rhs[2]);
730 }
731
740 template<typename T>
741 constexpr t_Vector3<T> operator*(const t_Vector3<T>& lhs, T rhs)
742 {
743 return t_Vector3<T>(lhs[0] * rhs, lhs[1] * rhs, lhs[2] * rhs);
744 }
745
754 template<typename T>
755 constexpr t_Vector3<T> operator*(const t_Vector3<T>& lhs, const t_Vector3<T>& rhs)
756 {
757 return t_Vector3<T>(lhs[0] * rhs[0], lhs[1] * rhs[1], lhs[2] * rhs[2]);
758 }
759
772 template<typename T>
773 constexpr t_Vector3<T> operator/(const t_Vector3<T>& lhs, T rhs)
774 {
775 if constexpr (std::is_floating_point_v<T>)
776 {
777 T inv = static_cast<T>(1.0) / rhs;
778 return t_Vector3<T>(lhs[0] * inv, lhs[1] * inv, lhs[2] * inv);
779 }
780 else
781 {
782 return t_Vector3<T>(lhs[0] / rhs, lhs[1] / rhs, lhs[2] / rhs);
783 }
784 }
785
796 template<typename T>
797 inline std::ostream& operator<<(std::ostream& out, const t_Vector3<T>& v)
798 {
799 out << v.x << " " << v.y << " " << v.z;
800 return out;
801 }
802
813 template<typename T>
814 inline std::istream& operator>>(std::istream& in, t_Vector3<T>& v)
815 {
816 in >> v.x >> v.y >> v.z;
817 return in;
818 }
819
830 template<>
831 inline std::istream& operator>>(std::istream& in, t_Vector3<uint8_t>& v) {
832 int temp_x, temp_y, temp_z;
833 in >> temp_x >> temp_y >> temp_z;
834
835 v.x = static_cast<uint8_t>(temp_x);
836 v.y = static_cast<uint8_t>(temp_y);
837 v.z = static_cast<uint8_t>(temp_z);
838 return in;
839 }
840
841}
842
843// Register Value<Vector3> specializations in the type system
854
855// Register Array<Vector3> specializations in the type system
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
Template wrapper for primitive types to integrate with the Infinity type system.
Definition Value.hpp:89
Definition Math.hpp:9
std::istream & operator>>(std::istream &in, t_Matrix3< T > &m)
Stream extraction operator for single-line text input.
Definition Matrix3.hpp:726
constexpr Rect operator-(const Rect &lhs, const Rect &rhs)
Rectangle subtraction operator.
Definition Rect.hpp:371
constexpr bool operator<(const t_Vector2< T > &lhs, const t_Vector2< T > &rhs)
Less-than comparison operator for ordering.
Definition Vector2.hpp:589
constexpr t_Vector2< T > operator/(const t_Vector2< T > &lhs, T rhs)
Scalar division.
Definition Vector2.hpp:678
constexpr t_Vector2< T > operator*(const t_Vector2< T > &lhs, T rhs)
Scalar multiplication.
Definition Vector2.hpp:646
constexpr Rect operator+(const Rect &lhs, const Rect &rhs)
Rectangle addition operator.
Definition Rect.hpp:398
std::ostream & operator<<(std::ostream &out, const t_Matrix3< T > &m)
Stream insertion operator for single-line text output.
Definition Matrix3.hpp:709
constexpr bool operator!=(const Rect &lhs, const Rect &rhs)
Inequality comparison operator.
Definition Rect.hpp:357
constexpr bool operator==(const Rect &lhs, const Rect &rhs)
Equality comparison operator.
Definition Rect.hpp:345
Template structure representing a 3-component vector.
Definition Vector3.hpp:82
static constexpr t_Vector3< T > front()
Returns the front/forward direction vector (0, 0, 1).
Definition Vector3.hpp:566
T * data()
Gets a mutable pointer to the underlying data.
Definition Vector3.hpp:202
constexpr t_Vector3 & operator=(const t_Vector3 &)=default
Copy assignment operator.
constexpr t_Vector3(const t_Vector3< R > &v)
Explicit type-converting constructor.
Definition Vector3.hpp:158
t_Vector3 & operator*=(T rhs)
Scalar multiplication assignment.
Definition Vector3.hpp:264
T p
Texture coordinate interpretation.
Definition Vector3.hpp:105
void set(T in_x, T in_y, T in_z)
Sets all three components simultaneously.
Definition Vector3.hpp:184
constexpr t_Vector3()
Default constructor.
Definition Vector3.hpp:114
float squaredMagnitude() const
Computes the squared magnitude of this vector.
Definition Vector3.hpp:421
static constexpr t_Vector3< T > right()
Returns the right direction vector (1, 0, 0).
Definition Vector3.hpp:545
T x
Definition Vector3.hpp:97
float magnitude() const
Computes the magnitude of this vector.
Definition Vector3.hpp:401
t_Vector3 & operator+=(const t_Vector3 &rhs)
Component-wise addition assignment.
Definition Vector3.hpp:236
T z
Cartesian coordinate interpretation.
Definition Vector3.hpp:97
float angle(const t_Vector3< T > &other) const
Computes the angle between this vector and another.
Definition Vector3.hpp:485
T value[3]
Array access to components.
Definition Vector3.hpp:94
float length() const
Computes the length (magnitude) of this vector.
Definition Vector3.hpp:389
static constexpr t_Vector3< T > zero()
Returns a zero vector (0, 0, 0).
Definition Vector3.hpp:522
t_Vector3 & operator*=(const t_Vector3 &rhs)
Component-wise multiplication assignment.
Definition Vector3.hpp:278
static constexpr t_Vector3< T > identity()
Returns an identity/zero vector (0, 0, 0).
Definition Vector3.hpp:531
const T * data() const
Gets a const pointer to the underlying data.
Definition Vector3.hpp:209
t_Vector3 & operator=(const t_Vector3< R > &rhs)
Type-converting assignment operator.
Definition Vector3.hpp:222
t_Vector3< T > cross(const t_Vector3< T > &other) const
Computes the cross product with another vector.
Definition Vector3.hpp:370
t_Vector3< T > abs() const
Computes the component-wise absolute value.
Definition Vector3.hpp:508
constexpr t_Vector3(T x, T y, T z)
Constructs a vector from component values.
Definition Vector3.hpp:141
t_Vector3< T > normalized() const
Computes the normalized version of this vector.
Definition Vector3.hpp:344
T b
Color component interpretation.
Definition Vector3.hpp:101
static constexpr t_Vector3< T > left()
Returns the left direction vector (-1, 0, 0).
Definition Vector3.hpp:538
t_Vector3 & operator/=(T rhs)
Scalar division assignment.
Definition Vector3.hpp:297
constexpr t_Vector3(const t_Vector3 &v)
Copy constructor.
Definition Vector3.hpp:121
float distance(const t_Vector3< T > &other) const
Computes the Euclidean distance to another vector.
Definition Vector3.hpp:438
T y
Definition Vector3.hpp:97
static constexpr t_Vector3< T > down()
Returns the down direction vector (0, -1, 0).
Definition Vector3.hpp:559
T operator[](size_t i) const
Array subscript operator (const).
Definition Vector3.hpp:175
float dot(const t_Vector3< T > &other) const
Computes the dot product with another vector.
Definition Vector3.hpp:464
T & operator[](size_t i)
Array subscript operator (mutable).
Definition Vector3.hpp:167
static constexpr t_Vector3< T > up()
Returns the up direction vector (0, 1, 0).
Definition Vector3.hpp:552
static constexpr t_Vector3< T > back()
Returns the back/backward direction vector (0, 0, -1).
Definition Vector3.hpp:573
t_Vector3 & operator-=(const t_Vector3 &rhs)
Component-wise subtraction assignment.
Definition Vector3.hpp:250