Debian Codegraph @

lib/nodekits/src/SoNodeKitPath.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.2 $
44 |
45 | Classes:
46 | SoNodeKitPath
47 |
48 | Author(s) : Paul Isaacs, 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/SoDB.h>
56 #include <Inventor/SoPath.h>
57 #include <Inventor/SoNodeKitPath.h>
58 #include <Inventor/nodekits/SoBaseKit.h>
59 #include <Inventor/errors/SoDebugError.h>
60 #include <Inventor/actions/SoSearchAction.h>
61
62 SoSearchAction *SoNodeKitPath::searchAction = NULL;
63
64 ////////////////////////////////////////////////////////////////////////
65 //
66 // SoNodeKitPath class
67 //
68 ////////////////////////////////////////////////////////////////////////
69
70 ////////////////////////////////////////////////////////////////////////
71 //
72 // Description:
73 // Gives the length of the nodeKitPath. This is 1 for the head
74 // plus one more for any other nodekit in the path.
75 //
76 // Use: public
77
78 int
79 SoNodeKitPath::getLength() const
80 //
81 ////////////////////////////////////////////////////////////////////////
82 {
83 SoFullPath *fp = (SoFullPath *) this;
84
85 if (fp->getLength() == 0)
86 return 0;
87
88 int count = 1;
89
90 for ( int i = 1; i < fp->getLength(); i++ )
91 if ( fp->getNode(i)->isOfType( SoBaseKit::getClassTypeId() ) )
92 count++;
93
94 return count;
95 }
96
97 ////////////////////////////////////////////////////////////////////////
98 //
99 // Description:
100 // Returns the last nodekit along the path, or the head if none are found.
101 //
102 // Use: public
103
104 SoNode *
105 SoNodeKitPath::getTail() const
106 //
107 ////////////////////////////////////////////////////////////////////////
108 {
109 SoFullPath *fp = (SoFullPath *) this;
110
111 if (fp->getLength() == 0)
112 return NULL;
113
114 for ( int i = fp->getLength() - 1; i >= 0; i-- )
115 if ( fp->getNode(i)->isOfType( SoBaseKit::getClassTypeId() ) )
116 return fp->getNode(i);
117
118 return fp->getHead();
119 }
120
121 ////////////////////////////////////////////////////////////////////////
122 //
123 // Description:
124 // Returns the i'th nodekit in the path. The head always counts as node 0
125 // whether it is a nodekit or not.
126 //
127 // Use: public
128
129 SoNode *
130 SoNodeKitPath::getNode(int i) const
131 //
132 ////////////////////////////////////////////////////////////////////////
133 {
134 SoFullPath *fp = (SoFullPath *) this;
135
136 if (fp->getLength() == 0 || i < 0 )
137 return NULL;
138
139 SoNode *answer;
140 int curKitCount = -1;
141
142 for ( int j = 0; j < fp->getLength(); j++ ) {
143 if (j == 0 || fp->getNode(j)->isOfType( SoBaseKit::getClassTypeId() )) {
144 answer = fp->getNode(j);
145 curKitCount++;
146
147 if (curKitCount == i )
148 return answer;
149 }
150 }
151 return answer;
152 }
153
154 ////////////////////////////////////////////////////////////////////////
155 //
156 // Description:
157 // Returns the i'th nodekit from the tail in the path.
158 // i.e. index 0 == tail, index 1 == 1 before tail, etc.
159 // whether it is a nodekit or not.
160 //
161 // Use: public
162
163 SoNode *
164 SoNodeKitPath::getNodeFromTail(int i) const
165 //
166 ////////////////////////////////////////////////////////////////////////
167 {
168 SoFullPath *fp = (SoFullPath *) this;
169
170 if (fp->getLength() == 0 )
171 return NULL;
172
173 if ( i < 0 )
174 // return last nodekit. Use 'this' not 'fp'
175 return getTail();
176
177 SoNode *answer;
178 int curKitCount = -1;
179
180 for ( int j = fp->getLength() - 1; j >= 0; j-- ) {
181 if (j == 0 || fp->getNode(j)->isOfType( SoBaseKit::getClassTypeId() )) {
182 answer = fp->getNode(j);
183 curKitCount++;
184
185 if (curKitCount == i )
186 return answer;
187 }
188 }
189 return answer;
190 }
191
192 ////////////////////////////////////////////////////////////////////////
193 //
194 // Description:
195 // Removes all nodes from the indexed nodeKit on
196 //
197 // Use: public
198
199 void
200 SoNodeKitPath::truncate(int start)
201 //
202 ////////////////////////////////////////////////////////////////////////
203 {
204 // Get the start'th nodekit
205 SoNode *n = getNode(start);
206
207 int fullStart = 0;
208 SoFullPath *fp = (SoFullPath *) this;
209 for ( int i = 0; i < fp->getLength(); i++ )
210 if ( fp->getNode(i) == n )
211 fullStart = i;
212
213 fp->truncate( fullStart);
214 }
215
216 ////////////////////////////////////////////////////////////////////////
217 //
218 // Description:
219 // Pop all nodes off the path up 'til the nodeKit before the last
220 // one.
221 //
222 // Use: public
223
224 void
225 SoNodeKitPath::pop()
226 //
227 ////////////////////////////////////////////////////////////////////////
228 {
229 truncate( getLength() - 1 );
230 }
231
232 ////////////////////////////////////////////////////////////////////////
233 //
234 // Description:
235 // Adds a node to the end of the current chain of nodes in a path.
236 // The node is passed by pointer, so it must be a descendant of the
237 // current last nodeKit in the chain.
238 // If it is used more than once as a descendant, the first occurrance
239 // is appended. If the path is empty, this is equivalent to setHead(node).
240 //
241 // Use: public
242
243 void
244 SoNodeKitPath::append(SoBaseKit *childKit)
245 //
246 ////////////////////////////////////////////////////////////////////////
247 {
248 // If the path is empty, this is a setHead() operation
249 if (getLength() == 0) {
250 setHead(childKit);
251 return;
252 }
253
254 // Get tail kit in path.
255 SoNode *tail;
256 tail = getTail();
257
258 #ifdef DEBUG
259 // Make sure tail node can have children.
260 if (tail->getChildren() == NULL ) {
261 SoDebugError::post("SoNodeKitPath::append",
262 "tail node can not have children");
263 return;
264 }
265 #endif
266
267 // Turn ON searching below nodekits...
268 SbBool wasSearching = SoBaseKit::isSearchingChildren();
269 SoBaseKit::setSearchingChildren(TRUE);
270
271 // Search for childKit under tail
272
273 if (searchAction == NULL)
274 searchAction = new SoSearchAction;
275 else
276 searchAction->reset();
277 searchAction->setNode( childKit );
278 searchAction->apply( tail );
279 SoFullPath *kidPath = (SoFullPath *) searchAction->getPath();
280
281 // Restore searching under nodekits...
282 SoBaseKit::setSearchingChildren(wasSearching);
283
284 #ifdef DEBUG
285 // Make sure we found it...
286 if (kidPath == NULL) {
287 SoDebugError::post("SoNodeKitPath::append",
288 "node to append is not a descendent of path tail");
289 return;
290 }
291 #endif
292
293 SoFullPath *fp = (SoFullPath *) this;
294
295 // truncate full path to end at tail (if the tail is a nodekit but
296 // the fullPath tail is not, this is necessary).
297 int i;
298 for ( i = fp->getLength() - 1; i >= 0; i-- ) {
299 if ( fp->getNode(i) != tail )
300 fp->pop();
301 else
302 break;
303 }
304
305 // append all nodes from the kidPath (except its head, which is tail of fp)
306 for ( i = 1; i < kidPath->getLength(); i++ )
307 fp->append( kidPath->getIndex(i) );
308 }
309
310 ////////////////////////////////////////////////////////////////////////
311 //
312 // Description:
313 // Adds all nodes in path to end of chain; head node of fromPath
314 // must be a descendant of current last nodeKit.
315 //
316 // Use: public
317
318 void
319 SoNodeKitPath::append(const SoNodeKitPath *fromPath)
320 //
321 ////////////////////////////////////////////////////////////////////////
322 {
323 #ifdef DEBUG
324 // Make sure there is already a node in the path
325 if (getLength() == 0) {
326 SoDebugError::post("SoNodeKitPath::append",
327 "no head node to append to");
328 return;
329 }
330 #endif
331
332 // Get tail of path.
333 SoNode *tail;
334 tail = getTail();
335
336 #ifdef DEBUG
337 // Make sure tail node can have children.
338 if (tail->getChildren() == NULL ) {
339 SoDebugError::post("SoNodeKitPath::append",
340 "tail node can not have children.");
341 return;
342 }
343 #endif
344
345 // Turn ON searching below nodekits...
346 SbBool wasSearching = SoBaseKit::isSearchingChildren();
347 SoBaseKit::setSearchingChildren(TRUE);
348
349 // Search for head of second path under tail
350 if (searchAction == NULL)
351 searchAction = new SoSearchAction;
352 else
353 searchAction->reset();
354 searchAction->setNode( fromPath->getHead() );
355 searchAction->apply( tail );
356
357 // Restore searching under nodekits...
358 SoBaseKit::setSearchingChildren(wasSearching);
359
360 SoFullPath *kidPath = (SoFullPath *) searchAction->getPath();
361
362 #ifdef DEBUG
363 // Make sure we found it...
364 if (kidPath == NULL) {
365 SoDebugError::post("SoNodeKitPath::append",
366 "head of append path is not descendent of path tail");
367 return;
368 }
369 #endif
370
371 SoFullPath *fp = (SoFullPath *) this;
372
373 // truncate full path to end at tail (if the tail is a nodekit but
374 // the fullPath tail is not, this is necessary).
375 int i;
376 for ( i = fp->getLength() - 1; i >= 0; i-- ) {
377 if ( fp->getNode(i) != tail )
378 fp->pop();
379 else
380 break;
381 }
382
383 // append all nodes from the kidPath (except the head, which is tail of fp)
384 for ( i = 1; i < kidPath->getLength(); i++ )
385 fp->append( kidPath->getIndex(i) );
386
387 // Append each node in fromPath (except head, which we just appended)
388 // to end of path
389 SoFullPath *fullFrom = (SoFullPath *) fromPath;
390 for (i = 1; i < fullFrom->getLength(); i++)
391 fp->append( fullFrom->getIndex(i) );
392 }
393
394 ////////////////////////////////////////////////////////////////////////
395 //
396 // Description:
397 // Returns TRUE if the passed node is in the path chain.
398 //
399 // Use: public
400
401 SbBool
402 SoNodeKitPath::containsNode(SoBaseKit *node) const
403 //
404 ////////////////////////////////////////////////////////////////////////
405 {
406 for (int i = 0; i < getLength(); i++)
407 if (getNode(i) == node)
408 return TRUE;
409
410 return FALSE;
411 }
412
413 ////////////////////////////////////////////////////////////////////////
414 //
415 // Description:
416 // If the paths have different head nodes, this returns -1.
417 // Otherwise, it returns the index into the chain of the last node
418 // (starting at the head) that is the same for both paths.
419 //
420 // Use: public
421
422 int
423 SoNodeKitPath::findFork(const SoNodeKitPath *path) const
424 //
425 ////////////////////////////////////////////////////////////////////////
426 {
427 int shorterLength, i;
428
429 // Return -1 if heads are different nodes
430 if (path->getHead() != getHead())
431 return -1;
432
433 // Find length of shorter path
434 shorterLength = path->getLength();
435 if (getLength() < shorterLength )
436 shorterLength = getLength();
437
438 // Return the index of the last pair of nodes that match
439 for (i = 1; i < shorterLength; i++)
440 if (getNode(i) != path->getNode(i))
441 return i - 1;
442
443 // All the nodes matched - return the index of the tail
444 return shorterLength - 1;
445 }
446
447 ////////////////////////////////////////////////////////////////////////
448 //
449 // Description:
450 // Comparison operator tests path equality.
451 // Two paths are equal if their chains of nodekits
452 // are the same length, the heads are the same, and all the nodekits
453 // on them are the same.
454 //
455 int
456 operator ==(const SoNodeKitPath &p1, const SoNodeKitPath &p2)
457 {
458 int i;
459
460 if (p1.getLength() != p2.getLength())
461 return FALSE;
462
463 // Compare path nodes from bottom up since there is more
464 // likelihood that they differ at the bottom. That means that
465 // unequal paths will exit this loop sooner.
466
467 for (i = p1.getLength() -1; i >= 0; --i )
468 if ( p1.getNode(i) != p2.getNode(i) )
469 return FALSE;
470
471 return TRUE;
472 }
473
474
475 ////////////////////////////////////////////////////////////////////////
476 //
477 // Description:
478 // Destructor
479 //
480 // Use: private
481
482 SoNodeKitPath::~SoNodeKitPath()
483 //
484 ////////////////////////////////////////////////////////////////////////
485 {
486 }
487