From e939cca6fdb9d86f4807d18bc96783c1819fd275 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 15 Apr 2015 14:20:59 +1000 Subject: [PATCH] ray: return NaN for negative aabb intersection --- ray.cpp | 7 +++++-- test/ray.cpp | 13 ++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ray.cpp b/ray.cpp index 1910fbfa..7b37b9ab 100644 --- a/ray.cpp +++ b/ray.cpp @@ -47,7 +47,7 @@ util::ray::intersect (plane q) const /// returns the distance from origin to AABB intersection /// /// returns NaN on miss -/// returns -ve if behind +/// returns NaN if behind template T util::ray::intersect (AABB r) const @@ -61,9 +61,11 @@ util::ray::intersect (AABB r) const auto tmin = max (vmin); auto tmax = min (vmax); + // closest intersection is behind us if (tmax < 0) - return tmax; + return std::numeric_limits::quiet_NaN (); + // missed intersection if (tmin > tmax) return std::numeric_limits::quiet_NaN (); @@ -75,6 +77,7 @@ util::ray::intersect (AABB r) const /// returns the smallest distance from ray origin to a sphere intersection /// /// returns NaN on miss +/// returns NaN if behind template T util::ray::intersect (sphere s) const diff --git a/test/ray.cpp b/test/ray.cpp index a42ebeab..f3da84c2 100644 --- a/test/ray.cpp +++ b/test/ray.cpp @@ -22,17 +22,24 @@ void test_intersect_aabb (util::TAP::logger &tap) { // 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 }, { 1.f, 1.f } }; - const util::ray2f l { + const util::ray2f forward { { 0.5f, -0.5f }, { 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"); }