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
121 FloatingPointType<T> length() const noexcept {
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
156 [[nodiscard]] vec4<T> toVec4() const noexcept;
157
163 [[nodiscard]] vec2<T> toVec2() const noexcept;
164
176 [[nodiscard]] vec4<T> toVec4(T w) const noexcept;
177
183 [[nodiscard]] vec3<FloatingPointType<T>> normalize() const noexcept;
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, const T epsilon = static_cast<T>(EPSILON_LENGTH)) const noexcept requires std::floating_point<T> {
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 constexpr bool same(const vec3<T>& rgt) const noexcept requires std::integral<T> {
228 return v[0] == rgt[0] &&
229 v[1] == rgt[1] &&
230 v[2] == rgt[2];
231 }
232
233
239 constexpr helios::math::vec3<T> flipY() const noexcept {
240 return helios::math::vec3<T>{v[0], -v[1], v[2]};
241 }
242
249 constexpr helios::math::vec3<T> withY(T y) const noexcept {
250 return helios::math::vec3<T>{v[0], y, v[2]};
251 }
252
258 constexpr helios::math::vec3<T> flipX() const noexcept {
259 return helios::math::vec3<T>{-v[0], v[1], v[2]};
260 }
261
268 constexpr helios::math::vec3<T> withX(T x) const noexcept {
269 return helios::math::vec3<T>{x, v[1], v[2]};
270 }
271
278 constexpr helios::math::vec3<T> withZ(T z) const noexcept {
279 return helios::math::vec3<T>{v[0], v[1], z};
280 }
281
290 constexpr bool isNormalized() const noexcept {
291 const auto lenSquared =
292 static_cast<FloatingPointType<T>>(v[0]) * static_cast<FloatingPointType<T>>(v[0]) +
293 static_cast<FloatingPointType<T>>(v[1]) * static_cast<FloatingPointType<T>>(v[1]) +
294 static_cast<FloatingPointType<T>>(v[2]) * static_cast<FloatingPointType<T>>(v[2]);
295
296 return std::abs(lenSquared - static_cast<FloatingPointType<T>>(1.0)) <= helios::math::EPSILON_LENGTH;
297 }
298 };
299
300
311 template<helios::math::concepts::IsNumeric T>
312 constexpr vec3<T> operator*(const vec3<T>& v, const T n) noexcept {
313 return vec3<T>{v[0] * n, v[1] * n, v[2] * n};
314 }
315
327 template<helios::math::concepts::IsNumeric T>
328 constexpr vec3<T> operator/(const vec3<T>& v, T s) noexcept {
329 assert(static_cast<T>(0) != s && "s must not be 0");
330 const T inv = static_cast<T>(1) / s;
331 return vec3<T>{ v[0] * inv, v[1] * inv, v[2] * inv };
332 }
333
344 template<helios::math::concepts::IsNumeric T>
345 constexpr vec3<T> operator*(const T n, const vec3<T>& v) noexcept {
346 return vec3<T>{v[0] * n, v[1] * n, v[2] * n};
347 }
348
359 template<helios::math::concepts::IsNumeric T>
360 constexpr vec3<T> operator*(const vec3<T>& v1, const vec3<T>& v2) noexcept {
361 return vec3<T>{v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]};
362 }
363
364
374 template<helios::math::concepts::IsNumeric T>
375 constexpr vec3<T> operator+(const vec3<T>& v1, const vec3<T>& v2) noexcept {
376 return vec3<T>{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]};
377 }
378
379
389 template<helios::math::concepts::IsNumeric T>
390 constexpr vec3<T> cross(const vec3<T>& v1, const vec3<T>& v2) noexcept {
391 return vec3{
392 v1[1]*v2[2] - v1[2]*v2[1],
393 v1[2]*v2[0] - v1[0]*v2[2],
394 v1[0]*v2[1] - v1[1]*v2[0]
395 };
396 }
397
407 template<helios::math::concepts::IsNumeric T>
408 constexpr T dot(const vec3<T>& v1, const vec3<T>& v2) noexcept {
409 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
410 }
411
412
422 template<helios::math::concepts::IsNumeric T>
423 constexpr vec3<T> operator-(const vec3<T>& v1, const vec3<T>& v2) noexcept {
424 return vec3{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]};
425 }
426
427 template<helios::math::concepts::IsNumeric T>
428 inline vec3<T> vec3<T>::cross(const vec3<T>& v2) const noexcept {
429 return vec3{
430 v[1]*v2[2] - v[2]*v2[1],
431 v[2]*v2[0] - v[0]*v2[2],
432 v[0]*v2[1] - v[1]*v2[0]
433 };
434 }
435
436 template<helios::math::concepts::IsNumeric T>
437 inline vec4<T> vec3<T>::toVec4() const noexcept {
438 return vec4<T>{v[0], v[1], v[2], static_cast<T>(0)};
439 }
440
441 template<helios::math::concepts::IsNumeric T>
442 inline vec2<T> vec3<T>::toVec2() const noexcept {
443 return vec2<T>{v[0], v[1]};
444 }
445
446 template<helios::math::concepts::IsNumeric T>
447 inline vec4<T> vec3<T>::toVec4(T w) const noexcept {
448 return vec4<T>{v[0], v[1], v[2], w};
449 }
450
451 template<helios::math::concepts::IsNumeric T>
452 inline T vec3<T>::dot(const vec3<T>& v2) const noexcept {
453 return v[0]*v2[0] + v[1]*v2[1] + v[2]*v2[2];
454 }
455
456
457 template<helios::math::concepts::IsNumeric T>
459 if (this->length() == static_cast<FloatingPointType<T>>(0)) {
461 static_cast<FloatingPointType<T>>(0),
462 static_cast<FloatingPointType<T>>(0),
463 static_cast<FloatingPointType<T>>(0)
464 );
465 }
466
468 static_cast<FloatingPointType<T>>(v[0]) / this->length(),
469 static_cast<FloatingPointType<T>>(v[1]) / this->length(),
470 static_cast<FloatingPointType<T>>(v[2]) / this->length()
471 );
472 }
473
478
483
488
495 inline constexpr vec3f X_AXISf{1.0f, 0.0f, 0.0f};
496
503 inline constexpr vec3f Y_AXISf{0.0f, 1.0f, 0.0f};
504
511 inline constexpr vec3f Z_AXISf{0.0f, 0.0f, 1.0f};
512
513
514} // namespace helios::math

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.