libcruft-util/test/geom/aabb.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

#include "geom/aabb.hpp"
2018-05-03 17:33:42 +10:00
#include "geom/ray.hpp"
2015-04-09 14:05:01 +10:00
2015-04-13 16:45:56 +10:00
#include "tap.hpp"
#include <tuple>
2015-04-09 14:05:01 +10:00
int
main (int, char**)
{
cruft::TAP::logger tap;
2018-05-03 17:33:42 +10:00
{
const auto upright = normalised (cruft::vector2f {1});
2018-05-03 17:33:42 +10:00
static struct {
cruft::point2f lo;
cruft::point2f hi;
2018-05-03 17:33:42 +10:00
cruft::point2f base;
cruft::vector2f direction;
2018-05-03 17:33:42 +10:00
float distance;
const char *message;
} const TESTS[] {
{
{ 0, 0 }, { 1, 1 },
{ 0, 0 }, upright,
std::sqrt (2.f),
"ray on aabb corner, pointing to opposite corner",
},
{
{ 0, 0 }, { 1, 1 },
{ 0, 1 }, upright,
INFINITY,
"ray on aabb implied corner, pointing away",
},
2018-05-03 17:33:42 +10:00
{
{ 1, 1 }, { 9, 9 },
{ 2, 2 }, upright,
2018-05-03 17:33:42 +10:00
std::sqrt (7.f * 7 * 2),
"ray inside aabb, close to one corner and pointing to far corner",
},
2018-05-03 17:33:42 +10:00
};
for (auto const &t: TESTS) {
cruft::geom::aabb2f const shape (t.lo, t.hi);
cruft::geom::ray2f const r {t.base, t.direction};
2018-05-03 17:33:42 +10:00
tap.expect_eq (distance (r, shape), t.distance, "%!", t.message);
}
};
return tap.status ();
2015-04-09 14:05:01 +10:00
}