#include <osg/NodeVisitor>
#include <osg/BoundingBox>
#include <osg/BoundingSphere>
#include <osg/MatrixTransform>
#include <osg/Billboard>
class CcalculateBoundingBox : public osg::NodeVisitor {
public:
CcalculateBoundingBox() : NodeVisitor( NodeVisitor::TRAVERSE_ALL_CHILDREN ) {
m_transformMatrix.makeIdentity();
}
virtual ~CcalculateBoundingBox() {}
virtual
void apply( osg::Geode &geode ) {
osg::BoundingBox bbox;
for( unsigned int i = 0; i < geode.getNumDrawables(); ++i ){
bbox.expandBy( geode.getDrawable( i )->getBound());
}
osg::BoundingBox bboxTrans;
for( unsigned int i = 0; i < 8; ++i ) {
osg::Vec3 xvec = bbox.corner( i ) * m_transformMatrix;
bboxTrans.expandBy( xvec );
}
m_boundingBox.expandBy( bboxTrans );
traverse( geode );
}
virtual
void apply( osg::MatrixTransform &node ) {
m_transformMatrix *= node.getMatrix();
traverse( node );
}
virtual
void apply( osg::Billboard &node ){
traverse( node );
}
osg::BoundingBox &getBoundBox { return m_boundingBox; }
protected :
osg::BoundingBox m_boundingBox;
osg::Matrix m_transformMatrix;
};
|