P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
Vector4.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <cmath>
14#include <cstdint>
15#include <type_traits>
16
17#include "Constants.hpp"
18
19template <typename T>
20struct Vector4 {
21 T w{}, x{}, y{}, z{};
22
23 // Comparison Operators
24 template<typename = typename std::enable_if<std::is_convertible<decltype(std::declval<T>() == std::declval<T>()), bool>::value>::type>
25 bool operator==(const Vector4& other) const { return w == other.w && x == other.x && y == other.y && z == other.z; }
26
27 template<typename = typename std::enable_if<std::is_convertible<decltype(std::declval<T>() == std::declval<T>()), bool>::value>::type>
28 bool operator!=(const Vector4& other) const { return !(*this == other); }
29
30 // Arithmetic Operators
31 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
32 Vector4 operator+(const Vector4& other) const { return Vector4{w + other.w, x + other.x, y + other.y, z + other.z}; }
33
34 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
35 Vector4 operator-(const Vector4& other) const { return Vector4{w - other.w, x - other.x, y - other.y, z - other.z}; }
36
37 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
38 Vector4 operator*(T scalar) const { return Vector4{w * scalar, x * scalar, y * scalar, z * scalar}; }
39
40 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
41 Vector4 operator/(T scalar) const { return Vector4{w / scalar, x / scalar, y / scalar, z / scalar}; }
42
43 // Compound Assignment Operators
44 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
45 Vector4& operator+=(const Vector4& other) { w += other.w; x += other.x; y += other.y; z += other.z; return *this; }
46
47 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
48 Vector4& operator-=(const Vector4& other) { w += other.w; x -= other.x; y -= other.y; z -= other.z; return *this; }
49
50 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
51 Vector4& operator*=(T scalar) { w *= scalar; x *= scalar; y *= scalar; z *= scalar; return *this; }
52
53 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
54 Vector4& operator/=(T scalar) { w /= scalar; x /= scalar; y /= scalar; z /= scalar; return *this; }
55
56 // Unary Operators
57 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
58 Vector4 operator-() const { return Vector4{-w, -x, -y, -z}; }
59
60 //methods
61 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
62 T dot(const Vector4& other) const { return w * other.w + x * other.x + y * other.y + z * other.z; }
63
64 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
65 T square_magnitude() const { return w*w + x*x + y*y + z*z; }
66
67 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
68 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
69 magnitude() const { return std::sqrt(square_magnitude()); }
70
71 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
72 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
73 distance(const Vector4& other) const { return (*this - other).magnitude(); }
74
75 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
77 auto len = magnitude();
78
79 if (len == 0) return {0, 0};
80 return {T(w / len), T(x / len), T(y / len), T(z / len)};
81 }
82
83 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
84 void normalize() {
85 double len = magnitude();
86
87 if (len == 0) return;
88 w /= len;
89 x /= len;
90 y /= len;
91 z /= len;
92 }
93
94 template<typename U = T, typename = typename std::enable_if<std::is_floating_point<U>::value>::type>
95 bool same(const Vector4& other, T epsilon = epsilon_v<T>) const {
96 return std::abs(w - other.w) < epsilon &&
97 std::abs(x - other.x) < epsilon &&
98 std::abs(y - other.y) < epsilon &&
99 std::abs(z - other.z) < epsilon;
100 }
101};
102
107
constexpr T epsilon_v
Definition Constants.hpp:32
Vector4< std::uint32_t > Vector4u
Definition Vector4.hpp:103
Vector4< double > Vector4f
Definition Vector4.hpp:105
Vector4< std::int32_t > Vector4i
Definition Vector4.hpp:104
Definition Vector4.hpp:20
Vector4 operator-(const Vector4 &other) const
Definition Vector4.hpp:35
Vector4 & operator-=(const Vector4 &other)
Definition Vector4.hpp:48
T x
Definition Vector4.hpp:21
Vector4 & operator/=(T scalar)
Definition Vector4.hpp:54
Vector4 operator*(T scalar) const
Definition Vector4.hpp:38
std::conditional< std::is_floating_point< T >::value, T, double >::type magnitude() const
Definition Vector4.hpp:69
T dot(const Vector4 &other) const
Definition Vector4.hpp:62
T z
Definition Vector4.hpp:21
Vector4 & operator*=(T scalar)
Definition Vector4.hpp:51
void normalize()
Definition Vector4.hpp:84
std::conditional< std::is_floating_point< T >::value, T, double >::type distance(const Vector4 &other) const
Definition Vector4.hpp:73
std::uint32_t w
Definition Vector4.hpp:21
Vector4 normalized() const
Definition Vector4.hpp:76
T square_magnitude() const
Definition Vector4.hpp:65
Vector4 & operator+=(const Vector4 &other)
Definition Vector4.hpp:45
bool same(const Vector4 &other, T epsilon=epsilon_v< T >) const
Definition Vector4.hpp:95
T y
Definition Vector4.hpp:21
Vector4 operator/(T scalar) const
Definition Vector4.hpp:41
Vector4 operator+(const Vector4 &other) const
Definition Vector4.hpp:32
Vector4 operator-() const
Definition Vector4.hpp:58