#pragma once #include #include #include #include namespace Void::Math { // ── Mat4 (column-major, row-major storage) ──────────────────────────────────── // Stored row-major: m[row][col]. template struct Mat4 { std::array, 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 operator*(Vec4 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; using Mat4d = Mat4; } // namespace Void::Math