Debian Codegraph @

lib/database/src/so/nodes/SoNode.c++

raw
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,92 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.1.1.1 $
44 |
45 | Classes:
46 | SoNode
47 |
48 | Author(s) : Paul S. Strauss, Nick Thompson, Gavin Bell
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/SoDB.h>
55 #include <Inventor/SoOutput.h>
56 #include <Inventor/actions/SoActions.h>
57 #include <Inventor/elements/SoCacheElement.h>
58 #include <Inventor/errors/SoDebugError.h>
59 #include <Inventor/fields/SoFieldData.h>
60 #include <Inventor/misc/SoChildList.h>
61 #include <Inventor/misc/SoNotification.h>
62 #include <Inventor/nodes/SoSubNode.h> /* Has some handy stuff */
63
64 SoType SoNode::classTypeId; // Type identifier
65 uint32_t SoNode::nextUniqueId; // Unique ID counter
66 int SoNode::nextActionMethodIndex; // Index for action tables
67
68 ////////////////////////////////////////////////////////////////////////
69 //
70 // Description:
71 // Constructor
72 //
73 // Use: protected
74
75 SoNode::SoNode()
76 //
77 ////////////////////////////////////////////////////////////////////////
78 {
79 #ifdef DEBUG
80 if (! SoDB::isInitialized()) {
81 SoDebugError::post("SoNode::SoNode",
82 "Cannot construct nodes before "
83 "calling SoDB::init()");
84 }
85 #endif /* DEBUG */
86
87 override = FALSE;
88 uniqueId = nextUniqueId++;
89 }
90
91 ////////////////////////////////////////////////////////////////////////
92 //
93 // Description:
94 // Destructor
95 //
96 // Use: protected
97
98 SoNode::~SoNode()
99 //
100 ////////////////////////////////////////////////////////////////////////
101 {
102 }
103
104 ////////////////////////////////////////////////////////////////////////
105 //
106 // Description:
107 // Turns override flag on or off. Causes notification.
108 //
109 // Use: public
110
111 void
112 SoNode::setOverride(SbBool state)
113 //
114 ////////////////////////////////////////////////////////////////////////
115 {
116 override = state;
117 startNotify();
118 }
119
120 ////////////////////////////////////////////////////////////////////////
121 //
122 // Description:
123 // Creates and returns an exact copy of the node.
124 //
125 // Use: public
126
127 SoNode *
128 SoNode::copy(SbBool copyConnections) const
129 //
130 ////////////////////////////////////////////////////////////////////////
131 {
132 ////////////////////////////////////////////
133 //
134 // The copy operation is done in two passes:
135 //
136 // (1) Determine which nodes are "inside" the copy. That is, which
137 // nodes are under the original node being copied.
138 //
139 // (2) Copy the graph. Inside nodes and engines re-use the same
140 // instance each time encountered. References to "outside"
141 // nodes and engines are copied as just pointers.
142 // (An engine is inside only if there is at least one
143 // inside node connected somewhere at both ends.)
144 //
145 // To make these steps more efficient, a dictionary of inside
146 // nodes is created in step (1) and used in step (2). Engines
147 // determined to be inside in step (2) are also added to the
148 // dictionary. This dictionary is maintained in the
149 // SoFieldContainer class so both nodes and engines can access it.
150 //
151 ////////////////////////////////////////////
152
153 // Ref ourselves, just in case our ref count is 0
154 ref();
155
156 // Step (1):
157
158 // Set up a new dictionary. Recursive copy operations use new
159 // dictionaries to avoid confusion.
160 initCopyDict();
161
162 // Recursively figure out which nodes are inside and add them to
163 // the copy dictionary, each with NO ref().
164 SoNode *newNode = addToCopyDict();
165 newNode->ref();
166
167 // Step (2):
168
169 // Copy the contents of this node into the new copy. This will
170 // recurse (for groups) and will also handle connections and
171 // fields that point to nodes, paths, or engines.
172 newNode->copyContents(this, copyConnections);
173
174 // Get rid of the dictionary
175 copyDone();
176
177 // Return the copy
178 newNode->unrefNoDelete();
179 unref();
180 return newNode;
181 }
182
183 ////////////////////////////////////////////////////////////////////////
184 //
185 // Description:
186 // Recursively adds this node and all nodes under it to the copy
187 // dictionary. Returns the copy of this node.
188 //
189 // Use: internal, virtual
190
191 SoNode *
192 SoNode::addToCopyDict() const
193 //
194 ////////////////////////////////////////////////////////////////////////
195 {
196 // If this node is already in the dictionary, nothing else to do
197 SoNode *copy = (SoNode *) checkCopy(this);
198 if (copy == NULL) {
199
200 // Create and add a new instance to the dictionary
201 copy = (SoNode *) getTypeId().createInstance();
202 copy->ref();
203 addCopy(this, copy); // Adds a ref()
204 copy->unrefNoDelete();
205
206 // Recurse on children, if any
207 SoChildList *kids = getChildren();
208 if (kids != NULL)
209 for (int i = 0; i < kids->getLength(); i++)
210 (*kids)[i]->addToCopyDict();
211 }
212
213 return copy;
214 }
215
216 ////////////////////////////////////////////////////////////////////////
217 //
218 // Description:
219 // Copies the contents of the given node into this instance. This
220 // calls the default implementation and also copies the override
221 // flag.
222 //
223 // Use: internal, virtual
224
225 void
226 SoNode::copyContents(const SoFieldContainer *fromFC, SbBool copyConnections)
227 //
228 ////////////////////////////////////////////////////////////////////////
229 {
230 // Copy the regular stuff
231 SoFieldContainer::copyContents(fromFC, copyConnections);
232
233 // Copy the override flag
234 override = ((SoNode *) fromFC)->override;
235 }
236
237 ////////////////////////////////////////////////////////////////////////
238 //
239 // Description:
240 // During a copy operation, this copies an instance that is
241 // encountered through a field connection.
242 //
243 // Use: internal, virtual
244
245 SoFieldContainer *
246 SoNode::copyThroughConnection() const
247 //
248 ////////////////////////////////////////////////////////////////////////
249 {
250 // If there is already a copy of this node (created during the
251 // first pass of a copy() operation), use it. Otherwise, just use
252 // this node.
253 SoFieldContainer *copy = findCopy(this, TRUE);
254 if (copy != NULL)
255 return copy;
256 return (SoFieldContainer *) this;
257 }
258
259 ////////////////////////////////////////////////////////////////////////
260 //
261 // Description:
262 // Returns the last node given the name 'name'. Returns NULL if
263 // there is no node with the given name.
264 //
265 // Use: public
266
267 SoNode *
268 SoNode::getByName(const SbName &name)
269 //
270 ////////////////////////////////////////////////////////////////////////
271 {
272 return (SoNode *)getNamedBase(name, SoNode::getClassTypeId());
273 }
274
275 ////////////////////////////////////////////////////////////////////////
276 //
277 // Description:
278 // Adds all nodes named 'name' to the list. Returns the number of
279 // nodes found.
280 //
281 // Use: public
282
283 int
284 SoNode::getByName(const SbName &name, SoNodeList &list)
285 //
286 ////////////////////////////////////////////////////////////////////////
287 {
288 return getNamedBases(name, list, SoNode::getClassTypeId());
289 }
290
291 ////////////////////////////////////////////////////////////////////////
292 //
293 // Description:
294 // Returns TRUE if a node has an affect on the state during
295 // traversal. The default method returns TRUE. Node classes (such
296 // as SoSeparator) that isolate their effects from the rest of the
297 // graph override this method to return FALSE.
298 //
299 // Use: public
300
301 SbBool
302 SoNode::affectsState() const
303 //
304 ////////////////////////////////////////////////////////////////////////
305 {
306 return TRUE;
307 }
308
309 ////////////////////////////////////////////////////////////////////////
310 //
311 // Description:
312 // Virtual methods implementing actions handled in subclasses. Most
313 // of these do nothing at all.
314 //
315 // Use: extender (all)
316 //
317 ////////////////////////////////////////////////////////////////////////
318
319 void
320 SoNode::doAction(SoAction *)
321 {
322 #ifdef DEBUG_DEFAULT_METHODS
323 SoDebugInfo::post("SoNode::doAction",
324 "Called for %s", getTypeId().getName().getString());
325 #endif /* DEBUG_DEFAULT_METHODS */
326 }
327
328 void
329 SoNode::callback(SoCallbackAction *)
330 {
331 #ifdef DEBUG_DEFAULT_METHODS
332 SoDebugInfo::post("SoNode::callback",
333 "Called for %s", getTypeId().getName().getString());
334 #endif /* DEBUG_DEFAULT_METHODS */
335 }
336
337 void
338 SoNode::GLRender(SoGLRenderAction *)
339 {
340 #ifdef DEBUG_DEFAULT_METHODS
341 SoDebugInfo::post("SoNode::GLRender",
342 "Called for %s", getTypeId().getName().getString());
343 #endif /* DEBUG_DEFAULT_METHODS */
344 }
345
346 void
347 SoNode::GLRenderBelowPath(SoGLRenderAction *action)
348 {
349 GLRender(action);
350 }
351
352 void
353 SoNode::GLRenderInPath(SoGLRenderAction *action)
354 {
355 GLRender(action);
356 }
357
358 void
359 SoNode::GLRenderOffPath(SoGLRenderAction *action)
360 {
361 GLRender(action);
362 }
363
364 void
365 SoNode::getBoundingBox(SoGetBoundingBoxAction *)
366 {
367 #ifdef DEBUG_DEFAULT_METHODS
368 SoDebugInfo::post("SoNode::getBoundingBox",
369 "Called for %s", getTypeId().getName().getString());
370 #endif /* DEBUG_DEFAULT_METHODS */
371 }
372
373 void
374 SoNode::getMatrix(SoGetMatrixAction *)
375 {
376 #ifdef DEBUG_DEFAULT_METHODS
377 SoDebugInfo::post("SoNode::getMatrix",
378 "Called for %s", getTypeId().getName().getString());
379 #endif /* DEBUG_DEFAULT_METHODS */
380 }
381
382 void
383 SoNode::handleEvent(SoHandleEventAction *)
384 {
385 #ifdef DEBUG_DEFAULT_METHODS
386 SoDebugInfo::post("SoNode::handleEvent",
387 "Called for %s", getTypeId().getName().getString());
388 #endif /* DEBUG_DEFAULT_METHODS */
389 }
390
391 void
392 SoNode::pick(SoPickAction *)
393 {
394 #ifdef DEBUG_DEFAULT_METHODS
395 SoDebugInfo::post("SoNode::pick",
396 "Called for %s", getTypeId().getName().getString());
397 #endif /* DEBUG_DEFAULT_METHODS */
398 }
399
400 void
401 SoNode::rayPick(SoRayPickAction *action)
402 {
403 // If the node doesn't have a specific rayPick() method, it may
404 // have a more general pick() method for any pick action.
405 pick(action);
406 }
407
408 ////////////////////////////////////////////////////////////////////////
409 //
410 // Description:
411 // Implements search action for most nodes.
412 //
413 // Use: extender
414
415 void
416 SoNode::search(SoSearchAction *action)
417 //
418 ////////////////////////////////////////////////////////////////////////
419 {
420 int lookingFor = action->getFind();
421 SbBool foundMe = TRUE;
422
423 // We have to match everything set by the search action.
424 // First, see if node doesn't match:
425 if ((lookingFor & SoSearchAction::NODE) && action->getNode() != this)
426 foundMe = FALSE;
427
428 // Next, see if the name doesn't match:
429 if ((lookingFor & SoSearchAction::NAME) &&
430 action->getName() != this->getName())
431 foundMe = FALSE;
432
433 // Finally, figure out if types match:
434 if (lookingFor & SoSearchAction::TYPE) {
435 int derivedOk;
436 SoType t = action->getType(derivedOk);
437 if (! (derivedOk ? isOfType(t) : getTypeId() == t))
438 foundMe = FALSE;
439 }
440
441 if (foundMe) {
442 // We have a match! Add it to the action.
443
444 if (action->getInterest() == SoSearchAction::ALL)
445 action->getPaths().append(action->getCurPath()->copy());
446
447 else {
448 action->addPath(action->getCurPath()->copy());
449 if (action->getInterest() == SoSearchAction::FIRST)
450 action->setFound();
451 }
452 }
453 }
454
455 ////////////////////////////////////////////////////////////////////////
456 //
457 // Description:
458 // Implements write action for most nodes.
459 //
460 // Use: extender
461
462 void
463 SoNode::write(SoWriteAction *action)
464 //
465 ////////////////////////////////////////////////////////////////////////
466 {
467 if (action->getOutput()->getStage() == SoOutput::COUNT_REFS)
468 addWriteReference(action->getOutput());
469
470 // We assume this node would not be traversed if is has not been counted
471 else if (! writeHeader(action->getOutput(), FALSE, FALSE)) {
472
473 const SoFieldData *fieldData = getFieldData();
474
475 fieldData->write(action->getOutput(), this);
476
477 writeFooter(action->getOutput());
478 }
479 }
480
481 ////////////////////////////////////////////////////////////////////////
482 //
483 // Description:
484 // Implements default setup of event grabbing
485 //
486 // Use: extender
487
488 void
489 SoNode::grabEventsSetup()
490 //
491 ////////////////////////////////////////////////////////////////////////
492 {
493 }
494
495 ////////////////////////////////////////////////////////////////////////
496 //
497 // Description:
498 // Implements default cleanup of event grabbing
499 //
500 // Use: extender
501
502 void
503 SoNode::grabEventsCleanup()
504 //
505 ////////////////////////////////////////////////////////////////////////
506 {
507 }
508
509 ////////////////////////////////////////////////////////////////////////
510 //
511 // Description:
512 // Initiates notification from the node.
513 //
514 // Use: internal
515
516 void
517 SoNode::startNotify()
518 //
519 ////////////////////////////////////////////////////////////////////////
520 {
521 // Update our unique id to indicate that we are a different node.
522 uniqueId = nextUniqueId++;
523
524 // Let FieldContainer pass notification on to auditors...
525 SoFieldContainer::startNotify();
526 }
527
528 ////////////////////////////////////////////////////////////////////////
529 //
530 // Description:
531 // Propagates notification through the node. First updates uniqueId
532 // and then does base class action.
533 //
534 // Use: internal
535
536 void
537 SoNode::notify(SoNotList *list)
538 //
539 ////////////////////////////////////////////////////////////////////////
540 {
541 #ifdef DEBUG
542 if (list == NULL) {
543 SoDebugError::post("SoNode::notify",
544 "notification list pointer is NULL");
545 return;
546 }
547 #endif /* DEBUG */
548
549 // Do nothing if this node has already been notified by this
550 // current notification. We can tell this by comparing the
551 // notification list's time stamp with the node id.
552 if (list->getTimeStamp() <= uniqueId)
553 return;
554
555 // Update our unique id to indicate that we are a different node
556 uniqueId = nextUniqueId++;
557
558 // Let FieldContainer do most of the work.
559 SoFieldContainer::notify(list);
560 }
561
562 ////////////////////////////////////////////////////////////////////////
563 //
564 // Description:
565 // Returns pointer to children. Default returns NULL since most
566 // nodes don't have children.
567 //
568 // Use: internal
569
570 SoChildList *
571 SoNode::getChildren() const
572 //
573 ////////////////////////////////////////////////////////////////////////
574 {
575 return NULL;
576 }
577
578 ////////////////////////////////////////////////////////////////////////
579 //
580 // Description:
581 // Writes instance to SoOutput. This is called only through
582 // non-standard traversal mechanisms. (write() is used for the
583 // standard one.) For example, this is called when a node connected
584 // to a field or pointed to by a field is written out. This needs
585 // to create a new SoWriteAction to continue the writing process.
586 //
587 // Use: internal
588
589 void
590 SoNode::writeInstance(SoOutput *out)
591 //
592 ////////////////////////////////////////////////////////////////////////
593 {
594 SoWriteAction wa(out);
595 wa.continueToApply(this);
596 }
597
598 ////////////////////////////////////////////////////////////////////////
599 //
600 // Description:
601 // Static callbacks for most actions just call corresponding
602 // virtual method.
603 //
604 // Use: private (all)
605 //
606 ////////////////////////////////////////////////////////////////////////
607
608 void
609 SoNode::getMatrixS(SoAction *action, SoNode *node)
610 {
611 node->getMatrix((SoGetMatrixAction *) action);
612 }
613
614 void
615 SoNode::handleEventS(SoAction *action, SoNode *node)
616 {
617 node->handleEvent((SoHandleEventAction *) action);
618 }
619
620 void
621 SoNode::pickS(SoAction *action, SoNode *node)
622 {
623 node->pick((SoPickAction *) action);
624 }
625
626 void
627 SoNode::rayPickS(SoAction *action, SoNode *node)
628 {
629 node->rayPick((SoRayPickAction *) action);
630 }
631
632 void
633 SoNode::searchS(SoAction *action, SoNode *node)
634 {
635 node->search((SoSearchAction *) action);
636 }
637
638 void
639 SoNode::writeS(SoAction *action, SoNode *node)
640 {
641 node->write((SoWriteAction *) action);
642 }
643
644 ////////////////////////////////////////////////////////////////////////
645 //
646 // Description:
647 // These three methods are treated differently because they do some
648 // extra work.
649 //
650 // Use: private (all)
651 //
652 ////////////////////////////////////////////////////////////////////////
653
654 void
655 SoNode::callbackS(SoAction *action, SoNode *node)
656 {
657 SoCallbackAction *a = (SoCallbackAction *)action;
658
659 // Pre/post callbacks are automatically handled. If the callbacks
660 // set the 'response' flag to stop traversal, handle that also.
661
662 if (a->hasTerminated())
663 return;
664
665 a->setCurrentNode(node);
666
667 a->invokePreCallbacks(node);
668
669 if (! a->hasTerminated() &&
670 a->getCurrentResponse() != SoCallbackAction::PRUNE)
671 node->callback(a);
672
673 a->invokePostCallbacks(node);
674 }
675
676 void
677 SoNode::GLRenderS(SoAction *action, SoNode *node)
678 {
679 SoGLRenderAction *a = (SoGLRenderAction *) action;
680
681 if (! a->abortNow())
682 node->GLRender(a);
683 else {
684 SoCacheElement::invalidate(action->getState());
685 }
686 }
687
688 void
689 SoNode::getBoundingBoxS(SoAction *action, SoNode *node)
690 {
691 SoGetBoundingBoxAction *a = (SoGetBoundingBoxAction *)action;
692
693 a->checkResetBefore();
694 node->getBoundingBox(a);
695 a->checkResetAfter();
696 }