The structure here is
osg::MatrixTransform
|
osg::Geode
|
several drawables
how can i get AABB bounding box from osg::MatrixTransform
?
The structure here is
osg::MatrixTransform
|
osg::Geode
|
several drawables
how can i get AABB bounding box from osg::MatrixTransform
?
There's not direct method, as MatrixTransform only exposes a getter for the Bounding Sphere, BoundingBox is available only on Drawable class and derivatives.
With your scene graph structure you could collect all the drawables and expand a bounding box to include every drawable's BB with this method.
This will give you a BB which includes all of the others in the drawables coordinates. If you need the world coords, you'll have to apply the MatrixTransform (and the other transformation you might have along the nodepath to the root of the graph)
To calculate the bounding box in the coordinate frame of an osg::MatrixTransform t
:
#include <osg/ComputeBoundsVisitor>
osg::ComputeBoundsVisitor cbv;
t->accept(cbv);
osg::BoundingBox bb = cbv.getBoundingBox(); // in local coords.
You can then e.g. get the size and center in local coordinates:
osg::Vec3 size(bb.xMax() - bb.xMin(), bb.yMax() - bb.yMin(), bb.zMax() - bb.zMin());
osg::Vec3 center(bb.xMin() + size.x()/2.0f, bb.yMin() + size.y()/2.0f, bb.zMin() + size.z()/2.0f);
To convert to world coordinates, you need to get the localToWorld transform for the nodepath from t
to the root, and then you can transform any of the coordinates into the world reference frame:
osg::Matrix localToWorld = osg::computeLocalToWorld(t->getParentalNodePaths().front());
osg::Vec3 centerWorld = center * localToWorld;