root/OpenSceneGraph/trunk/src/osg/Switch.cpp @ 13041

Revision 13041, 5.4 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osg/Switch>
14#include <osg/BoundingBox>
15#include <osg/Transform>
16#include <osg/Notify>
17
18#include <algorithm>
19
20using namespace osg;
21
22Switch::Switch():
23    _newChildDefaultValue(true)
24{
25}
26
27Switch::Switch(const Switch& sw,const CopyOp& copyop):
28    Group(sw,copyop),
29    _newChildDefaultValue(sw._newChildDefaultValue),
30    _values(sw._values)
31{
32}
33
34void Switch::traverse(NodeVisitor& nv)
35{
36    if (nv.getTraversalMode()==NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
37    {
38        for(unsigned int pos=0;pos<_children.size();++pos)
39        {
40            if (_values[pos]) _children[pos]->accept(nv);
41        }
42    }
43    else
44    {
45        Group::traverse(nv);
46    }
47}
48
49bool Switch::addChild( Node *child )
50{
51    if (Group::addChild(child))
52    {
53        if (_children.size()>_values.size())
54        {
55            _values.resize(_children.size(),_newChildDefaultValue);
56        }
57        // note, we don't override any pre-existing _values[childPosition] setting
58        // like in addChild(child,value) below.
59        return true;
60    }
61    return false;
62}
63
64bool Switch::addChild( Node *child, bool value )
65{
66    unsigned int childPosition = _children.size();
67    if (Group::addChild(child))
68    {
69        if (_children.size()>_values.size())
70        {
71            _values.resize(_children.size(),_newChildDefaultValue);
72        }
73        _values[childPosition]=value;
74        return true;
75    }
76    return false;
77}
78
79bool Switch::insertChild( unsigned int index, Node *child )
80{
81    return insertChild(index,child,_newChildDefaultValue);
82}
83
84bool Switch::insertChild( unsigned int index, Node *child, bool value )
85{
86    if (Group::insertChild(index,child))
87    {
88        if (index>=_values.size())
89        {
90            _values.push_back(value);
91        }
92        else
93        {
94            _values.insert(_values.begin()+index, value);
95        }
96
97        return true;
98    }
99    return false;
100}
101
102bool Switch::removeChildren(unsigned int pos,unsigned int numChildrenToRemove)
103{
104    if (pos<_values.size()) _values.erase(_values.begin()+pos, osg::minimum(_values.begin()+(pos+numChildrenToRemove), _values.end()) );
105
106    return Group::removeChildren(pos,numChildrenToRemove);
107}
108
109void Switch::setValue(unsigned int pos,bool value)
110{
111    if (pos>=_values.size()) _values.resize(pos+1,_newChildDefaultValue);
112    _values[pos]=value;
113    dirtyBound();
114}
115
116void Switch::setChildValue(const Node* child,bool value)
117{
118    // find the child's position.
119    unsigned int pos=getChildIndex(child);
120    if (pos==_children.size()) return;
121
122    _values[pos]=value;
123    dirtyBound();
124}
125
126bool Switch::getValue(unsigned int pos) const
127{
128    if (pos>=_values.size()) return false;
129    return _values[pos];
130}
131
132bool Switch::getChildValue(const Node* child) const
133{
134    // find the child's position.
135    unsigned int pos=getChildIndex(child);
136    if (pos==_children.size()) return false;
137
138    return _values[pos];
139}
140
141bool Switch::setAllChildrenOff()
142{
143    _newChildDefaultValue = false;
144    for(ValueList::iterator itr=_values.begin();
145        itr!=_values.end();
146        ++itr)
147    {
148        *itr = false;
149    }
150    dirtyBound();
151    return true;
152}
153
154bool Switch::setAllChildrenOn()
155{
156    _newChildDefaultValue = true;
157    for(ValueList::iterator itr=_values.begin();
158        itr!=_values.end();
159        ++itr)
160    {
161        *itr = true;
162    }
163    dirtyBound();
164    return true;
165}
166
167bool Switch::setSingleChildOn(unsigned int pos)
168{
169    for(ValueList::iterator itr=_values.begin();
170        itr!=_values.end();
171        ++itr)
172    {
173        *itr = false;
174    }
175    setValue(pos,true);
176    return true;
177}
178
179BoundingSphere Switch::computeBound() const
180{
181    BoundingSphere bsphere;
182    if (_children.empty())
183    {
184        return bsphere;
185    }
186
187    // note, special handling of the case when a child is an Transform,
188    // such that only Transforms which are relative to their parents coordinates frame (i.e this group)
189    // are handled, Transform relative to and absolute reference frame are ignored.
190
191    BoundingBox bb;
192    bb.init();
193    for(unsigned int pos=0;pos<_children.size();++pos)
194    {
195        const osg::Transform* transform = _children[pos]->asTransform();
196        if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
197        {
198            if( _values[pos] == true )
199                bb.expandBy(_children[pos]->getBound());
200        }
201    }
202
203    if (!bb.valid())
204    {
205        return bsphere;
206    }
207
208    bsphere._center = bb.center();
209    bsphere._radius = 0.0f;
210    for(unsigned int pos=0;pos<_children.size();++pos)
211    {
212        const osg::Transform* transform = _children[pos]->asTransform();
213        if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
214        {
215            if( _values[pos] == true )
216                bsphere.expandRadiusBy(_children[pos]->getBound());
217        }
218    }
219    return bsphere;
220}
221
Note: See TracBrowser for help on using the browser.