主要涉及三個類:
1. osgUtil::PolytopeIntersector // 具體不同算法實現類
2. osgUtil::IntersectionVisitor //用來遍歷節點樹的每個節點
3.osg::Node * mNode; // 你要做相交測試的根節點
先看用法:
osg::ref_ptr<osgUtil::PolytopeIntersector> intersector = new osgUtil::PolytopeIntersector(osgUtil::Intersector::WINDOW, xMin, yMin, xMax, yMax);
intersector->setIntersectionLimit(osgUtil::Intersector::LIMIT_ONE_PER_DRAWABLE);
osgUtil::IntersectionVisitor iv( intersector.get() );
mRootNode->accept(iv);
關係
osgUtil::IntersectionVisitor 中含有一個osgUtil::PolytopeIntersector的實例化對象,功能是主要負責遍歷每個節點,然後把每個節點的信息傳入到 osgUtil::PolytopeIntersector 中
(含有一系列的apply方法)如:
void IntersectionVisitor::apply(osg::Group& group)
{
if (!enter(group)) return;
traverse(group);
leave();
}
void IntersectionVisitor::apply(osg::Geode& geode)
{
// OSG_NOTICE<<"apply(Geode&)"<<std::endl;
if (!enter(geode)) return;
// OSG_NOTICE<<"inside apply(Geode&)"<<std::endl;
for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
{
intersect( geode.getDrawable(i) );
}
leave();
}
其中enter、leave 、intersect 函數中又會反過來調用 osgUtil::PolytopeIntersector 中相應的方法,如:
inline bool enter(const osg::Node& node) { return _intersectorStack.empty() ? false : _intersectorStack.back()->enter(node); }
inline void leave() { _intersectorStack.back()->leave(); }
inline void intersect(osg::Drawable* drawable) { _intersectorStack.back()->intersect(*this, drawable); }
inline void push_clone() { _intersectorStack.push_back ( _intersectorStack.front()->clone(*this) ); }
inline void pop_clone() { if (_intersectorStack.size()>=2) _intersectorStack.pop_back(); }
這樣幾乎所有的算法都集中到了 osgUtil::PolytopeIntersector 中 intersect 方法中
void PolytopeIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)
如果想寫自己的相交測試算法,也基本上只需重寫intersect 函數即可。