2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/matrix.hpp>
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/tap.hpp>
|
|
|
|
#include <cruft/util/vector.hpp>
|
|
|
|
#include <cruft/util/coord/iostream.hpp>
|
|
|
|
#include <cruft/util/quaternion.hpp>
|
2014-08-19 20:45:28 +10:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2016-09-14 17:45:33 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-08-19 20:45:28 +10:00
|
|
|
int
|
2015-07-13 16:29:01 +10:00
|
|
|
main (void)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2015-07-13 16:29:01 +10:00
|
|
|
|
2017-11-24 12:24:44 +11:00
|
|
|
// a quick check to make sure this function is actually provided
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (sum (cruft::matrix4f::zeroes ()), 0.f, "zero matrix sums to zero");
|
2017-11-24 12:24:44 +11:00
|
|
|
|
|
|
|
|
|
|
|
// trivial check for matrix summation. useful to sanity test some
|
|
|
|
// alignment constraints if run under a tool like memorysanitizer or
|
|
|
|
// valgrind.
|
2018-08-05 14:42:02 +10:00
|
|
|
static constexpr cruft::matrix4f SEQ { {
|
2016-08-15 20:32:24 +10:00
|
|
|
{ 1, 2, 3, 4 },
|
|
|
|
{ 5, 6, 7, 8 },
|
|
|
|
{ 9, 10, 11, 12 },
|
|
|
|
{ 13, 14, 15, 16 }
|
|
|
|
} };
|
2016-08-15 18:45:25 +10:00
|
|
|
|
2016-08-15 20:32:24 +10:00
|
|
|
tap.expect_eq (sum (SEQ), 136.f, "element summation");
|
|
|
|
|
2016-10-11 20:57:03 +11:00
|
|
|
// tranposition
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
static constexpr cruft::matrix4f QES {{
|
2016-10-11 20:57:03 +11:00
|
|
|
{ 1, 5, 9, 13 },
|
|
|
|
{ 2, 6, 10, 14 },
|
|
|
|
{ 3, 7, 11, 15 },
|
|
|
|
{ 4, 8, 12, 16 }
|
|
|
|
}};
|
|
|
|
tap.expect_eq (transposed (SEQ), QES, "transposition");
|
|
|
|
tap.expect_eq (transposed (transposed (SEQ)), SEQ, "double tranposition is identity");
|
|
|
|
}
|
|
|
|
|
2016-08-15 20:32:24 +10:00
|
|
|
// matrix-scalar operations
|
|
|
|
{
|
|
|
|
tap.expect_eq (sum (SEQ + 1.f), 152.f, "matrix-scalar addition");
|
|
|
|
tap.expect_eq (sum (SEQ - 1.f), 120.f, "matrix-scalar subtraction");
|
|
|
|
tap.expect_eq (sum (SEQ * 2.f), 272.f, "matrix-scalar multiplication");
|
|
|
|
tap.expect_eq (sum (SEQ / 2.f), 68.f, "matrix-scalar division");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple matrix-vector multiplication
|
2014-08-19 20:45:28 +10:00
|
|
|
{
|
|
|
|
// Identity matrix-vector multiplication
|
2018-08-05 14:42:02 +10:00
|
|
|
auto v = cruft::vector4f { 1.f, 2.f, 3.f, 4.f };
|
|
|
|
auto r = cruft::matrix4f::identity () * v;
|
2015-07-13 16:29:01 +10:00
|
|
|
tap.expect_eq (r, v, "identity matrix-vector multiplication");
|
2014-08-19 20:45:28 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::vector<4,float> v { 1.f, 2.f, 3.f, 4.f };
|
2014-08-19 20:45:28 +10:00
|
|
|
|
2016-08-15 20:32:24 +10:00
|
|
|
auto r = SEQ * v;
|
2014-08-19 20:45:28 +10:00
|
|
|
|
2015-07-13 16:29:01 +10:00
|
|
|
tap.expect (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::almost_equal (r.x, 30.f) &&
|
|
|
|
cruft::almost_equal (r.y, 70.f) &&
|
|
|
|
cruft::almost_equal (r.z, 110.f) &&
|
|
|
|
cruft::almost_equal (r.w, 150.f),
|
2015-07-13 16:29:01 +10:00
|
|
|
"simple matrix-vector multiplication"
|
|
|
|
);
|
2014-08-19 20:45:28 +10:00
|
|
|
}
|
|
|
|
|
2014-12-15 13:30:37 +11:00
|
|
|
{
|
|
|
|
// Simple matrix-matrix multiplication
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix4f a { {
|
2014-12-15 13:30:37 +11:00
|
|
|
{ 1, 2, 3, 4 },
|
|
|
|
{ 5, 6, 7, 8 },
|
|
|
|
{ 9, 10, 11, 12 },
|
|
|
|
{ 13, 14, 15, 16 },
|
|
|
|
} };
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix4f b { {
|
2014-12-15 13:30:37 +11:00
|
|
|
{ 17, 18, 19, 20 },
|
|
|
|
{ 21, 22, 23, 24 },
|
|
|
|
{ -1, -2, -3, -4 },
|
|
|
|
{ -5, -6, -7, -8 }
|
|
|
|
} };
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix4f ab { {
|
2014-12-15 13:30:37 +11:00
|
|
|
{ 9, 8, 7, 6 },
|
|
|
|
{ 41, 40, 39, 38 },
|
|
|
|
{ 73, 72, 71, 70 },
|
|
|
|
{ 105, 104, 103, 102 },
|
|
|
|
} };
|
|
|
|
|
2015-10-30 23:40:13 +11:00
|
|
|
ab *= 4.f;
|
2014-12-15 13:30:37 +11:00
|
|
|
|
|
|
|
auto res = a * b;
|
|
|
|
|
2015-07-13 16:29:01 +10:00
|
|
|
tap.expect_eq (ab, res, "simple matrix-matrix multiplication");
|
2014-12-15 13:30:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-19 20:45:28 +10:00
|
|
|
{
|
2015-07-13 16:29:01 +10:00
|
|
|
bool success = true;
|
|
|
|
|
2014-08-19 20:45:28 +10:00
|
|
|
// Ensure identity inverts to identity
|
2018-08-05 14:42:02 +10:00
|
|
|
auto m = cruft::matrix4f::identity ().inverse ();
|
2014-08-19 20:45:28 +10:00
|
|
|
for (size_t r = 0; r < m.rows; ++r)
|
|
|
|
for (size_t c = 0; c < m.cols; ++c)
|
|
|
|
if (r == c)
|
2018-08-05 14:42:02 +10:00
|
|
|
success = success && cruft::almost_equal (m[r][c], 1.f);
|
2014-08-19 20:45:28 +10:00
|
|
|
else
|
2018-08-05 14:42:02 +10:00
|
|
|
success = success && cruft::almost_equal (m[r][c], 0.f);
|
2015-07-13 16:29:01 +10:00
|
|
|
|
|
|
|
tap.expect (success, "identity inversion");
|
2014-08-19 20:45:28 +10:00
|
|
|
}
|
|
|
|
|
2015-11-04 23:23:46 +11:00
|
|
|
// Simple 2x2 inversion test
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix2f m { {
|
2015-11-04 23:23:46 +11:00
|
|
|
{ 1, 2 },
|
|
|
|
{ 3, 4 }
|
|
|
|
} };
|
|
|
|
|
2015-11-13 17:10:10 +11:00
|
|
|
tap.expect_eq (-2.f, m.determinant (), "2x2 determinant");
|
2015-11-04 23:23:46 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix2f r { {
|
2015-11-04 23:23:46 +11:00
|
|
|
{ -4, 2 },
|
|
|
|
{ 3, -1 }
|
|
|
|
} };
|
|
|
|
|
|
|
|
tap.expect_eq (r / 2.f, m.inverse (), "2x2 inversion");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple 3x3 inversion test
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix3f m { {
|
2015-11-04 23:23:46 +11:00
|
|
|
{ 3, 1, 2 },
|
|
|
|
{ 2, 3, 1 },
|
|
|
|
{ 4, 0, 2 }
|
|
|
|
} };
|
|
|
|
|
2015-11-13 17:10:10 +11:00
|
|
|
tap.expect_eq (-6.f, m.determinant (), "3x3 determinant");
|
2015-11-04 23:23:46 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix3f r { {
|
2015-11-04 23:23:46 +11:00
|
|
|
{ -6, 2, 5 },
|
|
|
|
{ 0, 2, -1 },
|
|
|
|
{ 12, -4, -7 }
|
|
|
|
} };
|
|
|
|
|
|
|
|
tap.expect_eq (m.inverse (), r / 6.f, "3x3 inversion");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple 4x4 inversion test
|
2014-08-19 20:45:28 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix4f m { {
|
2014-08-19 20:45:28 +10:00
|
|
|
{ 4, 1, 2, 3 },
|
|
|
|
{ 2, 3, 4, 1 },
|
|
|
|
{ 3, 4, 1, 2 },
|
|
|
|
{ 1, 2, 3, 4 }
|
|
|
|
} };
|
|
|
|
|
2015-11-04 23:23:46 +11:00
|
|
|
tap.expect_eq (-160.f, m.determinant (), "4x4 determinant");
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix4f r { {
|
2014-08-19 20:45:28 +10:00
|
|
|
{ 11, 1, 1, -9 },
|
|
|
|
{ -9, 1, 11, 1 },
|
|
|
|
{ 1, 11, -9, 1 },
|
|
|
|
{ 1, -9, 1, 11 }
|
|
|
|
} };
|
|
|
|
|
2015-11-04 23:23:46 +11:00
|
|
|
tap.expect_eq (m.inverse (), r / 40.f, "4x4 inversion");
|
2014-08-19 20:45:28 +10:00
|
|
|
}
|
|
|
|
|
2016-09-14 17:45:33 +10:00
|
|
|
// sanity check euler rotations
|
|
|
|
{
|
|
|
|
static const struct {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::vector3f euler;
|
2016-09-14 17:45:33 +10:00
|
|
|
const char *msg;
|
|
|
|
} TESTS[] = {
|
2018-08-05 14:42:02 +10:00
|
|
|
{ cruft::vector3f { 0 }, "zeroes" },
|
2016-09-14 17:45:33 +10:00
|
|
|
|
|
|
|
{ { 1, 0, 0 }, "x-axis" },
|
|
|
|
{ { 0, 1, 0 }, "y-axis" },
|
|
|
|
{ { 0, 0, 1 }, "z-axis" },
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
{ cruft::vector3f { 1 }, "ones" },
|
2016-09-14 17:45:33 +10:00
|
|
|
|
|
|
|
{ { 3, 5, 7 }, "positive primes" },
|
|
|
|
{ { -3, -5, -7 }, "negative primes" },
|
|
|
|
{ { 3, -5, 7 }, "mixed primes" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto t: TESTS) {
|
2018-08-05 14:42:02 +10:00
|
|
|
constexpr auto PI2 = 2 * cruft::pi<float>;
|
2016-09-14 17:45:33 +10:00
|
|
|
|
|
|
|
auto matrix = (
|
2020-01-01 06:54:25 +11:00
|
|
|
cruft::quaternionf::angle_axis (std::fmod (t.euler[2], PI2), { 0, 0, 1 }) *
|
|
|
|
cruft::quaternionf::angle_axis (std::fmod (t.euler[1], PI2), { 0, 1, 0 }) *
|
|
|
|
cruft::quaternionf::angle_axis (std::fmod (t.euler[0], PI2), { 1, 0, 0 })
|
2016-09-14 17:45:33 +10:00
|
|
|
).as_matrix ();
|
|
|
|
|
|
|
|
auto euler = to_euler (matrix);
|
|
|
|
auto truth = t.euler;
|
|
|
|
|
|
|
|
euler = mod (euler + 4 * PI2, PI2);
|
|
|
|
truth = mod (truth + 4 * PI2, PI2);
|
|
|
|
|
2017-08-27 12:32:00 +10:00
|
|
|
tap.expect (
|
|
|
|
all (compare (
|
|
|
|
truth, euler,
|
2018-08-05 14:42:02 +10:00
|
|
|
[] (auto a, auto b) { return cruft::almost_equal (a, b); }
|
2017-08-27 12:32:00 +10:00
|
|
|
)),
|
2021-04-13 16:05:08 +10:00
|
|
|
"matrix-to-euler, {:s}",
|
2017-08-27 12:32:00 +10:00
|
|
|
t.msg
|
|
|
|
);
|
2016-09-14 17:45:33 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 16:29:01 +10:00
|
|
|
return tap.status ();
|
2014-08-19 20:45:28 +10:00
|
|
|
}
|