| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/Switch> |
|---|
| 14 | #include <osg/BoundingBox> |
|---|
| 15 | #include <osg/Transform> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | |
|---|
| 18 | #include <algorithm> |
|---|
| 19 | |
|---|
| 20 | using namespace osg; |
|---|
| 21 | |
|---|
| 22 | Switch::Switch(): |
|---|
| 23 | _newChildDefaultValue(true) |
|---|
| 24 | { |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | Switch::Switch(const Switch& sw,const CopyOp& copyop): |
|---|
| 28 | Group(sw,copyop), |
|---|
| 29 | _newChildDefaultValue(sw._newChildDefaultValue), |
|---|
| 30 | _values(sw._values) |
|---|
| 31 | { |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | void 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 | |
|---|
| 49 | bool 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 | |
|---|
| 58 | |
|---|
| 59 | return true; |
|---|
| 60 | } |
|---|
| 61 | return false; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | bool 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 | |
|---|
| 79 | bool Switch::insertChild( unsigned int index, Node *child ) |
|---|
| 80 | { |
|---|
| 81 | return insertChild(index,child,_newChildDefaultValue); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | bool 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 | |
|---|
| 102 | bool 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 | |
|---|
| 109 | void 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 | |
|---|
| 116 | void Switch::setChildValue(const Node* child,bool value) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | unsigned int pos=getChildIndex(child); |
|---|
| 120 | if (pos==_children.size()) return; |
|---|
| 121 | |
|---|
| 122 | _values[pos]=value; |
|---|
| 123 | dirtyBound(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | bool Switch::getValue(unsigned int pos) const |
|---|
| 127 | { |
|---|
| 128 | if (pos>=_values.size()) return false; |
|---|
| 129 | return _values[pos]; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | bool Switch::getChildValue(const Node* child) const |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | unsigned int pos=getChildIndex(child); |
|---|
| 136 | if (pos==_children.size()) return false; |
|---|
| 137 | |
|---|
| 138 | return _values[pos]; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | bool 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 | |
|---|
| 154 | bool 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 | |
|---|
| 167 | bool 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 | |
|---|
| 179 | BoundingSphere Switch::computeBound() const |
|---|
| 180 | { |
|---|
| 181 | BoundingSphere bsphere; |
|---|
| 182 | if (_children.empty()) |
|---|
| 183 | { |
|---|
| 184 | return bsphere; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 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 | } |
|---|