I have come up with a possible solution to my Drawable::getBound()
conundrum, it's a bit hacky/convoluted and itself might introduce
problems... but here it is for discussion, code below.

The basic idea is to pass back a temporary Bound object from
getBound() and have this provide the type conversion:


#include <osg/BoundingBox>
#include <osg/BoundingSphere>

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;

    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;

    return 1;
}

The little OSG_NOTICE output works as expected, returning a valid
BoundingSphere and NULL BoundingBox for BaseClass, and a valid
BoundingSphere and valid BoundingBox for SubClass.

It could be that we have this Bound helper class just compiled in when
users want compatibility, but can disable this if they have converted
all their code to use the Drawable::getBoundingBox() method.

Thoughts?
Robert.
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to