diff --git a/include/osg/KdTree b/include/osg/KdTree index 19b9fed22..24d1995e0 100644 --- a/include/osg/KdTree +++ b/include/osg/KdTree @@ -176,10 +176,12 @@ class OSG_EXPORT KdTree : public osg::Shape functor.intersect(_vertices.get(), i, tri.p0, tri.p1, tri.p2); } } - else if (functor.intersect(node.bb)) + else if (functor.enter(node.bb)) { if (node.first>0) intersect(functor, _kdNodes[node.first]); if (node.second>0) intersect(functor, _kdNodes[node.second]); + + functor.leave(); } } diff --git a/include/osg/Polytope b/include/osg/Polytope index aa3d10097..c3dceceb3 100644 --- a/include/osg/Polytope +++ b/include/osg/Polytope @@ -390,8 +390,8 @@ class OSG_EXPORT Polytope if (_resultMask&selector_mask) { itr->transformProvidingInverse(matrix); - selector_mask <<= 1; } + selector_mask <<= 1; } } diff --git a/src/osg/Polytope.cpp b/src/osg/Polytope.cpp index 65e876af2..69473caa7 100644 --- a/src/osg/Polytope.cpp +++ b/src/osg/Polytope.cpp @@ -18,9 +18,85 @@ using namespace osg; bool Polytope::contains(const osg::Vec3f& v0, const osg::Vec3f& v1, const osg::Vec3f& v2) const { - if (contains(v0)) return true; - if (contains(v1)) return true; - if (contains(v2)) return true; + if (!_maskStack.back()) return true; - return false; + // initialize the set of vertices to test. + typedef std::vector Vertices; + + Vertices src, dest; + src.reserve(4+_planeList.size()); + dest.reserve(4+_planeList.size()); + + src.push_back(v0); + src.push_back(v1); + src.push_back(v2); + src.push_back(v0); + + ClippingMask resultMask = _maskStack.back(); + ClippingMask selector_mask = 0x1; + + for(PlaneList::const_iterator pitr = _planeList.begin(); + pitr != _planeList.end(); + ++pitr) + { + if (resultMask&selector_mask) + { + //OSG_NOTICE<<"Polytope::contains() Plane testing"<=0.0) + { + dest.push_back(*v_previous); + + } + + if (d_previous*d_current<0.0) + { + // edge crosses plane so insert the vertex between them. + double distance = d_previous-d_current; + double r_current = d_previous/distance; + osg::Vec3d v_new = (*v_previous)*(1.0-r_current) + (*v_current)*r_current; + dest.push_back(v_new); + } + + d_previous = d_current; + v_previous = v_current; + + } + + if (d_previous>=0.0) + { + dest.push_back(*v_previous); + } + + if (dest.size()<=1) + { + // OSG_NOTICE<<"Polytope::contains() All points on triangle culled, dest.size()="<