diff --git a/src/osgUtil/PolytopeIntersector.cpp b/src/osgUtil/PolytopeIntersector.cpp index 7e2535c9a..957fe56e5 100644 --- a/src/osgUtil/PolytopeIntersector.cpp +++ b/src/osgUtil/PolytopeIntersector.cpp @@ -565,12 +565,16 @@ void PolytopeIntersector::leave() struct IntersectFunctor { - IntersectFunctor(osgUtil::PolytopeIntersector* pi, osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable): - _polytopeIntersector(pi), - _iv(iv), - _drawable(drawable) - { + typedef std::vector Vertices; + IntersectFunctor(): + _polytopeIntersector(0), + _iv(0), + _drawable(0), + _primitiveIndex(0) + { + src.reserve(10); + dest.reserve(10); } bool enter(const osg::BoundingBox& bb) @@ -591,26 +595,50 @@ struct IntersectFunctor _polytopeIntersector->getPolytope().popCurrentMask(); } - typedef std::vector Vertices; - Vertices src, dest; + void addIntersection() + { + osg::Vec3d center(0.0,0.0,0.0); + double maxDistance = -DBL_MAX; + const osg::Plane& referencePlane = _polytopeIntersector->getReferencePlane(); + for(Vertices::iterator itr = src.begin(); + itr != src.end(); + ++itr) + { + center += *itr; + double d = referencePlane.distance(*itr); + if (d>maxDistance) maxDistance = d; - bool contains(const osg::Vec3f& v0, const osg::Vec3f& v1, const osg::Vec3f& v2) + } + + center /= double(src.size()); + + PolytopeIntersector::Intersection intersection; + intersection.primitiveIndex = _primitiveIndex; + intersection.distance = referencePlane.distance(center); + intersection.maxDistance = maxDistance; + intersection.nodePath = _iv->getNodePath(); + intersection.drawable = _drawable; + intersection.matrix = _iv->getModelMatrix(); + intersection.localIntersectionPoint = center; + + if (src.size() func; - func.setPolytope( _polytope, _referencePlane ); - func.setDimensionMask( _dimensionMask ); - func.setLimitOneIntersection( _intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE ); - - drawable->accept(func); - - if (func.intersections.empty()) return; - - - for(PolytopeIntersectorUtils::Intersections::const_iterator it=func.intersections.begin(); - it!=func.intersections.end(); - ++it) + else +#if 1 { - const PolytopeIntersectorUtils::PolytopeIntersection& intersection = *it; - - Intersection hit; - hit.distance = intersection._distance; - hit.maxDistance = intersection._maxDistance; - hit.primitiveIndex = intersection._index; - hit.nodePath = iv.getNodePath(); - hit.drawable = drawable; - hit.matrix = iv.getModelMatrix(); - - PolytopeIntersectorUtils::Vec3_type center; - for (unsigned int i=0; iaccept(intersector); } +#else + { + osg::TemplatePrimitiveFunctor func; + func.setPolytope( _polytope, _referencePlane ); + func.setDimensionMask( _dimensionMask ); + func.setLimitOneIntersection( _intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE ); + + drawable->accept(func); + + if (func.intersections.empty()) return; + + + for(PolytopeIntersectorUtils::Intersections::const_iterator it=func.intersections.begin(); + it!=func.intersections.end(); + ++it) + { + const PolytopeIntersectorUtils::PolytopeIntersection& intersection = *it; + + Intersection hit; + hit.distance = intersection._distance; + hit.maxDistance = intersection._maxDistance; + hit.primitiveIndex = intersection._index; + hit.nodePath = iv.getNodePath(); + hit.drawable = drawable; + hit.matrix = iv.getModelMatrix(); + + PolytopeIntersectorUtils::Vec3_type center; + for (unsigned int i=0; i