Files
Library/include/Void/Math/Vec.hpp
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

136 lines
6.2 KiB
C++

#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