Added Node::asDrawable() and Node::asGeometry() methods to provide a low cost way of casting a node to Drawable and Geoemtry.

Changed the Group::computeBound() method so that it takes account of the a Drawable's BoundingBox.
This commit is contained in:
Robert Osfield
2014-05-15 09:26:59 +00:00
parent afcf54b108
commit 20b9f3ff88
4 changed files with 35 additions and 19 deletions

View File

@@ -106,13 +106,8 @@ class OSG_EXPORT Drawable : public Node
META_Node(osg, Drawable);
/** Convert 'this' into a Geometry pointer if Drawable is a Geometry, otherwise return 0.
* Equivalent to dynamic_cast<Geometry*>(this).*/
virtual Geometry* asGeometry() { return 0; }
/** Convert 'const this' into a const Geometry pointer if Drawable is a Geometry, otherwise return 0.
* Equivalent to dynamic_cast<const Geometry*>(this).*/
virtual const Geometry* asGeometry() const { return 0; }
virtual Drawable* asDrawable() { return this; }
virtual const Drawable* asDrawable() const { return this; }
/** Compute the DataVariance based on an assessment of callback etc.*/
virtual void computeDataVariance();

View File

@@ -35,17 +35,11 @@ class OSG_EXPORT Geometry : public Drawable
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Geometry(const Geometry& geometry,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
virtual Object* cloneType() const { return new Geometry(); }
virtual Object* clone(const CopyOp& copyop) const { return new Geometry(*this,copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Geometry*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "Geometry"; }
META_Node(osg, Geometry);
virtual Geometry* asGeometry() { return this; }
virtual const Geometry* asGeometry() const { return this; }
virtual void accept(NodeVisitor& nv) { nv.apply(*this); }
bool empty() const;
typedef std::vector< osg::ref_ptr<osg::Array> > ArrayList;

View File

@@ -34,6 +34,8 @@ namespace osg {
// forcing declare classes to enable declaration of as*() methods.
class NodeVisitor;
class Drawable;
class Geometry;
class Group;
class Transform;
class Node;
@@ -132,6 +134,20 @@ class OSG_EXPORT Node : public Object
/** return the name of the node's class type.*/
virtual const char* className() const { return "Node"; }
/** convert 'this' into a Drawable pointer if Node is a Drawable, otherwise return 0.
* Equivalent to dynamic_cast<Group*>(this).*/
virtual Drawable* asDrawable() { return 0; }
/** convert 'const this' into a const Drawable pointer if Node is a Drawable, otherwise return 0.
* Equivalent to dynamic_cast<const Group*>(this).*/
virtual const Drawable* asDrawable() const { return 0; }
/** convert 'this' into a Geometry pointer if Node is a Geometry, otherwise return 0.
* Equivalent to dynamic_cast<Group*>(this).*/
virtual Geometry* asGeometry() { return 0; }
/** convert 'const this' into a const Geometry pointer if Node is a Geometry, otherwise return 0.
* Equivalent to dynamic_cast<const Group*>(this).*/
virtual const Geometry* asGeometry() const { return 0; }
/** convert 'this' into a Group pointer if Node is a Group, otherwise return 0.
* Equivalent to dynamic_cast<Group*>(this).*/
virtual Group* asGroup() { return 0; }

View File

@@ -15,6 +15,7 @@
#include <osg/BoundingBox>
#include <osg/Transform>
#include <osg/OccluderNode>
#include <osg/Drawable>
#include <osg/Notify>
#include <stdio.h>
@@ -366,11 +367,20 @@ BoundingSphere Group::computeBound() const
itr!=_children.end();
++itr)
{
const osg::Transform* transform = (*itr)->asTransform();
osg::Node* child = itr->get();
const osg::Transform* transform = child->asTransform();
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
{
const osg::BoundingSphere& bs = (*itr)->getBound();
bb.expandBy(bs);
osg::Drawable* drawable = child->asDrawable();
if (drawable)
{
bb.expandBy(drawable->getBoundingBox());
}
else
{
const osg::BoundingSphere& bs = child->getBound();
bb.expandBy(bs);
}
}
}
@@ -385,10 +395,11 @@ BoundingSphere Group::computeBound() const
itr!=_children.end();
++itr)
{
const osg::Transform* transform = (*itr)->asTransform();
osg::Node* child = itr->get();
const osg::Transform* transform = child->asTransform();
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
{
const BoundingSphere& bs = (*itr)->getBound();
const BoundingSphere& bs = child->getBound();
bsphere.expandRadiusBy(bs);
}
}