Files
Library/tests/standalone/Math/VecMat.cpp
T

156 lines
5.0 KiB
C++

#include <Void/Math/Mat.hpp>
#include <Void/Math/Vec.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cmath>
#include <numbers>
static constexpr float EPS = 1e-5f;
// ── Vec2 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test vec2_add()
{
constexpr auto v = Void::Math::Vec2f{1, 2} + Void::Math::Vec2f{3, 4};
static_assert(v.x == 4.f && v.y == 6.f);
Void::Testing::expect_eq(v.x, 4.f);
}
Void::Testing::Test vec2_subtract()
{
constexpr auto v = Void::Math::Vec2f{3, 4} - Void::Math::Vec2f{1, 1};
Void::Testing::expect_eq(v.x, 2.f);
Void::Testing::expect_eq(v.y, 3.f);
}
Void::Testing::Test vec2_scale()
{
constexpr auto v = Void::Math::Vec2f{2, 3} * 2.f;
Void::Testing::expect_eq(v.x, 4.f);
Void::Testing::expect_eq(v.y, 6.f);
}
Void::Testing::Test vec2_dot()
{
constexpr auto d = Void::Math::Vec2f{1, 0}.dot({0, 1});
static_assert(d == 0.f);
Void::Testing::expect_eq(d, 0.f);
}
Void::Testing::Test vec2_length()
{
const Void::Math::Vec2f v{3, 4};
Void::Testing::expect(std::abs(v.length() - 5.f) < EPS);
}
Void::Testing::Test vec2_normalized()
{
const Void::Math::Vec2f v = Void::Math::Vec2f{3, 4}.normalized();
Void::Testing::expect(std::abs(v.length() - 1.f) < EPS);
}
Void::Testing::Test vec2_lerp()
{
const auto v = Void::Math::lerp(Void::Math::Vec2f{0, 0},
Void::Math::Vec2f{2, 4}, 0.5f);
Void::Testing::expect(std::abs(v.x - 1.f) < EPS);
Void::Testing::expect(std::abs(v.y - 2.f) < EPS);
}
// ── Vec3 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test vec3_cross_unit_vectors()
{
constexpr Void::Math::Vec3f x{1, 0, 0};
constexpr Void::Math::Vec3f y{0, 1, 0};
constexpr Void::Math::Vec3f z = x.cross(y);
static_assert(z.x == 0.f && z.y == 0.f && z.z == 1.f);
Void::Testing::expect_eq(z.z, 1.f);
}
Void::Testing::Test vec3_length()
{
const Void::Math::Vec3f v{1, 2, 2};
Void::Testing::expect(std::abs(v.length() - 3.f) < EPS);
}
Void::Testing::Test vec3_normalized_unit_length()
{
const auto v = Void::Math::Vec3f{2, 3, 6}.normalized();
Void::Testing::expect(std::abs(v.length() - 1.f) < EPS);
}
// ── Vec4 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test vec4_from_vec3()
{
constexpr Void::Math::Vec4f v{Void::Math::Vec3f{1, 2, 3}, 1.f};
static_assert(v.w == 1.f);
Void::Testing::expect_eq(v.z, 3.f);
}
Void::Testing::Test vec4_xyz()
{
constexpr Void::Math::Vec4f v{1, 2, 3, 4};
constexpr auto xyz = v.xyz();
static_assert(xyz.x == 1.f && xyz.y == 2.f && xyz.z == 3.f);
}
// ── Mat4 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test mat4_identity_times_vec()
{
constexpr auto I = Void::Math::Mat4f::identity();
constexpr Void::Math::Vec4f v{1, 2, 3, 1};
constexpr auto r = I * v;
static_assert(r.x == 1.f && r.y == 2.f && r.z == 3.f && r.w == 1.f);
}
Void::Testing::Test mat4_translation()
{
const auto T = Void::Math::Mat4f::translation(1, 2, 3);
const Void::Math::Vec4f v{0, 0, 0, 1};
const auto r = T * v;
Void::Testing::expect(std::abs(r.x - 1.f) < EPS);
Void::Testing::expect(std::abs(r.y - 2.f) < EPS);
Void::Testing::expect(std::abs(r.z - 3.f) < EPS);
}
Void::Testing::Test mat4_scale()
{
const auto S = Void::Math::Mat4f::scale(2, 3, 4);
const Void::Math::Vec4f v{1, 1, 1, 1};
const auto r = S * v;
Void::Testing::expect(std::abs(r.x - 2.f) < EPS);
Void::Testing::expect(std::abs(r.y - 3.f) < EPS);
Void::Testing::expect(std::abs(r.z - 4.f) < EPS);
}
Void::Testing::Test mat4_rotation_z_90_degrees()
{
const auto R = Void::Math::Mat4f::rotation_z(
std::numbers::pi_v<float> / 2.f);
const Void::Math::Vec4f v{1, 0, 0, 0};
const auto r = R * v;
Void::Testing::expect(std::abs(r.x - 0.f) < EPS);
Void::Testing::expect(std::abs(r.y - 1.f) < EPS);
}
Void::Testing::Test mat4_mul_identity_nop()
{
const auto A = Void::Math::Mat4f::translation(1, 2, 3);
const auto I = Void::Math::Mat4f::identity();
const auto R = A * I;
Void::Testing::expect_eq(R, A);
}
Void::Testing::Test mat4_transpose()
{
auto M = Void::Math::Mat4f::identity();
M.at(0, 1) = 5.f;
const auto T = M.transposed();
Void::Testing::expect_eq(T.at(1, 0), 5.f);
Void::Testing::expect_eq(T.at(0, 1), 0.f);
}