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>
This commit is contained in:
1 parent
42268c6c35
commit
a09e000f0f
65 files changed
+4959
-138
No files matched your search
@@ -0,0 +1,137 @@
|
||||
#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
|
||||
@@ -0,0 +1,135 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <concepts>
|
||||
#include <format>
|
||||
#include <string>
|
||||
|
||||
namespace Void::Math
|
||||
{
|
||||
|
||||
// ── Vec2 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
template <std::floating_point T = float>
|
||||
struct Vec2
|
||||
{
|
||||
T x{}, y{};
|
||||
|
||||
constexpr Vec2() noexcept = default;
|
||||
constexpr Vec2(T x, T y) noexcept : x{x}, y{y} {}
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vec2&) const noexcept = default;
|
||||
|
||||
[[nodiscard]] constexpr Vec2 operator+(Vec2 o) const noexcept { return {x + o.x, y + o.y}; }
|
||||
[[nodiscard]] constexpr Vec2 operator-(Vec2 o) const noexcept { return {x - o.x, y - o.y}; }
|
||||
[[nodiscard]] constexpr Vec2 operator*(T s) const noexcept { return {x * s, y * s}; }
|
||||
[[nodiscard]] constexpr Vec2 operator/(T s) const noexcept { return {x / s, y / s}; }
|
||||
[[nodiscard]] constexpr Vec2 operator-() const noexcept { return {-x, -y}; }
|
||||
|
||||
constexpr Vec2& operator+=(Vec2 o) noexcept { x += o.x; y += o.y; return *this; }
|
||||
constexpr Vec2& operator-=(Vec2 o) noexcept { x -= o.x; y -= o.y; return *this; }
|
||||
constexpr Vec2& operator*=(T s) noexcept { x *= s; y *= s; return *this; }
|
||||
constexpr Vec2& operator/=(T s) noexcept { x /= s; y /= s; return *this; }
|
||||
|
||||
[[nodiscard]] constexpr T dot(Vec2 o) const noexcept { return x * o.x + y * o.y; }
|
||||
[[nodiscard]] constexpr T length_sq() const noexcept { return dot(*this); }
|
||||
[[nodiscard]] T length() const noexcept { return std::sqrt(length_sq()); }
|
||||
[[nodiscard]] Vec2 normalized() const noexcept { return *this / length(); }
|
||||
|
||||
[[nodiscard]] std::string to_string() const
|
||||
{
|
||||
return std::format("({}, {})", x, y);
|
||||
}
|
||||
};
|
||||
|
||||
template <std::floating_point T>
|
||||
[[nodiscard]] constexpr Vec2<T> operator*(T s, Vec2<T> v) noexcept { return v * s; }
|
||||
|
||||
// ── Vec3 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
template <std::floating_point T = float>
|
||||
struct Vec3
|
||||
{
|
||||
T x{}, y{}, z{};
|
||||
|
||||
constexpr Vec3() noexcept = default;
|
||||
constexpr Vec3(T x, T y, T z) noexcept : x{x}, y{y}, z{z} {}
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vec3&) const noexcept = default;
|
||||
|
||||
[[nodiscard]] constexpr Vec3 operator+(Vec3 o) const noexcept { return {x + o.x, y + o.y, z + o.z}; }
|
||||
[[nodiscard]] constexpr Vec3 operator-(Vec3 o) const noexcept { return {x - o.x, y - o.y, z - o.z}; }
|
||||
[[nodiscard]] constexpr Vec3 operator*(T s) const noexcept { return {x * s, y * s, z * s}; }
|
||||
[[nodiscard]] constexpr Vec3 operator/(T s) const noexcept { return {x / s, y / s, z / s}; }
|
||||
[[nodiscard]] constexpr Vec3 operator-() const noexcept { return {-x, -y, -z}; }
|
||||
|
||||
constexpr Vec3& operator+=(Vec3 o) noexcept { x += o.x; y += o.y; z += o.z; return *this; }
|
||||
constexpr Vec3& operator-=(Vec3 o) noexcept { x -= o.x; y -= o.y; z -= o.z; return *this; }
|
||||
constexpr Vec3& operator*=(T s) noexcept { x *= s; y *= s; z *= s; return *this; }
|
||||
constexpr Vec3& operator/=(T s) noexcept { x /= s; y /= s; z /= s; return *this; }
|
||||
|
||||
[[nodiscard]] constexpr T dot(Vec3 o) const noexcept { return x * o.x + y * o.y + z * o.z; }
|
||||
[[nodiscard]] constexpr Vec3 cross(Vec3 o) const noexcept
|
||||
{
|
||||
return {y * o.z - z * o.y, z * o.x - x * o.z, x * o.y - y * o.x};
|
||||
}
|
||||
[[nodiscard]] constexpr T length_sq() const noexcept { return dot(*this); }
|
||||
[[nodiscard]] T length() const noexcept { return std::sqrt(length_sq()); }
|
||||
[[nodiscard]] Vec3 normalized() const noexcept { return *this / length(); }
|
||||
|
||||
[[nodiscard]] std::string to_string() const
|
||||
{
|
||||
return std::format("({}, {}, {})", x, y, z);
|
||||
}
|
||||
};
|
||||
|
||||
template <std::floating_point T>
|
||||
[[nodiscard]] constexpr Vec3<T> operator*(T s, Vec3<T> v) noexcept { return v * s; }
|
||||
|
||||
// ── Vec4 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
template <std::floating_point T = float>
|
||||
struct Vec4
|
||||
{
|
||||
T x{}, y{}, z{}, w{};
|
||||
|
||||
constexpr Vec4() noexcept = default;
|
||||
constexpr Vec4(T x, T y, T z, T w) noexcept : x{x}, y{y}, z{z}, w{w} {}
|
||||
explicit constexpr Vec4(Vec3<T> v, T w = T{1}) noexcept : x{v.x}, y{v.y}, z{v.z}, w{w} {}
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vec4&) const noexcept = default;
|
||||
|
||||
[[nodiscard]] constexpr Vec4 operator+(Vec4 o) const noexcept { return {x+o.x, y+o.y, z+o.z, w+o.w}; }
|
||||
[[nodiscard]] constexpr Vec4 operator-(Vec4 o) const noexcept { return {x-o.x, y-o.y, z-o.z, w-o.w}; }
|
||||
[[nodiscard]] constexpr Vec4 operator*(T s) const noexcept { return {x*s, y*s, z*s, w*s}; }
|
||||
[[nodiscard]] constexpr Vec4 operator/(T s) const noexcept { return {x/s, y/s, z/s, w/s}; }
|
||||
|
||||
[[nodiscard]] constexpr T dot(Vec4 o) const noexcept { return x*o.x + y*o.y + z*o.z + w*o.w; }
|
||||
[[nodiscard]] constexpr T length_sq() const noexcept { return dot(*this); }
|
||||
[[nodiscard]] T length() const noexcept { return std::sqrt(length_sq()); }
|
||||
[[nodiscard]] constexpr Vec3<T> xyz() const noexcept { return {x, y, z}; }
|
||||
};
|
||||
|
||||
// ── Free functions ────────────────────────────────────────────────────────────
|
||||
|
||||
template <std::floating_point T>
|
||||
[[nodiscard]] constexpr T distance(Vec2<T> a, Vec2<T> b) noexcept { return (a - b).length(); }
|
||||
|
||||
template <std::floating_point T>
|
||||
[[nodiscard]] constexpr T distance(Vec3<T> a, Vec3<T> b) noexcept { return (a - b).length(); }
|
||||
|
||||
template <std::floating_point T>
|
||||
[[nodiscard]] constexpr Vec2<T> lerp(Vec2<T> a, Vec2<T> b, T t) noexcept { return a + (b - a) * t; }
|
||||
|
||||
template <std::floating_point T>
|
||||
[[nodiscard]] constexpr Vec3<T> lerp(Vec3<T> a, Vec3<T> b, T t) noexcept { return a + (b - a) * t; }
|
||||
|
||||
// Convenience aliases
|
||||
using Vec2f = Vec2<float>;
|
||||
using Vec2d = Vec2<double>;
|
||||
using Vec3f = Vec3<float>;
|
||||
using Vec3d = Vec3<double>;
|
||||
using Vec4f = Vec4<float>;
|
||||
using Vec4d = Vec4<double>;
|
||||
|
||||
} // namespace Void::Math
|
||||
Reference in new issue
Block a user