28template <
typename T =
float>
29 requires std::is_floating_point_v<T>
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} {}
53 :
data_{vec3.
x(), vec3.
y(), vec3.
z(), coord_w} {}
59 constexpr explicit Vector4(
const std::array<T, 4> &arr) noexcept :
data_{arr} {}
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); }
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 {
71 [[nodiscard]]
constexpr const std::array<T, 4> &
data() const noexcept {
return data_; }
74 if (std::abs(
data_.at(3)) >
static_cast<T
>(1e-7)) {
83 data_.at(2) + rhs.data_.at(2),
data_.at(3) + rhs.data_.at(3));
88 data_.at(2) - rhs.data_.at(2),
data_.at(3) - rhs.data_.at(3));
93 data_.at(3) * scalar);
97 if (std::abs(scalar) <
static_cast<T
>(1e-9)) {
98 throw std::runtime_error(
"Division by zero in Vector4.");
100 const T inv =
static_cast<T
>(1) / scalar;
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);
109 [[nodiscard]] T
length() const noexcept {
return std::sqrt(
dot(*
this)); }
113 if (len <
static_cast<T
>(1e-9)) {
116 return *
this * (
static_cast<T
>(1) / len);
120 return data_ == rhs.data_;
136template <
typename T =
float>
137 requires std::is_floating_point_v<T>
149 constexpr explicit
Matrix4(const std::array<T, 16> &raw) noexcept :
data_{raw} {}
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);
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);
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);
179 [[nodiscard]]
constexpr const std::array<T, 16> &
data() const noexcept {
return data_; }
180 [[nodiscard]]
constexpr std::array<T, 16> &
data() noexcept {
return data_; }
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());
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) {
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);
211 result_mat(row_idx, col_idx) = sum;
224 const Vector4<T> vec(point,
static_cast<T
>(1));
236 const Vector4<T> vec(direction,
static_cast<T
>(0));
250 result_mat(0, 3) = offset_x;
251 result_mat(1, 3) = offset_y;
252 result_mat(2, 3) = offset_z;
257 return translation(offset.x(), offset.y(), offset.z());
267 [[nodiscard]]
static constexpr Matrix4 scaling(T scale_x, T scale_y, T scale_z)
noexcept {
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);
277 return scaling(uniform_scale, uniform_scale, uniform_scale);
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);
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);
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);
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);
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().");
339 const T tan_half_fov = std::tan(fov_y_rad /
static_cast<T
>(2));
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);
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);
383template <
typename T =
float>
384 requires std::is_floating_point_v<T>
408 :
w_{components.real_w},
x_{components.imag_x},
y_{components.imag_y},
z_{components.imag_z} {
417 :
w_{real_val},
x_{imag_vec.x()},
y_{imag_vec.y()},
z_{imag_vec.z()} {}
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_; }
431 const T half_angle = angle_rad /
static_cast<T
>(2);
432 const T sin_half = std::sin(half_angle);
434 return Quaternion(std::cos(half_angle), norm_axis * sin_half);
437 [[nodiscard]] T
length() const noexcept {
443 if (len <
static_cast<T
>(1e-9)) {
446 const T inv_len =
static_cast<T
>(1) / len;
460 const T new_w =
w_ * rhs.w_ -
x_ * rhs.x_ -
y_ * rhs.y_ -
z_ * rhs.z_;
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_);
473 const Quaternion pure_quat(
static_cast<T
>(0), vector3);
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_;
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);
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);
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);
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)};
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
T imag_y
Definition Math3D.hpp:396
T imag_z
Definition Math3D.hpp:397
T real_w
Definition Math3D.hpp:394
T imag_x
Definition Math3D.hpp:395