Add asOccluderNode method to avoid dynamic casts in group child handling

This commit is contained in:
Capostrophic 2019-03-15 15:44:01 +03:00
parent cad7418eb2
commit da16693b4a
3 changed files with 17 additions and 4 deletions

View File

@ -42,6 +42,7 @@ class Node;
class Switch;
class Geode;
class Camera;
class OccluderNode;
/** A vector of Nodes pointers which is used to describe the path from a root node to a descendant.*/
typedef std::vector< Node* > NodePath;
@ -132,6 +133,12 @@ class OSG_EXPORT Node : public Object
* Equivalent to dynamic_cast<const Transform*>(this).*/
virtual const Transform* asTransform() const { return 0; }
/** Convert 'this' into an OccluderNode pointer if Node is an OccluderNode, otherwise return 0.
* Equivalent to dynamic_cast<OccluderNode*>(this).*/
virtual OccluderNode* asOccluderNode() { return 0; }
/** Convert 'const this' into a const OccluderNode pointer if Node is an OccluderNode, otherwise return 0.
* Equivalent to dynamic_cast<const OccluderNode*>(this).*/
virtual const OccluderNode* asOccluderNode() const { return 0; }
/** Convert 'this' into a Switch pointer if Node is a Switch, otherwise return 0.

View File

@ -34,6 +34,12 @@ class OSG_EXPORT OccluderNode : public Group
META_Node(osg, OccluderNode);
/** Convert 'this' into an OccluderNode pointer if Node is an OccluderNode, otherwise return 0.
* Equivalent to dynamic_cast<OccluderNode*>(this).*/
virtual OccluderNode* asOccluderNode() { return this; }
/** Convert 'const this' into a const OccluderNode pointer if Node is an OccluderNode, otherwise return 0.
* Equivalent to dynamic_cast<const OccluderNode*>(this).*/
virtual const OccluderNode* asOccluderNode() const { return this; }
/** Attach a ConvexPlanarOccluder to an OccluderNode.*/
void setOccluder(ConvexPlanarOccluder* occluder) { _occluder = occluder; }

View File

@ -137,7 +137,7 @@ bool Group::insertChild( unsigned int index, Node *child )
}
if (child->getNumChildrenWithOccluderNodes()>0 ||
dynamic_cast<osg::OccluderNode*>(child))
child->asOccluderNode())
{
setNumChildrenWithOccluderNodes(
getNumChildrenWithOccluderNodes()+1
@ -190,7 +190,7 @@ bool Group::removeChildren(unsigned int pos,unsigned int numChildrenToRemove)
if (child->getNumChildrenWithCullingDisabled()>0 || !child->getCullingActive()) ++numChildrenWithCullingDisabledRemoved;
if (child->getNumChildrenWithOccluderNodes()>0 || dynamic_cast<osg::OccluderNode*>(child)) ++numChildrenWithOccludersRemoved;
if (child->getNumChildrenWithOccluderNodes()>0 || child->asOccluderNode()) ++numChildrenWithOccludersRemoved;
}
@ -326,12 +326,12 @@ bool Group::setChild( unsigned int i, Node* newNode )
// so need to check and update if required.
int delta_numChildrenWithOccluderNodes = 0;
if (origNode->getNumChildrenWithOccluderNodes()>0 ||
dynamic_cast<osg::OccluderNode*>(origNode.get()))
origNode.get()->asOccluderNode())
{
--delta_numChildrenWithOccluderNodes;
}
if (newNode->getNumChildrenWithOccluderNodes()>0 ||
dynamic_cast<osg::OccluderNode*>(newNode))
newNode->asOccluderNode())
{
++delta_numChildrenWithOccluderNodes;
}