From 2851429333216f0fee0a3084097050b5b8d1ee9c Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 8 Jul 2008 17:35:10 +0000 Subject: [PATCH] Added optional control of whether to use KdTrees for intersections --- include/osgUtil/IntersectionVisitor | 17 ++++++++++++++--- src/osgUtil/IntersectionVisitor.cpp | 2 ++ src/osgUtil/LineSegmentIntersector.cpp | 10 +++------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/osgUtil/IntersectionVisitor b/include/osgUtil/IntersectionVisitor index f169a520f..886ac73e9 100644 --- a/include/osgUtil/IntersectionVisitor +++ b/include/osgUtil/IntersectionVisitor @@ -46,11 +46,12 @@ class Intersector : public osg::Referenced _coordinateFrame(cf), _disabledCount(0) {} + void setCoordinateFrame(CoordinateFrame cf) { _coordinateFrame = cf; } CoordinateFrame getCoordinateFrame() const { return _coordinateFrame; } - - + + virtual Intersector* clone(osgUtil::IntersectionVisitor& iv) = 0; virtual bool enter(const osg::Node& node) = 0; @@ -72,7 +73,7 @@ class Intersector : public osg::Referenced protected: CoordinateFrame _coordinateFrame; - unsigned int _disabledCount; + unsigned int _disabledCount; }; @@ -138,6 +139,7 @@ class OSGUTIL_EXPORT IntersectionVisitor : public osg::NodeVisitor virtual void reset(); + /** Set the intersector that will be used to intersect with the scene, and to store any hits that occur.*/ void setIntersector(Intersector* intersector); @@ -148,6 +150,13 @@ class OSGUTIL_EXPORT IntersectionVisitor : public osg::NodeVisitor const Intersector* getIntersector() const { return _intersectorStack.empty() ? 0 : _intersectorStack.front().get(); } + /** Set whether the intersectors should use KdTrees when they are found on the scene graph.*/ + void setUseKdTreeWhenAvailable(bool useKdTrees) { _useKdTreesWhenAvailable; } + + /** Set whether the intersectors should use KdTrees.*/ + bool getUseKdTreeWhenAvailable() const { return _useKdTreesWhenAvailable; } + + /** Set the read callback.*/ void setReadCallback(ReadCallback* rc) { _readCallback = rc; } @@ -201,6 +210,8 @@ class OSGUTIL_EXPORT IntersectionVisitor : public osg::NodeVisitor typedef std::list< osg::ref_ptr > IntersectorStack; IntersectorStack _intersectorStack; + + bool _useKdTreesWhenAvailable; osg::ref_ptr _readCallback; diff --git a/src/osgUtil/IntersectionVisitor.cpp b/src/osgUtil/IntersectionVisitor.cpp index 9c4350eb6..010ddef8e 100644 --- a/src/osgUtil/IntersectionVisitor.cpp +++ b/src/osgUtil/IntersectionVisitor.cpp @@ -155,6 +155,8 @@ IntersectionVisitor::IntersectionVisitor(Intersector* intersector, ReadCallback* // override the default node visitor mode. setTraversalMode(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN); + _useKdTreesWhenAvailable = true; + setIntersector(intersector); setReadCallback(readCallback); diff --git a/src/osgUtil/LineSegmentIntersector.cpp b/src/osgUtil/LineSegmentIntersector.cpp index 004385386..2caeb0a5f 100644 --- a/src/osgUtil/LineSegmentIntersector.cpp +++ b/src/osgUtil/LineSegmentIntersector.cpp @@ -299,8 +299,8 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr unsigned int numKdTreeHits = 0; unsigned int numConventionalHits = 0; - osg::KdTree* kdTree = dynamic_cast(drawable->getShape()); osg::Vec3d kdTreeHit; + osg::KdTree* kdTree = iv.getUseKdTreeWhenAvailable() ? dynamic_cast(drawable->getShape()) : 0; if (kdTree) { osg::KdTree::LineSegmentIntersections intersections; @@ -343,11 +343,7 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr } } - // return; - } - else - { - // osg::notify(osg::NOTICE)<<"Not KdTree available"<tick(); @@ -423,7 +419,7 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr double timeKdTree = osg::Timer::instance()->delta_m(before_kdTree, after_kdTree); double timeConventional = osg::Timer::instance()->delta_m(after_kdTree, after_conventional); -#if 1 +#if 0 if (kdTree) { osg::notify(osg::NOTICE)<<"KdTree ("<