Bomberman
Vector3.hpp
Go to the documentation of this file.
1 //
2 // Created by Zoe Roux on 5/17/21.
3 //
4 
5 
6 #pragma once
7 
8 #include <iostream>
9 #include <cmath>
10 #include "Vector/Vector3.hpp"
11 
12 namespace BBM
13 {
15  template<typename T>
16  class Vector3
17  {
18  public:
20  T x;
22  T y;
24  T z;
25 
28  : x(0), y(0), z(0)
29  {}
30 
32  Vector3(T _x, T _y, T _z)
33  : x(_x), y(_y), z(_z)
34  {}
35 
37  ~Vector3() = default;
38 
39  bool operator==(const Vector3<T> &other) const
40  {
41  return this->x == other.x && this->y == other.y && this->z == other.z;
42  }
43 
44  bool operator!=(const Vector3<T> &other) const
45  {
46  return !this->operator==(other);
47  }
48 
49  template<typename T2>
51  {
52  this->x += vec.x;
53  this->y += vec.y;
54  this->z += vec.z;
55  return *this;
56  }
57 
58  template<typename T2>
59  Vector3<T> operator+(const Vector3<T2> &vec) const
60  {
61  return Vector3<T>(this->x + vec.x, this->y + vec.y, this->z + vec.z);
62  }
63 
64  template<typename T2>
66  {
67  this->x -= vec.x;
68  this->y -= vec.y;
69  this->z -= vec.z;
70  return *this;
71  }
72 
73  template<typename T2>
74  Vector3<T> operator-(const Vector3<T2> &vec) const
75  {
76  return Vector3<T>(this->x - vec.x, this->y - vec.y, this->z - vec.z);
77  }
78 
79  template<typename T2>
80  Vector3<T> &operator*=(const T2 d)
81  {
82  this->x *= d;
83  this->y *= d;
84  this->z *= d;
85  return *this;
86  }
87 
88  template<typename T2>
89  Vector3<T> operator*(const T2 d) const
90  {
91  return Vector3<T>(this->x * d, this->y * d, this->z * d);
92  }
93 
94  template<typename T2>
96  {
97  return Vector3<T>(this->x * b.x, this->y * b.y, this->z * b.z);
98  }
99 
100  template<typename T2>
102  {
103  this->x /= b.x;
104  this->y /= b.y;
105  this->z /= b.z;
106  return this;
107  }
108 
109  template<typename T2>
111  {
112  return Vector3<T>(this->x / b.x, this->y / b.y, this->z / b.z);
113  }
114 
115  template<typename T2>
117  {
118  this->x /= b;
119  this->y /= b;
120  this->z /= b;
121  return this;
122  }
123 
124  template<typename T2>
125  Vector3<T> operator/(T2 b) const
126  {
127  return Vector3<T>(this->x / b, this->y / b, this->z / b);
128  }
129 
130  template<typename T2>
131  double distance(const Vector3<T2> &o) const
132  {
133  return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2) + std::pow(this->z - o.z, 2));
134  }
135 
136  double magnitude() const
137  {
138  return (std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2) + std::pow(this->z, 2)));
139  }
140 
142  {
143  double mag = this->magnitude();
144 
145  if (mag == 0) {
146  this->x = 0;
147  this->y = 0;
148  this->z = 0;
149  return *this;
150  }
151  this->x /= mag;
152  this->y /= mag;
153  this->z /= mag;
154  return *this;
155  }
156 
158  {
159  T mag = this->magnitude();
160 
161  if (mag == 0)
162  return Vector3<T>();
163  return Vector3<T>(this->x / mag, this->y / mag, this->z / mag);
164  }
165 
166  Vector3<T> projection(const Vector3<T> &point) const
167  {
168  return (point * this) / std::pow(this->magnitude(), 2) * this;
169  }
170 
171  Vector3<T> abs() const
172  {
173  return Vector3<T>(std::abs(this->x), std::abs(this->y), std::abs(this->z));
174  }
175 
176  Vector3<T> trunc() const requires(std::is_floating_point_v<T>)
177  {
178  return Vector3<T>(std::trunc(this->x), std::trunc(this->y), std::trunc(this->z));
179  }
180 
181  Vector3<T> ceil() const requires(std::is_floating_point_v<T>)
182  {
183  return Vector3<T>(std::ceil(this->x), std::ceil(this->y), std::ceil(this->z));
184  }
185 
186  Vector3<T> floor() const requires(std::is_floating_point_v<T>)
187  {
188  return Vector3<T>(std::floor(this->x), std::floor(this->y), std::floor(this->z));
189  }
190 
191  Vector3<T> round() const requires(std::is_floating_point_v<T>)
192  {
193  return Vector3<T>(std::round(this->x), std::round(this->y), std::round(this->z));
194  }
195 
196  [[nodiscard]] bool isNull() const
197  {
198  return this->x == 0 && this->y == 0 && this->z == 0;
199  }
200 
201  operator RAY::Vector3() const requires(std::is_same_v<T, float>)
202  {
203  return RAY::Vector3(this->x, this->y, this->z);
204  }
205 
207  {
208  Vector3<T> min = { std::min(a.x, b.x),
209  std::min(a.y, b.y),
210  std::min(a.z, b.z)};
211  return min;
212  }
213 
215  {
216  Vector3<T> max = { std::max(a.x, b.x),
217  std::max(a.y, b.y),
218  std::max(a.z, b.z)};
219  return max;
220  }
221  };
222 
226 }
227 
228 
229 template<typename T>
230 std::ostream &operator<<(std::ostream &s, const BBM::Vector3<T> &v)
231 {
232  s << "Vector3<" << typeid(T).name() << ">("<< v.x << ", " << v.y << ", " << v.z << ")";
233  return s;
234 }
Vector3.hpp
BBM::Vector3::y
T y
The y value of the vector.
Definition: Vector3.hpp:22
BBM::Vector3u
Vector3< unsigned > Vector3u
Definition: Vector3.hpp:224
RAY::Vector3
A Three-dimensionnal Vector data type.
Definition: Vector3.hpp:15
BBM::Vector3::trunc
Vector3< T > trunc() const requires(std
Definition: Vector3.hpp:176
BBM::Vector3::operator*
Vector3< T > operator*(const T2 d) const
Definition: Vector3.hpp:89
BBM::Vector3::round
Vector3< T > round() const requires(std
Definition: Vector3.hpp:191
BBM::Vector3::Vector3
Vector3()
Create a new nil vector3.
Definition: Vector3.hpp:27
BBM::Vector3::max
static Vector3< T > max(Vector3< T > a, Vector3< T > b)
Definition: Vector3.hpp:214
BBM::Vector3::operator*
Vector3< T > operator*(const Vector3< T2 > &b) const
Definition: Vector3.hpp:95
BBM::Vector3::isNull
bool isNull() const
Definition: Vector3.hpp:196
BBM::Vector3::operator+=
Vector3< T > & operator+=(const Vector3< T2 > &vec)
Definition: Vector3.hpp:50
BBM::Vector3::abs
Vector3< T > abs() const
Definition: Vector3.hpp:171
BBM
Definition: AnimationsComponent.cpp:9
BBM::Vector3::operator+
Vector3< T > operator+(const Vector3< T2 > &vec) const
Definition: Vector3.hpp:59
BBM::Vector3::operator!=
bool operator!=(const Vector3< T > &other) const
Definition: Vector3.hpp:44
BBM::Vector3::x
T x
The x value of the vector.
Definition: Vector3.hpp:20
BBM::Vector3::floor
Vector3< T > floor() const requires(std
Definition: Vector3.hpp:186
BBM::Vector3::operator/
Vector3< T > operator/(const Vector3< T2 > &b) const
Definition: Vector3.hpp:110
BBM::Vector3
A Vector3 data type. (templated to allow any kind of vector3)
Definition: Vector3.hpp:16
BBM::Vector3::min
static Vector3< T > min(Vector3< T > a, Vector3< T > b)
Definition: Vector3.hpp:206
BBM::Vector3::distance
double distance(const Vector3< T2 > &o) const
Definition: Vector3.hpp:131
BBM::Vector3::operator*=
Vector3< T > & operator*=(const T2 d)
Definition: Vector3.hpp:80
BBM::Vector3f
Vector3< float > Vector3f
Definition: Vector3.hpp:223
BBM::Vector3::operator/
Vector3< T > operator/(T2 b) const
Definition: Vector3.hpp:125
BBM::Vector3i
Vector3< int > Vector3i
Definition: Vector3.hpp:225
BBM::Vector3::~Vector3
~Vector3()=default
A default destructor.
BBM::Vector3::Vector3
Vector3(T _x, T _y, T _z)
Create a new vector3 representing a specific coordinate.
Definition: Vector3.hpp:32
BBM::Vector3::ceil
Vector3< T > ceil() const requires(std
Definition: Vector3.hpp:181
BBM::Vector3::operator-
Vector3< T > operator-(const Vector3< T2 > &vec) const
Definition: Vector3.hpp:74
BBM::Vector3::operator-=
Vector3< T > & operator-=(const Vector3< T2 > &vec)
Definition: Vector3.hpp:65
BBM::Vector3::z
T z
The y value of the vector.
Definition: Vector3.hpp:24
std
Definition: View.hpp:210
BBM::Vector3::normalize
Vector3< T > normalize()
Definition: Vector3.hpp:141
BBM::Vector3::operator==
bool operator==(const Vector3< T > &other) const
Definition: Vector3.hpp:39
operator<<
std::ostream & operator<<(std::ostream &s, const BBM::Vector3< T > &v)
Definition: Vector3.hpp:230
BBM::Vector3::operator/=
Vector3< T > operator/=(T2 b)
Definition: Vector3.hpp:116
BBM::Vector3::normalized
Vector3< T > normalized() const
Definition: Vector3.hpp:157
BBM::Vector3::projection
Vector3< T > projection(const Vector3< T > &point) const
Definition: Vector3.hpp:166
BBM::Vector3::magnitude
double magnitude() const
Definition: Vector3.hpp:136
BBM::Vector3::operator/=
Vector3< T > operator/=(const Vector3< T2 > &b)
Definition: Vector3.hpp:101