Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
Math3D.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
12#include <array>
13#include <cmath>
14#include <stdexcept>
15#include <type_traits>
16
17namespace correlation::render3d {
18
19// -----------------------------------------------------------------------------
20// Vector4
21// -----------------------------------------------------------------------------
22
28template <typename T = float>
29 requires std::is_floating_point_v<T>
30class Vector4 {
31public:
32 using value_type = T;
33
35 constexpr Vector4() noexcept : data_{0, 0, 0, 0} {}
36
44 constexpr Vector4(T coord_x, T coord_y, T coord_z, T coord_w) noexcept
45 : data_{coord_x, coord_y, coord_z, coord_w} {}
46
52 constexpr explicit Vector4(const math::Vector3<T> &vec3, T coord_w = static_cast<T>(1)) noexcept
53 : data_{vec3.x(), vec3.y(), vec3.z(), coord_w} {}
54
59 constexpr explicit Vector4(const std::array<T, 4> &arr) noexcept : data_{arr} {}
60
61 [[nodiscard]] constexpr T x() const noexcept { return data_.at(0); }
62 [[nodiscard]] constexpr T y() const noexcept { return data_.at(1); }
63 [[nodiscard]] constexpr T z() const noexcept { return data_.at(2); }
64 [[nodiscard]] constexpr T w() const noexcept { return data_.at(3); }
65
66 [[nodiscard]] constexpr T &operator[](std::size_t idx) noexcept { return data_.at(idx); }
67 [[nodiscard]] constexpr const T &operator[](std::size_t idx) const noexcept {
68 return data_.at(idx);
69 }
70
71 [[nodiscard]] constexpr const std::array<T, 4> &data() const noexcept { return data_; }
72
73 [[nodiscard]] constexpr math::Vector3<T> toVector3() const noexcept {
74 if (std::abs(data_.at(3)) > static_cast<T>(1e-7)) {
75 return math::Vector3<T>(data_.at(0) / data_.at(3), data_.at(1) / data_.at(3),
76 data_.at(2) / data_.at(3));
77 }
78 return math::Vector3<T>(data_.at(0), data_.at(1), data_.at(2));
79 }
80
81 [[nodiscard]] constexpr Vector4 operator+(const Vector4 &rhs) const noexcept {
82 return Vector4(data_.at(0) + rhs.data_.at(0), data_.at(1) + rhs.data_.at(1),
83 data_.at(2) + rhs.data_.at(2), data_.at(3) + rhs.data_.at(3));
84 }
85
86 [[nodiscard]] constexpr Vector4 operator-(const Vector4 &rhs) const noexcept {
87 return Vector4(data_.at(0) - rhs.data_.at(0), data_.at(1) - rhs.data_.at(1),
88 data_.at(2) - rhs.data_.at(2), data_.at(3) - rhs.data_.at(3));
89 }
90
91 [[nodiscard]] constexpr Vector4 operator*(T scalar) const noexcept {
92 return Vector4(data_.at(0) * scalar, data_.at(1) * scalar, data_.at(2) * scalar,
93 data_.at(3) * scalar);
94 }
95
96 [[nodiscard]] constexpr Vector4 operator/(T scalar) const {
97 if (std::abs(scalar) < static_cast<T>(1e-9)) {
98 throw std::runtime_error("Division by zero in Vector4.");
99 }
100 const T inv = static_cast<T>(1) / scalar;
101 return *this * inv;
102 }
103
104 [[nodiscard]] constexpr T dot(const Vector4 &rhs) const noexcept {
105 return data_.at(0) * rhs.data_.at(0) + data_.at(1) * rhs.data_.at(1) +
106 data_.at(2) * rhs.data_.at(2) + data_.at(3) * rhs.data_.at(3);
107 }
108
109 [[nodiscard]] T length() const noexcept { return std::sqrt(dot(*this)); }
110
111 [[nodiscard]] Vector4 normalized() const {
112 const T len = length();
113 if (len < static_cast<T>(1e-9)) {
114 return Vector4();
115 }
116 return *this * (static_cast<T>(1) / len);
117 }
118
119 [[nodiscard]] constexpr bool operator==(const Vector4 &rhs) const noexcept {
120 return data_ == rhs.data_;
121 }
122
123private:
124 std::array<T, 4> data_{0, 0, 0, 0};
125};
126
127// -----------------------------------------------------------------------------
128// Matrix4 (Column-Major)
129// -----------------------------------------------------------------------------
130
136template <typename T = float>
137 requires std::is_floating_point_v<T>
138class Matrix4 {
139public:
140 using value_type = T;
141
143 constexpr Matrix4() noexcept = default;
144
149 constexpr explicit Matrix4(const std::array<T, 16> &raw) noexcept : data_{raw} {}
150
155 [[nodiscard]] static constexpr Matrix4 identity() noexcept {
156 Matrix4 result_mat;
157 result_mat.data_.at(0) = static_cast<T>(1);
158 result_mat.data_.at(5) = static_cast<T>(1);
159 result_mat.data_.at(10) = static_cast<T>(1);
160 result_mat.data_.at(15) = static_cast<T>(1);
161 return result_mat;
162 }
163
170 [[nodiscard]] constexpr T &operator()(std::size_t row_idx, std::size_t col_idx) noexcept {
171 return data_.at(col_idx * 4 + row_idx);
172 }
173
174 [[nodiscard]] constexpr const T &operator()(std::size_t row_idx,
175 std::size_t col_idx) const noexcept {
176 return data_.at(col_idx * 4 + row_idx);
177 }
178
179 [[nodiscard]] constexpr const std::array<T, 16> &data() const noexcept { return data_; }
180 [[nodiscard]] constexpr std::array<T, 16> &data() noexcept { return data_; }
181
187 [[nodiscard]] constexpr Vector4<T> operator*(const Vector4<T> &vec) const noexcept {
188 return Vector4<T>((*this)(0, 0) * vec.x() + (*this)(0, 1) * vec.y() + (*this)(0, 2) * vec.z() +
189 (*this)(0, 3) * vec.w(),
190 (*this)(1, 0) * vec.x() + (*this)(1, 1) * vec.y() + (*this)(1, 2) * vec.z() +
191 (*this)(1, 3) * vec.w(),
192 (*this)(2, 0) * vec.x() + (*this)(2, 1) * vec.y() + (*this)(2, 2) * vec.z() +
193 (*this)(2, 3) * vec.w(),
194 (*this)(3, 0) * vec.x() + (*this)(3, 1) * vec.y() + (*this)(3, 2) * vec.z() +
195 (*this)(3, 3) * vec.w());
196 }
197
203 [[nodiscard]] constexpr Matrix4 operator*(const Matrix4 &rhs) const noexcept {
204 Matrix4 result_mat;
205 for (std::size_t col_idx = 0; col_idx < 4; ++col_idx) {
206 for (std::size_t row_idx = 0; row_idx < 4; ++row_idx) {
207 T sum = 0;
208 for (std::size_t elem_idx = 0; elem_idx < 4; ++elem_idx) {
209 sum += (*this)(row_idx, elem_idx) * rhs(elem_idx, col_idx);
210 }
211 result_mat(row_idx, col_idx) = sum;
212 }
213 }
214 return result_mat;
215 }
216
222 [[nodiscard]] constexpr math::Vector3<T>
223 transformPoint(const math::Vector3<T> &point) const noexcept {
224 const Vector4<T> vec(point, static_cast<T>(1));
225 const Vector4<T> transformed = *this * vec;
226 return transformed.toVector3();
227 }
228
234 [[nodiscard]] constexpr math::Vector3<T>
235 transformDirection(const math::Vector3<T> &direction) const noexcept {
236 const Vector4<T> vec(direction, static_cast<T>(0));
237 const Vector4<T> transformed = *this * vec;
238 return math::Vector3<T>(transformed.x(), transformed.y(), transformed.z());
239 }
240
248 [[nodiscard]] static constexpr Matrix4 translation(T offset_x, T offset_y, T offset_z) noexcept {
249 Matrix4 result_mat = Matrix4::identity();
250 result_mat(0, 3) = offset_x;
251 result_mat(1, 3) = offset_y;
252 result_mat(2, 3) = offset_z;
253 return result_mat;
254 }
255
256 [[nodiscard]] static constexpr Matrix4 translation(const math::Vector3<T> &offset) noexcept {
257 return translation(offset.x(), offset.y(), offset.z());
258 }
259
267 [[nodiscard]] static constexpr Matrix4 scaling(T scale_x, T scale_y, T scale_z) noexcept {
268 Matrix4 result_mat;
269 result_mat(0, 0) = scale_x;
270 result_mat(1, 1) = scale_y;
271 result_mat(2, 2) = scale_z;
272 result_mat(3, 3) = static_cast<T>(1);
273 return result_mat;
274 }
275
276 [[nodiscard]] static constexpr Matrix4 scaling(T uniform_scale) noexcept {
277 return scaling(uniform_scale, uniform_scale, uniform_scale);
278 }
279
284 [[nodiscard]] constexpr Matrix4 transpose() const noexcept {
285 Matrix4 result_mat;
286 for (std::size_t row_idx = 0; row_idx < 4; ++row_idx) {
287 for (std::size_t col_idx = 0; col_idx < 4; ++col_idx) {
288 result_mat(col_idx, row_idx) = (*this)(row_idx, col_idx);
289 }
290 }
291 return result_mat;
292 }
293
301 [[nodiscard]] static Matrix4 lookAt(const math::Vector3<T> &eye, const math::Vector3<T> &target,
302 const math::Vector3<T> &up_direction) {
303 const math::Vector3<T> forward = math::normalize(target - eye);
304 const math::Vector3<T> right = math::normalize(math::cross(forward, up_direction));
305 const math::Vector3<T> actual_up = math::cross(right, forward);
306
307 Matrix4 result_mat = Matrix4::identity();
308 result_mat(0, 0) = right.x();
309 result_mat(0, 1) = right.y();
310 result_mat(0, 2) = right.z();
311 result_mat(0, 3) = -math::dot(right, eye);
312
313 result_mat(1, 0) = actual_up.x();
314 result_mat(1, 1) = actual_up.y();
315 result_mat(1, 2) = actual_up.z();
316 result_mat(1, 3) = -math::dot(actual_up, eye);
317
318 result_mat(2, 0) = -forward.x();
319 result_mat(2, 1) = -forward.y();
320 result_mat(2, 2) = -forward.z();
321 result_mat(2, 3) = math::dot(forward, eye);
322
323 return result_mat;
324 }
325
335 [[nodiscard]] static Matrix4 perspective(T fov_y_rad, T aspect, T z_near, T z_far) {
336 if (std::abs(aspect) < static_cast<T>(1e-6) || z_near <= 0 || z_far <= z_near) {
337 throw std::invalid_argument("Invalid projection parameters in perspective().");
338 }
339 const T tan_half_fov = std::tan(fov_y_rad / static_cast<T>(2));
340 Matrix4 result_mat;
341 result_mat(0, 0) = static_cast<T>(1) / (aspect * tan_half_fov);
342 result_mat(1, 1) = static_cast<T>(1) / tan_half_fov;
343 result_mat(2, 2) = z_far / (z_near - z_far);
344 result_mat(2, 3) = (z_near * z_far) / (z_near - z_far);
345 result_mat(3, 2) = static_cast<T>(-1);
346 return result_mat;
347 }
348
359 [[nodiscard]] static Matrix4 orthographic(T left, T right, T bottom, T top, T z_near, T z_far) {
360 Matrix4 result_mat = Matrix4::identity();
361 result_mat(0, 0) = static_cast<T>(2) / (right - left);
362 result_mat(1, 1) = static_cast<T>(2) / (top - bottom);
363 result_mat(2, 2) = static_cast<T>(1) / (z_near - z_far);
364 result_mat(0, 3) = -(right + left) / (right - left);
365 result_mat(1, 3) = -(top + bottom) / (top - bottom);
366 result_mat(2, 3) = z_near / (z_near - z_far);
367 return result_mat;
368 }
369
370private:
371 std::array<T, 16> data_{};
372};
373
374// -----------------------------------------------------------------------------
375// Quaternion
376// -----------------------------------------------------------------------------
377
383template <typename T = float>
384 requires std::is_floating_point_v<T>
386public:
387 using value_type = T;
388
393 struct Components {
394 T real_w{static_cast<T>(1)};
395 T imag_x{static_cast<T>(0)};
396 T imag_y{static_cast<T>(0)};
397 T imag_z{static_cast<T>(0)};
398 };
399
401 constexpr Quaternion() noexcept = default;
402
407 constexpr explicit Quaternion(const Components &components) noexcept
408 : w_{components.real_w}, x_{components.imag_x}, y_{components.imag_y}, z_{components.imag_z} {
409 }
410
416 constexpr Quaternion(T real_val, const math::Vector3<T> &imag_vec) noexcept
417 : w_{real_val}, x_{imag_vec.x()}, y_{imag_vec.y()}, z_{imag_vec.z()} {}
418
419 [[nodiscard]] constexpr T w() const noexcept { return w_; }
420 [[nodiscard]] constexpr T x() const noexcept { return x_; }
421 [[nodiscard]] constexpr T y() const noexcept { return y_; }
422 [[nodiscard]] constexpr T z() const noexcept { return z_; }
423
430 [[nodiscard]] static Quaternion fromAxisAngle(const math::Vector3<T> &axis, T angle_rad) {
431 const T half_angle = angle_rad / static_cast<T>(2);
432 const T sin_half = std::sin(half_angle);
433 const math::Vector3<T> norm_axis = math::normalize(axis);
434 return Quaternion(std::cos(half_angle), norm_axis * sin_half);
435 }
436
437 [[nodiscard]] T length() const noexcept {
438 return std::sqrt(w_ * w_ + x_ * x_ + y_ * y_ + z_ * z_);
439 }
440
441 [[nodiscard]] Quaternion normalized() const {
442 const T len = length();
443 if (len < static_cast<T>(1e-9)) {
444 return Quaternion();
445 }
446 const T inv_len = static_cast<T>(1) / len;
447 return Quaternion(w_ * inv_len, math::Vector3<T>(x_ * inv_len, y_ * inv_len, z_ * inv_len));
448 }
449
450 [[nodiscard]] constexpr Quaternion conjugate() const noexcept {
451 return Quaternion(w_, math::Vector3<T>(-x_, -y_, -z_));
452 }
453
459 [[nodiscard]] constexpr Quaternion operator*(const Quaternion &rhs) const noexcept {
460 const T new_w = w_ * rhs.w_ - x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_;
461 const math::Vector3<T> new_imag(w_ * rhs.x_ + x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_,
462 w_ * rhs.y_ - x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_,
463 w_ * rhs.z_ + x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_);
464 return Quaternion(new_w, new_imag);
465 }
466
472 [[nodiscard]] math::Vector3<T> rotate(const math::Vector3<T> &vector3) const {
473 const Quaternion pure_quat(static_cast<T>(0), vector3);
474 const Quaternion quat_norm = normalized();
475 const Quaternion quat_res = quat_norm * pure_quat * quat_norm.conjugate();
476 return math::Vector3<T>(quat_res.x(), quat_res.y(), quat_res.z());
477 }
478
483 [[nodiscard]] Matrix4<T> toMatrix4() const {
484 const Quaternion quat = normalized();
485 Matrix4<T> result_mat = Matrix4<T>::identity();
486
487 const T x_sq = quat.x_ * quat.x_;
488 const T y_sq = quat.y_ * quat.y_;
489 const T z_sq = quat.z_ * quat.z_;
490 const T xy_prod = quat.x_ * quat.y_;
491 const T xz_prod = quat.x_ * quat.z_;
492 const T yz_prod = quat.y_ * quat.z_;
493 const T wx_prod = quat.w_ * quat.x_;
494 const T wy_prod = quat.w_ * quat.y_;
495 const T wz_prod = quat.w_ * quat.z_;
496
497 result_mat(0, 0) = static_cast<T>(1) - static_cast<T>(2) * (y_sq + z_sq);
498 result_mat(0, 1) = static_cast<T>(2) * (xy_prod - wz_prod);
499 result_mat(0, 2) = static_cast<T>(2) * (xz_prod + wy_prod);
500
501 result_mat(1, 0) = static_cast<T>(2) * (xy_prod + wz_prod);
502 result_mat(1, 1) = static_cast<T>(1) - static_cast<T>(2) * (x_sq + z_sq);
503 result_mat(1, 2) = static_cast<T>(2) * (yz_prod - wx_prod);
504
505 result_mat(2, 0) = static_cast<T>(2) * (xz_prod - wy_prod);
506 result_mat(2, 1) = static_cast<T>(2) * (yz_prod + wx_prod);
507 result_mat(2, 2) = static_cast<T>(1) - static_cast<T>(2) * (x_sq + y_sq);
508
509 return result_mat;
510 }
511
512private:
513 T w_{static_cast<T>(1)};
514 T x_{static_cast<T>(0)};
515 T y_{static_cast<T>(0)};
516 T z_{static_cast<T>(0)};
517};
518
519} // namespace correlation::render3d
Lightweight 3D vector and matrix classes for geometric calculations.
A lightweight, constexpr, stack-based 3D vector.
Definition LinearAlgebra.hpp:32
constexpr T z() const noexcept
Definition LinearAlgebra.hpp:84
constexpr T x() const noexcept
Definition LinearAlgebra.hpp:76
constexpr T y() const noexcept
Definition LinearAlgebra.hpp:80
4x4 column-major transformation matrix.
Definition Math3D.hpp:138
constexpr math::Vector3< T > transformPoint(const math::Vector3< T > &point) const noexcept
Transforms a 3D point (w = 1) with perspective division.
Definition Math3D.hpp:223
constexpr const std::array< T, 16 > & data() const noexcept
Definition Math3D.hpp:179
static Matrix4 perspective(T fov_y_rad, T aspect, T z_near, T z_far)
Generates perspective projection matrix (RH, depth in [0, 1] suitable for Vulkan/Software).
Definition Math3D.hpp:335
static constexpr Matrix4 scaling(T uniform_scale) noexcept
Definition Math3D.hpp:276
constexpr Matrix4 transpose() const noexcept
Transposes the matrix.
Definition Math3D.hpp:284
T value_type
Definition Math3D.hpp:140
static Matrix4 lookAt(const math::Vector3< T > &eye, const math::Vector3< T > &target, const math::Vector3< T > &up_direction)
Generates a Look-At view matrix (Right-Handed).
Definition Math3D.hpp:301
constexpr std::array< T, 16 > & data() noexcept
Definition Math3D.hpp:180
static constexpr Matrix4 scaling(T scale_x, T scale_y, T scale_z) noexcept
Creates a non-uniform scaling matrix.
Definition Math3D.hpp:267
constexpr Matrix4 operator*(const Matrix4 &rhs) const noexcept
Matrix-matrix multiplication.
Definition Math3D.hpp:203
constexpr math::Vector3< T > transformDirection(const math::Vector3< T > &direction) const noexcept
Transforms a 3D direction vector (w = 0).
Definition Math3D.hpp:235
static constexpr Matrix4 identity() noexcept
Creates a 4x4 identity matrix.
Definition Math3D.hpp:155
constexpr const T & operator()(std::size_t row_idx, std::size_t col_idx) const noexcept
Definition Math3D.hpp:174
static Matrix4 orthographic(T left, T right, T bottom, T top, T z_near, T z_far)
Generates orthographic projection matrix (RH, depth in [0, 1]).
Definition Math3D.hpp:359
constexpr Vector4< T > operator*(const Vector4< T > &vec) const noexcept
Matrix-vector multiplication.
Definition Math3D.hpp:187
static constexpr Matrix4 translation(T offset_x, T offset_y, T offset_z) noexcept
Creates a translation matrix.
Definition Math3D.hpp:248
constexpr T & operator()(std::size_t row_idx, std::size_t col_idx) noexcept
Element accessor by (row, column).
Definition Math3D.hpp:170
static constexpr Matrix4 translation(const math::Vector3< T > &offset) noexcept
Definition Math3D.hpp:256
std::array< T, 16 > data_
Definition Math3D.hpp:371
constexpr Matrix4() noexcept=default
Default constructor. Initializes to zero matrix.
T w_
Definition Math3D.hpp:513
constexpr T w() const noexcept
Definition Math3D.hpp:419
static Quaternion fromAxisAngle(const math::Vector3< T > &axis, T angle_rad)
Creates quaternion from rotation axis and angle.
Definition Math3D.hpp:430
constexpr T x() const noexcept
Definition Math3D.hpp:420
T z_
Definition Math3D.hpp:516
T y_
Definition Math3D.hpp:515
Quaternion normalized() const
Definition Math3D.hpp:441
constexpr T z() const noexcept
Definition Math3D.hpp:422
constexpr Quaternion operator*(const Quaternion &rhs) const noexcept
Hamilton quaternion multiplication (q1 * q2).
Definition Math3D.hpp:459
Matrix4< T > toMatrix4() const
Converts unit quaternion to a 4x4 rotation matrix.
Definition Math3D.hpp:483
constexpr Quaternion(T real_val, const math::Vector3< T > &imag_vec) noexcept
Constructs quaternion from scalar real part and 3D imaginary vector.
Definition Math3D.hpp:416
math::Vector3< T > rotate(const math::Vector3< T > &vector3) const
Rotates a 3D vector by this quaternion.
Definition Math3D.hpp:472
T x_
Definition Math3D.hpp:514
T value_type
Definition Math3D.hpp:387
constexpr Quaternion conjugate() const noexcept
Definition Math3D.hpp:450
constexpr Quaternion() noexcept=default
Default constructor. Initializes to identity quaternion (w = 1).
T length() const noexcept
Definition Math3D.hpp:437
constexpr T y() const noexcept
Definition Math3D.hpp:421
Stack-allocated 4D homogeneous vector.
Definition Math3D.hpp:30
constexpr Vector4(T coord_x, T coord_y, T coord_z, T coord_w) noexcept
Component-wise constructor.
Definition Math3D.hpp:44
constexpr T x() const noexcept
Definition Math3D.hpp:61
constexpr Vector4 operator-(const Vector4 &rhs) const noexcept
Definition Math3D.hpp:86
constexpr Vector4 operator*(T scalar) const noexcept
Definition Math3D.hpp:91
constexpr Vector4 operator/(T scalar) const
Definition Math3D.hpp:96
constexpr T y() const noexcept
Definition Math3D.hpp:62
constexpr T & operator[](std::size_t idx) noexcept
Definition Math3D.hpp:66
constexpr Vector4(const math::Vector3< T > &vec3, T coord_w=static_cast< T >(1)) noexcept
Constructs Vector4 from 3D vector and w component.
Definition Math3D.hpp:52
constexpr T w() const noexcept
Definition Math3D.hpp:64
constexpr math::Vector3< T > toVector3() const noexcept
Definition Math3D.hpp:73
Vector4 normalized() const
Definition Math3D.hpp:111
constexpr Vector4(const std::array< T, 4 > &arr) noexcept
Constructs Vector4 from std::array.
Definition Math3D.hpp:59
constexpr T dot(const Vector4 &rhs) const noexcept
Definition Math3D.hpp:104
constexpr bool operator==(const Vector4 &rhs) const noexcept
Definition Math3D.hpp:119
constexpr T z() const noexcept
Definition Math3D.hpp:63
constexpr const T & operator[](std::size_t idx) const noexcept
Definition Math3D.hpp:67
constexpr Vector4() noexcept
Default constructor. Initializes all components to zero.
Definition Math3D.hpp:35
constexpr Vector4 operator+(const Vector4 &rhs) const noexcept
Definition Math3D.hpp:81
constexpr const std::array< T, 4 > & data() const noexcept
Definition Math3D.hpp:71
std::array< T, 4 > data_
Definition Math3D.hpp:124
T length() const noexcept
Definition Math3D.hpp:109
T value_type
Definition Math3D.hpp:32
constexpr T dot(const Vector3< T > &vec_a, const Vector3< T > &vec_b) noexcept
Computes the dot product of two vectors.
Definition LinearAlgebra.hpp:466
constexpr Vector3< T > normalize(const Vector3< T > &vec_a)
Normalizes the given vector.
Definition LinearAlgebra.hpp:544
constexpr Vector3< T > cross(const Vector3< T > &vec_a, const Vector3< T > &vec_b) noexcept
Computes the cross product of two 3D vectors.
Definition LinearAlgebra.hpp:478
Definition Camera3D.hpp:14
Explicit bundle of quaternion scalar and imaginary components.
Definition Math3D.hpp:393