P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
Vector3.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 Vector3 {
21 T 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 Vector3& other) const { return 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 Vector3& other) const { return !(*this == other); }
29
30 // Arithmetic Operators
31 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
32 Vector3 operator+(const Vector3& other) const { return Vector3{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 Vector3 operator-(const Vector3& other) const { return Vector3{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 Vector3 operator*(T scalar) const { return Vector3{x * scalar, y * scalar, z * scalar}; }
39
40 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
41 Vector3 operator/(T scalar) const { return Vector3{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 Vector3& operator+=(const Vector3& other) { 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 Vector3& operator-=(const Vector3& other) { 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 Vector3& operator*=(T 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 Vector3& operator/=(T 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 Vector3 operator-() const { return Vector3{-x, -y, -z}; }
59
60 //methods
61 template<typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
62 T dot(const Vector3& other) const { return x * other.x + y * other.y + z * other.z; }
63
64 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
65 Vector3 cross(const Vector3& other) const {
66 return {
67 y * other.z - z * other.y,
68 z * other.x - x * other.z,
69 x * other.y - y * other.x
70 };
71 }
72
73 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
74 T square_magnitude() const { return x*x + y*y + z*z; }
75
76 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
77 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
78 magnitude() const { return std::sqrt(square_magnitude()); }
79
80 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
81 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
82 distance(const Vector3& other) const { return (*this - other).magnitude(); }
83
84 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
85 double angle(const Vector3& other) const {
86 //en 3D l'angle est toujours positif : le produit vectoriel donne une
87 //norme, pas un signe. Un signe demanderait un axe de reference.
88 const double sine = double(this->cross(other).magnitude());
89
90 return std::atan2(sine, double(this->dot(other))); //radians, [0, pi]
91 }
92
93 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
95 T len = magnitude();
96
97 if (len == 0) return {0, 0, 0};
98 return {T(x / len), T(y / len), T(z / len)};
99 }
100
101 template<typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
102 void normalize() {
103 double len = magnitude();
104
105 if (len == 0) return;
106 x /= len;
107 y /= len;
108 z /= len;
109 }
110
111 template<typename U = T, typename = typename std::enable_if<std::is_floating_point<U>::value>::type>
112 bool same(const Vector3& other, T epsilon = epsilon_v<T>) const {
113 return std::abs(x - other.x) < epsilon &&
114 std::abs(y - other.y) < epsilon &&
115 std::abs(z - other.z) < epsilon;
116 }
117};
118
123
constexpr T epsilon_v
Definition Constants.hpp:32
Vector3< double > Vector3f
Definition Vector3.hpp:121
Vector3< std::uint32_t > Vector3u
Definition Vector3.hpp:119
Vector3< std::int32_t > Vector3i
Definition Vector3.hpp:120
Definition Vector3.hpp:20
Vector3 normalized() const
Definition Vector3.hpp:94
std::uint32_t x
Definition Vector3.hpp:21
Vector3 & operator*=(T scalar)
Definition Vector3.hpp:51
void normalize()
Definition Vector3.hpp:102
double angle(const Vector3 &other) const
Definition Vector3.hpp:85
Vector3 operator*(T scalar) const
Definition Vector3.hpp:38
std::conditional< std::is_floating_point< T >::value, T, double >::type magnitude() const
Definition Vector3.hpp:78
T y
Definition Vector3.hpp:21
Vector3 & operator+=(const Vector3 &other)
Definition Vector3.hpp:45
T square_magnitude() const
Definition Vector3.hpp:74
Vector3 cross(const Vector3 &other) const
Definition Vector3.hpp:65
std::conditional< std::is_floating_point< T >::value, T, double >::type distance(const Vector3 &other) const
Definition Vector3.hpp:82
bool same(const Vector3 &other, T epsilon=epsilon_v< T >) const
Definition Vector3.hpp:112
Vector3 & operator/=(T scalar)
Definition Vector3.hpp:54
Vector3 operator-() const
Definition Vector3.hpp:58
T dot(const Vector3 &other) const
Definition Vector3.hpp:62
Vector3 operator/(T scalar) const
Definition Vector3.hpp:41
T z
Definition Vector3.hpp:21
Vector3 & operator-=(const Vector3 &other)
Definition Vector3.hpp:48
Vector3 operator+(const Vector3 &other) const
Definition Vector3.hpp:32
Vector3 operator-(const Vector3 &other) const
Definition Vector3.hpp:35