2015-10-13 18:19:47 +11:00
|
|
|
#include "geom/aabb.hpp"
|
|
|
|
#include "geom/plane.hpp"
|
|
|
|
#include "geom/ray.hpp"
|
2015-04-13 16:45:56 +10:00
|
|
|
#include "tap.hpp"
|
2015-02-19 13:28:56 +11:00
|
|
|
|
2015-10-13 18:19:47 +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.
|
2017-08-22 15:46:49 +10:00
|
|
|
const util::geom::ray3f l (util::point3f {0,0,0}, util::vector3f {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
|
|
|
|
2015-04-13 21:47:37 +10:00
|
|
|
tap.expect_eq (l.intersect (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
|
|
|
{
|
2015-10-13 18:19:47 +11: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
|
2015-10-13 18:19:47 +11:00
|
|
|
const AABB2f box {
|
2015-03-07 03:20:50 +11:00
|
|
|
{ 0.f, 0.f },
|
|
|
|
{ 1.f, 1.f }
|
|
|
|
};
|
|
|
|
|
2015-10-13 18:19:47 +11:00
|
|
|
const ray2f forward {
|
2017-08-22 15:46:49 +10:00
|
|
|
util::point2f { 0.5f, -0.5f },
|
|
|
|
util::vector2f { 0.f, 1.f }
|
2015-03-07 03:20:50 +11:00
|
|
|
};
|
|
|
|
|
2015-04-15 14:20:59 +10:00
|
|
|
tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect");
|
|
|
|
|
2015-10-13 18:19:47 +11:00
|
|
|
const ray2f behind {
|
2017-08-22 15:46:49 +10:00
|
|
|
util::point2f { 0.5f, 2.f },
|
|
|
|
util::vector2f { 0.f, 1.f }
|
2015-04-15 14:20:59 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
tap.expect_nan (behind.intersect (box), "ray-aabb intersect behind");
|
2015-03-07 03:20:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-13 21:47:37 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-04-13 16:45:56 +10:00
|
|
|
void
|
2015-04-13 21:47:37 +10:00
|
|
|
test_intersect_sphere (util::TAP::logger &tap)
|
2015-04-13 16:45:56 +10:00
|
|
|
{
|
2015-10-13 18:19:47 +11:00
|
|
|
using util::geom::sphere3f;
|
|
|
|
|
|
|
|
const sphere3f s = {{0.f, 0.f, 0.f}, 1.f};
|
2015-04-13 21:47:51 +10:00
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
const ray3f r0 {util::point3f {0.f, 2.f, 0.f}, util::vector3f {0.f, -1.f, 0.f}};
|
2015-04-13 21:47:51 +10:00
|
|
|
tap.expect_eq (r0.intersect (s), 1.f, "ray-sphere simple");
|
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
const ray3f r1 {util::point3f {0.f, 1.f, 0.f}, util::vector3f {0.f, 1.f, 0.f}};
|
2015-04-13 21:47:51 +10:00
|
|
|
tap.expect_eq (r1.intersect (s), 0.f, "ray-sphere adjacent");
|
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
const ray3f r2 {util::point3f {0.f, 2.f, 0.f}, util::vector3f {0.f, 1.f, 0.f}};
|
2015-04-13 21:47:51 +10:00
|
|
|
tap.expect_nan (r2.intersect (s), "ray-sphere no-intersect");
|
2015-04-13 16:45:56 +10: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);
|
|
|
|
test_intersect_sphere (tap);
|
|
|
|
|
|
|
|
return tap.status ();
|
2015-03-07 03:20:50 +11:00
|
|
|
}
|