ray: return NaN for negative aabb intersection

This commit is contained in:
Danny Robson 2015-04-15 14:20:59 +10:00
parent 012f8107c6
commit e939cca6fd
2 changed files with 15 additions and 5 deletions

View File

@ -47,7 +47,7 @@ util::ray<S,T>::intersect (plane<S,T> q) const
/// returns the distance from origin to AABB intersection /// returns the distance from origin to AABB intersection
/// ///
/// returns NaN on miss /// returns NaN on miss
/// returns -ve if behind /// returns NaN if behind
template <size_t S, typename T> template <size_t S, typename T>
T T
util::ray<S,T>::intersect (AABB<S,T> r) const util::ray<S,T>::intersect (AABB<S,T> r) const
@ -61,9 +61,11 @@ util::ray<S,T>::intersect (AABB<S,T> r) const
auto tmin = max (vmin); auto tmin = max (vmin);
auto tmax = min (vmax); auto tmax = min (vmax);
// closest intersection is behind us
if (tmax < 0) if (tmax < 0)
return tmax; return std::numeric_limits<T>::quiet_NaN ();
// missed intersection
if (tmin > tmax) if (tmin > tmax)
return std::numeric_limits<T>::quiet_NaN (); return std::numeric_limits<T>::quiet_NaN ();
@ -75,6 +77,7 @@ util::ray<S,T>::intersect (AABB<S,T> r) const
/// returns the smallest distance from ray origin to a sphere intersection /// returns the smallest distance from ray origin to a sphere intersection
/// ///
/// returns NaN on miss /// returns NaN on miss
/// returns NaN if behind
template <size_t S, typename T> template <size_t S, typename T>
T T
util::ray<S,T>::intersect (sphere<S,T> s) const util::ray<S,T>::intersect (sphere<S,T> s) const

View File

@ -22,17 +22,24 @@ void
test_intersect_aabb (util::TAP::logger &tap) test_intersect_aabb (util::TAP::logger &tap)
{ {
// trivial case: unit aabb at origin, ray from (0.5,-0.5) upwards // trivial case: unit aabb at origin, ray from (0.5,-0.5) upwards
const util::AABB2f b { const util::AABB2f box {
{ 0.f, 0.f }, { 0.f, 0.f },
{ 1.f, 1.f } { 1.f, 1.f }
}; };
const util::ray2f l { const util::ray2f forward {
{ 0.5f, -0.5f }, { 0.5f, -0.5f },
{ 0.f, 1.f } { 0.f, 1.f }
}; };
tap.expect_eq (l.intersect (b), 0.5f, "ray-aabb intersect"); tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect");
const util::ray2f behind {
{ 0.5f, 2.f },
{ 0.f, 1.f }
};
tap.expect_nan (behind.intersect (box), "ray-aabb intersect behind");
} }