| 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * Further, this software is distributed without any warranty that it is |
| 16 | * free of the rightful claim of any third person regarding infringement |
| 17 | * or the like. Any license provided herein, whether implied or |
| 18 | * otherwise, applies only to this software file. Patent licenses, if |
| 19 | * any, provided herein do not apply to combinations of this program with |
| 20 | * other software, or any other product whatsoever. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 | * Mountain View, CA 94043, or: |
| 28 | * |
| 29 | * http://www.sgi.com |
| 30 | * |
| 31 | * For further information regarding this notice, see: |
| 32 | * |
| 33 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 | * |
| 40 | _______________________________________________________________________ |
| 41 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 | | |
| 43 | | $Revision: 1.2 $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoSwitch |
| 47 | | |
| 48 | | Author(s) : Paul S. Strauss, Nick Thompson |
| 49 | | |
| 50 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 | _______________________________________________________________________ |
| 52 | */ |
| 53 | |
| 54 | |
| 55 | #include <Inventor/actions/SoCallbackAction.h> |
| 56 | #include <Inventor/actions/SoGLRenderAction.h> |
| 57 | #include <Inventor/actions/SoGetBoundingBoxAction.h> |
| 58 | #include <Inventor/actions/SoGetMatrixAction.h> |
| 59 | #include <Inventor/actions/SoHandleEventAction.h> |
| 60 | #include <Inventor/actions/SoPickAction.h> |
| 61 | #include <Inventor/actions/SoSearchAction.h> |
| 62 | #include <Inventor/actions/SoWriteAction.h> |
| 63 | #include <Inventor/elements/SoSwitchElement.h> |
| 64 | #include <Inventor/errors/SoDebugError.h> |
| 65 | #include <Inventor/misc/SoChildList.h> |
| 66 | #include <Inventor/misc/SoState.h> |
| 67 | #include <Inventor/nodes/SoSwitch.h> |
| 68 | |
| 69 | SO_NODE_SOURCE(SoSwitch); |
| 70 | |
| 71 | //////////////////////////////////////////////////////////////////////// |
| 72 | // |
| 73 | // Description: |
| 74 | // Constructor |
| 75 | // |
| 76 | // Use: public |
| 77 | |
| 78 | SoSwitch::SoSwitch() |
| 79 | // |
| 80 | //////////////////////////////////////////////////////////////////////// |
| 81 | { |
| 82 | SO_NODE_CONSTRUCTOR(SoSwitch); |
| 83 | SO_NODE_ADD_FIELD(whichChild, (SO_SWITCH_NONE)); |
| 84 | isBuiltIn = TRUE; |
| 85 | } |
| 86 | |
| 87 | //////////////////////////////////////////////////////////////////////// |
| 88 | // |
| 89 | // Description: |
| 90 | // Constructor that takes approximate number of children. |
| 91 | // |
| 92 | // Use: public |
| 93 | |
| 94 | SoSwitch::SoSwitch(int nChildren) : SoGroup(nChildren) |
| 95 | // |
| 96 | //////////////////////////////////////////////////////////////////////// |
| 97 | { |
| 98 | SO_NODE_CONSTRUCTOR(SoSwitch); |
| 99 | SO_NODE_ADD_FIELD(whichChild, (SO_SWITCH_NONE)); |
| 100 | isBuiltIn = TRUE; |
| 101 | } |
| 102 | |
| 103 | //////////////////////////////////////////////////////////////////////// |
| 104 | // |
| 105 | // Description: |
| 106 | // Destructor |
| 107 | // |
| 108 | // Use: private |
| 109 | |
| 110 | SoSwitch::~SoSwitch() |
| 111 | // |
| 112 | //////////////////////////////////////////////////////////////////////// |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | //////////////////////////////////////////////////////////////////////// |
| 117 | // |
| 118 | // Description: |
| 119 | // Overrides method in SoNode to return FALSE if there is no |
| 120 | // selected child or the selected child does not affect the state. |
| 121 | // |
| 122 | // Use: public |
| 123 | |
| 124 | SbBool |
| 125 | SoSwitch::affectsState() const |
| 126 | // |
| 127 | //////////////////////////////////////////////////////////////////////// |
| 128 | { |
| 129 | int which; |
| 130 | if (whichChild.isIgnored()) |
| 131 | which = SO_SWITCH_NONE; |
| 132 | else |
| 133 | which = whichChild.getValue(); |
| 134 | |
| 135 | // Special case-- if called during application of an |
| 136 | // SoSearchAction that is searching all: |
| 137 | if (SoSearchAction::duringSearchAll) |
| 138 | which = SO_SWITCH_ALL; |
| 139 | |
| 140 | if (whichChild.isIgnored() || which == SO_SWITCH_NONE) |
| 141 | return FALSE; |
| 142 | |
| 143 | if (which == SO_SWITCH_ALL || |
| 144 | which == SO_SWITCH_INHERIT) |
| 145 | return TRUE; |
| 146 | |
| 147 | if (getChild(which)->affectsState()) |
| 148 | return TRUE; |
| 149 | |
| 150 | return FALSE; |
| 151 | } |
| 152 | |
| 153 | //////////////////////////////////////////////////////////////////////// |
| 154 | // |
| 155 | // Description: |
| 156 | // Typical action traversal. |
| 157 | // |
| 158 | // Use: extender |
| 159 | |
| 160 | void |
| 161 | SoSwitch::doAction(SoAction *action) |
| 162 | // |
| 163 | //////////////////////////////////////////////////////////////////////// |
| 164 | { |
| 165 | int numIndices; |
| 166 | const int *indices; |
| 167 | |
| 168 | if (action->getPathCode(numIndices, indices) == SoAction::IN_PATH) { |
| 169 | int i; |
| 170 | for (i = 0; i < numIndices; i++) |
| 171 | doChild(action, indices[i]); |
| 172 | } |
| 173 | |
| 174 | else |
| 175 | doChild(action); |
| 176 | } |
| 177 | |
| 178 | //////////////////////////////////////////////////////////////////////// |
| 179 | // |
| 180 | // Description: |
| 181 | // Traversal for callback action. |
| 182 | // |
| 183 | // Use: extender |
| 184 | |
| 185 | void |
| 186 | SoSwitch::callback(SoCallbackAction *action) |
| 187 | // |
| 188 | //////////////////////////////////////////////////////////////////////// |
| 189 | { |
| 190 | SoSwitch::doAction(action); |
| 191 | } |
| 192 | |
| 193 | //////////////////////////////////////////////////////////////////////// |
| 194 | // |
| 195 | // Description: |
| 196 | // Traversal for GL rendering |
| 197 | // |
| 198 | // Use: extender |
| 199 | |
| 200 | void |
| 201 | SoSwitch::GLRender(SoGLRenderAction *action) |
| 202 | // |
| 203 | //////////////////////////////////////////////////////////////////////// |
| 204 | { |
| 205 | SoSwitch::doAction(action); |
| 206 | } |
| 207 | |
| 208 | //////////////////////////////////////////////////////////////////////// |
| 209 | // |
| 210 | // Description: |
| 211 | // Traversal for picking |
| 212 | // |
| 213 | // Use: extender |
| 214 | |
| 215 | void |
| 216 | SoSwitch::pick(SoPickAction *action) |
| 217 | // |
| 218 | //////////////////////////////////////////////////////////////////////// |
| 219 | { |
| 220 | SoSwitch::doAction(action); |
| 221 | } |
| 222 | |
| 223 | //////////////////////////////////////////////////////////////////////// |
| 224 | // |
| 225 | // Description: |
| 226 | // Traversal for get bounding box |
| 227 | // |
| 228 | // Use: extender |
| 229 | |
| 230 | void |
| 231 | SoSwitch::getBoundingBox(SoGetBoundingBoxAction *action) |
| 232 | // |
| 233 | //////////////////////////////////////////////////////////////////////// |
| 234 | { |
| 235 | SoSwitch::doAction(action); |
| 236 | } |
| 237 | |
| 238 | //////////////////////////////////////////////////////////////////////// |
| 239 | // |
| 240 | // Description: |
| 241 | // Traversal for handle event |
| 242 | // |
| 243 | // Use: extender |
| 244 | |
| 245 | void |
| 246 | SoSwitch::handleEvent(SoHandleEventAction *action) |
| 247 | // |
| 248 | //////////////////////////////////////////////////////////////////////// |
| 249 | { |
| 250 | SoSwitch::doAction(action); |
| 251 | } |
| 252 | |
| 253 | //////////////////////////////////////////////////////////////////////// |
| 254 | // |
| 255 | // Description: |
| 256 | // Implements getMatrix action. |
| 257 | // |
| 258 | // Use: extender |
| 259 | |
| 260 | void |
| 261 | SoSwitch::getMatrix(SoGetMatrixAction *action) |
| 262 | // |
| 263 | //////////////////////////////////////////////////////////////////////// |
| 264 | { |
| 265 | int numIndices; |
| 266 | const int *indices; |
| 267 | |
| 268 | // Only need to compute matrix if switch is a node in middle of |
| 269 | // current path chain or is off path chain (since the only way |
| 270 | // this could be called if it is off the chain is if the switch is |
| 271 | // under a group that affects the chain). |
| 272 | |
| 273 | switch (action->getPathCode(numIndices, indices)) { |
| 274 | |
| 275 | case SoAction::NO_PATH: |
| 276 | case SoAction::BELOW_PATH: |
| 277 | break; |
| 278 | |
| 279 | case SoAction::OFF_PATH: |
| 280 | case SoAction::IN_PATH: |
| 281 | SoSwitch::doAction(action); |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | //////////////////////////////////////////////////////////////////////// |
| 287 | // |
| 288 | // Description: |
| 289 | // Implements search action. |
| 290 | // |
| 291 | // Use: extender |
| 292 | // |
| 293 | //////////////////////////////////////////////////////////////////////// |
| 294 | |
| 295 | void |
| 296 | SoSwitch::search(SoSearchAction *action) |
| 297 | { |
| 298 | // if the action is searching everything, then do so... |
| 299 | if (action->isSearchingAll()) |
| 300 | SoGroup::search(action); |
| 301 | |
| 302 | // Otherwise, traverse according to the regular switch node rules |
| 303 | else { |
| 304 | // First, make sure this node is found if we are searching for |
| 305 | // switches |
| 306 | SoNode::search(action); |
| 307 | |
| 308 | // Recurse |
| 309 | if (! action->isFound()) |
| 310 | SoSwitch::doAction(action); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | //////////////////////////////////////////////////////////////////////// |
| 315 | // |
| 316 | // Description: |
| 317 | // Traverses correct child based on value of whichChild field. If |
| 318 | // matchIndex is non-negative, the indicated child must match it |
| 319 | // for it to be traversed. Sets switch element in state unless we |
| 320 | // are inheriting the value. |
| 321 | // |
| 322 | // Use: private |
| 323 | |
| 324 | void |
| 325 | SoSwitch::doChild(SoAction *action, int matchIndex) |
| 326 | // |
| 327 | //////////////////////////////////////////////////////////////////////// |
| 328 | { |
| 329 | int32_t which; |
| 330 | |
| 331 | if (whichChild.isIgnored()) |
| 332 | which = SO_SWITCH_NONE; |
| 333 | |
| 334 | else |
| 335 | which = whichChild.getValue(); |
| 336 | |
| 337 | // If index is inherited from state, get value from there |
| 338 | if (which == SO_SWITCH_INHERIT) { |
| 339 | |
| 340 | which = SoSwitchElement::get(action->getState()); |
| 341 | |
| 342 | // Make sure it is in range |
| 343 | if (which >= getNumChildren()) |
| 344 | which %= getNumChildren(); |
| 345 | } |
| 346 | |
| 347 | // Store resulting index in state if not already inherited from there |
| 348 | else |
| 349 | SoSwitchElement::set(action->getState(), which); |
| 350 | |
| 351 | // Now we have the real value to deal with |
| 352 | |
| 353 | switch (which) { |
| 354 | |
| 355 | case SO_SWITCH_NONE: |
| 356 | break; |
| 357 | |
| 358 | case SO_SWITCH_ALL: |
| 359 | // If traversing to compute bounding box, we have to do some |
| 360 | // work in between children |
| 361 | if (action->isOfType(SoGetBoundingBoxAction::getClassTypeId())) { |
| 362 | SoGetBoundingBoxAction *bba = (SoGetBoundingBoxAction *) action; |
| 363 | SbVec3f totalCenter(0.0, 0.0, 0.0); |
| 364 | int numCenters = 0; |
| 365 | int lastChild = (matchIndex >= 0 ? matchIndex : |
| 366 | getNumChildren() - 1); |
| 367 | |
| 368 | for (int i = 0; i <= lastChild; i++) { |
| 369 | children->traverse(bba, i, i); |
| 370 | if (bba->isCenterSet()) { |
| 371 | totalCenter += bba->getCenter(); |
| 372 | numCenters++; |
| 373 | bba->resetCenter(); |
| 374 | } |
| 375 | } |
| 376 | // Now, set the center to be the average: |
| 377 | if (numCenters != 0) |
| 378 | bba->setCenter(totalCenter / numCenters, FALSE); |
| 379 | } |
| 380 | else { |
| 381 | if (matchIndex >= 0) |
| 382 | children->traverse(action, 0, matchIndex); |
| 383 | else |
| 384 | children->traverse(action); |
| 385 | } |
| 386 | break; |
| 387 | |
| 388 | default: |
| 389 | |
| 390 | // Make sure index is reasonable |
| 391 | if (which < 0 || which >= getNumChildren()) { |
| 392 | #ifdef DEBUG |
| 393 | SoDebugError::post("SoSwitch::doChild", |
| 394 | "Child index %d is out of range %d - %d " |
| 395 | "(applying %s)", |
| 396 | which, 0, getNumChildren() - 1, |
| 397 | action->getTypeId().getName().getString()); |
| 398 | #endif /* DEBUG */ |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | // Traverse indicated child |
| 403 | if (matchIndex < 0 || matchIndex == which) |
| 404 | children->traverse(action, (int) which); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | //////////////////////////////////////////////////////////////////////// |
| 409 | // |
| 410 | // Description: |
| 411 | // Implements write action. |
| 412 | // |
| 413 | // Use: extender |
| 414 | |
| 415 | void |
| 416 | SoSwitch::write(SoWriteAction *action) |
| 417 | // |
| 418 | //////////////////////////////////////////////////////////////////////// |
| 419 | { |
| 420 | SoOutput *out = action->getOutput(); |
| 421 | |
| 422 | // When writing out a switch that is in a path, we always want to |
| 423 | // write out ALL children of the switch. If we did the default |
| 424 | // thing of writing out just those children that affect the nodes |
| 425 | // in the path, we could screw up. Consider a switch that has two |
| 426 | // child separators and whichChild set to 1. If a path goes |
| 427 | // through the switch to the second child, the first child, being |
| 428 | // a separator, does not affect the path. But if we don't write |
| 429 | // out the separator, the whichChild will reference a |
| 430 | // nonexistent child. So we always write out all children. |
| 431 | |
| 432 | // NOTE: SoChildList::traverse() checks the current path code and |
| 433 | // skips children off the path that do not affect the |
| 434 | // state. Because of this we have to avoid calling it. Instead, we |
| 435 | // do its work here. |
| 436 | |
| 437 | // This code is stolen and modified from SoGroup::write() and |
| 438 | // SoChildList::traverse() |
| 439 | |
| 440 | int lastChild = getNumChildren() - 1; |
| 441 | SoAction::PathCode pc = action->getCurPathCode(); |
| 442 | |
| 443 | // In write-reference counting phase |
| 444 | if (out->getStage() == SoOutput::COUNT_REFS) { |
| 445 | |
| 446 | // Increment our write reference count |
| 447 | addWriteReference(out); |
| 448 | |
| 449 | // If this is the first reference (i.e., we don't now have |
| 450 | // multiple references), also count all appropriate children |
| 451 | if (! hasMultipleWriteRefs()) { |
| 452 | for (int i = 0; i <= lastChild; i++) { |
| 453 | action->pushCurPath(i); |
| 454 | action->traverse(getChild(i)); |
| 455 | action->popCurPath(pc); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // In writing phase, we have to do some more work |
| 461 | else if (! writeHeader(out, TRUE, FALSE)) { |
| 462 | |
| 463 | // Write fields |
| 464 | const SoFieldData *fieldData = getFieldData(); |
| 465 | fieldData->write(out, this); |
| 466 | |
| 467 | // We KNOW that all children should be written, so don't |
| 468 | // bother calling shouldWrite() |
| 469 | |
| 470 | // If writing binary format, write out number of children |
| 471 | // that are going to be written |
| 472 | if (out->isBinary()) |
| 473 | out->write(getNumChildren()); |
| 474 | |
| 475 | for (int i = 0; i <= lastChild; i++) { |
| 476 | action->pushCurPath(i); |
| 477 | action->traverse(getChild(i)); |
| 478 | action->popCurPath(pc); |
| 479 | } |
| 480 | |
| 481 | // Write post-children stuff |
| 482 | writeFooter(out); |
| 483 | } |
| 484 | } |