2016-08-10 17:42:52 +10:00
|
|
|
#include <quaternion.hpp>
|
|
|
|
|
|
|
|
#include "tap.hpp"
|
|
|
|
#include "types.hpp"
|
2016-10-12 23:03:04 +11:00
|
|
|
#include "maths.hpp"
|
|
|
|
#include "coord/iostream.hpp"
|
2016-08-10 17:42:52 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::quaternion;
|
|
|
|
using cruft::quaternionf;
|
2016-08-10 17:42:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2016-08-10 17:42:52 +10:00
|
|
|
|
2016-10-12 23:03:04 +11:00
|
|
|
// identity relations
|
2016-08-10 17:42:52 +10:00
|
|
|
tap.expect_eq (
|
2016-10-17 22:14:57 +11:00
|
|
|
norm (quaternionf::identity ()), 1.f,
|
2016-08-10 17:42:52 +10:00
|
|
|
"identity magnitude is unit"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect_eq (
|
2016-10-17 22:14:57 +11:00
|
|
|
quaternionf::identity () * quaternionf::identity (),
|
|
|
|
quaternionf::identity (),
|
2016-08-10 17:42:52 +10:00
|
|
|
"identity multiplication with identity"
|
|
|
|
);
|
|
|
|
|
2016-10-12 23:03:04 +11:00
|
|
|
// normalisation
|
2016-08-10 17:42:52 +10:00
|
|
|
{
|
2016-10-17 22:14:57 +11:00
|
|
|
auto val = normalised (quaternionf {2, 3, 4, 7});
|
2016-08-10 17:42:52 +10:00
|
|
|
|
|
|
|
tap.expect_eq (
|
2016-10-17 22:14:57 +11:00
|
|
|
val * quaternionf::identity (),
|
2016-08-10 17:42:52 +10:00
|
|
|
val,
|
|
|
|
"identity multiplication with quaternion constant"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-08-11 14:58:46 +10:00
|
|
|
// construct two quaternions for a trivial multiplication check.
|
|
|
|
//
|
|
|
|
// we use vector as an intermediate form so that normalisation can be
|
|
|
|
// done before the quaternion code is invoked (which may consider the
|
|
|
|
// values to be invalid and throw an exception given it's geared more
|
|
|
|
// towards rotations than general maths).
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::vector4f a_v { 2, -11, 5, -17};
|
|
|
|
cruft::vector4f b_v { 3, 13, -7, -19};
|
2016-10-17 22:14:57 +11:00
|
|
|
a_v = normalised (a_v);
|
|
|
|
b_v = normalised (b_v);
|
2016-08-10 17:42:52 +10:00
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
auto a = quaternionf { a_v[0], a_v[1], a_v[2], a_v[3] };
|
|
|
|
auto b = quaternionf { b_v[0], b_v[1], b_v[2], b_v[3] };
|
2016-08-10 17:42:52 +10:00
|
|
|
auto c = quaternionf {
|
|
|
|
-0.27358657116960006f,
|
|
|
|
-0.43498209092420004f,
|
|
|
|
-0.8443769181970001f,
|
|
|
|
-0.15155515799559996f,
|
|
|
|
};
|
|
|
|
|
|
|
|
tap.expect_eq (a * b, c, "multiplication");
|
|
|
|
tap.expect_neq (b * a, c, "multiplication is not commutative");
|
|
|
|
}
|
|
|
|
|
|
|
|
tap.expect_eq (
|
2016-10-17 22:14:57 +11:00
|
|
|
quaternionf::identity ().as_matrix (),
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::matrix4f::identity (),
|
2016-08-10 17:42:52 +10:00
|
|
|
"identity quaternion to matrix"
|
|
|
|
);
|
|
|
|
|
2016-10-12 23:03:04 +11:00
|
|
|
|
|
|
|
// check that concatenated transforms are identical to their matrix
|
|
|
|
// equivalent (which we are more likely to have correct).
|
2016-08-10 17:42:52 +10:00
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
float mag;
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::vector3f axis;
|
2016-08-10 17:42:52 +10:00
|
|
|
} ROTATIONS[] = {
|
2016-09-14 16:48:31 +10:00
|
|
|
{ 1.f, { 1.f, 0.f, 0.f } },
|
2016-08-10 17:42:52 +10:00
|
|
|
{ 1.f, { 0.f, 1.f, 0.f } },
|
2016-09-14 16:48:31 +10:00
|
|
|
{ 1.f, { 0.f, 0.f, 1.f } },
|
2016-08-10 17:42:52 +10:00
|
|
|
};
|
|
|
|
|
2016-11-17 18:06:39 +11:00
|
|
|
for (size_t i = 0; i < std::size (ROTATIONS); ++i) {
|
2016-08-10 17:42:52 +10:00
|
|
|
const auto &r = ROTATIONS[i];
|
|
|
|
|
2016-10-12 23:00:47 +11:00
|
|
|
auto q = quaternionf::angle_axis (r.mag, r.axis).as_matrix ();
|
2018-08-05 14:42:02 +10:00
|
|
|
auto m = cruft::rotation<float> (r.mag, r.axis);
|
|
|
|
auto diff = cruft::abs (q - m);
|
2016-08-15 20:54:01 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_lt (cruft::sum (diff), 1e-6f, "single basis rotation %zu", i);
|
2016-08-10 17:42:52 +10:00
|
|
|
}
|
|
|
|
|
2016-10-17 22:14:57 +11:00
|
|
|
auto q = quaternionf::identity ();
|
2018-08-05 14:42:02 +10:00
|
|
|
auto m = cruft::matrix4f::identity ();
|
2016-08-10 17:42:52 +10:00
|
|
|
|
|
|
|
for (auto r: ROTATIONS) {
|
2016-10-12 23:00:47 +11:00
|
|
|
q = q.angle_axis (r.mag, r.axis) * q;
|
2018-08-05 14:42:02 +10:00
|
|
|
m = cruft::rotation<float> (r.mag, r.axis) * m;
|
2016-08-10 17:42:52 +10:00
|
|
|
}
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto diff = cruft::abs (q.as_matrix () - m);
|
|
|
|
tap.expect_lt (cruft::sum (diff), 1e-6f, "chained single axis rotations");
|
2016-08-10 17:42:52 +10:00
|
|
|
}
|
|
|
|
|
2016-10-12 23:03:04 +11:00
|
|
|
// ensure vector rotation quaternions actually rotate a vector
|
|
|
|
{
|
|
|
|
const struct {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::vector3f src;
|
|
|
|
cruft::vector3f dst;
|
2016-10-12 23:03:04 +11:00
|
|
|
const char *msg;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ { 1, 0, 0 }, { 1, 0, 0 }, "x-axis identity" },
|
|
|
|
{ { 0, 1, 0 }, { 0, 1, 0 }, "y-axis identity" },
|
|
|
|
{ { 0, 0, 1 }, { 0, 0, 1 }, "z-axis identity" },
|
|
|
|
{ { 0, 0, 1 }, { 0, 0, -1 }, "+z to -z" },
|
|
|
|
{ { 0, 0, 1 }, { -1, 0, 0 }, "+z to -x" },
|
|
|
|
{ { 1, -2, 3 }, { -4, 5, -6 }, "incremental" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &t: TESTS) {
|
|
|
|
auto src = normalised (t.src);
|
|
|
|
auto dst = normalised (t.dst);
|
|
|
|
|
|
|
|
auto q = quaternionf::from_to (src, dst);
|
|
|
|
auto v = rotate (src, q);
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto diff = std::abs (cruft::sum (dst - v));
|
2016-10-12 23:03:04 +11:00
|
|
|
tap.expect_lt (diff, 1e-06f, "quaternion from-to, %s", t.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 17:42:52 +10:00
|
|
|
return tap.status ();
|
|
|
|
}
|