OpenSceneGraph/src/osg/Group.cpp

141 lines
2.9 KiB
C++
Raw Normal View History

2001-01-11 00:32:10 +08:00
#include <stdio.h>
#include <math.h>
#include "osg/Group"
#include "osg/BoundingBox"
#include <algorithm>
// #ifdef __sgi
// using std::find;
// using std::for_each;
// using std::string;
// #endif
#define square(x) ((x)*(x))
using namespace osg;
Group::Group()
{
}
Group::~Group()
{
for(ChildList::iterator itr=_children.begin();
itr!=_children.end();
++itr)
2001-01-11 00:32:10 +08:00
{
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)
2001-01-11 00:32:10 +08:00
{
(*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();
return true;
}
else return false;
}
2001-01-11 00:32:10 +08:00
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);
2001-01-11 00:32:10 +08:00
// note ref_ptr<> automatically handles decrementing child's reference count.
_children.erase(itr);
dirtyBound();
return true;
}
else return false;
}
2001-01-11 00:32:10 +08:00
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);
2001-01-11 00:32:10 +08:00
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();
return true;
}
else return false;
}
const bool Group::computeBound() const
2001-01-11 00:32:10 +08:00
{
_bsphere_computed = true;
_bsphere.init();
if (_children.empty()) return false;
2001-01-11 00:32:10 +08:00
BoundingBox bb;
bb.init();
ChildList::const_iterator itr;
2001-01-11 00:32:10 +08:00
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
bb.expandBy((*itr)->getBound());
}
2001-01-11 00:32:10 +08:00
if (!bb.isValid()) return false;
2001-01-11 00:32:10 +08:00
_bsphere._center = bb.center();
_bsphere._radius = 0.0f;
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
_bsphere.expandRadiusBy((*itr)->getBound());
}
2001-01-11 00:32:10 +08:00
return true;
}