libcruft-util/test/geom/ray.cpp

62 lines
1.5 KiB
C++
Raw Normal View History

#include "geom/aabb.hpp"
#include "geom/plane.hpp"
#include "geom/ray.hpp"
#include "geom/iostream.hpp"
#include "tap.hpp"
2015-02-19 13:28:56 +11:00
using util::geom::ray2f;
using util::geom::ray3f;
2015-03-07 03:20:50 +11:00
2015-04-13 21:47:37 +10:00
//-----------------------------------------------------------------------------
2015-03-07 03:20:50 +11:00
void
2015-04-13 21:47:37 +10:00
test_intersect_plane (util::TAP::logger &tap)
2015-02-19 13:28:56 +11:00
{
2015-03-11 22:31:35 +11:00
// trivial case: origin ray facing z, plane at unit z facing -z.
const util::geom::ray3f l { .origin = 0, .direction = {0, 0, 1} };
const util::geom::plane3f p (util::point3f {0,0,1}, util::vector3f {0,0,-1});
2015-02-19 13:28:56 +11:00
tap.expect_eq (distance (l, p), 1.f, "ray-plane intersect");
2015-02-19 13:28:56 +11:00
}
2015-03-07 03:20:50 +11:00
2015-04-13 21:47:37 +10:00
//-----------------------------------------------------------------------------
2015-03-07 03:20:50 +11:00
void
2015-04-13 21:47:37 +10:00
test_intersect_aabb (util::TAP::logger &tap)
2015-03-07 03:20:50 +11:00
{
2017-08-24 16:43:54 +10:00
using util::geom::aabb2f;
2015-03-07 03:20:50 +11:00
// trivial case: unit aabb at origin, ray from (0.5,-0.5) upwards
2017-08-24 16:43:54 +10:00
const aabb2f box {
2015-03-07 03:20:50 +11:00
{ 0.f, 0.f },
{ 1.f, 1.f }
};
const ray2f forward {
util::point2f { 0.5f, -0.5f },
util::vector2f { 0.0f, 1.0f }
2015-03-07 03:20:50 +11:00
};
tap.expect_eq (distance (forward, box), 0.5f, "ray-aabb intersect");
const ray2f behind {
util::point2f { 0.5f, 2.0f },
util::vector2f { 0.0f, 1.0f }
};
tap.expect_eq (distance (behind, box), -1.f, "ray-aabb intersect behind");
2015-03-07 03:20:50 +11:00
}
2015-04-13 21:47:37 +10:00
//-----------------------------------------------------------------------------
2015-03-07 03:20:50 +11:00
int
main (void)
{
2015-04-13 16:45:56 +10:00
util::TAP::logger tap;
2015-04-13 21:47:37 +10:00
test_intersect_plane (tap);
test_intersect_aabb (tap);
return tap.status ();
2015-03-07 03:20:50 +11:00
}