2016-09-14 17:54:25 +10:00
|
|
|
#include "tap.hpp"
|
|
|
|
|
|
|
|
#include "vector.hpp"
|
|
|
|
#include "matrix.hpp"
|
|
|
|
#include "quaternion.hpp"
|
|
|
|
|
2016-10-17 16:51:06 +11:00
|
|
|
#include "coord/iostream.hpp"
|
2018-04-11 18:26:30 +10:00
|
|
|
#include "matrix.hpp"
|
2016-09-14 17:54:25 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
#include <array>
|
|
|
|
|
|
|
|
|
2016-10-12 22:58:34 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_matrix_identities (cruft::TAP::logger &tap)
|
2016-10-12 22:58:34 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
static constexpr cruft::vector3f UP {0,1,0};
|
2016-10-12 22:58:34 +11:00
|
|
|
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point3f p { 1, 2, 3 };
|
2016-10-12 22:58:34 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto m = cruft::translation (0-p);
|
2018-04-17 14:26:23 +10:00
|
|
|
auto x = m * p;
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (x, cruft::point3f {0}, "trivial translation to origin");
|
2016-10-17 16:51:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point3f p {0};
|
|
|
|
auto m = cruft::rotation<float> (1, { 1, 0, 0 }) *
|
|
|
|
cruft::rotation<float> (1, { 0, 1, 0 }) *
|
|
|
|
cruft::rotation<float> (1, { 0, 0, 1 });
|
2018-04-17 14:26:23 +10:00
|
|
|
auto x = m * p.homog ();
|
2016-10-17 16:51:06 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (x, cruft::point4f {0,0,0,1}, "trivial rotation at origin");
|
2016-10-12 22:58:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point3f eye { 1, 2, 3 };
|
|
|
|
cruft::point3f tgt { 0, 0, 0 };
|
2016-10-12 22:58:34 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto m = cruft::look_at (eye, tgt, UP);
|
2018-04-17 14:26:23 +10:00
|
|
|
auto x = m * eye.homog ();
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (x, cruft::point4f {0,0,0,1}, "look_at eye translation");
|
2016-10-12 22:58:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point3f eye { 1, 2, 3 };
|
|
|
|
cruft::point3f tgt { 4, 5, 6 };
|
2016-10-12 22:58:34 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto m = cruft::look_at (eye, tgt, UP);
|
2018-04-17 14:26:23 +10:00
|
|
|
auto x = m * eye.homog ();
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (x, cruft::point4f {0,0,0,1}, "look_at eye translation with target");
|
2016-10-12 22:58:34 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-11 18:26:30 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_perspective (cruft::TAP::logger &tap)
|
2018-04-11 18:26:30 +10:00
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
float w, h;
|
|
|
|
float near, far;
|
|
|
|
float fov;
|
|
|
|
float a, b, c, d;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ 1.f, 1.f, 1.f, 100.f, 45.f, 2.414213419f, 2.414213419f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 1.f, 1.f, 1.f, 100.f, 60.f, 1.732050776f, 1.732050776f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 1.f, 1.f, 1.f, 100.f, 90.f, 1.000000000f, 1.000000000f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 1.f, 1.f, 1.f, 100.f, 120.f, 0.577350199f, 0.577350199f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 1.f, 1.f, 1e-08f, 1e+08f, 45.f, 2.414213419f, 2.414213419f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 1.f, 1.f, 1e-08f, 1e+08f, 60.f, 1.732050776f, 1.732050776f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 1.f, 1.f, 1e-08f, 1e+08f, 90.f, 1.000000000f, 1.000000000f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 1.f, 1.f, 1e-08f, 1e+08f, 120.f, 0.577350199f, 0.577350199f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 1.f, 1.f, 0.01f, 1000.f, 45.f, 2.414213419f, 2.414213419f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 1.f, 1.f, 0.01f, 1000.f, 60.f, 1.732050776f, 1.732050776f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 1.f, 1.f, 0.01f, 1000.f, 90.f, 1.000000000f, 1.000000000f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 1.f, 1.f, 0.01f, 1000.f, 120.f, 0.577350199f, 0.577350199f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 4.f, 3.f, 1.f, 100.f, 45.f, 1.810660124f, 2.414213419f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 4.f, 3.f, 1.f, 100.f, 60.f, 1.299038053f, 1.732050776f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 4.f, 3.f, 1.f, 100.f, 90.f, 0.750000000f, 1.000000000f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 4.f, 3.f, 1.f, 100.f, 120.f, 0.433012664f, 0.577350199f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 4.f, 3.f, 1e-08f, 1e+08f, 45.f, 1.810660124f, 2.414213419f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 4.f, 3.f, 1e-08f, 1e+08f, 60.f, 1.299038053f, 1.732050776f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 4.f, 3.f, 1e-08f, 1e+08f, 90.f, 0.750000000f, 1.000000000f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 4.f, 3.f, 1e-08f, 1e+08f, 120.f, 0.433012664f, 0.577350199f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 4.f, 3.f, 0.01f, 1000.f, 45.f, 1.810660124f, 2.414213419f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 4.f, 3.f, 0.01f, 1000.f, 60.f, 1.299038053f, 1.732050776f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 4.f, 3.f, 0.01f, 1000.f, 90.f, 0.750000000f, 1.000000000f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 4.f, 3.f, 0.01f, 1000.f, 120.f, 0.433012664f, 0.577350199f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 9.f, 1.f, 100.f, 45.f, 1.357995033f, 2.414213419f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 9.f, 1.f, 100.f, 60.f, 0.974278569f, 1.732050776f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 9.f, 1.f, 100.f, 90.f, 0.562500000f, 1.000000000f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 9.f, 1.f, 100.f, 120.f, 0.324759483f, 0.577350199f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 9.f, 1e-08f, 1e+08f, 45.f, 1.357995033f, 2.414213419f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 9.f, 1e-08f, 1e+08f, 60.f, 0.974278569f, 1.732050776f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 9.f, 1e-08f, 1e+08f, 90.f, 0.562500000f, 1.000000000f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 9.f, 1e-08f, 1e+08f, 120.f, 0.324759483f, 0.577350199f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 9.f, 0.01f, 1000.f, 45.f, 1.357995033f, 2.414213419f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 9.f, 0.01f, 1000.f, 60.f, 0.974278569f, 1.732050776f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 9.f, 0.01f, 1000.f, 90.f, 0.562500000f, 1.000000000f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 9.f, 0.01f, 1000.f, 120.f, 0.324759483f, 0.577350199f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 10.f, 1.f, 100.f, 45.f, 1.508883357f, 2.414213419f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 10.f, 1.f, 100.f, 60.f, 1.082531691f, 1.732050776f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 10.f, 1.f, 100.f, 90.f, 0.625000000f, 1.000000000f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 10.f, 1.f, 100.f, 120.f, 0.360843867f, 0.577350199f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 16.f, 10.f, 1e-08f, 1e+08f, 45.f, 1.508883357f, 2.414213419f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 10.f, 1e-08f, 1e+08f, 60.f, 1.082531691f, 1.732050776f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 10.f, 1e-08f, 1e+08f, 90.f, 0.625000000f, 1.000000000f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 10.f, 1e-08f, 1e+08f, 120.f, 0.360843867f, 0.577350199f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 16.f, 10.f, 0.01f, 1000.f, 45.f, 1.508883357f, 2.414213419f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 10.f, 0.01f, 1000.f, 60.f, 1.082531691f, 1.732050776f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 10.f, 0.01f, 1000.f, 90.f, 0.625000000f, 1.000000000f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 16.f, 10.f, 0.01f, 1000.f, 120.f, 0.360843867f, 0.577350199f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 21.f, 9.f, 1.f, 100.f, 45.f, 1.034662843f, 2.414213419f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 21.f, 9.f, 1.f, 100.f, 60.f, 0.742307484f, 1.732050776f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 21.f, 9.f, 1.f, 100.f, 90.f, 0.428571433f, 1.000000000f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 21.f, 9.f, 1.f, 100.f, 120.f, 0.247435793f, 0.577350199f, -1.020202041f, -2.020201921f },
|
|
|
|
{ 21.f, 9.f, 1e-08f, 1e+08f, 45.f, 1.034662843f, 2.414213419f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 21.f, 9.f, 1e-08f, 1e+08f, 60.f, 0.742307484f, 1.732050776f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 21.f, 9.f, 1e-08f, 1e+08f, 90.f, 0.428571433f, 1.000000000f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 21.f, 9.f, 1e-08f, 1e+08f, 120.f, 0.247435793f, 0.577350199f, -1.000000000f, -0.000000020f },
|
|
|
|
{ 21.f, 9.f, 0.01f, 1000.f, 45.f, 1.034662843f, 2.414213419f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 21.f, 9.f, 0.01f, 1000.f, 60.f, 0.742307484f, 1.732050776f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 21.f, 9.f, 0.01f, 1000.f, 90.f, 0.428571433f, 1.000000000f, -1.000020027f, -0.020000201f },
|
|
|
|
{ 21.f, 9.f, 0.01f, 1000.f, 120.f, 0.247435793f, 0.577350199f, -1.000020027f, -0.020000201f },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &t: TESTS) {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix4f m {0};
|
2018-04-11 18:26:30 +10:00
|
|
|
m[0][0] = t.a;
|
|
|
|
m[1][1] = t.b;
|
|
|
|
m[2][2] = t.c;
|
|
|
|
m[2][3] = t.d;
|
|
|
|
m[3][2] = -1.f;
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
const auto p = cruft::perspective (cruft::to_radians (t.fov), t.w / t.h, {t.near, t.far});
|
2018-04-11 18:26:30 +10:00
|
|
|
|
|
|
|
tap.expect (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::relatively_equal (m, p, 1e-7f),
|
2018-04-11 18:26:30 +10:00
|
|
|
"perspective %!:%!@%!",
|
|
|
|
t.w, t.h, t.fov
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-14 17:54:25 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_mq_axis (cruft::TAP::logger &tap)
|
2016-09-14 17:54:25 +10:00
|
|
|
{
|
|
|
|
static const struct {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::vector3f euler;
|
2016-09-14 17:54:25 +10:00
|
|
|
const char *msg;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ { 1, 0, 0 }, "x-axis" },
|
|
|
|
{ { 0, 1, 0 }, "y-axis" },
|
|
|
|
{ { 0, 0, 1 }, "z-axis" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto t: TESTS) {
|
2018-08-05 14:42:02 +10:00
|
|
|
auto m = cruft::rotation<float> (1, t.euler);
|
|
|
|
auto q = cruft::quaternionf::angle_axis (1, t.euler);
|
2016-09-14 17:54:25 +10:00
|
|
|
|
|
|
|
auto diff = sum (abs (m - q.as_matrix ()));
|
|
|
|
tap.expect_le (diff, 1e-6f, "matrix/quaternion rotation identities, %s", t.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_mq_euler (cruft::TAP::logger &tap)
|
2016-09-14 17:54:25 +10:00
|
|
|
{
|
|
|
|
static const struct {
|
2017-11-22 17:03:00 +11:00
|
|
|
float x, y, z;
|
2016-09-14 17:54:25 +10:00
|
|
|
const char *msg;
|
|
|
|
} TESTS[] = {
|
2017-11-22 17:03:00 +11:00
|
|
|
{ 0, 0, 0, "zeroes" },
|
|
|
|
{ 1, 0, 0, "x-axis" },
|
|
|
|
{ 0, 1, 0, "y-axis" },
|
|
|
|
{ 0, 0, 1, "z-axis" },
|
|
|
|
{ 1, 1, 1, "ones" },
|
2020-01-01 06:54:25 +11:00
|
|
|
{ 3, 3, 3, "threes" }
|
2016-09-14 17:54:25 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto t: TESTS) {
|
2018-08-05 14:42:02 +10:00
|
|
|
const auto mx = cruft::rotation<float> (t.x, {1,0,0});
|
|
|
|
const auto my = cruft::rotation<float> (t.y, {0,1,0});
|
|
|
|
const auto mz = cruft::rotation<float> (t.z, {0,0,1});
|
2017-11-22 17:03:00 +11:00
|
|
|
const auto m = mx * my * mz;
|
|
|
|
|
2016-09-14 17:54:25 +10:00
|
|
|
auto q = (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::quaternionf::angle_axis (t.x, { 1, 0, 0 }) *
|
|
|
|
cruft::quaternionf::angle_axis (t.y, { 0, 1, 0 }) *
|
|
|
|
cruft::quaternionf::angle_axis (t.z, { 0, 0, 1 })
|
2016-09-14 17:54:25 +10:00
|
|
|
).as_matrix ();
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto diff = cruft::sum (abs (m - q));
|
2016-09-14 17:54:25 +10:00
|
|
|
tap.expect_le (diff, 1e-6f, "matrix-quaternion xyz euler rotations, %s", t.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main (int, char**)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2016-09-14 17:54:25 +10:00
|
|
|
|
2016-10-12 22:58:34 +11:00
|
|
|
test_matrix_identities (tap);
|
2016-09-14 17:54:25 +10:00
|
|
|
test_mq_axis (tap);
|
|
|
|
test_mq_euler (tap);
|
2018-04-11 18:26:30 +10:00
|
|
|
test_perspective (tap);
|
2016-09-14 17:54:25 +10:00
|
|
|
|
|
|
|
return tap.status ();
|
|
|
|
}
|