2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-22 05:27:40 +08:00
|
|
|
#include <osg/Switch>
|
2004-12-18 04:51:55 +08:00
|
|
|
#include <osg/BoundingBox>
|
|
|
|
#include <osg/Transform>
|
2005-12-09 19:22:09 +08:00
|
|
|
#include <osg/Notify>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2002-10-08 04:17:57 +08:00
|
|
|
Switch::Switch():
|
|
|
|
_newChildDefaultValue(true)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-01-29 22:04:06 +08:00
|
|
|
Switch::Switch(const Switch& sw,const CopyOp& copyop):
|
|
|
|
Group(sw,copyop),
|
2002-10-08 04:17:57 +08:00
|
|
|
_newChildDefaultValue(sw._newChildDefaultValue),
|
2002-10-04 22:50:33 +08:00
|
|
|
_values(sw._values)
|
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-29 05:17:01 +08:00
|
|
|
{
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
void Switch::traverse(NodeVisitor& nv)
|
|
|
|
{
|
2002-12-08 05:18:12 +08:00
|
|
|
if (nv.getTraversalMode()==NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-12-08 05:18:12 +08:00
|
|
|
for(unsigned int pos=0;pos<_children.size();++pos)
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
2002-12-08 05:18:12 +08:00
|
|
|
if (_values[pos]) _children[pos]->accept(nv);
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
2002-12-08 05:18:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Group::traverse(nv);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:50:33 +08:00
|
|
|
bool Switch::addChild( Node *child )
|
2002-10-08 04:17:57 +08:00
|
|
|
{
|
2005-02-22 18:08:01 +08:00
|
|
|
if (Group::addChild(child))
|
|
|
|
{
|
|
|
|
if (_children.size()>_values.size())
|
2005-06-24 23:34:46 +08:00
|
|
|
{
|
|
|
|
_values.resize(_children.size(),_newChildDefaultValue);
|
|
|
|
}
|
2005-02-22 18:08:01 +08:00
|
|
|
// note, we don't override any pre-existing _values[childPosition] setting
|
|
|
|
// like in addChild(child,value) below.
|
2005-06-24 23:34:46 +08:00
|
|
|
return true;
|
2005-02-22 18:08:01 +08:00
|
|
|
}
|
|
|
|
return false;
|
2002-10-08 04:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::addChild( Node *child, bool value )
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
2002-10-30 18:07:16 +08:00
|
|
|
unsigned int childPosition = _children.size();
|
2002-10-04 22:50:33 +08:00
|
|
|
if (Group::addChild(child))
|
|
|
|
{
|
2002-10-30 18:07:16 +08:00
|
|
|
if (_children.size()>_values.size())
|
2005-06-24 23:34:46 +08:00
|
|
|
{
|
|
|
|
_values.resize(_children.size(),_newChildDefaultValue);
|
|
|
|
}
|
|
|
|
_values[childPosition]=value;
|
|
|
|
return true;
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-11-20 20:03:51 +08:00
|
|
|
bool Switch::insertChild( unsigned int index, Node *child )
|
|
|
|
{
|
|
|
|
return insertChild(index,child,_newChildDefaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::insertChild( unsigned int index, Node *child, bool value )
|
|
|
|
{
|
|
|
|
if (Group::insertChild(index,child))
|
|
|
|
{
|
|
|
|
if (index>=_values.size())
|
2005-06-24 23:34:46 +08:00
|
|
|
{
|
|
|
|
_values.push_back(value);
|
|
|
|
}
|
2003-11-20 20:03:51 +08:00
|
|
|
else
|
|
|
|
{
|
2005-06-24 23:34:46 +08:00
|
|
|
_values.insert(_values.begin()+index, value);
|
2003-11-20 20:03:51 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-06-24 23:34:46 +08:00
|
|
|
return true;
|
2003-11-20 20:03:51 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-05-02 17:45:31 +08:00
|
|
|
bool Switch::removeChildren(unsigned int pos,unsigned int numChildrenToRemove)
|
2005-12-16 19:04:33 +08:00
|
|
|
{
|
2006-05-02 17:45:31 +08:00
|
|
|
if (pos<_values.size()) _values.erase(_values.begin()+pos, osg::minimum(_values.begin()+(pos+numChildrenToRemove), _values.end()) );
|
2005-12-09 19:22:09 +08:00
|
|
|
|
2006-05-02 17:45:31 +08:00
|
|
|
return Group::removeChildren(pos,numChildrenToRemove);
|
2005-12-09 19:22:09 +08:00
|
|
|
}
|
|
|
|
|
2002-10-04 22:50:33 +08:00
|
|
|
void Switch::setValue(unsigned int pos,bool value)
|
|
|
|
{
|
2002-10-08 04:17:57 +08:00
|
|
|
if (pos>=_values.size()) _values.resize(pos+1,_newChildDefaultValue);
|
2002-10-04 22:50:33 +08:00
|
|
|
_values[pos]=value;
|
2005-06-24 23:34:46 +08:00
|
|
|
dirtyBound();
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
|
|
|
|
2002-12-08 05:18:12 +08:00
|
|
|
void Switch::setChildValue(const Node* child,bool value)
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
|
|
|
// find the child's position.
|
2002-11-21 17:07:11 +08:00
|
|
|
unsigned int pos=getChildIndex(child);
|
2002-10-04 22:50:33 +08:00
|
|
|
if (pos==_children.size()) return;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-10-04 22:50:33 +08:00
|
|
|
_values[pos]=value;
|
2005-06-24 23:34:46 +08:00
|
|
|
dirtyBound();
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::getValue(unsigned int pos) const
|
|
|
|
{
|
|
|
|
if (pos>=_values.size()) return false;
|
|
|
|
return _values[pos];
|
|
|
|
}
|
|
|
|
|
2002-12-08 05:18:12 +08:00
|
|
|
bool Switch::getChildValue(const Node* child) const
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
|
|
|
// find the child's position.
|
2002-11-21 17:07:11 +08:00
|
|
|
unsigned int pos=getChildIndex(child);
|
2002-10-04 22:50:33 +08:00
|
|
|
if (pos==_children.size()) return false;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-10-04 22:50:33 +08:00
|
|
|
return _values[pos];
|
|
|
|
}
|
|
|
|
|
2002-12-08 05:18:12 +08:00
|
|
|
bool Switch::setAllChildrenOff()
|
|
|
|
{
|
|
|
|
_newChildDefaultValue = false;
|
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = false;
|
|
|
|
}
|
2005-06-24 23:34:46 +08:00
|
|
|
dirtyBound();
|
2002-12-08 05:18:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::setAllChildrenOn()
|
|
|
|
{
|
|
|
|
_newChildDefaultValue = true;
|
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = true;
|
|
|
|
}
|
2005-06-27 16:54:43 +08:00
|
|
|
dirtyBound();
|
2002-12-08 05:18:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::setSingleChildOn(unsigned int pos)
|
|
|
|
{
|
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = false;
|
|
|
|
}
|
|
|
|
setValue(pos,true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
BoundingSphere Switch::computeBound() const
|
2004-12-18 04:51:55 +08:00
|
|
|
{
|
2005-05-12 22:03:22 +08:00
|
|
|
BoundingSphere bsphere;
|
2012-03-22 01:36:20 +08:00
|
|
|
if (_children.empty())
|
2004-12-18 04:51:55 +08:00
|
|
|
{
|
2005-05-12 22:03:22 +08:00
|
|
|
return bsphere;
|
2004-12-18 04:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// note, special handling of the case when a child is an Transform,
|
|
|
|
// such that only Transforms which are relative to their parents coordinates frame (i.e this group)
|
|
|
|
// are handled, Transform relative to and absolute reference frame are ignored.
|
|
|
|
|
|
|
|
BoundingBox bb;
|
|
|
|
bb.init();
|
2006-10-30 20:14:00 +08:00
|
|
|
for(unsigned int pos=0;pos<_children.size();++pos)
|
2004-12-18 04:51:55 +08:00
|
|
|
{
|
2006-10-30 20:14:00 +08:00
|
|
|
const osg::Transform* transform = _children[pos]->asTransform();
|
2004-12-18 04:51:55 +08:00
|
|
|
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
|
|
|
|
{
|
2006-10-30 20:14:00 +08:00
|
|
|
if( _values[pos] == true )
|
2014-05-15 00:01:40 +08:00
|
|
|
{
|
|
|
|
const osg::BoundingSphere& bs = _children[pos]->getBound();
|
|
|
|
bb.expandBy(bs);
|
|
|
|
}
|
2004-12-18 04:51:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
if (!bb.valid())
|
2004-12-18 04:51:55 +08:00
|
|
|
{
|
2005-05-12 22:03:22 +08:00
|
|
|
return bsphere;
|
2004-12-18 04:51:55 +08:00
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
bsphere._center = bb.center();
|
|
|
|
bsphere._radius = 0.0f;
|
2006-10-30 20:14:00 +08:00
|
|
|
for(unsigned int pos=0;pos<_children.size();++pos)
|
2004-12-18 04:51:55 +08:00
|
|
|
{
|
2006-10-30 20:14:00 +08:00
|
|
|
const osg::Transform* transform = _children[pos]->asTransform();
|
2004-12-18 04:51:55 +08:00
|
|
|
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
|
|
|
|
{
|
2006-10-30 20:14:00 +08:00
|
|
|
if( _values[pos] == true )
|
2014-05-15 00:01:40 +08:00
|
|
|
{
|
|
|
|
const osg::BoundingSphere& bs = _children[pos]->getBound();
|
|
|
|
bsphere.expandRadiusBy(bs);
|
|
|
|
}
|
2004-12-18 04:51:55 +08:00
|
|
|
}
|
|
|
|
}
|
2005-05-12 22:03:22 +08:00
|
|
|
return bsphere;
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
2004-12-18 04:51:55 +08:00
|
|
|
|