vector: use separate cartesian/spherical tests

some tests aren't uniquely invertible, so it's more reliable to use test
data that doesn't suffer from this issue.
This commit is contained in:
Danny Robson 2018-01-17 17:45:21 +11:00
parent 51ffcbb07e
commit c7ce526383

View File

@ -3,6 +3,9 @@
#include "maths.hpp"
#include "tap.hpp"
#include "float.hpp"
#include "coord/iostream.hpp"
using util::vector;
using util::vector2f;
@ -106,42 +109,97 @@ test_euler (util::TAP::logger &tap)
void
test_spherical (util::TAP::logger &tap)
{
constexpr auto q = util::PI<float> / 2.f;
static constexpr struct {
util::vector3f spherical;
util::vector3f cartesian;
const char *message;
} TESTS[] = {
{ { 1, 0, 0 }, { 0, 0, 1 }, "+zero", },
{ { -1, 0, 0 }, { 0, 0, -1 }, "-zero", },
} S2C [] = {
{ { 1, 0 * q, 0 * q }, { 0, 0, 1 }, "+zero", },
{ { 1, 2 * q, 0 * q }, { 0, 0, -1 }, "-zero", },
{ { 1, 1, 0 }, { 1, 0, 0 }, "90-theta", },
{ { 1, 2, 0 }, { 0, 0, -1 }, "180-theta", },
{ { 1, 3, 0 }, { -1, 0, 0 }, "270-theta", },
{ { 1, 1 * q, 0 * q }, { 1, 0, 0 }, "90-theta", },
{ { 1, 2 * q, 0 * q }, { 0, 0, -1 }, "180-theta", },
{ { 1, 3 * q, 0 * q }, { -1, 0, 0 }, "270-theta", },
{ { 1, 0, 1 }, { 0, 0, 1 }, "90-phi", },
{ { 1, 0, 2 }, { 0, 0, 1 }, "180-phi", },
{ { 1, 0, 3 }, { 0, 0, 1 }, "270-phi", },
{ { 1, 0 * q, 1 * q }, { 0, 0, 1 }, "90-phi", },
{ { 1, 0 * q, 2 * q }, { 0, 0, 1 }, "180-phi", },
{ { 1, 0 * q, 3 * q }, { 0, 0, 1 }, "270-phi", },
{ { 1, 1, 1 }, { 0, 1, 0 }, "90-theta, 90-phi" },
{ { 1, 1, 2 }, { -1, 0, 0 }, "90-theta, 180-phi" },
{ { 1, 1, 3 }, { 0, -1, 0 }, "90-theta, 270-phi" },
{ { 1, 1 * q, 1 * q }, { 0, 1, 0 }, "90-theta, 90-phi" },
{ { 1, 1 * q, 2 * q }, { -1, 0, 0 }, "90-theta, 180-phi" },
{ { 1, 1 * q, 3 * q }, { 0, -1, 0 }, "90-theta, 270-phi" },
};
for (const auto t: TESTS) {
tap.expect_eq (
util::spherical_to_cartesian (t.spherical),
t.cartesian,
for (const auto t: S2C) {
tap.expect (
all (abs (util::spherical_to_cartesian (t.spherical) - t.cartesian) < 1e-7f),
"%s, spherical-cartesian",
t.message
);
}
tap.expect_eq (
util::cartesian_to_spherical (t.cartesian),
t.spherical,
// double check origin points are transformed correctly. the zeros and
// multiple representations need to be accounted for.
tap.expect_eq (
util::cartesian_to_spherical (util::vector3f{0}).x,
0,
"origin, cartesian-spherical"
);
tap.expect_eq (
util::spherical_to_cartesian (util::vector3f{0,1,-1}),
util::vector3f{0},
"origin, cartesian-spherical"
);
//-------------------------------------------------------------------------
// dedicated cartesian-spherical test cases. it's hard to use the
// spherical-cartesian cases from above because. some of the tests don't
// have unique representations in spherical space,
// eg, {1,0,z} is always {0,0,1})
// so the reverse transform isn't usable as a test.
static constexpr struct {
util::vector3f cartesian;
util::vector3f spherical;
const char *message;
} C2S[] = {
{ { 0, 0, 1 }, { 1, 0, 0 }, "+z" },
{ { 0, 0, -1 }, { 1, q*2, 0 }, "-z" },
{ { 0, 1, 0 }, { 1, q, q }, "+y" },
{ { 0, -1, 0 }, { 1, q, -q }, "-y" },
{ { 1, 0, 0 }, { 1, q, 0 }, "+x" },
{ { -1, 0, 0 }, { 1, q, q*2 }, "-x" },
{ { 1, 1, 1 }, { 1.732f, 0.95539f, q/2 }, "+ve" },
{ { -1, -1, -1 }, { 1.732f, 2.18619942f, -2.35619f}, "-ve" },
{ { 9, 9, 9 }, { 15.5885f, 0.955317f, q/2 }, "+9ve" },
};
for (const auto &t: C2S) {
const auto s0 = util::canonical_spherical (util::cartesian_to_spherical (t.cartesian));
const auto s1 = util::canonical_spherical (t.spherical);
tap.expect (
all (abs (s0 - s1) < 1e-4f),
"%s, cartesian-spherical",
t.message
);
}
//auto s = util::cartesian_to_spherical (t.cartesian);
//std::clog << s << " == " << t.spherical << '\n';
//tap.expect_eq (
// util::cartesian_to_spherical (t.cartesian),
// t.spherical,
// "%s, cartesian-spherical",
// t.message
//);
{