system
The vocabulary types everything else shares
Loading...
Searching...
No Matches
Vector3.hpp
Go to the documentation of this file.
1
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
25 bool operator==(const Vector3& other) const { return x == other.x && y == other.y && z == other.z; }
26
28 bool operator!=(const Vector3& other) const { return !(*this == other); }
29
30 // Arithmetic Operators
32 Vector3 operator+(const Vector3& other) const { return Vector3{x + other.x, y + other.y, z + other.z}; }
33
35 Vector3 operator-(const Vector3& other) const { return Vector3{x - other.x, y - other.y, z - other.z}; }
36
38 Vector3 operator*(T scalar) const { return Vector3{x * scalar, y * scalar, z * scalar}; }
39
41 Vector3 operator/(T scalar) const { return Vector3{x / scalar, y / scalar, z / scalar}; }
42
43 // Compound Assignment Operators
45 Vector3& operator+=(const Vector3& other) { x += other.x; y += other.y; z += other.z; return *this; }
46
48 Vector3& operator-=(const Vector3& other) { x -= other.x; y -= other.y; z -= other.z; return *this; }
49
51 Vector3& operator*=(T scalar) { x *= scalar; y *= scalar; z *= scalar; return *this; }
52
54 Vector3& operator/=(T scalar) { x /= scalar; y /= scalar; z /= scalar; return *this; }
55
56 // Unary Operators
58 Vector3 operator-() const { return Vector3{-x, -y, -z}; }
59
60 //methods
62 T dot(const Vector3& other) const { return x * other.x + y * other.y + z * other.z; }
63
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
74 T square_magnitude() const { return x*x + y*y + z*z; }
75
77 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
78 magnitude() const { return std::sqrt(square_magnitude()); }
79
81 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
82 distance(const Vector3& other) const { return (*this - other).magnitude(); }
83
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
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
102 void normalize() {
103 double len = magnitude();
104
105 if (len == 0) return;
106 x /= len;
107 y /= len;
108 z /= len;
109 }
110
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
constexpr T epsilon_v
Definition Constants.hpp:32
Definition Vector3.hpp:20
Vector3 normalized() const
Definition Vector3.hpp:94
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