Sen API
Sen Libraries
Loading...
Searching...
No Matches
vec3.h
Go to the documentation of this file.
1// === vec3.h ==========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_LIBS_UTIL_SRC_DR_VEC3_H
9#define SEN_LIBS_UTIL_SRC_DR_VEC3_H
10
11// sen
14
15// std
16#include <cmath>
17#include <initializer_list>
18
19namespace sen::util
20{
22template <typename T>
23class Vec3
24{
25public:
26 SEN_COPY_MOVE(Vec3)
27
28public:
29 Vec3() noexcept = default;
30
31 Vec3(T x, T y, T z) noexcept;
32
33 ~Vec3() = default;
34
35public: // data
36 T* ptr() noexcept;
37
38 [[nodiscard]] const T* ptr() const noexcept;
39
40public: // setters
41 void set(T x, T y, T z) noexcept;
42
43 void setX(T x) noexcept;
44
45 void setY(T y) noexcept;
46
47 void setZ(T z) noexcept;
48
49 void set(const Vec3& rhs) noexcept;
50
51public: // getters
52 [[nodiscard]] T getX() const noexcept;
53
54 [[nodiscard]] T getY() const noexcept;
55
56 [[nodiscard]] T getZ() const noexcept;
57
58public: // comparison
59 bool operator==(const Vec3& v) const noexcept;
60
61 bool operator!=(const Vec3& v) const noexcept;
62
63 bool operator<(const Vec3& v) const noexcept;
64
65public: // operators
67 T operator*(const Vec3& rhs) const noexcept;
68
70 Vec3 operator^(const Vec3& rhs) const noexcept;
71
73 Vec3 operator*(T rhs) const noexcept;
74
76 Vec3& operator*=(T rhs) noexcept;
77
79 Vec3 operator/(T rhs) const noexcept;
80
82 Vec3& operator/=(T rhs) noexcept;
83
85 Vec3 operator+(const Vec3& rhs) const noexcept;
86
88 Vec3& operator+=(const Vec3& rhs) noexcept;
89
91 Vec3 operator-(const Vec3& rhs) const noexcept;
92
94 Vec3& operator-=(const Vec3& rhs) noexcept;
95
97 Vec3 operator-() const noexcept;
98
99public:
100 // Get the module of the vector
101 [[nodiscard]] T length() const noexcept;
102
103 // Get the module squared of the vector
104 [[nodiscard]] T length2() const noexcept;
105
108 T normalize() noexcept;
109
110private:
111 T v_[3] {};
112};
113
116
117//-------------------------------------------------------------------------------------------------------------------
118// Inline implementation
119//-------------------------------------------------------------------------------------------------------------------
120
121template <typename T>
122inline Vec3<T>::Vec3(T x, T y, T z) noexcept
123{
124 v_[0] = x;
125 v_[1] = y;
126 v_[2] = z;
127}
128
129template <typename T>
130inline T* Vec3<T>::ptr() noexcept
131{
132 return v_;
133}
134
135template <typename T>
136inline const T* Vec3<T>::ptr() const noexcept
137{
138 return v_;
139}
140
141template <typename T>
142inline void Vec3<T>::set(T x, T y, T z) noexcept
143{
144 v_[0] = x;
145 v_[1] = y;
146 v_[2] = z;
147}
148
149template <typename T>
150inline void Vec3<T>::setX(T x) noexcept
151{
152 v_[0] = x;
153}
154
155template <typename T>
156inline void Vec3<T>::setY(T y) noexcept
157{
158 v_[1] = y;
159}
160
161template <typename T>
162inline void Vec3<T>::setZ(T z) noexcept
163{
164 v_[2] = z;
165}
166
167template <typename T>
168inline void Vec3<T>::set(const Vec3& rhs) noexcept
169{
170 v_[0] = rhs.v_[0];
171 v_[1] = rhs.v_[1];
172 v_[2] = rhs.v_[2];
173}
174
175template <typename T>
176[[nodiscard]] inline T Vec3<T>::getX() const noexcept
177{
178 return v_[0];
179}
180
181template <typename T>
182[[nodiscard]] inline T Vec3<T>::getY() const noexcept
183{
184 return v_[1];
185}
186
187template <typename T>
188[[nodiscard]] inline T Vec3<T>::getZ() const noexcept
189{
190 return v_[2];
191}
192
193template <typename T>
194inline bool Vec3<T>::operator==(const Vec3& v) const noexcept
195{
196 return v_[0] == v.v_[0] && v_[1] == v.v_[1] && v_[2] == v.v_[2];
197}
198
199template <typename T>
200inline bool Vec3<T>::operator!=(const Vec3& v) const noexcept
201{
202 return v_[0] != v.v_[0] || v_[1] != v.v_[1] || v_[2] != v.v_[2];
203}
204
205template <typename T>
206inline bool Vec3<T>::operator<(const Vec3& v) const noexcept
207{
208 if (v_[0] < v.v_[0])
209 {
210 return true;
211 }
212 if (v_[0] > v.v_[0])
213 {
214 return false;
215 }
216 if (v_[1] < v.v_[1])
217 {
218 return true;
219 }
220 if (v_[1] > v.v_[1])
221 {
222 return false;
223 }
224 return (v_[2] < v.v_[2]);
225}
226
227template <typename T>
228inline T Vec3<T>::operator*(const Vec3& rhs) const noexcept
229{
230 return v_[0] * rhs.v_[0] + v_[1] * rhs.v_[1] + v_[2] * rhs.v_[2];
231}
232
233template <typename T>
234inline Vec3<T> Vec3<T>::operator^(const Vec3& rhs) const noexcept
235{
236 return Vec3(v_[1] * rhs.v_[2] - v_[2] * rhs.v_[1],
237 v_[2] * rhs.v_[0] - v_[0] * rhs.v_[2],
238 v_[0] * rhs.v_[1] - v_[1] * rhs.v_[0]);
239}
240
241template <typename T>
242inline Vec3<T> Vec3<T>::operator*(T rhs) const noexcept
243{
244 return Vec3(v_[0] * rhs, v_[1] * rhs, v_[2] * rhs);
245}
246
247template <typename T>
248inline Vec3<T>& Vec3<T>::operator*=(T rhs) noexcept
249{
250 v_[0] *= rhs;
251 v_[1] *= rhs;
252 v_[2] *= rhs;
253 return *this;
254}
255
256template <typename T>
257inline Vec3<T> Vec3<T>::operator/(T rhs) const noexcept
258{
259 return Vec3(v_[0] / rhs, v_[1] / rhs, v_[2] / rhs);
260}
261
262template <typename T>
263inline Vec3<T>& Vec3<T>::operator/=(T rhs) noexcept
264{
265 v_[0] /= rhs;
266 v_[1] /= rhs;
267 v_[2] /= rhs;
268 return *this;
269}
270
271template <typename T>
272inline Vec3<T> Vec3<T>::operator+(const Vec3& rhs) const noexcept
273{
274 return Vec3(v_[0] + rhs.v_[0], v_[1] + rhs.v_[1], v_[2] + rhs.v_[2]);
275}
276
277template <typename T>
278inline Vec3<T>& Vec3<T>::operator+=(const Vec3& rhs) noexcept
279{
280 v_[0] += rhs.v_[0];
281 v_[1] += rhs.v_[1];
282 v_[2] += rhs.v_[2];
283 return *this;
284}
285
286template <typename T>
287inline Vec3<T> Vec3<T>::operator-(const Vec3& rhs) const noexcept
288{
289 return Vec3(v_[0] - rhs.v_[0], v_[1] - rhs.v_[1], v_[2] - rhs.v_[2]);
290}
291
292template <typename T>
293inline Vec3<T>& Vec3<T>::operator-=(const Vec3& rhs) noexcept
294{
295 v_[0] -= rhs.v_[0];
296 v_[1] -= rhs.v_[1];
297 v_[2] -= rhs.v_[2];
298 return *this;
299}
300
301template <typename T>
302inline Vec3<T> Vec3<T>::operator-() const noexcept
303{
304 return Vec3(-v_[0], -v_[1], -v_[2]);
305}
306
307template <typename T>
308[[nodiscard]] inline T Vec3<T>::length() const noexcept
309{
310 return std::sqrt(v_[0] * v_[0] + v_[1] * v_[1] + v_[2] * v_[2]);
311}
312
313template <typename T>
314[[nodiscard]] inline T Vec3<T>::length2() const noexcept
315{
316 return v_[0] * v_[0] + v_[1] * v_[1] + v_[2] * v_[2];
317}
318
319template <typename T>
320inline T Vec3<T>::normalize() noexcept
321{
322 T norm = Vec3::length();
323 if (norm > 0.0)
324 {
325 T inv = 1.0f / norm;
326 v_[0] *= inv;
327 v_[1] *= inv;
328 v_[2] *= inv;
329 }
330 return (norm);
331}
332
333} // namespace sen::util
334
335#endif // SEN_LIBS_UTIL_SRC_DR_VEC3_H
Handles all mathematical ops involving 3D Vectors.
Definition vec3.h:24
f32 getY() const noexcept
f32 normalize() noexcept
Vec3 operator+(const Vec3 &rhs) const noexcept
Binary vector addition.
Definition vec3.h:272
bool operator<(const Vec3 &v) const noexcept
Definition vec3.h:206
bool operator==(const Vec3 &v) const noexcept
Definition vec3.h:194
f32 length2() const noexcept
f32 getX() const noexcept
void set(f32 x, f32 y, f32 z) noexcept
Vec3 & operator/=(T rhs) noexcept
Unary division by scalar.
Definition vec3.h:263
void setX(f32 x) noexcept
void setY(f32 y) noexcept
f32 * ptr() noexcept
Vec3 & operator+=(const Vec3 &rhs) noexcept
Binary vector addition.
Definition vec3.h:278
f32 length() const noexcept
Vec3 & operator-=(const Vec3 &rhs) noexcept
Binary vector subtraction.
Definition vec3.h:293
bool operator!=(const Vec3 &v) const noexcept
Definition vec3.h:200
f32 getZ() const noexcept
Vec3 & operator*=(T rhs) noexcept
Unary scalar multiplication.
Definition vec3.h:248
T operator*(const Vec3 &rhs) const noexcept
Point multiplication.
Definition vec3.h:228
Vec3 operator/(T rhs) const noexcept
Division by scalar.
Definition vec3.h:257
Vec3 operator-() const noexcept
Negates the internal elements of the vector.
Definition vec3.h:302
Vec3() noexcept=default
void setZ(f32 z) noexcept
Vec3 operator^(const Vec3 &rhs) const noexcept
Cross product.
Definition vec3.h:234
Definition iterator_adapters.h:16
Vec3< f64 > Vec3d
Definition vec3.h:115
Vec3< f32 > Vec3f
Definition vec3.h:114