主要涉及三个类:
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 函数即可。