Libftpp
A modern C++ library
ivector3.hpp
Go to the documentation of this file.
1 #ifndef IVECTOR3_HPP
2 #define IVECTOR3_HPP
3 
4 #include <cmath>
5 
6 // Debattre sur l'utilite du fichier cpp
7 template <typename TType>
8 struct IVector3
9 {
10  TType x;
11  TType y;
12  TType z;
13 
14  IVector3() : x(0), y(0), z(0) {}
15  IVector3(TType x, TType y, TType z) : x(x), y(y), z(z) {}
16 
17  IVector3 operator+(const IVector3& other) const
18  {
19  return IVector3(x + other.x, y + other.y, z + other.z);
20  }
21 
22  IVector3 operator-(const IVector3& other) const
23  {
24  return IVector3(x - other.x, y - other.y, z - other.z);
25  }
26 
27  IVector3 operator*(const IVector3& other) const
28  {
29  return IVector3(x * other.x, y * other.y, z * other.z);
30  }
31  IVector3 operator/(const IVector3& other) const
32  {
33  if (other.x == 0 || other.y == 0 || other.z == 0)
34  throw std::invalid_argument("Division by zero: denominator has a zero component");
35 
36  return IVector3(x / other.x, y / other.y, z / other.z);
37  }
38 
39  bool operator==(const IVector3& other) const
40  {
41  return (x == other.x) && (y == other.y) && (z == other.z);
42  }
43 
44  bool operator!=(const IVector3& other) const
45  {
46  return !(*this == other);
47  }
48 
49  // On retourne une reference pour permettre le chainage
50  IVector3& operator+=(const IVector3& other)
51  {
52  x += other.x;
53  y += other.y;
54  z += other.z;
55  return *this;
56  }
57  IVector3& operator-=(const IVector3& other)
58  {
59  x -= other.x;
60  y -= other.y;
61  z -= other.z;
62  return *this;
63  }
64 
65  // Calcule la longueur (norme) du vecteur
66  float length() const
67  {
68  // C++11 feature, plus stable sur les overflows et underflows
69  return sqrt(x * x + y * y + z * z);
70  }
71 
72  // Ramène le vecteur à longueur 1, tout en conservant sa direction
74  {
75  float length = this->length();
76  if (length == 0)
77  throw std::invalid_argument("Cannot normalized zero vector");
78  return IVector3<TType>(x / length, y / length, z / length);
79  }
80 
81  // Dot sans argument : renvoie ∣𝑣∣2, utile pour comparer des longueurs sans racine carrée.
82  float dot()
83  {
84  return (x * x + y * y + z * z);
85  }
86 
87  float dot(const IVector3& other)
88  {
89  return (x * other.x + y * other.y + z * other.z);
90  }
91 
92  // Renvoie un vecteur orthogonal aux deux vecteurs
93  // .
94  // Utile pour :
95  // Trouver la normale d’un plan
96  // Calculer des forces
97  // perpendiculaires(comme en physique)
98  // Déterminer l’orientation dans l’espace.
99  IVector3 cross(const IVector3& other) const
100  {
101  return IVector3(
102  y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
103  }
104 };
105 #endif
TType z
Definition: ivector3.hpp:12
IVector3 & operator+=(const IVector3 &other)
Definition: ivector3.hpp:50
IVector3 operator+(const IVector3 &other) const
Definition: ivector3.hpp:17
IVector3 operator-(const IVector3 &other) const
Definition: ivector3.hpp:22
float length() const
Definition: ivector3.hpp:66
float dot()
Definition: ivector3.hpp:82
IVector3< TType > normalize()
Definition: ivector3.hpp:73
IVector3 operator*(const IVector3 &other) const
Definition: ivector3.hpp:27
IVector3(TType x, TType y, TType z)
Definition: ivector3.hpp:15
IVector3 cross(const IVector3 &other) const
Definition: ivector3.hpp:99
IVector3 operator/(const IVector3 &other) const
Definition: ivector3.hpp:31
bool operator==(const IVector3 &other) const
Definition: ivector3.hpp:39
TType y
Definition: ivector3.hpp:11
float dot(const IVector3 &other)
Definition: ivector3.hpp:87
IVector3 & operator-=(const IVector3 &other)
Definition: ivector3.hpp:57
TType x
Definition: ivector3.hpp:10
bool operator!=(const IVector3 &other) const
Definition: ivector3.hpp:44
IVector3()
Definition: ivector3.hpp:14