Debian Codegraph @

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

raw symbols SCIP
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.2 $
44 |
45 | Classes:
46 | SoGroup
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/misc/SoChildList.h>
56 #include <Inventor/SoDB.h>
57 #include <Inventor/SoInput.h>
58 #include <Inventor/SoOutput.h>
59 #include <Inventor/actions/SoActions.h>
60 #include <Inventor/errors/SoDebugError.h>
61 #include <Inventor/errors/SoReadError.h>
62 #include <Inventor/fields/SoFieldData.h>
63 #include <Inventor/nodes/SoGroup.h>
64 #include <Inventor/elements/SoCacheElement.h>
65
66 SO_NODE_SOURCE(SoGroup);
67
68 ////////////////////////////////////////////////////////////////////////
69 //
70 // Description:
71 // Default constructor
72 //
73 // Use: public
74
75 SoGroup::SoGroup()
76 //
77 ////////////////////////////////////////////////////////////////////////
78 {
79 children = new SoChildList(this);
80 SO_NODE_CONSTRUCTOR(SoGroup);
81 isBuiltIn = TRUE;
82 }
83
84 ////////////////////////////////////////////////////////////////////////
85 //
86 // Description:
87 // Constructor that takes approximate number of children.
88 //
89 // Use: public
90
91 SoGroup::SoGroup(int nChildren)
92 //
93 ////////////////////////////////////////////////////////////////////////
94 {
95 children = new SoChildList(this, nChildren);
96 SO_NODE_CONSTRUCTOR(SoGroup);
97 isBuiltIn = TRUE;
98 }
99
100 ////////////////////////////////////////////////////////////////////////
101 //
102 // Description:
103 // Destructor.
104 //
105 // Use: private
106
107 SoGroup::~SoGroup()
108 //
109 ////////////////////////////////////////////////////////////////////////
110 {
111 delete children;
112 }
113
114 ////////////////////////////////////////////////////////////////////////
115 //
116 // Description:
117 // This adds a child as the last one in the group.
118 //
119 // Use: public
120
121 void
122 SoGroup::addChild(SoNode *child)
123 //
124 ////////////////////////////////////////////////////////////////////////
125 {
126 #ifdef DEBUG
127 if (child == NULL) {
128 SoDebugError::post("SoGroup::addChild", "NULL child node");
129 return;
130 }
131 #endif /* DEBUG */
132
133 children->append(child);
134 }
135
136 ////////////////////////////////////////////////////////////////////////
137 //
138 // Description:
139 // This inserts a child into a group so that it will have the given
140 // index.
141 //
142 // Use: public
143
144 void
145 SoGroup::insertChild(SoNode *child, int newChildIndex)
146 //
147 ////////////////////////////////////////////////////////////////////////
148 {
149 #ifdef DEBUG
150 if (child == NULL) {
151 SoDebugError::post("SoGroup::insertChild", "NULL child node");
152 return;
153 }
154
155 // Make sure index is reasonable
156 if (newChildIndex < 0 || newChildIndex > getNumChildren()) {
157 SoDebugError::post("SoGroup::insertChild",
158 "Index %d is out of range %d - %d",
159 newChildIndex, 0, getNumChildren());
160 return;
161 }
162 #endif /* DEBUG */
163
164 // See if adding at end
165 if (newChildIndex >= getNumChildren())
166 children->append(child);
167
168 else
169 children->insert(child, newChildIndex);
170 }
171
172 ////////////////////////////////////////////////////////////////////////
173 //
174 // Description:
175 // Returns child with the given index
176 //
177 // Use: public
178
179 SoNode *
180 SoGroup::getChild(int index) const
181 //
182 ////////////////////////////////////////////////////////////////////////
183 {
184 return(*children)[index];
185 }
186
187 ////////////////////////////////////////////////////////////////////////
188 //
189 // Description:
190 // Returns number of children
191 //
192 // Use: public
193
194 int
195 SoGroup::getNumChildren() const
196 //
197 ////////////////////////////////////////////////////////////////////////
198 {
199 return children->getLength();
200 }
201
202 ////////////////////////////////////////////////////////////////////////
203 //
204 // Description:
205 // Returns the first index (starting with 0) of a child that
206 // matches the given node pointer, or -1 if no such child is found.
207 //
208 // Use: public
209
210 int
211 SoGroup::findChild(const SoNode *child) const
212 //
213 ////////////////////////////////////////////////////////////////////////
214 {
215 int i, num;
216
217 num = getNumChildren();
218
219 for (i = 0; i < num; i++)
220 if (getChild(i) == child) return(i);
221
222 return(-1);
223 }
224
225 ////////////////////////////////////////////////////////////////////////
226 //
227 // Description:
228 // Removes the child with the given index from the group.
229 //
230 // Use: public
231
232 void
233 SoGroup::removeChild(int index)
234 //
235 ////////////////////////////////////////////////////////////////////////
236 {
237 #ifdef DEBUG
238 if (index < 0 || index >= getNumChildren()) {
239 SoDebugError::post("SoGroup::removeChild",
240 "Index %d is out of range %d - %d",
241 index, 0, getNumChildren() - 1);
242 return;
243 }
244 #endif /* DEBUG */
245
246 // Play it safe anyway...
247 if (index >= 0) {
248 children->remove(index);
249 }
250 }
251
252 ////////////////////////////////////////////////////////////////////////
253 //
254 // Description:
255 // Removes all children from the group.
256 //
257 // Use: public
258
259 void
260 SoGroup::removeAllChildren()
261 //
262 ////////////////////////////////////////////////////////////////////////
263 {
264 children->truncate(0);
265 }
266
267 ////////////////////////////////////////////////////////////////////////
268 //
269 // Description:
270 // Replaces child with given index with new child.
271 //
272 // Use: public
273
274 void
275 SoGroup::replaceChild(int index, SoNode *newChild)
276 //
277 ////////////////////////////////////////////////////////////////////////
278 {
279 #ifdef DEBUG
280 if (index < 0 || index >= getNumChildren()) {
281 SoDebugError::post("SoGroup::replaceChild",
282 "Index %d is out of range %d - %d",
283 index, 0, getNumChildren() - 1);
284 return;
285 }
286 #endif /* DEBUG */
287
288 // Play it safe anyway...
289 if (index >= 0)
290 children->set(index, newChild);
291 }
292
293 ////////////////////////////////////////////////////////////////////////
294 //
295 // Description:
296 // Returns pointer to children.
297 //
298 // Use: internal
299
300 SoChildList *
301 SoGroup::getChildren() const
302 //
303 ////////////////////////////////////////////////////////////////////////
304 {
305 return children;
306 }
307
308 ////////////////////////////////////////////////////////////////////////
309 //
310 // Description:
311 // Reads stuff into instance of SoGroup. Returns FALSE on error.
312 // This also deals with field data (if any), so this method should
313 // be useful for most subclasses of SoGroup as well.
314 //
315 // Use: protected
316
317 SbBool
318 SoGroup::readInstance(SoInput *in, unsigned short flags)
319 //
320 ////////////////////////////////////////////////////////////////////////
321 {
322 SbBool readOK = TRUE;
323
324 // First, turn off notification for this node
325 SbBool saveNotify = enableNotify(FALSE);
326
327 // Read field info. We can't just call
328 // SoFieldContainer::readInstance() to read the fields here
329 // because we need to tell the SoFieldData that it's ok if a name
330 // is found that is not a valid field name - it could be the name
331 // of a child node.
332 SbBool notBuiltIn; // Not used
333 readOK = getFieldData()->read(in, this, FALSE, notBuiltIn);
334 if (!readOK) return readOK;
335
336 // If binary BUT was written without children (which can happen
337 // if it was read as an unknown node and then written out in
338 // binary), don't try to read children:
339 if (!in->isBinary() || (flags & IS_GROUP))
340 readOK = readChildren(in);
341
342 // Re-enable notification
343 enableNotify(saveNotify);
344
345 return readOK;
346 }
347
348 ////////////////////////////////////////////////////////////////////////
349 //
350 // Description:
351 // Reads children into instance of SoGroup. Returns FALSE on error.
352 //
353 // Use: protected
354
355 SbBool
356 SoGroup::readChildren(SoInput *in)
357 //
358 ////////////////////////////////////////////////////////////////////////
359 {
360 SoBase *base;
361 SbBool ret = TRUE;
362
363 // If reading binary, read number of children first
364 if (in->isBinary()) {
365
366 int numToRead, i;
367
368 if (!in->read(numToRead))
369 ret = FALSE;
370
371 else {
372 for (i = 0; i < numToRead; i++) {
373
374 if (SoBase::read(in, base, SoNode::getClassTypeId()) &&
375 base != NULL)
376 addChild((SoNode *) base);
377
378 // Running out of children is now an error, since the
379 // number of children in the file must be exact
380 else {
381 ret = FALSE;
382 break;
383 }
384 }
385 // If we are reading a 1.0 file, read the GROUP_END_MARKER
386 if (ret && in->getIVVersion() == 1.0f) {
387 const int GROUP_END_MARKER = -1;
388 int marker;
389
390 // Read end marker if it is there. If not, some sort of
391 // error occurred.
392 if (! in->read(marker) || marker != GROUP_END_MARKER)
393 ret = FALSE;
394 }
395 }
396 }
397
398 // ASCII: Read children until none left. Deal with children
399 // causing errors by adding them as is.
400 else {
401 while (TRUE) {
402 ret = SoBase::read(in, base, SoNode::getClassTypeId()) && ret;
403
404 // Add child, even if error occurred, unless there is no
405 // child to add.
406 if (base != NULL)
407 addChild((SoNode *) base);
408
409 // Stop when we run out of valid children
410 else
411 break;
412 }
413 }
414
415 return ret;
416 }
417
418 ////////////////////////////////////////////////////////////////////////
419 //
420 // Description:
421 // Implements typical traversal.
422 //
423 // Use: extender
424
425 void
426 SoGroup::doAction(SoAction *action)
427 //
428 ////////////////////////////////////////////////////////////////////////
429 {
430 int numIndices;
431 const int *indices;
432
433 if (action->getPathCode(numIndices, indices) == SoAction::IN_PATH)
434 children->traverse(action, 0, indices[numIndices - 1]);
435
436 else
437 children->traverse(action);
438 }
439
440 ////////////////////////////////////////////////////////////////////////
441 //
442 // Description:
443 // Does the callback action
444 //
445 // Use: extender
446
447 void
448 SoGroup::callback(SoCallbackAction *action)
449 //
450 ////////////////////////////////////////////////////////////////////////
451 {
452 SoGroup::doAction(action);
453 }
454
455 ////////////////////////////////////////////////////////////////////////
456 //
457 // Description:
458 // Does the GL render action
459 //
460 // Use: extender
461
462 void
463 SoGroup::GLRender(SoGLRenderAction *action)
464
465 ////////////////////////////////////////////////////////////////////////
466 {
467 int numIndices;
468 const int *indices;
469 SoAction::PathCode pc = action->getPathCode(numIndices, indices);
470
471 // Perform fast-path GLRender traversal:
472 if (pc != SoAction::IN_PATH) {
473 action->pushCurPath();
474 for (int i = 0; i < children->getLength(); i++) {
475
476 action->popPushCurPath(i);
477 if (! action->abortNow())
478 ((SoNode*)(children->get(i)))->GLRender(action);
479 else
480 SoCacheElement::invalidate(action->getState());
481
482 // Stop if action has reached termination condition. (For
483 // example, search has found what it was looking for, or event
484 // was handled.)
485 if (action->hasTerminated())
486 break;
487 }
488 action->popCurPath();
489 }
490
491 else {
492
493 // This is the same as SoChildList::traverse(), except that it
494 // checks render abort for each child
495
496 int lastChild = indices[numIndices - 1];
497 for (int i = 0; i <= lastChild; i++) {
498
499 SoNode *child = (SoNode *) children->get(i);
500
501 if (pc == SoAction::OFF_PATH && ! child->affectsState())
502 continue;
503
504 action->pushCurPath(i);
505 if (action->getCurPathCode() != SoAction::OFF_PATH ||
506 child->affectsState()) {
507
508 if (! action->abortNow())
509 child->GLRender(action);
510 else
511 SoCacheElement::invalidate(action->getState());
512 }
513
514 action->popCurPath(pc);
515
516 if (action->hasTerminated())
517 break;
518 }
519 }
520 }
521
522 ////////////////////////////////////////////////////////////////////////
523 //
524 // Description:
525 // Does the get bounding box action. This takes care of averaging
526 // the centers of all children to get a combined center.
527 //
528 // Use: extender
529
530 void
531 SoGroup::getBoundingBox(SoGetBoundingBoxAction *action)
532 //
533 ////////////////////////////////////////////////////////////////////////
534 {
535 SbVec3f totalCenter(0,0,0);
536 int numCenters = 0;
537 int numIndices;
538 const int *indices;
539 int lastChild;
540
541 if (action->getPathCode(numIndices, indices) == SoAction::IN_PATH)
542 lastChild = indices[numIndices - 1];
543 else
544 lastChild = getNumChildren() - 1;
545
546 for (int i = 0; i <= lastChild; i++) {
547 children->traverse(action, i, i);
548 if (action->isCenterSet()) {
549 totalCenter += action->getCenter();
550 numCenters++;
551 action->resetCenter();
552 }
553 }
554 // Now, set the center to be the average. Don't re-transform the
555 // average, which should already be transformed.
556 if (numCenters != 0)
557 action->setCenter(totalCenter / numCenters, FALSE);
558 }
559
560 ////////////////////////////////////////////////////////////////////////
561 //
562 // Description:
563 // Does the handle event thing
564 //
565 // Use: extender
566
567 void
568 SoGroup::handleEvent(SoHandleEventAction *action)
569 //
570 ////////////////////////////////////////////////////////////////////////
571 {
572 SoGroup::doAction(action);
573 }
574
575 ////////////////////////////////////////////////////////////////////////
576 //
577 // Description:
578 // Pick.
579 //
580 // Use: extender
581
582 void
583 SoGroup::pick(SoPickAction *action)
584 //
585 ////////////////////////////////////////////////////////////////////////
586 {
587 SoGroup::doAction(action);
588 }
589
590 ////////////////////////////////////////////////////////////////////////
591 //
592 // Description:
593 // Does search...
594 //
595 // Use: extender
596
597 void
598 SoGroup::search(SoSearchAction *action)
599 //
600 ////////////////////////////////////////////////////////////////////////
601 {
602 // First see if the caller is searching for this node
603 SoNode::search(action);
604
605 // Then recurse on children if not already found
606 if (! action->isFound())
607 SoGroup::doAction(action);
608 }
609
610 ////////////////////////////////////////////////////////////////////////
611 //
612 // Description:
613 // Implements get matrix action.
614 //
615 // Use: extender
616
617 void
618 SoGroup::getMatrix(SoGetMatrixAction *action)
619 //
620 ////////////////////////////////////////////////////////////////////////
621 {
622 int numIndices;
623 const int *indices;
624
625 // Only need to compute matrix if group is a node in middle of
626 // current path chain or is off path chain (since the only way
627 // this could be called if it is off the chain is if the group is
628 // under a group that affects the chain).
629
630 switch (action->getPathCode(numIndices, indices)) {
631
632 case SoAction::NO_PATH:
633 break;
634
635 case SoAction::IN_PATH:
636 children->traverse(action, 0, indices[numIndices - 1]);
637 break;
638
639 case SoAction::BELOW_PATH:
640 break;
641
642 case SoAction::OFF_PATH:
643 children->traverse(action);
644 break;
645 }
646 }
647
648 ////////////////////////////////////////////////////////////////////////
649 //
650 // Description:
651 // Implements write action.
652 //
653 // Use: extender
654
655 void
656 SoGroup::write(SoWriteAction *action)
657
658 //
659 ////////////////////////////////////////////////////////////////////////
660 {
661
662 SoOutput *out = action->getOutput();
663
664 // In write-reference counting phase
665 if (out->getStage() == SoOutput::COUNT_REFS) {
666
667 // Increment our write reference count
668 addWriteReference(out);
669
670 // If this is the first reference (i.e., we don't now have
671 // multiple references), also count the appropriate children
672 if (! hasMultipleWriteRefs())
673 SoGroup::doAction(action);
674 }
675
676 // In writing phase, we have to do some more work
677 else if (! writeHeader(out, TRUE, FALSE)) {
678
679 // Write fields
680 const SoFieldData *fieldData = getFieldData();
681 fieldData->write(out, this);
682
683 // See which children to traverse
684 int i, lastChild, numIndices;
685 const int *indices;
686 if (action->getPathCode(numIndices, indices) == SoAction::IN_PATH)
687 lastChild = indices[numIndices - 1];
688 else
689 lastChild = getNumChildren() - 1;
690
691 int numChildren = 0;
692 for (i = 0; i <= lastChild; i++)
693 if (getChild(i)->shouldWrite() > 0)
694 numChildren++;
695
696 // If writing binary format, write out number of children
697 // that are going to be written
698 if (out->isBinary())
699 out->write(numChildren);
700
701 // In the writing stage, we can't use standard traversal,
702 // because if we are writing out one path of a path list, we
703 // might need to write out children that are not relevant to
704 // the current path (but are relevant to another path in the
705 // list). Therefore, we implement a different traversal that
706 // writes out each child that is referenced.
707 for (i = 0; i <= lastChild; i++)
708 if (getChild(i)->shouldWrite())
709 children->traverse(action, i);
710
711 // Write post-children stuff
712 writeFooter(out);
713 }
714 }
715
716 ////////////////////////////////////////////////////////////////////////
717 //
718 // Description:
719 // Copies the contents of the given node into this instance.
720 //
721 // Use: protected, virtual
722
723 void
724 SoGroup::copyContents(const SoFieldContainer *fromFC, SbBool copyConnections)
725 //
726 ////////////////////////////////////////////////////////////////////////
727 {
728 // Copy the usual stuff
729 SoNode::copyContents(fromFC, copyConnections);
730
731 // Copy the kids
732 const SoGroup *fromGroup = (const SoGroup *) fromFC;
733 for (int i = 0; i < fromGroup->getNumChildren(); i++) {
734
735 // If this node is being copied, it must be "inside" (see
736 // SoNode::copy() for details.) Therefore, all of its children
737 // must be inside, as well.
738 SoNode *fromKid = fromGroup->getChild(i);
739 SoNode *kidCopy = (SoNode *) findCopy(fromKid, copyConnections);
740
741 #ifdef DEBUG
742 if (kidCopy == NULL)
743 SoDebugError::post("(internal) SoGroup::copyContents",
744 "Child %d has not been copied yet", i);
745 #endif /* DEBUG */
746
747 addChild(kidCopy);
748 }
749 }