Skip to main content

vec3.ixx File

3D vector type and utilities. More...

Included Headers

#include <cassert> #include <cmath> #include <memory> #include <cstddef> #include <helios.math.utils> #include <helios.math.traits.FloatingPointType> #include <helios.math.concepts> #include <helios.math.types:vec2>

Namespaces Index

namespacehelios
namespacemath

Classes Index

structvec3<T>

Represents a 3-dimensional vector of the generic type <T>. More...

Description

3D vector type and utilities.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <cassert>
8#include <cmath>
9#include <memory>
10#include <cstddef>
11
12export module helios.math.types:vec3;
13
14import :vec2;
15import helios.math.concepts;
16import helios.math.traits.FloatingPointType;
17import helios.math.utils;
18
19export namespace helios::math {
20
21 template<helios::math::concepts::IsNumeric T>
22 struct vec4;
23
33 template<helios::math::concepts::IsNumeric T>
34 struct vec3 {
35
36 private:
40 T v[3];
41
42 public:
43
44
48 constexpr vec3() noexcept : v{static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)} {}
49
50
59 constexpr vec3(const T x, const T y, const T z) noexcept : v{x, y, z} {}
60
61
68 constexpr explicit vec3(const T v) noexcept : v{v, v, v} {}
69
77 constexpr explicit vec3(const helios::math::vec2<T> v) noexcept : v{v[0], v[1], static_cast<T>(0)} {}
78
86 constexpr explicit vec3(const helios::math::vec2<T> v, T f) noexcept : v{v[0], v[1], f} {}
87
96 constexpr const T& operator[](const size_t i) const noexcept {
97 assert(i <= 2 && "vec3 - Index out of bounds.");
98 return this->v[i];
99 }
100
101
110 constexpr T& operator[](const size_t i) noexcept {
111 assert(i <= 2 && "vec3 - Index out of bounds.");
112 return this->v[i];
113 }
114
115
122 return static_cast<FloatingPointType<T>>(std::sqrt(
123 static_cast<double>(this->v[0]) * static_cast<double>(this->v[0]) +
124 static_cast<double>(this->v[1]) * static_cast<double>(this->v[1]) +
125 static_cast<double>(this->v[2]) * static_cast<double>(this->v[2])
126 ));
127 }
128
136 [[nodiscard]] vec3<T> cross(const helios::math::vec3<T>& v2) const noexcept;
137
145 [[nodiscard]] T dot(const helios::math::vec3<T>& v2) const noexcept;
146
157
164
177
184
193 constexpr bool operator==(const vec3<T>& rgt) const {
194 return v[0] == rgt[0] && v[1] == rgt[1] && v[2] == rgt[2];
195 }
196
197
215 constexpr bool same(const vec3<T>& rgt, T epsilon = 0.0001) const {
216 return std::fabs(v[0] - rgt[0]) <= epsilon &&
217 std::fabs(v[1] - rgt[1]) <= epsilon &&
218 std::fabs(v[2] - rgt[2]) <= epsilon;
219 }
220
227 return helios::math::vec3<T>{v[0], -v[1], v[2]};
228 }
229
236 constexpr helios::math::vec3<T> withY(T y) const noexcept {
237 return helios::math::vec3<T>{v[0], y, v[2]};
238 }
239
246 return helios::math::vec3<T>{-v[0], v[1], v[2]};
247 }
248
255 constexpr helios::math::vec3<T> withX(T x) const noexcept {
256 return helios::math::vec3<T>{x, v[1], v[2]};
257 }
258
267 constexpr bool isNormalized() const noexcept {
268 const auto lenSquared =
269 static_cast<FloatingPointType<T>>(v[0]) * static_cast<FloatingPointType<T>>(v[0]) +
270 static_cast<FloatingPointType<T>>(v[1]) * static_cast<FloatingPointType<T>>(v[1]) +
271 static_cast<FloatingPointType<T>>(v[2]) * static_cast<FloatingPointType<T>>(v[2]);
272
273 return std::abs(lenSquared - static_cast<FloatingPointType<T>>(1.0)) <= helios::math::EPSILON_LENGTH;
274 }
275 };
276
277
288 template<helios::math::concepts::IsNumeric T>
289 constexpr vec3<T> operator*(const vec3<T>& v, const T n) noexcept {
290 return vec3<T>{v[0] * n, v[1] * n, v[2] * n};
291 }
292
304 template<helios::math::concepts::IsNumeric T>
305 constexpr vec3<T> operator/(const vec3<T>& v, T s) noexcept {
306 assert(static_cast<T>(0) != s && "s must not be 0");
307 const T inv = static_cast<T>(1) / s;
308 return vec3<T>{ v[0] * inv, v[1] * inv, v[2] * inv };
309 }
310
321 template<helios::math::concepts::IsNumeric T>
322 constexpr vec3<T> operator*(const T n, const vec3<T>& v) noexcept {
323 return vec3<T>{v[0] * n, v[1] * n, v[2] * n};
324 }
325
336 template<helios::math::concepts::IsNumeric T>
337 constexpr vec3<T> operator*(const vec3<T>& v1, const vec3<T>& v2) noexcept {
338 return vec3<T>{v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]};
339 }
340
341
351 template<helios::math::concepts::IsNumeric T>
352 constexpr vec3<T> operator+(const vec3<T>& v1, const vec3<T>& v2) noexcept {
353 return vec3<T>{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]};
354 }
355
356
366 template<helios::math::concepts::IsNumeric T>
367 constexpr vec3<T> cross(const vec3<T>& v1, const vec3<T>& v2) noexcept {
368 return vec3{
369 v1[1]*v2[2] - v1[2]*v2[1],
370 v1[2]*v2[0] - v1[0]*v2[2],
371 v1[0]*v2[1] - v1[1]*v2[0]
372 };
373 }
374
384 template<helios::math::concepts::IsNumeric T>
385 constexpr T dot(const vec3<T>& v1, const vec3<T>& v2) noexcept {
386 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
387 }
388
389
399 template<helios::math::concepts::IsNumeric T>
400 constexpr vec3<T> operator-(const vec3<T>& v1, const vec3<T>& v2) noexcept {
401 return vec3{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]};
402 }
403
404 template<helios::math::concepts::IsNumeric T>
405 inline vec3<T> vec3<T>::cross(const vec3<T>& v2) const noexcept {
406 return vec3{
407 v[1]*v2[2] - v[2]*v2[1],
408 v[2]*v2[0] - v[0]*v2[2],
409 v[0]*v2[1] - v[1]*v2[0]
410 };
411 }
412
413 template<helios::math::concepts::IsNumeric T>
415 return vec4<T>{v[0], v[1], v[2], static_cast<T>(0)};
416 }
417
418 template<helios::math::concepts::IsNumeric T>
420 return vec2<T>{v[0], v[1]};
421 }
422
423 template<helios::math::concepts::IsNumeric T>
424 inline vec4<T> vec3<T>::toVec4(T w) const noexcept {
425 return vec4<T>{v[0], v[1], v[2], w};
426 }
427
428 template<helios::math::concepts::IsNumeric T>
429 inline T vec3<T>::dot(const vec3<T>& v2) const noexcept {
430 return v[0]*v2[0] + v[1]*v2[1] + v[2]*v2[2];
431 }
432
433
434 template<helios::math::concepts::IsNumeric T>
436 if (this->length() == static_cast<FloatingPointType<T>>(0)) {
438 static_cast<FloatingPointType<T>>(0),
439 static_cast<FloatingPointType<T>>(0),
440 static_cast<FloatingPointType<T>>(0)
441 );
442 }
443
445 static_cast<FloatingPointType<T>>(v[0]) / this->length(),
446 static_cast<FloatingPointType<T>>(v[1]) / this->length(),
447 static_cast<FloatingPointType<T>>(v[2]) / this->length()
448 );
449 }
450
455
460
465
472 inline constexpr vec3f X_AXISf{1.0f, 0.0f, 0.0f};
473
480 inline constexpr vec3f Y_AXISf{0.0f, 1.0f, 0.0f};
481
488 inline constexpr vec3f Z_AXISf{0.0f, 0.0f, 1.0f};
489
490
491} // namespace helios::math

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.