Debian Codegraph @

apps/converters/ivdowngrade/downgradeToV1.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) 1993-94 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.3 $
44 |
45 | Author(s) : David Mott
46 |
47 ______________ S I L I C O N G R A P H I C S I N C . ____________
48 _______________________________________________________________________
49 */
50
51 #include <cstdio>
52
53 #include <assert.h>
54 #include <Inventor/SbDict.h>
55 #include <Inventor/SoDB.h>
56 #include <Inventor/SoInteraction.h>
57 #include <Inventor/SoInput.h>
58 #include <Inventor/actions/SoSearchAction.h>
59 #include <Inventor/actions/SoWriteAction.h>
60 #include <Inventor/errors/SoReadError.h>
61 #include <Inventor/manips/SoCenterballManip.h>
62 #include <Inventor/manips/SoDirectionalLightManip.h>
63 #include <Inventor/manips/SoHandleBoxManip.h>
64 #include <Inventor/manips/SoJackManip.h>
65 #include <Inventor/manips/SoPointLightManip.h>
66 #include <Inventor/manips/SoSpotLightManip.h>
67 #include <Inventor/manips/SoTabBoxManip.h>
68 #include <Inventor/manips/SoTrackballManip.h>
69 #include <Inventor/manips/SoTransformBoxManip.h>
70 #include <Inventor/manips/SoTransformManip.h>
71 #include <Inventor/nodes/SoAnnotation.h>
72 #include <Inventor/nodes/SoAntiSquish.h>
73 #include <Inventor/nodes/SoAsciiText.h>
74 #include <Inventor/nodes/SoBlinker.h>
75 #include <Inventor/nodes/SoClipPlane.h>
76 #include <Inventor/nodes/SoColorIndex.h>
77 #include <Inventor/nodes/SoDirectionalLight.h>
78 #include <Inventor/nodes/SoDrawStyle.h>
79 #include <Inventor/nodes/SoEnvironment.h>
80 #include <Inventor/nodes/SoFile.h>
81 #include <Inventor/nodes/SoFont.h>
82 #include <Inventor/nodes/SoFontStyle.h>
83 #include <Inventor/nodes/SoIndexedTriangleStripSet.h>
84 #include <Inventor/nodes/SoLabel.h>
85 #include <Inventor/nodes/SoLevelOfDetail.h>
86 #include <Inventor/nodes/SoLOD.h>
87 #include <Inventor/nodes/SoPendulum.h>
88 #include <Inventor/nodes/SoPickStyle.h>
89 #include <Inventor/nodes/SoPointLight.h>
90 #include <Inventor/nodes/SoRotor.h>
91 #include <Inventor/nodes/SoSelection.h>
92 #include <Inventor/nodes/SoSeparator.h>
93 #include <Inventor/nodes/SoShapeHints.h>
94 #include <Inventor/nodes/SoShuttle.h>
95 #include <Inventor/nodes/SoSpotLight.h>
96 #include <Inventor/nodes/SoSurroundScale.h>
97 #include <Inventor/nodes/SoSwitch.h>
98 #include <Inventor/nodes/SoText3.h>
99 #include <Inventor/nodes/SoTexture2.h>
100 #include <Inventor/nodes/SoTextureCoordinateDefault.h>
101 #include <Inventor/nodes/SoTextureCoordinatePlane.h>
102 #include <Inventor/nodes/SoTransformSeparator.h>
103 #include <Inventor/nodes/SoVertexProperty.h>
104 #include <Inventor/nodes/SoWWWAnchor.h>
105 #include <Inventor/nodes/SoWWWInline.h>
106
107 #include "SoEnvironmentV1.h"
108 #include "SoPickStyleV1.h"
109 #include "SoShapeHintsV1.h"
110 #include "SoTextureCoordinatePlaneV1.h"
111 #include "SoIndexedTriangleMeshV1.h"
112 #include "Util.h"
113 #include "SoAsciiTextV2.h"
114 #include "SoVertexPropertyV2.h"
115
116 SbDict *version2Only = NULL;
117 SbDict *needsDowngrading = NULL;
118
119 #define DICT_ENTER(dict,nodeClass,dictData) \
120 (dict->enter((unsigned long) nodeClass::getClassTypeId().getName().getString(), (void *) dictData))
121
122 #define DICT_FIND(dict,node,dictData) \
123 (dict->find((unsigned long) node->getTypeId().getName().getString(), dictData))
124
125 typedef SoNode *DowngradeFunc(SoNode *n);
126
127 // Extern
128 extern SoNode *downgradeVertexShape(SoVertexShape *vs);
129
130 // Forward reference.
131 SoNode *downgradeCopy(SoNode *node);
132
133
134 // We do not want the fields or the children of the Separator (v1.0 did not
135 // have fields, and the children we'll copy later)
136 SoNode *
137 downgradeSep(SoNode *)
138 {
139 return new SoSeparator;
140 }
141
142 // We do not want the fields or the children of the Selection (v1.0 did not
143 // have fields, and the children we'll copy later)
144 SoNode *
145 downgradeSelection(SoNode *)
146 {
147 return new SoSelection;
148 }
149
150 SoNode *
151 downgradeInline(SoNode *node2)
152 {
153 SoGroup *group = new SoGroup;
154 SoWWWInline *winline = (SoWWWInline *) node2;
155
156 if (winline->getChildData() != NULL)
157 group->addChild(downgradeCopy(winline->getChildData()));
158
159 return group;
160 }
161
162 SoNode *
163 downgradeFontStyle(SoNode *node2)
164 {
165 // Downgrade to SoFont
166 SoFontStyle *fs = (SoFontStyle *) node2;
167 SoFont *f = new SoFont;
168
169 f->name = fs->getFontName();
170 COPY_FIELD(f, fs, size); // f->size = fs->size
171
172 return f;
173 }
174
175 SoNode *
176 downgradeAsciiText(SoNode *node2)
177 {
178 return SoAsciiTextV2::convert((SoAsciiText *) node2);
179 }
180
181 SoNode *
182 downgradeVertexProperty(SoNode *node2)
183 {
184 // Have to downgrade the group that gets returned
185 // since SoPackedColor (a field of VertexProperty) must get downgraded
186 // after it is convert to the PackedColor node.
187 SoGroup *g = SoVertexPropertyV2::convert((SoVertexProperty *) node2);
188 return downgradeCopy(g);
189 }
190
191 // LOD and LevelOfDetail can be written as SoSwitch.
192 // Set whichChild to 0 to show highest detail.
193 SoNode *
194 downgradeSwitch(SoNode *)
195 {
196 SoSwitch *sw = new SoSwitch;
197 sw->whichChild = 0;
198 return sw;
199 }
200
201 SoNode *
202 downgradeDrawStyle(SoNode *node2)
203 {
204 // v1.0 does not have a pointSize field. Hide it.
205 SoDrawStyle *ds = (SoDrawStyle *) node2;
206 ds->pointSize.setDefault(TRUE);
207 return ds;
208 }
209
210 SoNode *
211 downgradeLight(SoNode *node2)
212 {
213 // v1.0 does not have a pointSize field. Hide it.
214 SoLight *light = (SoLight *) node2;
215 light->on.setDefault(TRUE);
216 return light;
217 }
218
219 SoNode *
220 downgradeEnv(SoNode *node2)
221 {
222 return SoEnvironmentV1::downgrade((SoEnvironment *) node2);
223 }
224
225 SoNode *
226 downgradePickStyle(SoNode *node2)
227 {
228 return SoPickStyleV1::downgrade((SoPickStyle *) node2);
229 }
230
231 SoNode *
232 downgradeText3(SoNode *node2)
233 {
234 SoText3 *t = (SoText3 *) node2;
235 if (t->parts.isDefault())
236 t->parts.setValue(SoText3::FRONT); // 2.0 default means FRONT in 1.0
237 return t;
238 }
239
240 SoNode *
241 downgradeShapeHints(SoNode *node2)
242 {
243 return SoShapeHintsV1::downgrade((SoShapeHints *) node2);
244 }
245
246 SoNode *
247 downgradeTexture2(SoNode *node2)
248 {
249 // v1.0 does not have an image field. Hide it.
250 SoTexture2 *t = (SoTexture2 *) node2;
251 t->image.setDefault(TRUE);
252 return t;
253 }
254
255 SoNode *
256 downgradeTexCoordPlane(SoNode *node2)
257 {
258 return SoTextureCoordinatePlaneV1::downgrade((SoTextureCoordinatePlane *) node2);
259 }
260
261 SoNode *
262 downgradeIdxTriStripSet(SoNode *node2)
263 {
264 return SoIndexedTriangleMeshV1::downgrade((SoIndexedTriangleStripSet *) node2);
265 }
266
267 //
268 // Traverse the scene graph, converting from 2.0 to 1.0.
269 // This is where the work is done.
270 //
271 SoNode *
272 downgradeCopy(SoNode *node)
273 {
274 SoNode *nodeCopy = NULL;
275 SoField *altRep;
276 void *dictData;
277
278 // If not an Inventor built in node, do not try to copy.
279 // Simply return a label.
280 if (! node->getIsBuiltIn()) {
281 // If there is an alternate representation, use it
282 if ((altRep = node->getField("alternateRep")) != NULL &&
283 (altRep->isOfType(SoSFNode::getClassTypeId()))) {
284 nodeCopy = downgradeCopy(((SoSFNode *) altRep)->getValue());
285 nodeCopy->setName("alternateRep");
286 }
287 else {
288 SoLabel *l = new SoLabel;
289 l->label = node->getTypeId().getName();
290 nodeCopy = l;
291 }
292 }
293 // If not a group, "copy" and return
294 else if (! node->isOfType(SoGroup::getClassTypeId())) {
295 // Is this a version 2.0 only node?
296 if (DICT_FIND(version2Only, node, dictData) ) {
297 SoLabel *l = new SoLabel;
298 l->label = node->getTypeId().getName();
299 nodeCopy = l;
300 }
301
302 // Is this a nodekit?
303 else if ( node->isOfType(SoBaseKit::getClassTypeId())) {
304 // [1] Create a vanilla group instead:
305 SoGroup *grp = new SoGroup;
306 // [2] Copy the children,
307 SoChildList *kids = ((SoBaseKit *) node)->getChildren();
308 if (kids != NULL) {
309 for (int i = 0; i < kids->getLength(); i++)
310 grp->addChild( downgradeCopy( (*kids)[i] ) );
311 }
312 // [3] Add a first child indicating that this used to be a kit.
313 SoLabel *l = new SoLabel;
314 l->label = node->getTypeId().getName();
315 grp->insertChild(l,0);
316 // [4] assign grp to be the nodeCopy
317 nodeCopy = grp;
318 }
319
320 // Or does it need downgrading?
321 else if (DICT_FIND(needsDowngrading, node, dictData)) {
322 // downgrade! (dictData was set in DICT_FIND)
323 DowngradeFunc *f = (DowngradeFunc *) dictData;
324 nodeCopy = (*f)(node);
325 }
326
327 // Is it a vertex shape? Need to remove the vertexProperty field if set.
328 else if ( node->isOfType(SoVertexShape::getClassTypeId())) {
329 nodeCopy = downgradeVertexShape((SoVertexShape *) node);
330
331 // If the shape was downgraded to something else, we have to convert
332 // that to 1.0 format.
333 if (nodeCopy != node)
334 nodeCopy = downgradeCopy(nodeCopy);
335 }
336
337 // If not a group, simply use this node, first disconnecting fields
338 // (We could copy it, but that can get expensive with large data sets.)
339 else {
340 SoFieldList list;
341 int num = node->getFields(list);
342 for (int i = 0; i < num; i++) {
343 if (list[i]->isConnected())
344 list[i]->disconnect();
345 }
346 nodeCopy = node;
347 }
348 }
349
350 // For a group, copy the node, then traverse the children
351 else {
352 SoGroup *group2 = (SoGroup *) node; // version 2.0
353 SoGroup *group1; // version 1.0
354
355 // Is this a version 2.0 only node?
356 if (DICT_FIND(version2Only, node, dictData)) {
357 // Treat it like a group
358 group1 = new SoGroup;
359 }
360
361 // Or does it need downgrading?
362 else if (DICT_FIND(needsDowngrading, node, dictData)) {
363 // downgrade! (dictData was set in DICT_FIND)
364 DowngradeFunc *f = (DowngradeFunc *) dictData;
365 group1 = (SoGroup *)(*f)(node);
366 }
367
368 // Else use the group. We have to downgrade its children.
369 else {
370 nodeCopy = group2;
371 for (int i = 0; i < group2->getNumChildren(); i++) {
372 SoNode *n = downgradeCopy(group2->getChild(i));
373 if (n != group2->getChild(i))
374 group2->replaceChild(i, n);
375 }
376
377 return nodeCopy;
378 }
379
380 // Traverse to copy the children from group2 to group1
381 for (int i = 0; i < group2->getNumChildren(); i++)
382 group1->addChild(downgradeCopy(group2->getChild(i)));
383
384 nodeCopy = group1;
385 }
386
387 return nodeCopy;
388 }
389
390 //
391 // This routine searches for and expands all SoFile nodes in the
392 // given scene graph. It does this by making all the children of a
393 // SoFile node the children of its parent.
394 // (Code for this function taken from ivcat)
395 //
396 void
397 expandFileNodes(SoNode *&root)
398 {
399 //
400 // Special case: if root is a file node, replace it with a group.
401 //
402 if (root->isOfType(SoFile::getClassTypeId())) {
403 SoFile *f = (SoFile *)root;
404 SoGroup *g = f->copyChildren();
405 root->unref();
406 root = g;
407 root->ref();
408 }
409
410 // Search for all file nodes
411 SoSearchAction sa;
412 sa.setType(SoFile::getClassTypeId());
413 sa.setInterest(SoSearchAction::FIRST);
414 sa.setSearchingAll(TRUE);
415
416 sa.apply(root);
417
418 // We'll keep on searching until there are no more file nodes
419 // left. We don't search for all file nodes at once, because we
420 // need to modify the scene graph, and so the paths returned may
421 // be truncated (if there are several file nodes under a group, if
422 // there are files within files, etc). Dealing properly with that
423 // is complicated-- it is easier (but slower) to just reapply the
424 // search until it fails.
425 // We need an SoFullPath here because we're searching node kit
426 // contents.
427 SoFullPath *p = (SoFullPath *) sa.getPath();
428 while (p != NULL) {
429 SoGroup *parent = (SoGroup *)p->getNodeFromTail(1);
430 assert(parent != NULL);
431
432 SoFile *file = (SoFile *)p->getTail();
433
434 // If the filename includes a directory path, add the directory name
435 // to the list of directories where to look for input files
436 const char* filename = file->name.getValue().getString();
437 const char *slashPtr;
438 char *searchPath = NULL;
439 if ((slashPtr = strrchr(filename, '/')) != NULL) {
440 searchPath = strdup(filename);
441 searchPath[slashPtr - filename] = '\0';
442 SoInput::addDirectoryFirst(searchPath);
443 }
444
445 int fileIndex = p->getIndexFromTail(0);
446 assert(fileIndex != -1);
447
448 // Now, add group of all children to file's parent's list of children,
449 // right after the file node:
450 SoGroup *fileGroup = file->copyChildren();
451 fileGroup->ref();
452 if (fileGroup != NULL) {
453 parent->insertChild(fileGroup, fileIndex+1);
454 }
455 else {
456 // So we can at least see where the file node contents were
457 // supposed to go.
458 parent->insertChild(new SoGroup, fileIndex+1);
459 }
460
461 // And expand the child node from the group.
462 // Note that if the File node is multiply instanced,
463 // the groups will not be instanced, but the children of the
464 // groups will be.
465 parent->removeChild(fileIndex);
466
467 sa.apply(root);
468 p = (SoFullPath *) sa.getPath();
469 }
470 }
471
472
473 ////////////////////////////////////////////////////////////////////////
474 //
475 // Down-grade the passed scene to 1.0 format.
476 //
477 SoNode *
478 downgradeToV1(SoNode *n)
479 //
480 ////////////////////////////////////////////////////////////////////////
481 {
482 // "Copy" the scene graph from 2.0 to 1.0 format.
483 // That is, traverse the graph reconstrucing a new one.
484 // When a node is the same format in 1.0 and 2.0, we simply copy it.
485 // When the format has changed, we build the 1.0 counterpart.
486
487 // First, set up a dictionary of nodes that exist in 2.0,
488 // but have no 1.0 counterpart.
489 version2Only = new SbDict;
490
491 #define VERSION_2_DICT_ENTER(nodeClass) \
492 DICT_ENTER(version2Only, nodeClass, NULL)
493
494 VERSION_2_DICT_ENTER(SoAnnotation);
495 VERSION_2_DICT_ENTER(SoAntiSquish);
496 VERSION_2_DICT_ENTER(SoBlinker);
497 VERSION_2_DICT_ENTER(SoClipPlane);
498 VERSION_2_DICT_ENTER(SoColorIndex);
499 VERSION_2_DICT_ENTER(SoPendulum);
500 VERSION_2_DICT_ENTER(SoRotor);
501 VERSION_2_DICT_ENTER(SoShuttle);
502 VERSION_2_DICT_ENTER(SoSurroundScale);
503 VERSION_2_DICT_ENTER(SoTextureCoordinateDefault);
504 VERSION_2_DICT_ENTER(SoTransformSeparator);
505 VERSION_2_DICT_ENTER(SoCenterballManip);
506 VERSION_2_DICT_ENTER(SoDirectionalLightManip);
507 VERSION_2_DICT_ENTER(SoHandleBoxManip);
508 VERSION_2_DICT_ENTER(SoJackManip);
509 VERSION_2_DICT_ENTER(SoPointLightManip);
510 VERSION_2_DICT_ENTER(SoSpotLightManip);
511 VERSION_2_DICT_ENTER(SoTabBoxManip);
512 VERSION_2_DICT_ENTER(SoTrackballManip);
513 VERSION_2_DICT_ENTER(SoTransformBoxManip);
514 VERSION_2_DICT_ENTER(SoTransformManip);
515
516 // And a dictionary of nodes that need downgrading
517 needsDowngrading = new SbDict;
518
519 #define DOWNGRADE_DICT_ENTER(nodeClass, func) \
520 DICT_ENTER(needsDowngrading, nodeClass, func)
521
522 DOWNGRADE_DICT_ENTER(SoSelection, downgradeSelection);
523 DOWNGRADE_DICT_ENTER(SoSeparator, downgradeSep);
524 DOWNGRADE_DICT_ENTER(SoWWWAnchor, downgradeSep);
525 DOWNGRADE_DICT_ENTER(SoWWWInline, downgradeInline);
526 DOWNGRADE_DICT_ENTER(SoLevelOfDetail, downgradeSwitch);
527 DOWNGRADE_DICT_ENTER(SoLOD, downgradeSwitch);
528 DOWNGRADE_DICT_ENTER(SoDirectionalLight, downgradeLight);
529 DOWNGRADE_DICT_ENTER(SoPointLight, downgradeLight);
530 DOWNGRADE_DICT_ENTER(SoSpotLight, downgradeLight);
531 DOWNGRADE_DICT_ENTER(SoDrawStyle, downgradeDrawStyle);
532 DOWNGRADE_DICT_ENTER(SoEnvironment, downgradeEnv);
533 DOWNGRADE_DICT_ENTER(SoPickStyle, downgradePickStyle);
534 DOWNGRADE_DICT_ENTER(SoText3, downgradeText3);
535 DOWNGRADE_DICT_ENTER(SoShapeHints, downgradeShapeHints);
536 DOWNGRADE_DICT_ENTER(SoTexture2, downgradeTexture2);
537 DOWNGRADE_DICT_ENTER(SoTextureCoordinatePlane, downgradeTexCoordPlane);
538 DOWNGRADE_DICT_ENTER(SoIndexedTriangleStripSet, downgradeIdxTriStripSet);
539 DOWNGRADE_DICT_ENTER(SoFontStyle, downgradeFontStyle);
540 DOWNGRADE_DICT_ENTER(SoAsciiText, downgradeAsciiText);
541 DOWNGRADE_DICT_ENTER(SoVertexProperty, downgradeVertexProperty);
542
543 // Traverse the graph
544 return downgradeCopy(n);
545 }