Files
Library/include/Void/Math/Mat.hpp
T
faumarayandClaude Sonnet 4.6 a09e000f0f Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules:
- Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals
- Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string
- Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces
- Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode
- Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks
- Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation)

Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added
Detail/Color.hpp shim that delegates to Stacktrace::Color when present or
provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler,
ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE.

Fix Repr.hpp reflect_struct to handle template specializations that lack
std::meta::identifier_of (now guarded by has_identifier check).

33/33 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 21:52:22 +03:00

138 lines
3.7 KiB
C++

#pragma once
#include <Void/Math/Vec.hpp>
#include <array>
#include <cmath>
#include <concepts>
namespace Void::Math
{
// ── Mat4 (column-major, row-major storage) ────────────────────────────────────
// Stored row-major: m[row][col].
template <std::floating_point T = float>
struct Mat4
{
std::array<std::array<T, 4>, 4> m{};
constexpr Mat4() noexcept = default;
[[nodiscard]] static constexpr Mat4 identity() noexcept
{
Mat4 r;
r.m[0][0] = r.m[1][1] = r.m[2][2] = r.m[3][3] = T{1};
return r;
}
[[nodiscard]] constexpr T& at(int row, int col) noexcept { return m[row][col]; }
[[nodiscard]] constexpr T at(int row, int col) const noexcept { return m[row][col]; }
[[nodiscard]] constexpr bool operator==(const Mat4&) const noexcept = default;
[[nodiscard]] constexpr Mat4 operator+(const Mat4& o) const noexcept
{
Mat4 r;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
r.m[i][j] = m[i][j] + o.m[i][j];
return r;
}
[[nodiscard]] constexpr Mat4 operator*(T s) const noexcept
{
Mat4 r;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
r.m[i][j] = m[i][j] * s;
return r;
}
[[nodiscard]] constexpr Mat4 operator*(const Mat4& o) const noexcept
{
Mat4 r;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
T s{};
for (int k = 0; k < 4; ++k) s += m[i][k] * o.m[k][j];
r.m[i][j] = s;
}
return r;
}
[[nodiscard]] constexpr Vec4<T> operator*(Vec4<T> v) const noexcept
{
return {
m[0][0]*v.x + m[0][1]*v.y + m[0][2]*v.z + m[0][3]*v.w,
m[1][0]*v.x + m[1][1]*v.y + m[1][2]*v.z + m[1][3]*v.w,
m[2][0]*v.x + m[2][1]*v.y + m[2][2]*v.z + m[2][3]*v.w,
m[3][0]*v.x + m[3][1]*v.y + m[3][2]*v.z + m[3][3]*v.w,
};
}
[[nodiscard]] constexpr Mat4 transposed() const noexcept
{
Mat4 r;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
r.m[i][j] = m[j][i];
return r;
}
/// Translation matrix (affine, last row [0 0 0 1]).
[[nodiscard]] static constexpr Mat4 translation(T tx, T ty, T tz) noexcept
{
auto r = identity();
r.m[0][3] = tx;
r.m[1][3] = ty;
r.m[2][3] = tz;
return r;
}
/// Uniform scale matrix.
[[nodiscard]] static constexpr Mat4 scale(T sx, T sy, T sz) noexcept
{
auto r = identity();
r.m[0][0] = sx;
r.m[1][1] = sy;
r.m[2][2] = sz;
return r;
}
/// Rotation around X axis (angle in radians).
[[nodiscard]] static Mat4 rotation_x(T angle) noexcept
{
auto r = identity();
const T c = std::cos(angle), s = std::sin(angle);
r.m[1][1] = c; r.m[1][2] = -s;
r.m[2][1] = s; r.m[2][2] = c;
return r;
}
/// Rotation around Y axis.
[[nodiscard]] static Mat4 rotation_y(T angle) noexcept
{
auto r = identity();
const T c = std::cos(angle), s = std::sin(angle);
r.m[0][0] = c; r.m[0][2] = s;
r.m[2][0] = -s; r.m[2][2] = c;
return r;
}
/// Rotation around Z axis.
[[nodiscard]] static Mat4 rotation_z(T angle) noexcept
{
auto r = identity();
const T c = std::cos(angle), s = std::sin(angle);
r.m[0][0] = c; r.m[0][1] = -s;
r.m[1][0] = s; r.m[1][1] = c;
return r;
}
};
using Mat4f = Mat4<float>;
using Mat4d = Mat4<double>;
} // namespace Void::Math