OpenSceneGraph/src/osg/Group.cpp
Robert Osfield f612924a45 Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying.  The old functionality of clone() which was clone of type has
been renamed to cloneType().

Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc.  A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.

Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.

Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData.  These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.

method
2002-01-28 21:17:01 +00:00

231 lines
6.1 KiB
C++

#include <stdio.h>
#include <math.h>
#include <osg/Group>
#include <osg/BoundingBox>
#include <algorithm>
#define square(x) ((x)*(x))
using namespace osg;
Group::Group()
{
}
Group::Group(const Group& group,const Cloner& cloner):
Node(group,cloner)
{
for(ChildList::const_iterator itr=group._children.begin();
itr!=group._children.end();
++itr)
{
Node* child = cloner(itr->get());
if (child) _children.push_back(child);
}
}
Group::~Group()
{
for(ChildList::iterator itr=_children.begin();
itr!=_children.end();
++itr)
{
Node* child = itr->get();
ParentList::iterator pitr = std::find(child->_parents.begin(),child->_parents.end(),this);
if (pitr!=child->_parents.end()) child->_parents.erase(pitr);
}
}
void Group::traverse(NodeVisitor& nv)
{
for(ChildList::iterator itr=_children.begin();
itr!=_children.end();
++itr)
{
(*itr)->accept(nv);
}
}
bool Group::addChild( Node *child )
{
if (child && !containsNode(child))
{
// note ref_ptr<> automatically handles incrementing child's reference count.
_children.push_back(child);
// register as parent of child.
child->_parents.push_back(this);
dirtyBound();
// could now require app traversal thanks to the new subgraph,
// so need to check and update if required.
if (child->getNumChildrenRequiringAppTraversal()>0 ||
child->getAppCallback())
{
setNumChildrenRequiringAppTraversal(
getNumChildrenRequiringAppTraversal()+1
);
}
// could now require disabling of culling thanks to the new subgraph,
// so need to check and update if required.
if (child->getNumChildrenWithCullingDisabled()>0 ||
!child->getCullingActive())
{
setNumChildrenWithCullingDisabled(
getNumChildrenWithCullingDisabled()+1
);
}
return true;
}
else return false;
}
bool Group::removeChild( Node *child )
{
ChildList::iterator itr = findNode(child);
if (itr!=_children.end())
{
// remove this group from the child parent list.
ParentList::iterator pitr = std::find(child->_parents.begin(),child->_parents.end(),this);
if (pitr!=child->_parents.end()) child->_parents.erase(pitr);
// could now require app traversal thanks to the new subgraph,
// so need to check and update if required.
// note, need to do this checking before the erase of the child
// otherwise child will be invalid.
if (child->getNumChildrenRequiringAppTraversal()>0 ||
child->getAppCallback())
{
setNumChildrenRequiringAppTraversal(
getNumChildrenRequiringAppTraversal()-1
);
}
if (child->getNumChildrenWithCullingDisabled()>0 ||
!child->getCullingActive())
{
setNumChildrenWithCullingDisabled(
getNumChildrenWithCullingDisabled()-1
);
}
// note ref_ptr<> automatically handles decrementing child's reference count.
_children.erase(itr);
dirtyBound();
return true;
}
else return false;
}
bool Group::replaceChild( Node *origNode, Node *newNode )
{
if (newNode==NULL || origNode==newNode) return false;
ChildList::iterator itr = findNode(origNode);
if (itr!=_children.end())
{
ParentList::iterator pitr = std::find(origNode->_parents.begin(),origNode->_parents.end(),this);
if (pitr!=origNode->_parents.end()) origNode->_parents.erase(pitr);
// note ref_ptr<> automatically handles decrementing origNode's reference count,
// and inccrementing newNode's reference count.
*itr = newNode;
// register as parent of child.
newNode->_parents.push_back(this);
dirtyBound();
// could now require app traversal thanks to the new subgraph,
// so need to check and update if required.
int delta_numChildrenRequiringAppTraversal = 0;
if (origNode->getNumChildrenRequiringAppTraversal()>0 ||
origNode->getAppCallback())
{
--delta_numChildrenRequiringAppTraversal;
}
if (newNode->getNumChildrenRequiringAppTraversal()>0 ||
newNode->getAppCallback())
{
++delta_numChildrenRequiringAppTraversal;
}
if (delta_numChildrenRequiringAppTraversal!=0)
{
setNumChildrenRequiringAppTraversal(
getNumChildrenRequiringAppTraversal()+delta_numChildrenRequiringAppTraversal
);
}
// could now require disabling of culling thanks to the new subgraph,
// so need to check and update if required.
int delta_numChildrenWithCullingDisabled = 0;
if (origNode->getNumChildrenWithCullingDisabled()>0 ||
!origNode->getCullingActive())
{
--delta_numChildrenWithCullingDisabled;
}
if (newNode->getNumChildrenWithCullingDisabled()>0 ||
!newNode->getCullingActive())
{
++delta_numChildrenWithCullingDisabled;
}
if (delta_numChildrenWithCullingDisabled!=0)
{
setNumChildrenWithCullingDisabled(
getNumChildrenWithCullingDisabled()-1
);
}
return true;
}
else return false;
}
const bool Group::computeBound() const
{
_bsphere_computed = true;
_bsphere.init();
if (_children.empty()) return false;
BoundingBox bb;
bb.init();
ChildList::const_iterator itr;
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
bb.expandBy((*itr)->getBound());
}
if (!bb.isValid()) return false;
_bsphere._center = bb.center();
_bsphere._radius = 0.0f;
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
_bsphere.expandRadiusBy((*itr)->getBound());
}
return true;
}