From fcc8426226b238234a79b1cf04a431987f1cf8e6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 4 Aug 2008 08:45:08 +0000 Subject: [PATCH] Added a small epsilon expansion of the bounding box being clipped/intersected against, to avoid numberic errors causing problems. --- src/osgUtil/LineSegmentIntersector.cpp | 97 +++++++++++++------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/src/osgUtil/LineSegmentIntersector.cpp b/src/osgUtil/LineSegmentIntersector.cpp index fba241f5f..70124e7a8 100644 --- a/src/osgUtil/LineSegmentIntersector.cpp +++ b/src/osgUtil/LineSegmentIntersector.cpp @@ -287,16 +287,6 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr if (iv.getDoDummyTraversal()) return; - double epsilon = 1e-8; - if ((s-e).length()(drawable->getShape()) : 0; if (kdTree) { @@ -460,43 +450,56 @@ bool LineSegmentIntersector::intersects(const osg::BoundingSphere& bs) return true; } -bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb) +bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bbInput) { + osg::Vec3d bb_min(bbInput._min); + osg::Vec3d bb_max(bbInput._max); + +#if 1 + double epsilon = 1e-4; + bb_min.x() -= epsilon; + bb_min.y() -= epsilon; + bb_min.z() -= epsilon; + bb_max.x() += epsilon; + bb_max.y() += epsilon; + bb_max.z() += epsilon; +#endif + // compate s and e against the xMin to xMax range of bb. if (s.x()<=e.x()) { // trivial reject of segment wholely outside. - if (e.x()bb.xMax()) return false; + if (e.x()bb_max.x()) return false; - if (s.x()bb.xMax()) + if (e.x()>bb_max.x()) { // clip e to xMax. - e = s+(e-s)*(bb.xMax()-s.x())/(e.x()-s.x()); + e = s+(e-s)*(bb_max.x()-s.x())/(e.x()-s.x()); } } else { - if (s.x()bb.xMax()) return false; + if (s.x()bb_max.x()) return false; - if (e.x()bb.xMax()) + if (s.x()>bb_max.x()) { // clip e to xMax. - s = s+(e-s)*(bb.xMax()-s.x())/(e.x()-s.x()); + s = s+(e-s)*(bb_max.x()-s.x())/(e.x()-s.x()); } } @@ -505,36 +508,36 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const { // trivial reject of segment wholely outside. - if (e.y()bb.yMax()) return false; + if (e.y()bb_max.y()) return false; - if (s.y()bb.yMax()) + if (e.y()>bb_max.y()) { // clip e to yMax. - e = s+(e-s)*(bb.yMax()-s.y())/(e.y()-s.y()); + e = s+(e-s)*(bb_max.y()-s.y())/(e.y()-s.y()); } } else { - if (s.y()bb.yMax()) return false; + if (s.y()bb_max.y()) return false; - if (e.y()bb.yMax()) + if (s.y()>bb_max.y()) { // clip e to yMax. - s = s+(e-s)*(bb.yMax()-s.y())/(e.y()-s.y()); + s = s+(e-s)*(bb_max.y()-s.y())/(e.y()-s.y()); } } @@ -543,36 +546,36 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const { // trivial reject of segment wholely outside. - if (e.z()bb.zMax()) return false; + if (e.z()bb_max.z()) return false; - if (s.z()bb.zMax()) + if (e.z()>bb_max.z()) { // clip e to zMax. - e = s+(e-s)*(bb.zMax()-s.z())/(e.z()-s.z()); + e = s+(e-s)*(bb_max.z()-s.z())/(e.z()-s.z()); } } else { - if (s.z()bb.zMax()) return false; + if (s.z()bb_max.z()) return false; - if (e.z()bb.zMax()) + if (s.z()>bb_max.z()) { // clip e to zMax. - s = s+(e-s)*(bb.zMax()-s.z())/(e.z()-s.z()); + s = s+(e-s)*(bb_max.z()-s.z())/(e.z()-s.z()); } }