Added asGroup() and asTransform() methods to osg::Node to downcast nodes

to these types without requiring an expensive dynamic_cast<>.

Also added asGeometry() to osg::Drawable for the same reasons.
This commit is contained in:
Robert Osfield 2002-09-12 15:34:31 +00:00
parent b5870857c4
commit f867dd81be
6 changed files with 33 additions and 2 deletions

View File

@ -21,6 +21,7 @@ class Vec3;
class Vec4;
class UByte4;
class Node;
class Geometry;
// this is define to alter the way display lists are compiled inside the
// the draw method, it has been found that the NVidia drivers fail completely
@ -49,6 +50,12 @@ class SG_EXPORT Drawable : public Object
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "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; }
/** A vector of osg::Node pointers which is used to store the parent(s) of drawable.*/
typedef std::vector<Node*> ParentList;

View File

@ -30,6 +30,9 @@ class SG_EXPORT Geometry : public Drawable
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "Geometry"; }
virtual Geometry* asGeometry() { return this; }
virtual const Geometry* asGeometry() const { return this; }
enum AttributeBinding
{
BIND_OFF=0,

View File

@ -27,6 +27,9 @@ class SG_EXPORT Group : public Node
META_Node(osg, Group);
virtual Group* asGroup() { return this; }
virtual const Group* asGroup() const { return this; }
virtual void traverse(NodeVisitor& nv);
/** Add Node to Group.

View File

@ -17,6 +17,7 @@ namespace osg {
class NodeVisitor;
class Group;
class Transform;
/** META_Node macro define the standard clone, isSameKindAs, className
* and accept methods. Use when subclassing from Node to make it
@ -60,6 +61,20 @@ class SG_EXPORT Node : public Object
/** return the name of the node's class type.*/
virtual const char* className() const { return "Node"; }
/** 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; }
/** convert 'const this' into a const Group pointer if Node is a Group, otherwise return 0.
* Equivalent to dynamic_cast<const Group*>(this).*/
virtual const Group* asGroup() const { return 0; }
/** convert 'this' into a Transform pointer if Node is a Transform, otherwise return 0.
* Equivalent to dynamic_cast<Transform*>(this).*/
virtual Transform* asTransform() { return 0; }
/** convert 'const this' into a const Transform pointer if Node is a Transform, otherwise return 0.
* Equivalent to dynamic_cast<const Transform*>(this).*/
virtual const Transform* asTransform() const { return 0; }
/** Visitor Pattern : calls the apply method of a NodeVisitor with this node's type.*/
virtual void accept(NodeVisitor& nv);
/** Traverse upwards : calls parents' accept method with NodeVisitor.*/

View File

@ -45,6 +45,9 @@ class SG_EXPORT Transform : public Group
META_Node(osg, Transform);
virtual Transform* asTransform() { return this; }
virtual const Transform* asTransform() const { return this; }
enum ReferenceFrame
{
RELATIVE_TO_PARENTS,

View File

@ -229,7 +229,7 @@ bool Group::computeBound() const
itr!=_children.end();
++itr)
{
const osg::Transform* transform = dynamic_cast<const osg::Transform*>(itr->get());
const osg::Transform* transform = (*itr)->asTransform();
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_TO_PARENTS)
{
bb.expandBy((*itr)->getBound());
@ -244,7 +244,7 @@ bool Group::computeBound() const
itr!=_children.end();
++itr)
{
const osg::Transform* transform = dynamic_cast<const osg::Transform*>(itr->get());
const osg::Transform* transform = (*itr)->asTransform();
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_TO_PARENTS)
{
_bsphere.expandRadiusBy((*itr)->getBound());