The center() method exists in the BoundingBox interface. With the Bound
interface you provide, this code would throw a seg fault:
og::Vec3 c = geometry->getBound().center();
In order to avoid any modification of user code, the center method (and
all the methods defined in the BoundingBox and the BoundingSphere
classes) has to be redefined in the helper class:
osg::Vec3 Bound::center() const
{
if (bb) return bb->center();
return bs->center();
}
But it's a pity to return a new instance of Vec3 for BoundingSphere (it
should be a const reference).
Lionel.
On 13/05/2014 19:02, Robert Osfield wrote:
I have made a further tweak to my experimental Bound class, adding
center(), radius() from BoundingSphere and xMin(), yMin() etc. methods
from BoundingBox to help make it easier for Bound to act more like a
BoundingSphrere or BoundingBox.
In the code below I provide a segment that deliberately tries to
access BoundingBox methods from a BaseClass object that doesn't have a
BoundingBox so it accesses a NULL and creates a seg fault. One could
potentially compute xMin() from a BoundingSphere, or we could throw an
exception. I'm not quite sure how much effort to make with this
temporary helper class though as it's only meant as a temporary helper
class to add porting to new versions of the OSG.
Thoughts?
Robert.
Latest Bound implementation:
struct Bound
{
Bound():
bb(0),
bs(0) {}
Bound(const osg::BoundingSphere& bs):
bb(0),
bs(&bs) {}
Bound(const osg::BoundingBox& bb):
bb(&bb),
bs(0) {}
Bound(const osg::BoundingSphere& bs, const osg::BoundingBox& bb):
bb(&bb),
bs(&bs) {}
const osg::BoundingBox* bb;
const osg::BoundingSphere* bs;
const osg::Vec3& center() const { return bs->center(); }
float radius() const { return bs->radius(); }
float xMin() const { return bb->xMin(); }
float yMin() const { return bb->yMin(); }
float zMin() const { return bb->zMin(); }
float xMax() const { return bb->xMax(); }
float yMax() const { return bb->yMax(); }
float zMax() const { return bb->zMax(); }
operator const osg::BoundingBox& () const { return *bb; }
operator const osg::BoundingSphere& () const { return *bs; }
};
struct BaseClass
{
osg::BoundingSphere _bs;
virtual Bound getBound() const { return Bound(_bs); }
};
struct SubClass : public BaseClass
{
osg::BoundingBox _bb;
virtual Bound getBound() const { return Bound(_bs, _bb); }
};
int main(int, char**)
{
BaseClass object1;
SubClass object2;
const osg::BoundingSphere& bs1 = object1.getBound();
const osg::BoundingBox& bb1 = object1.getBound();
OSG_NOTICE<<"bs1 = "<<& bs1<<std::endl;
OSG_NOTICE<<"bb1 = "<<& bb1<<std::endl;
const osg::BoundingSphere& bs2 = object2.getBound();
const osg::BoundingBox& bb2 = object2.getBound();
OSG_NOTICE<<"bs2 = "<<& bs2<<std::endl;
OSG_NOTICE<<"bb2 = "<<& bb2<<std::endl;
OSG_NOTICE<<"center2 = "<<object2.getBound().center()<<std::endl;
OSG_NOTICE<<"radius2 = "<<object2.getBound().radius()<<std::endl;
OSG_NOTICE<<"xMin2 = "<<object2.getBound().xMin()<<std::endl;
OSG_NOTICE<<"xMax2 = "<<object2.getBound().xMax()<<std::endl;
OSG_NOTICE<<"xMin1 = "<<object1.getBound().xMin()<<std::endl;
OSG_NOTICE<<"xMax1 = "<<object1.getBound().xMax()<<std::endl;
return 1;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org