Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Vector4.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{
83 template<typename T>
84 struct INFINITY_API_TEMPLATE t_Vector4
85 {
95 union
96 {
97 T value[4];
98 struct
99 {
100 T x, y, z, w;
101 };
102 struct
103 {
104 T r, g, b, a;
105 };
106 struct
107 {
108 T s, t, p, q;
109 };
110 };
111
117 constexpr t_Vector4() : value{} {}
118
124 constexpr t_Vector4(const t_Vector4& v) : value{v.x, v.y, v.z, v.w} {}
125
129 constexpr t_Vector4& operator=(const t_Vector4&) = default;
130
145 constexpr t_Vector4(T x, T y, T z, T w) : value{x, y, z, w} {}
146
161 template<typename R>
162 constexpr explicit t_Vector4(const t_Vector4<R>& v) :
163 value{static_cast<T>(v.x), static_cast<T>(v.y), static_cast<T>(v.z), static_cast<T>(v.w)} {}
164
171 T& operator[](size_t i) { return value[i]; }
172
179 T operator[](size_t i) const { return value[i]; }
180
189 void set(T in_x, T in_y, T in_z, T in_w)
190 {
191 x = in_x;
192 y = in_y;
193 z = in_z;
194 w = in_w;
195 }
196
208 T* data() { return value; }
209
215 const T* data() const { return value; }
216
227 template<typename R>
229 {
230 value[0] = static_cast<T>(rhs[0]);
231 value[1] = static_cast<T>(rhs[1]);
232 value[2] = static_cast<T>(rhs[2]);
233 value[3] = static_cast<T>(rhs[3]);
234 return *this;
235 }
236
243 inline t_Vector4& operator+=(const t_Vector4& rhs)
244 {
245 value[0] += rhs.value[0];
246 value[1] += rhs.value[1];
247 value[2] += rhs.value[2];
248 value[3] += rhs.value[3];
249 return *this;
250 }
251
258 inline t_Vector4& operator-=(const t_Vector4& rhs)
259 {
260 value[0] -= rhs.value[0];
261 value[1] -= rhs.value[1];
262 value[2] -= rhs.value[2];
263 value[3] -= rhs.value[3];
264 return *this;
265 }
266
273 inline t_Vector4& operator*=(T rhs)
274 {
275 value[0] *= rhs;
276 value[1] *= rhs;
277 value[2] *= rhs;
278 value[3] *= rhs;
279 return *this;
280 }
281
288 inline t_Vector4& operator*=(const t_Vector4& rhs)
289 {
290 value[0] *= rhs.value[0];
291 value[1] *= rhs.value[1];
292 value[2] *= rhs.value[2];
293 value[3] *= rhs.value[3];
294 return *this;
295 }
296
308 inline t_Vector4& operator/=(T rhs)
309 {
310 if constexpr (std::is_floating_point_v<T>)
311 {
312 T inv = static_cast<T>(1.0) / rhs;
313 value[0] *= inv;
314 value[1] *= inv;
315 value[2] *= inv;
316 value[3] *= inv;
317 }
318 else
319 {
320 value[0] /= rhs;
321 value[1] /= rhs;
322 value[2] /= rhs;
323 value[3] /= rhs;
324 }
325 return *this;
326 }
327
342 explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0 || value[3] != 0.0; }
343
363 {
364 float len = length();
365 if (len > 1e-6f)
366 {
367 float invLength = 1.0f / len;
368 return t_Vector4<T>(x * invLength, y * invLength, z * invLength, w * invLength);
369 }
370
371 return t_Vector4<T>(0.0f, 0.0f, 0.0f, 1.0f);
372 }
373
394 t_Vector4<T> cross(const t_Vector4<T>& other) const
395 {
396 return t_Vector4<T>(
397 y * other.z - z * other.y,
398 z * other.x - x * other.z,
399 x * other.y - y * other.x,
400 0.0f // The w component is typically set to 0 for cross products in 3D space
401 );
402 }
403
416 float length() const
417 {
418 return std::sqrt(squaredMagnitude());
419 }
420
428 float magnitude() const
429 {
430 return std::sqrt(squaredMagnitude());
431 }
432
453 float squaredMagnitude() const
454 {
455 return x * x + y * y + z * z + w * w;
456 }
457
472 float distance(const t_Vector4<T>& other) const
473 {
474 return (*this - other).magnitude();
475 }
476
497 float dot(const t_Vector4<T>& other) const
498 {
499 return x * other.x + y * other.y + z * other.z + w * other.w;
500 }
501
518 float angle(const t_Vector4<T>& other) const
519 {
520 float dotProduct = dot(other);
521 float lengths = length() * other.length();
522 if (lengths == 0.0f) return 0.0f; // Avoid division by zero
523 return std::acos(dotProduct / lengths);
524 }
525
542 {
543 if constexpr (std::is_unsigned<T>::value) {
544 return *this; // No change for unsigned types
545 } else {
546 return t_Vector4<T>(std::abs(x), std::abs(y), std::abs(z), std::abs(w));
547 }
548 }
549 };
550
556
568
574
580
586
592
598
604
610
616
622
628
637 template<typename T>
638 constexpr bool operator==(const t_Vector4<T>& lhs, const t_Vector4<T>& rhs)
639 {
640 return lhs[0] == rhs[0] && lhs[1] == rhs[1] && lhs[2] == rhs[2] && lhs[3] == rhs[3];
641 }
642
651 template<typename T>
652 constexpr bool operator!=(const t_Vector4<T>& lhs, const t_Vector4<T>& rhs)
653 {
654 return lhs[0] != rhs[0] || lhs[1] != rhs[1] || lhs[2] != rhs[2] || lhs[3] != rhs[3];
655 }
656
668 template<typename T>
669 constexpr bool operator<(const t_Vector4<T>& lhs, const t_Vector4<T>& rhs)
670 {
671 if (lhs[0] < rhs[0]) return true;
672 if (lhs[0] > rhs[0]) return false;
673 if (lhs[1] < rhs[1]) return true;
674 if (lhs[1] > rhs[1]) return false;
675 if (lhs[2] < rhs[2]) return true;
676 if (lhs[2] > rhs[2]) return false;
677 return lhs[3] < rhs[3];
678 }
679
688 template<typename T>
689 constexpr t_Vector4<T> operator-(const t_Vector4<T>& lhs, const t_Vector4<T>& rhs)
690 {
691 return t_Vector4<T>(lhs[0] - rhs[0], lhs[1] - rhs[1], lhs[2] - rhs[2], lhs[3] - rhs[3]);
692 }
693
701 template<typename T>
703 {
704 return t_Vector4<T>(-v[0], -v[1], -v[2], -v[3]);
705 }
706
715 template<typename T>
716 constexpr t_Vector4<T> operator+(const t_Vector4<T>& lhs, const t_Vector4<T>& rhs)
717 {
718 return t_Vector4<T>(lhs[0] + rhs[0], lhs[1] + rhs[1], lhs[2] + rhs[2], lhs[3] + rhs[3]);
719 }
720
729 template<typename T>
730 constexpr t_Vector4<T> operator*(const t_Vector4<T>& lhs, T rhs)
731 {
732 return t_Vector4<T>(lhs[0] * rhs, lhs[1] * rhs, lhs[2] * rhs, lhs[3] * rhs);
733 }
734
743 template<typename T>
744 constexpr t_Vector4<T> operator*(const t_Vector4<T>& lhs, const t_Vector4<T>& rhs)
745 {
746 return t_Vector4<T>(lhs[0] * rhs[0], lhs[1] * rhs[1], lhs[2] * rhs[2], lhs[3] * rhs[3]);
747 }
748
761 template<typename T>
762 constexpr t_Vector4<T> operator/(const t_Vector4<T>& lhs, T rhs)
763 {
764 if constexpr (std::is_floating_point_v<T>)
765 {
766 T inv = static_cast<T>(1.0) / rhs;
767 return t_Vector4<T>(lhs[0] * inv, lhs[1] * inv, lhs[2] * inv, lhs[3] * inv);
768 }
769 else
770 {
771 return t_Vector4<T>(lhs[0] / rhs, lhs[1] / rhs, lhs[2] / rhs, lhs[3] / rhs);
772 }
773 }
774
785 template<typename T>
786 inline std::ostream& operator<<(std::ostream& out, const t_Vector4<T>& v)
787 {
788 out << v.x << " " << v.y << " " << v.z << " " << v.w;
789 return out;
790 }
791
802 template<typename T>
803 inline std::istream& operator>>(std::istream& in, t_Vector4<T>& v)
804 {
805 in >> v.x >> v.y >> v.z >> v.w;
806 return in;
807 }
808
820 template<>
821 inline std::istream& operator>>(std::istream& in, t_Vector4<uint8_t>& v) {
822 int temp_x, temp_y, temp_z, temp_w;
823 in >> temp_x >> temp_y >> temp_z >> temp_w;
824
825 v.x = static_cast<uint8_t>(temp_x);
826 v.y = static_cast<uint8_t>(temp_y);
827 v.z = static_cast<uint8_t>(temp_z);
828 v.w = static_cast<uint8_t>(temp_w);
829
830 return in;
831 }
832}
833
834// Register Value<Vector4> specializations in the type system
845
846// Register Array<Vector4> 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 4-component vector.
Definition Vector4.hpp:85
constexpr t_Vector4(const t_Vector4< R > &v)
Explicit type-converting constructor.
Definition Vector4.hpp:162
T y
Definition Vector4.hpp:100
T x
Definition Vector4.hpp:100
constexpr t_Vector4()
Default constructor.
Definition Vector4.hpp:117
const T * data() const
Gets a const pointer to the underlying data.
Definition Vector4.hpp:215
float dot(const t_Vector4< T > &other) const
Computes the dot product with another vector.
Definition Vector4.hpp:497
T a
Color with alpha interpretation.
Definition Vector4.hpp:104
T w
Homogeneous coordinate or quaternion interpretation.
Definition Vector4.hpp:100
T & operator[](size_t i)
Array subscript operator (mutable).
Definition Vector4.hpp:171
T operator[](size_t i) const
Array subscript operator (const).
Definition Vector4.hpp:179
constexpr t_Vector4(T x, T y, T z, T w)
Constructs a vector from component values.
Definition Vector4.hpp:145
T * data()
Gets a mutable pointer to the underlying data.
Definition Vector4.hpp:208
t_Vector4 & operator*=(T rhs)
Scalar multiplication assignment.
Definition Vector4.hpp:273
t_Vector4< T > cross(const t_Vector4< T > &other) const
Computes the cross product treating xyz as a 3D vector.
Definition Vector4.hpp:394
T p
Definition Vector4.hpp:108
T z
Definition Vector4.hpp:100
t_Vector4 & operator*=(const t_Vector4 &rhs)
Component-wise multiplication assignment.
Definition Vector4.hpp:288
T value[4]
Array access to components.
Definition Vector4.hpp:97
t_Vector4 & operator=(const t_Vector4< R > &rhs)
Type-converting assignment operator.
Definition Vector4.hpp:228
t_Vector4< T > abs() const
Computes the component-wise absolute value.
Definition Vector4.hpp:541
float distance(const t_Vector4< T > &other) const
Computes the Euclidean distance to another vector.
Definition Vector4.hpp:472
t_Vector4 & operator/=(T rhs)
Scalar division assignment.
Definition Vector4.hpp:308
constexpr t_Vector4 & operator=(const t_Vector4 &)=default
Copy assignment operator.
void set(T in_x, T in_y, T in_z, T in_w)
Sets all four components simultaneously.
Definition Vector4.hpp:189
float squaredMagnitude() const
Computes the squared magnitude of this vector.
Definition Vector4.hpp:453
t_Vector4 & operator-=(const t_Vector4 &rhs)
Component-wise subtraction assignment.
Definition Vector4.hpp:258
float angle(const t_Vector4< T > &other) const
Computes the angle between this vector and another.
Definition Vector4.hpp:518
float length() const
Computes the length (magnitude) of this vector.
Definition Vector4.hpp:416
constexpr t_Vector4(const t_Vector4 &v)
Copy constructor.
Definition Vector4.hpp:124
float magnitude() const
Computes the magnitude of this vector.
Definition Vector4.hpp:428
t_Vector4< T > normalized() const
Computes the normalized version of this vector.
Definition Vector4.hpp:362
t_Vector4 & operator+=(const t_Vector4 &rhs)
Component-wise addition assignment.
Definition Vector4.hpp:243