Debian Codegraph @

apps/demos/noodle/WorldInfo.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 | Classes:
39 | WorldInfo
40 |
41 | Author(s): Paul Isaacs
42 |
43 */
44
45
46 #include <cstdio>
47 #include <Inventor/SbLinear.h>
48 #include <Inventor/SoDB.h>
49 #include <Inventor/SoLists.h>
50 #include <Inventor/SbViewportRegion.h>
51 #include <Inventor/SoPickedPoint.h>
52
53 #include <Inventor/actions/SoGetBoundingBoxAction.h>
54 #include <Inventor/actions/SoSearchAction.h>
55 #include <Inventor/manips/SoHandleBoxManip.h>
56 #include <Inventor/nodes/SoSelection.h>
57 #include <Inventor/nodes/SoSeparator.h>
58 #include <Inventor/errors/SoDebugError.h>
59
60 #include "WorldInfo.h"
61 #include "GeneralizedCylinder.h"
62
63 WorldInfo::WorldInfo()
64 {
65 // Make worldRoot
66 worldRoot = new SoSeparator;
67 worldRoot->ref();
68
69 // Add selection node.
70
71 selector = new SoSelection;
72 worldRoot->addChild(selector);
73 selector->addSelectionCallback( &WorldInfo::selectionCB, this );
74 selector->addDeselectionCallback( &WorldInfo::deselectionCB, this );
75 selector->setPickFilterCallback( &WorldInfo::pickFilterCB, this );
76
77 // Put sceneRoot under selector
78 sceneRoot = new SoSeparator;
79 selector->addChild( sceneRoot );
80
81 // Create the deletedNoodles list. We store them so they can be un-deleted.
82 deletedNoodles = new SoNodeList;
83
84 // This piece will be dragged in z whenever the activePlane moves.
85 currentNoodle = NULL;
86
87 // Initialize other variables.
88 fileName = NULL;
89
90 manipType = SoTransform::getClassTypeId();
91 }
92
93 WorldInfo::~WorldInfo()
94 {
95 if ( worldRoot ) worldRoot->unref();
96 if ( deletedNoodles ) delete deletedNoodles;
97 }
98
99 /////////////////////////////////////////////////////////////////////
100 // Set the type of manipulator being used to move selected noodles.
101 // Then install one in each selected noodle.
102 //
103 void
104 WorldInfo::setManipType( SoType newType )
105 {
106 if ( newType == manipType)
107 return;
108
109 if ( ! newType.isDerivedFrom( SoTransform::getClassTypeId() ))
110 return;
111
112 manipType = newType;
113
114 // Get all selections; replace their transforms with the new type.
115 const SoPathList *pl = selector->getList();
116 for (int i = 0; i < selector->getNumSelected(); i++ ) {
117
118 SoNode *t = (*pl)[i]->getTail();
119 if (t->isOfType(GeneralizedCylinder::getClassTypeId()))
120 ((GeneralizedCylinder *)t)->changeTransformType(newType);
121 }
122 }
123
124 /////////////////////////////////////////////////////////////////////
125 // Returns a copy of the scene with all noodles replaced by subgraphs
126 // of standard inventor nodes.
127 SoSeparator *
128 WorldInfo::getVanillaSceneCopy()
129 {
130 if (sceneRoot == NULL)
131 return NULL;
132
133 SoSeparator *myCopy = (SoSeparator *) sceneRoot->copy();
134 myCopy->ref();
135
136 // Replace every GeneralizedCylinder in the copy with just the
137 // data that draws, removing the nodekit.
138 SoSearchAction *sa = new SoSearchAction;
139 sa->setType( GeneralizedCylinder::getClassTypeId() );
140 sa->setInterest( SoSearchAction::ALL );
141 sa->apply( myCopy );
142 SoPathList *genCylPaths = &(sa->getPaths());
143 SoFullPath *myPath;
144 SoNode *myParent;
145 GeneralizedCylinder *gc;
146 SoGroup *parentGroup;
147
148 for ( int i = 0; i < genCylPaths->getLength(); i++ ) {
149 myPath = (SoFullPath *) (*genCylPaths)[i];
150 myParent = myPath->getNode( myPath->getLength() - 2 );
151 gc = (GeneralizedCylinder *) myPath->getTail();
152 if ( myParent->isOfType( SoGroup::getClassTypeId() ) == FALSE ) {
153 fprintf(stderr,"Error converting scene to vanilla. Found a \n");
154 fprintf(stderr,"non-group parent of the generalized cyliinder\n");
155 break;
156 }
157 parentGroup = (SoGroup *) myParent;
158 SoSeparator *vanillaObject = gc->makeVanillaVersion();
159 parentGroup->replaceChild( gc, vanillaObject );
160 }
161
162 // Delete the search action BEFORE unrefNodelete. This takes
163 // count down to 1.
164 delete sa;
165
166 // Take count from 1 to 0
167 myCopy->unrefNoDelete();
168
169 return myCopy;
170 }
171
172 void
173 WorldInfo::setScene( SoSeparator *newScene )
174 {
175 if (newScene == sceneRoot)
176 return;
177
178 // Deselect everything.
179 selector->deselectAll();
180
181 // Always have at least an empty separator in the scene.
182 if (newScene == NULL)
183 newScene = new SoSeparator;
184
185 if (sceneRoot == NULL) {
186 #ifdef DEBUG
187 SoDebugError::post("WorldInfo::setScene", "old sceneRoot is NULL");
188 #endif
189 selector->insertChild( newScene, 0 );
190 }
191 else
192 selector->replaceChild( sceneRoot, newScene );
193
194 sceneRoot = newScene;
195
196 // Now, select the first noodle in the scene
197 setFirstNoodleCurrent();
198 }
199
200 SbBool
201 WorldInfo::isSceneEmpty()
202 {
203 if (sceneRoot == NULL) {
204 #ifdef DEBUG
205 SoDebugError::post("WorldInfo::setScene", "old sceneRoot is NULL");
206 #endif
207 return TRUE;
208 }
209 else
210 return ( sceneRoot->getNumChildren() == 0);
211 }
212
213 void
214 WorldInfo::setFileName( char *newFileName )
215 {
216 if (newFileName == NULL)
217 fileName = NULL;
218 else
219 fileName = strdup( newFileName );
220 }
221
222 void
223 WorldInfo::addNoodle( GeneralizedCylinder *newNoodle )
224 {
225 if (newNoodle == NULL)
226 return;
227 sceneRoot->addChild( newNoodle );
228 }
229
230 void
231 WorldInfo::deleteNoodle( GeneralizedCylinder *victim )
232 {
233 // See if victim is in the selection list...
234 // We'd prefer to delete based on selection path, if possible, since we
235 // can then deselect.
236 SoFullPath *victimPath = NULL;
237 for (int i = 0; i < selector->getNumSelected(); i++ ) {
238 if ( ((SoNodeKitPath *)selector->getPath(i))->getTail() == victim ) {
239 victimPath = (SoFullPath *) selector->getPath(i);
240 victimPath->ref();
241 break;
242 }
243 }
244 if (victimPath) {
245 // deselect old one if it is selected...
246 selector->deselect(victimPath);
247 }
248 else {
249 // If it wasn't a selection, get a path to it...
250 SoSearchAction sa;
251 sa.setNode( victim );
252 sa.setInterest( SoSearchAction::FIRST );
253 SbBool wasSearchingKits = SoBaseKit::isSearchingChildren();
254 SoBaseKit::setSearchingChildren(TRUE);
255 sa.apply( selector );
256 SoBaseKit::setSearchingChildren(wasSearchingKits);
257 victimPath = (SoFullPath *) sa.getPath();
258 if (victimPath)
259 victimPath->ref();
260 }
261
262 if ( ! victimPath )
263 return;
264
265 // Remove piece from parent.
266 if (victimPath != NULL) {
267 // Get index of victim in the path...
268 int objInd;
269 for (objInd = victimPath->getLength() - 1; objInd >= 0; objInd --) {
270 if ( victimPath->getNode(objInd) == victim )
271 break;
272 }
273 SoNode *parent = NULL;
274 SoType bkt = SoBaseKit::getClassTypeId();
275 // Remove victim from its parent.
276 // For parent, if there's a nodekit above victim, use that first kit.
277 for (int pInd = objInd - 1; pInd >= 0; pInd --) {
278 if (victimPath->getNode(pInd)->isOfType( bkt ) ) {
279 parent = victimPath->getNode(pInd);
280 SoBaseKit *k = (SoBaseKit *)parent;
281 k->setPart( k->getPartString(victim), NULL );
282 break;
283 }
284 }
285 // Otherwise, use the first node above victim.
286 if ( parent == NULL ) {
287 parent = victimPath->getNodeFromTail( 1 );
288 if (parent->isOfType( SoGroup::getClassTypeId() ))
289 ((SoGroup *)parent)->removeChild( victim );
290 }
291
292 }
293 victimPath->unref();
294 }
295
296
297 void
298 WorldInfo::deleteCurrentNoodle()
299 {
300 if (currentNoodle == NULL)
301 return;
302
303 currentNoodle->ref();
304 deleteNoodle( currentNoodle );
305 deletedNoodles->append( currentNoodle );
306 currentNoodle->unref();
307 }
308
309 GeneralizedCylinder *
310 WorldInfo::undeleteNoodle()
311 {
312 int num = deletedNoodles->getLength();
313 if ( num <= 0)
314 return NULL;
315
316 GeneralizedCylinder *theNoodle
317 = (GeneralizedCylinder *) (*deletedNoodles)[num - 1];
318
319 theNoodle->ref();
320
321 addNoodle( theNoodle );
322
323 // Since we're undeleting the noodle, make it the only selection.
324 selector->deselectAll();
325 selector->select( theNoodle );
326
327 setCurrentNoodle( theNoodle );
328
329 theNoodle->unrefNoDelete();
330
331 deletedNoodles->remove( num - 1);
332
333 return theNoodle;
334 }
335
336 GeneralizedCylinder *
337 WorldInfo::setFirstNoodleCurrent()
338 {
339 if (sceneRoot == NULL)
340 return NULL;
341
342 SoSearchAction sa;
343 sa.setType( GeneralizedCylinder::getClassTypeId() );
344 sa.apply( sceneRoot );
345 SoPath *pathToFirst = sa.getPath();
346 if (pathToFirst == NULL)
347 return NULL;
348
349 GeneralizedCylinder *first = (GeneralizedCylinder *) pathToFirst->getTail();
350
351 // Since we're undeleting the noodle, make it the only selection.
352 selector->deselectAll();
353 selector->select( first );
354
355 setCurrentNoodle( first );
356
357 return first;
358 }
359
360 GeneralizedCylinder *
361 WorldInfo::addNewNoodle()
362 {
363 GeneralizedCylinder *newOne = new GeneralizedCylinder;
364 newOne->ref();
365
366 addNoodle( newOne );
367
368 // Since we're adding a new noodle, make it the only selection.
369 selector->deselectAll();
370 selector->select( newOne );
371
372 setCurrentNoodle( newOne );
373 newOne->unrefNoDelete();
374
375 return newOne;
376 }
377
378 void
379 WorldInfo::setCurrentNoodle( GeneralizedCylinder *newNoodle )
380 {
381 currentNoodle = newNoodle;
382
383 // Add a manipulator if necessary...
384 if (currentNoodle)
385 currentNoodle->changeTransformType( getManipType() );
386 }
387
388 void
389 WorldInfo::selectionCB(void *userData, SoPath *selectPath)
390 {
391 // Return if pickFilter truncated path down to nothing.
392 if (selectPath->getLength() == 0)
393 return;
394
395 WorldInfo *myself = (WorldInfo *) userData;
396
397 SoNodeKitPath *nkPath = (SoNodeKitPath *) selectPath;
398 GeneralizedCylinder *g = (GeneralizedCylinder *) nkPath->getTail();
399
400 myself->setCurrentNoodle( g );
401 }
402
403 void
404 WorldInfo::deselectionCB(void *, SoPath *deselectPath)
405 {
406 if (deselectPath->getLength() == 0)
407 return;
408
409 SoNodeKitPath *nkPath = (SoNodeKitPath *) deselectPath;
410 GeneralizedCylinder *g = (GeneralizedCylinder *) nkPath->getTail();
411
412 g->changeTransformType( SoTransform::getClassTypeId() );
413 }
414
415 SoPath *
416 WorldInfo::pickFilterCB(void *, const SoPickedPoint *pick )
417 {
418 SoFullPath *fullP = (SoFullPath *) pick->getPath();
419
420 // Go up the path, and keep popping nodes until the tail is a
421 // Generalized Cylinder kit...
422 // If we hit the selection node first, then just stop right there...
423 while ( fullP->getLength() > 1 ) {
424
425 // If we're at a noodle, stop:
426 if ( fullP->getTail()->isOfType(
427 GeneralizedCylinder::getClassTypeId()))
428 return fullP;
429
430 // If we're at the selection node, stop.
431 if ( fullP->getTail()->isOfType( SoSelection::getClassTypeId()))
432 return fullP;
433
434 // Otherwise, pop off the end of the path.
435 fullP->pop();
436 }
437
438 // If we got here, the path emptied out.
439 return NULL;
440 }