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;
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
114using Vec3f = Vec3<f32>;
115using Vec3d = Vec3<f64>;
116
117//-------------------------------------------------------------------------------------------------------------------
118// Inline implementation
119//-------------------------------------------------------------------------------------------------------------------
120
121template <typename T>
122inline Vec3<T>::Vec3() noexcept
123{
124 v_[0] = 0.0f;
125 v_[1] = 0.0f;
126 v_[2] = 0.0f;
127}
128
129template <typename T>
130inline Vec3<T>::Vec3(T x, T y, T z) noexcept
131{
132 v_[0] = x;
133 v_[1] = y;
134 v_[2] = z;
135}
136
137template <typename T>
138inline T* Vec3<T>::ptr() noexcept
139{
140 return v_;
141}
142
143template <typename T>
144inline const T* Vec3<T>::ptr() const noexcept
145{
146 return v_;
147}
148
149template <typename T>
150inline void Vec3<T>::set(T x, T y, T z) noexcept
151{
152 v_[0] = x;
153 v_[1] = y;
154 v_[2] = z;
155}
156
157template <typename T>
158inline void Vec3<T>::setX(T x) noexcept
159{
160 v_[0] = x;
161}
162
163template <typename T>
164inline void Vec3<T>::setY(T y) noexcept
165{
166 v_[1] = y;
167}
168
169template <typename T>
170inline void Vec3<T>::setZ(T z) noexcept
171{
172 v_[2] = z;
173}
174
175template <typename T>
176inline void Vec3<T>::set(const Vec3& rhs) noexcept
177{
178 v_[0] = rhs.v_[0];
179 v_[1] = rhs.v_[1];
180 v_[2] = rhs.v_[2];
181}
182
183template <typename T>
184[[nodiscard]] inline T Vec3<T>::getX() const noexcept
185{
186 return v_[0];
187}
188
189template <typename T>
190[[nodiscard]] inline T Vec3<T>::getY() const noexcept
191{
192 return v_[1];
193}
194
195template <typename T>
196[[nodiscard]] inline T Vec3<T>::getZ() const noexcept
197{
198 return v_[2];
199}
200
201template <typename T>
202inline bool Vec3<T>::operator==(const Vec3& v) const noexcept
203{
204 return v_[0] == v.v_[0] && v_[1] == v.v_[1] && v_[2] == v.v_[2];
205}
206
207template <typename T>
208inline bool Vec3<T>::operator!=(const Vec3& v) const noexcept
209{
210 return v_[0] != v.v_[0] || v_[1] != v.v_[1] || v_[2] != v.v_[2];
211}
212
213template <typename T>
214inline bool Vec3<T>::operator<(const Vec3& v) const noexcept
215{
216 if (v_[0] < v.v_[0])
217 {
218 return true;
219 }
220 if (v_[0] > v.v_[0])
221 {
222 return false;
223 }
224 if (v_[1] < v.v_[1])
225 {
226 return true;
227 }
228 if (v_[1] > v.v_[1])
229 {
230 return false;
231 }
232 return (v_[2] < v.v_[2]);
233}
234
235template <typename T>
236inline T Vec3<T>::operator*(const Vec3& rhs) const noexcept
237{
238 return v_[0] * rhs.v_[0] + v_[1] * rhs.v_[1] + v_[2] * rhs.v_[2];
239}
240
241template <typename T>
242inline Vec3<T> Vec3<T>::operator^(const Vec3& rhs) const noexcept
243{
244 return Vec3(v_[1] * rhs.v_[2] - v_[2] * rhs.v_[1],
245 v_[2] * rhs.v_[0] - v_[0] * rhs.v_[2],
246 v_[0] * rhs.v_[1] - v_[1] * rhs.v_[0]);
247}
248
249template <typename T>
250inline Vec3<T> Vec3<T>::operator*(T rhs) const noexcept
251{
252 return Vec3(v_[0] * rhs, v_[1] * rhs, v_[2] * rhs);
253}
254
255template <typename T>
256inline Vec3<T>& Vec3<T>::operator*=(T rhs) noexcept
257{
258 v_[0] *= rhs;
259 v_[1] *= rhs;
260 v_[2] *= rhs;
261 return *this;
262}
263
264template <typename T>
265inline Vec3<T> Vec3<T>::operator/(T rhs) const noexcept
266{
267 return Vec3(v_[0] / rhs, v_[1] / rhs, v_[2] / rhs);
268}
269
270template <typename T>
271inline Vec3<T>& Vec3<T>::operator/=(T rhs) noexcept
272{
273 v_[0] /= rhs;
274 v_[1] /= rhs;
275 v_[2] /= rhs;
276 return *this;
277}
278
279template <typename T>
280inline Vec3<T> Vec3<T>::operator+(const Vec3& rhs) const noexcept
281{
282 return Vec3(v_[0] + rhs.v_[0], v_[1] + rhs.v_[1], v_[2] + rhs.v_[2]);
283}
284
285template <typename T>
286inline Vec3<T>& Vec3<T>::operator+=(const Vec3& rhs) noexcept
287{
288 v_[0] += rhs.v_[0];
289 v_[1] += rhs.v_[1];
290 v_[2] += rhs.v_[2];
291 return *this;
292}
293
294template <typename T>
295inline Vec3<T> Vec3<T>::operator-(const Vec3& rhs) const noexcept
296{
297 return Vec3(v_[0] - rhs.v_[0], v_[1] - rhs.v_[1], v_[2] - rhs.v_[2]);
298}
299
300template <typename T>
301inline Vec3<T>& Vec3<T>::operator-=(const Vec3& rhs) noexcept
302{
303 v_[0] -= rhs.v_[0];
304 v_[1] -= rhs.v_[1];
305 v_[2] -= rhs.v_[2];
306 return *this;
307}
308
309template <typename T>
310inline Vec3<T> Vec3<T>::operator-() const noexcept
311{
312 return Vec3(-v_[0], -v_[1], -v_[2]);
313}
314
315template <typename T>
316[[nodiscard]] inline T Vec3<T>::length() const noexcept
317{
318 return std::sqrt(v_[0] * v_[0] + v_[1] * v_[1] + v_[2] * v_[2]);
319}
320
321template <typename T>
322[[nodiscard]] inline T Vec3<T>::length2() const noexcept
323{
324 return v_[0] * v_[0] + v_[1] * v_[1] + v_[2] * v_[2];
325}
326
327template <typename T>
328inline T Vec3<T>::normalize() noexcept
329{
330 T norm = Vec3::length();
331 if (norm > 0.0)
332 {
333 T inv = 1.0f / norm;
334 v_[0] *= inv;
335 v_[1] *= inv;
336 v_[2] *= inv;
337 }
338 return (norm);
339}
340
341} // namespace sen::util
342
343#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:280
bool operator<(const Vec3 &v) const noexcept
Definition vec3.h:214
bool operator==(const Vec3 &v) const noexcept
Definition vec3.h:202
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:271
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:286
f32 length() const noexcept
Vec3 & operator-=(const Vec3 &rhs) noexcept
Binary vector subtraction.
Definition vec3.h:301
bool operator!=(const Vec3 &v) const noexcept
Definition vec3.h:208
f32 getZ() const noexcept
Vec3() noexcept
Definition vec3.h:122
Vec3 & operator*=(T rhs) noexcept
Unary scalar multiplication.
Definition vec3.h:256
T operator*(const Vec3 &rhs) const noexcept
Point multiplication.
Definition vec3.h:236
Vec3 operator/(T rhs) const noexcept
Division by scalar.
Definition vec3.h:265
Vec3 operator-() const noexcept
Negates the internal elements of the vector.
Definition vec3.h:310
void setZ(f32 z) noexcept
Vec3 operator^(const Vec3 &rhs) const noexcept
Cross product.
Definition vec3.h:242
float32_t f32
Definition numbers.h:28
float64_t f64
Definition numbers.h:29
Definition iterator_adapters.h:16
Vec3< f64 > Vec3d
Definition vec3.h:115
Vec3< f32 > Vec3f
Definition vec3.h:114