libcruft-util/test/matrix.cpp

148 lines
3.4 KiB
C++
Raw Normal View History

2014-08-19 20:45:28 +10:00
#include "matrix.hpp"
2015-04-13 16:45:56 +10:00
2014-08-19 20:45:28 +10:00
#include "debug.hpp"
2015-04-13 16:45:56 +10:00
#include "tap.hpp"
#include "vector.hpp"
2014-08-19 20:45:28 +10:00
#include <cstdlib>
int
2015-07-13 16:29:01 +10:00
main (void)
{
util::TAP::logger tap;
2014-08-19 20:45:28 +10:00
{
// Identity matrix-vector multiplication
2015-10-30 23:40:13 +11:00
auto v = util::vector4f { 1.f, 2.f, 3.f, 4.f };
auto r = util::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
}
{
// Simple matrix-vector multiplication
2015-10-30 23:40:13 +11:00
util::matrix4f m { {
2014-08-19 20:45:28 +10:00
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }
} };
util::vector<4,float> v { 1.f, 2.f, 3.f, 4.f };
2014-08-19 20:45:28 +10:00
auto r = m * v;
2015-07-13 16:29:01 +10:00
tap.expect (
almost_equal (r.x, 30) &&
almost_equal (r.y, 70) &&
almost_equal (r.z, 110) &&
almost_equal (r.w, 150),
"simple matrix-vector multiplication"
);
2014-08-19 20:45:28 +10:00
}
2014-12-15 13:30:37 +11:00
{
// Simple matrix-matrix multiplication
2015-10-30 23:40:13 +11:00
util::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 },
} };
2015-10-30 23:40:13 +11:00
util::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 }
} };
2015-10-30 23:40:13 +11:00
util::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
2015-10-30 23:40:13 +11:00
auto m = util::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)
2015-07-13 16:29:01 +10:00
success = success && almost_equal (m.values[r][c], 1);
2014-08-19 20:45:28 +10:00
else
2015-07-13 16:29:01 +10:00
success = success && almost_equal (m.values[r][c], 0);
tap.expect (success, "identity inversion");
2014-08-19 20:45:28 +10:00
}
// Simple 2x2 inversion test
{
util::matrix2f m { {
{ 1, 2 },
{ 3, 4 }
} };
tap.expect_eq (-2, m.determinant (), "2x2 determinant");
util::matrix2f r { {
{ -4, 2 },
{ 3, -1 }
} };
tap.expect_eq (r / 2.f, m.inverse (), "2x2 inversion");
}
// Simple 3x3 inversion test
{
util::matrix3f m { {
{ 3, 1, 2 },
{ 2, 3, 1 },
{ 4, 0, 2 }
} };
tap.expect_eq (-6, m.determinant (), "3x3 determinant");
util::matrix3f r { {
{ -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
{
2015-10-30 23:40:13 +11:00
util::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 }
} };
tap.expect_eq (-160.f, m.determinant (), "4x4 determinant");
2015-10-30 23:40:13 +11:00
util::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 }
} };
tap.expect_eq (m.inverse (), r / 40.f, "4x4 inversion");
2014-08-19 20:45:28 +10:00
}
2015-07-13 16:29:01 +10:00
return tap.status ();
2014-08-19 20:45:28 +10:00
}