diff --git a/src/osgUtil/LineSegmentIntersector.cpp b/src/osgUtil/LineSegmentIntersector.cpp index 04cfc8eee..7ffa743b5 100644 --- a/src/osgUtil/LineSegmentIntersector.cpp +++ b/src/osgUtil/LineSegmentIntersector.cpp @@ -484,20 +484,11 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const 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 + double epsilon = 1e-6; // 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_max.x()) return false; @@ -505,13 +496,15 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const if (s.x()0.0) s = s + (e-s)*r; } if (e.x()>bb_max.x()) { // clip e to xMax. - e = s+(e-s)*(bb_max.x()-s.x())/(e.x()-s.x()); + double r = (bb_max.x()-s.x())/(e.x()-s.x()) + epsilon; + if (r<1.0) e = s+(e-s)*r; } } else @@ -521,21 +514,22 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const if (e.x()0.0) e = e + (s-e)*r; } if (s.x()>bb_max.x()) { - // clip e to xMax. - s = s+(e-s)*(bb_max.x()-s.x())/(e.x()-s.x()); + // clip s to xMax. + double r = (bb_max.x()-e.x())/(s.x()-e.x()) + epsilon; + if (r<1.0) s = e + (s-e)*r; } } // compate s and e against the yMin to yMax range of bb. if (s.y()<=e.y()) { - // trivial reject of segment wholely outside. if (e.y()bb_max.y()) return false; @@ -543,13 +537,15 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const if (s.y()0.0) s = s + (e-s)*r; } if (e.y()>bb_max.y()) { // clip e to yMax. - e = s+(e-s)*(bb_max.y()-s.y())/(e.y()-s.y()); + double r = (bb_max.y()-s.y())/(e.y()-s.y()) + epsilon; + if (r<1.0) e = s+(e-s)*r; } } else @@ -559,21 +555,22 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const if (e.y()0.0) e = e + (s-e)*r; } if (s.y()>bb_max.y()) { - // clip e to yMax. - s = s+(e-s)*(bb_max.y()-s.y())/(e.y()-s.y()); + // clip s to yMax. + double r = (bb_max.y()-e.y())/(s.y()-e.y()) + epsilon; + if (r<1.0) s = e + (s-e)*r; } } // compate s and e against the zMin to zMax range of bb. if (s.z()<=e.z()) { - // trivial reject of segment wholely outside. if (e.z()bb_max.z()) return false; @@ -581,13 +578,15 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const if (s.z()0.0) s = s + (e-s)*r; } if (e.z()>bb_max.z()) { // clip e to zMax. - e = s+(e-s)*(bb_max.z()-s.z())/(e.z()-s.z()); + double r = (bb_max.z()-s.z())/(e.z()-s.z()) + epsilon; + if (r<1.0) e = s+(e-s)*r; } } else @@ -597,14 +596,16 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const if (e.z()0.0) e = e + (s-e)*r; } if (s.z()>bb_max.z()) { - // clip e to zMax. - s = s+(e-s)*(bb_max.z()-s.z())/(e.z()-s.z()); + // clip s to zMax. + double r = (bb_max.z()-e.z())/(s.z()-e.z()) + epsilon; + if (r<1.0) s = e + (s-e)*r; } } @@ -619,7 +620,7 @@ osg::Texture* LineSegmentIntersector::Intersection::getTextureLookUp(osg::Vec3& { osg::Geometry* geometry = drawable.valid() ? drawable->asGeometry() : 0; osg::Vec3Array* vertices = geometry ? dynamic_cast(geometry->getVertexArray()) : 0; - + if (vertices) { if (indexList.size()==3 && ratioList.size()==3) @@ -718,7 +719,7 @@ osg::Texture* LineSegmentIntersector::Intersection::getTextureLookUp(osg::Vec3& } return const_cast(activeTexture); - + } return 0; }