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

Reply via email to