From 20b9f3ff88056c2d6db32ec202e1c64eedda0220 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 15 May 2014 09:26:59 +0000 Subject: [PATCH] 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. --- include/osg/Drawable | 9 ++------- include/osg/Geometry | 8 +------- include/osg/Node | 16 ++++++++++++++++ src/osg/Group.cpp | 21 ++++++++++++++++----- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/include/osg/Drawable b/include/osg/Drawable index 6b5e02586..d75d7b4ca 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -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(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(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(); diff --git a/include/osg/Geometry b/include/osg/Geometry index 384ac6b6d..f1ab73537 100644 --- a/include/osg/Geometry +++ b/include/osg/Geometry @@ -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(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 > ArrayList; diff --git a/include/osg/Node b/include/osg/Node index 6cefbc0fe..314299abe 100644 --- a/include/osg/Node +++ b/include/osg/Node @@ -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(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(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(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(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(this).*/ virtual Group* asGroup() { return 0; } diff --git a/src/osg/Group.cpp b/src/osg/Group.cpp index d792c9430..750d6e277 100644 --- a/src/osg/Group.cpp +++ b/src/osg/Group.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -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); } }