2016-08-10 17:42:52 +10:00
|
|
|
#include <quaternion.hpp>
|
|
|
|
|
|
|
|
#include "tap.hpp"
|
|
|
|
#include "types.hpp"
|
|
|
|
|
|
|
|
using util::quaternion;
|
|
|
|
using util::quaternionf;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
|
|
|
util::TAP::logger tap;
|
|
|
|
|
|
|
|
tap.expect_eq (
|
2016-08-11 14:58:46 +10:00
|
|
|
norm (quaternionf::IDENTITY), 1.f,
|
2016-08-10 17:42:52 +10:00
|
|
|
"identity magnitude is unit"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
quaternionf::IDENTITY * quaternionf::IDENTITY,
|
|
|
|
quaternionf::IDENTITY,
|
|
|
|
"identity multiplication with identity"
|
|
|
|
);
|
|
|
|
|
|
|
|
{
|
2016-08-11 14:58:46 +10:00
|
|
|
auto val = normalised (quaternionf (2, 3, 4, 7));
|
2016-08-10 17:42:52 +10:00
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
val * quaternionf::IDENTITY,
|
|
|
|
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).
|
2016-08-10 17:42:52 +10:00
|
|
|
util::vector4f a_v { 2, -11, 5, -17};
|
|
|
|
util::vector4f b_v { 3, 13, -7, -19};
|
|
|
|
|
2016-08-11 14:58:46 +10:00
|
|
|
auto a = normalised (a_v).as<quaternion> ();
|
|
|
|
auto b = normalised (b_v).as<quaternion> ();
|
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 (
|
|
|
|
quaternionf::IDENTITY.as_matrix (),
|
|
|
|
util::matrix4f::IDENTITY,
|
|
|
|
"identity quaternion to matrix"
|
|
|
|
);
|
|
|
|
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
float mag;
|
|
|
|
util::vector3f axis;
|
|
|
|
} 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
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < elems (ROTATIONS); ++i) {
|
|
|
|
const auto &r = ROTATIONS[i];
|
|
|
|
|
2016-08-15 20:54:01 +10:00
|
|
|
auto q = quaternionf::rotation (r.mag, r.axis).as_matrix ();
|
|
|
|
auto m = util::matrix4f::rotation (r.mag, r.axis);
|
|
|
|
auto diff = util::abs (q - m);
|
|
|
|
|
|
|
|
tap.expect_lt (util::sum (diff), 1e-6f, "single basis rotation %zu", i);
|
2016-08-10 17:42:52 +10:00
|
|
|
}
|
|
|
|
|
2016-08-15 20:54:01 +10:00
|
|
|
auto q = quaternionf::IDENTITY;
|
|
|
|
auto m = util::matrix4f::IDENTITY;
|
2016-08-10 17:42:52 +10:00
|
|
|
|
|
|
|
for (auto r: ROTATIONS) {
|
2016-08-15 20:54:01 +10:00
|
|
|
q = q.rotation (r.mag, r.axis) * q;
|
|
|
|
m = m.rotation (r.mag, r.axis) * m;
|
2016-08-10 17:42:52 +10:00
|
|
|
}
|
|
|
|
|
2016-08-15 20:54:01 +10:00
|
|
|
auto diff = util::abs (q.as_matrix () - m);
|
|
|
|
tap.expect_lt (util::sum (diff), 1e-6f, "chained single axis rotations");
|
2016-08-10 17:42:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return tap.status ();
|
|
|
|
}
|