184 lines
5.3 KiB
C++
184 lines
5.3 KiB
C++
#include <jtest/jtest.hpp>
|
|
#include <jtest/Unit.hpp>
|
|
#include <J3ML/LinearAlgebra/Matrix4x4.hpp>
|
|
#include <J3ML/LinearAlgebra/Quaternion.hpp>
|
|
#include <J3ML/Math.hpp>
|
|
|
|
jtest::Unit Matrix4x4Unit {"Matrix4x4"};
|
|
namespace Matrix4x4Tests {
|
|
inline void Define() {
|
|
using namespace jtest;
|
|
using namespace J3ML::LinearAlgebra;
|
|
using namespace J3ML::Math;
|
|
|
|
Matrix4x4Unit += Test("Add_Unary", [] {
|
|
Matrix4x4 m(1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16);
|
|
Matrix4x4 m2 = +m;
|
|
jtest::check(m.Equals(m2));
|
|
});
|
|
|
|
Matrix4x4Unit += Test("Inverse", [] {
|
|
RNG rng;
|
|
Matrix4x4 A = Matrix4x4::RandomGeneral(rng, -10.f, 10.f);
|
|
bool mayFail = Math::EqualAbs(A.Determinant(), 0.f, 1e-2f);
|
|
|
|
Matrix4x4 A2 = A;
|
|
bool success = A2.Inverse();
|
|
|
|
jtest::check(success || mayFail);
|
|
|
|
if (success)
|
|
{
|
|
Matrix4x4 id = A * A2;
|
|
Matrix4x4 id2 = A2 * A;
|
|
|
|
jtest::check(id.Equals(Matrix4x4::Identity, 0.3f));
|
|
jtest::check(id2.Equals(Matrix4x4::Identity, 0.3f));
|
|
}
|
|
});
|
|
|
|
|
|
Matrix4x4Unit += Test("CtorFromRotationMatrix", []{
|
|
Matrix3x3 m = Matrix3x3::RotateX(40);
|
|
|
|
Matrix4x4 from3x3(m);
|
|
});
|
|
|
|
Matrix4x4Unit += Test("Ctor", []{
|
|
RNG rng;
|
|
Matrix3x3 m = Matrix3x3::RandomGeneral(rng, -10.f, 10.f);
|
|
|
|
Matrix4x4 m2(m);
|
|
|
|
for (int y = 0; y < 3; ++y)
|
|
for (int x = 0; x < 3; ++x)
|
|
check(Math::EqualAbs(m.At(y, x), m2.At(y, x)));
|
|
|
|
/*jtest::check(Math::EqualAbs(m2[0][3], 0.f));
|
|
jtest::check(Math::EqualAbs(m2[1][3], 0.f));
|
|
jtest::check(Math::EqualAbs(m2[2][3], 0.f));
|
|
|
|
jtest::check(Math::EqualAbs(m2[3][0], 0.f));
|
|
jtest::check(Math::EqualAbs(m2[3][1], 0.f));
|
|
jtest::check(Math::EqualAbs(m2[3][2], 0.f));
|
|
jtest::check(Math::EqualAbs(m2[3][3], 0.f));*/
|
|
});
|
|
|
|
Matrix4x4Unit += Test("SetRow", []
|
|
{
|
|
Matrix4x4 m;
|
|
m.SetRow(0, 1,2,3,4);
|
|
m.SetRow(1, Vector4(5,6,7,8));
|
|
m.SetRow(2, 9,10,11,12);
|
|
m.SetRow(3, 13, 14, 15, 16);
|
|
|
|
|
|
Matrix4x4 m3(1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16);
|
|
Matrix4x4 m2;
|
|
m2.Set(1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16);
|
|
jtest::check(m.Equals(m2));
|
|
jtest::check(m.Equals(m3));
|
|
});
|
|
|
|
Matrix4x4Unit += Test("SwapRows", []
|
|
{
|
|
Matrix4x4 m(1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16);
|
|
Matrix4x4 m2(13,14,15,16, 9,10,11,12, 5,6,7,8, 1,2,3,4);
|
|
m.SwapRows(0,3);
|
|
m.SwapRows(1,2);
|
|
jtest::check(m.Equals(m2));
|
|
});
|
|
|
|
Matrix4x4Unit += Test("CtorCols", []
|
|
{
|
|
Matrix4x4 m(Vector4(1,2,3,4), Vector4(5,6,7,8), Vector4(9,10,11,12), Vector4(13,14,15,16));
|
|
Matrix4x4 m2(1,5,9,13, 2,6,10,14, 3,7,11,15, 4,8,12,16);
|
|
jtest::check(m.Equals(m2));
|
|
});
|
|
|
|
Matrix4x4Unit += Test("CtorFromQuat", []
|
|
{
|
|
RNG rng;
|
|
// TODO: Multiple random passes
|
|
Quaternion q = Quaternion::RandomRotation(rng);
|
|
Matrix4x4 m(q);
|
|
|
|
Vector3 v = Vector3(-1, 5, 20.f);
|
|
Vector3 v1 = q * v;
|
|
Vector3 v2 = m.Transform(v); //m.TransformPos(v);
|
|
jtest::check(v1.Equals(v2));
|
|
});
|
|
|
|
|
|
Matrix4x4Unit += Test("CtorFromQuatTrans", [] {
|
|
RNG rng;
|
|
constexpr float SCALE = 1e2f;
|
|
Vector3 t = Vector3::RandomBox(rng, Vector3(-SCALE, -SCALE, -SCALE), Vector3(SCALE, SCALE, SCALE));
|
|
Quaternion q = Quaternion::RandomRotation(rng);
|
|
|
|
Matrix4x4 m (q, t);
|
|
|
|
Vector3 v = Vector3(-1, 5, 20.f);
|
|
Vector3 v1 = q * v + t;
|
|
Vector3 v2 = m.Transform(v);
|
|
jtest::check(v1.Equals(v2));
|
|
|
|
});
|
|
Matrix4x4Unit += Test("Translate", [] {
|
|
RNG rng;
|
|
constexpr float SCALE = 1e2f;
|
|
Vector3 t = Vector3::RandomBox(rng, Vector3(-SCALE, -SCALE, -SCALE), Vector3(SCALE, SCALE, SCALE));
|
|
Vector3 t2 = Vector3::RandomBox(rng, Vector3(-SCALE, -SCALE, -SCALE), Vector3(SCALE, SCALE, SCALE));
|
|
|
|
Matrix4x4 m = Matrix4x4::Translate(t);
|
|
Matrix4x4 m2 = Matrix4x4::Translate({t.x, t.y, t.z});
|
|
|
|
Vector3 v = t + t2;
|
|
Vector3 v1 = m.Transform(t2);
|
|
Vector3 v2 = m2.Transform(t2);
|
|
|
|
jtest::check(v1.Equals(v2));
|
|
jtest::check(v.Equals(v1));
|
|
});
|
|
Matrix4x4Unit += Test("Scale", [] {
|
|
Matrix4x4 m = Matrix4x4::Scale({2, 4, 6});
|
|
Matrix4x4 m2(2,0,0,0, 0,4,0,0, 0,0,6,0, 0,0,0,1);
|
|
jtest::check(m.Equals(m2));
|
|
});
|
|
Matrix4x4Unit += Test("MulMat3x3", [] {
|
|
RNG rng;
|
|
Matrix3x3 m = Matrix3x3::RandomGeneral(rng, -10.f, 10.f);
|
|
Matrix4x4 m_ = m;
|
|
Matrix4x4 m2 = Matrix4x4::RandomGeneral(rng, -10.f, 10.f);
|
|
|
|
Matrix4x4 test = m2 * m;
|
|
Matrix4x4 correct = m2 * m_;
|
|
|
|
jtest::check(test.Equals(correct));
|
|
});
|
|
}
|
|
|
|
inline void Run() {
|
|
Matrix4x4Unit.RunAll();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|