From e89885a0768fe5bf6f9a2e0b93a62316053172b5 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 15 Nov 2016 17:36:21 +0000 Subject: [PATCH] Added guards against division by zero --- src/osgUtil/RayIntersector.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/osgUtil/RayIntersector.cpp b/src/osgUtil/RayIntersector.cpp index 1642af280..eaa3e7236 100644 --- a/src/osgUtil/RayIntersector.cpp +++ b/src/osgUtil/RayIntersector.cpp @@ -202,7 +202,7 @@ bool RayIntersector::intersectAndClip(Vec3d& s, const Vec3d& d, Vec3d& e, const // trivial reject of segment wholly outside if (s[i] > bb_max[i]) return false; - if (s[i] < bb_min[i]) + if ((d[i] > epsilon) && (s[i] < bb_min[i])) { // clip s to xMin double t = (bb_min[i]-s[i])/d[i] - epsilon; @@ -214,7 +214,7 @@ bool RayIntersector::intersectAndClip(Vec3d& s, const Vec3d& d, Vec3d& e, const // trivial reject of segment wholly outside if (s[i] < bb_min[i]) return false; - if (s[i] > bb_max[i]) + if ((d[i] < -epsilon) && (s[i] > bb_max[i])) { // clip s to xMax double t = (bb_max[i]-s[i])/d[i] - epsilon; @@ -231,14 +231,14 @@ bool RayIntersector::intersectAndClip(Vec3d& s, const Vec3d& d, Vec3d& e, const for (int i=0; i<3; i++) { // test direction - if (d[i] >= 0.) + if (d[i] >= epsilon) { // compute end_t based on xMax double t = (bb_max[i]-s[i])/d[i] + epsilon; if (t < end_t) end_t = t; } - else + else if (d[i] <= -epsilon) { // compute end_t based on xMin double t = (bb_min[i]-s[i])/d[i] + epsilon; @@ -247,6 +247,9 @@ bool RayIntersector::intersectAndClip(Vec3d& s, const Vec3d& d, Vec3d& e, const } } + // if we failed to clamp the end point return false + if (end_t==std::numeric_limits::infinity()) return false; + // compute e e = s + d*end_t;