Debian Codegraph @

apps/converters/ivdowngrade/main.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-95 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.4 $
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 <unistd.h>
52 #include <cstdlib>
53 #include <cstdio>
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/nodes/SoFile.h>
62 #include <Inventor/nodes/SoSeparator.h>
63 #include "SoEnvironmentV1.h"
64 #include "SoPickStyleV1.h"
65 #include "SoShapeHintsV1.h"
66 #include "SoTextureCoordinatePlaneV1.h"
67 #include "SoIndexedTriangleMeshV1.h"
68 #include "SoWWWAnchorV2.h"
69 #include "SoWWWInlineV2.h"
70 #include "SoAsciiTextV2.h"
71 #include "SoFontStyleV2.h"
72 #include "SoPackedColorV2.h"
73 #include "SoVertexPropertyV2.h"
74 #include "SoText2V2.h"
75 #include "SoText3V2.h"
76 #include "SoLODV2.h"
77
78 static char *infile = NULL;
79 static char *outfile = NULL;
80 static float targetVersion = 2.0;
81
82 extern void expandFileNodes(SoNode *&root);
83 extern SoNode *downgradeToV1(SoNode *n);
84 extern SoNode *downgradeToV2(SoNode *n);
85
86
87 void
88 getargs(int argc, char *argv[])
89 {
90 int c, err=0, showUsage = 0;
91 while ((c = getopt(argc, argv, "hbv:")) != -1) switch (c) {
92 case 'h':
93 // help
94 showUsage = 1;
95 break;
96
97 case 'b':
98 // We cannot do a binary write, because the binary format
99 // for groups changed from 1.0 to 2.0. We check for the
100 // option only because it is expected that Inventor file
101 // translators support -b.
102 break;
103
104 case 'v':
105 // Version we are downgrading to: 1.0 or 2.0
106 targetVersion = atof(optarg);
107 break;
108 default: err = 1;
109 }
110 if (optind == argc-2) {
111 infile = argv[argc-2];
112 outfile = argv[argc-1];
113 }
114 else if (optind == argc-1) {
115 infile = argv[argc-1];
116 // outfile is stdout
117 }
118 // ??? else read from stdin ? ... else err = 1; //??? can't read from stdin right now.
119
120 if (targetVersion != 1.0 && targetVersion != 2.0)
121 err = 1;
122
123 if (showUsage || err) {
124 fprintf(stderr, "Usage: %s [-v targetVersion] inputFile outputFile\n", argv[0]);
125
126 if (err)
127 exit(-1);
128 }
129 }
130
131 // Fills in either the path list or the root node, depending what is in the file.
132 // If reading a node, we always create an extra group on top. This will not be written
133 // out later, it just lets us carry all the nodes around.
134 void
135 readAll(SoInput *in, SoPathList &list, SoNode *&root)
136 {
137 SoBase *base;
138 SoGroup *g = new SoGroup;
139 g->ref();
140
141 while (1) {
142 SoDB::read(in, base);
143 if (base != NULL) {
144 if (base->isOfType(SoPath::getClassTypeId()))
145 list.append((SoPath *) base);
146 else g->addChild((SoNode *) base);
147 }
148 else break;
149 }
150
151 // Did we read paths?
152 root = NULL;
153 if (list.getLength() > 0) {
154 g->unref();
155 }
156 else {
157 // We read nodes.
158 if (g->getNumChildren() == 0) {
159 g->unref();
160 }
161 else {
162 root = g;
163 }
164 }
165 }
166
167 //
168 // Try to recreate path through the passed scene graph.
169 // path was valid in the version 2 scene graph, but we may not
170 // be able to copy it fully in the version 1 scene graph (i.e.
171 // if some nodes could not be converted.)
172 //
173 SoPath *
174 recreatePath(SbPList &pathIndices, SoNode *sceneGraph)
175 {
176 SoPath *newPath = new SoPath(sceneGraph);
177
178 if (! sceneGraph->isOfType(SoGroup::getClassTypeId()))
179 return newPath;
180
181 SbBool haltHere = FALSE;
182 SoGroup *group = (SoGroup *) sceneGraph;
183 for (int i = 1; (i < pathIndices.getLength()) && (! haltHere); i++) {
184 unsigned long index = (unsigned long) pathIndices[i];
185
186 // Is this a valid index?
187 if (index < group->getNumChildren()) {
188 newPath->append(index);
189 group = (SoGroup *) group->getChild(index);
190 if (! group->isOfType(SoGroup::getClassTypeId()))
191 haltHere = TRUE;
192 }
193 else haltHere = TRUE;
194 }
195
196 return newPath;
197 }
198
199 void
200 readErrorCB(const SoError *, void *)
201 {
202 // We do not want inventor to output read errors,
203 // so we pass them through this dummy routine.
204 }
205
206 int
207 main(int argc, char *argv[])
208 {
209 //??? for (int k = 0; k < argc; k++) printf("%s ", argv[k]);printf("\n");
210
211 // Get arguments
212 getargs(argc, argv);
213
214 // Init Inventor
215 SoInteraction::init();
216 SoNode *root = NULL;
217 SoPathList pathList;
218
219 // Squelch error postings
220 SoReadError::setHandlerCallback(readErrorCB, NULL);
221
222 // For 2.1 to 2.0 downgrade, we init our helper classes before reading
223 if (targetVersion == 2.0) {
224 SoWWWAnchorV2::initClass();
225 SoWWWInlineV2::initClass();
226 SoAsciiTextV2::initClass();
227 SoFontStyleV2::initClass();
228 SoVertexPropertyV2::initClass();
229 SoLODV2::initClass();
230 }
231
232 // PackedColor downgrader is used for downgrades to both 1.0 and 2.0
233 // and so are text2 and text3:
234 SoPackedColorV2::initClass();
235 SoText3V2::initClass();
236 SoText2V2::initClass();
237
238 // See if this is an Inventor 2.0 file
239 SoInput in;
240 if (infile != NULL) {
241 if (in.openFile(infile)) {
242 char tmpbuf[81];
243 char *buf = fgets(tmpbuf, 80, in.getCurFile());
244 in.closeFile();
245
246 if (SoDB::isValidHeader(buf)) {
247 // If the filename includes a directory path, add the directory name
248 // to the list of directories where to look for input files
249 const char *slashPtr;
250 char *searchPath = NULL;
251 if ((slashPtr = strrchr(infile, '/')) != NULL) {
252 searchPath = strdup(infile);
253 searchPath[slashPtr - infile] = '\0';
254 in.addDirectoryFirst(searchPath);
255 }
256
257 // NOTE: file could contain nodes, or it could contain paths,
258 // i.e. for copy/paste. If paths, we need to write paths back out!
259 // Call our own routine to read...
260 in.openFile(infile);
261 readAll(&in, pathList, root);
262 in.closeFile();
263 }
264 }
265 }
266 else {
267 // Reading from stdin.
268 readAll(&in, pathList, root);
269 }
270
271 // Bail if we hit an error
272 if ((root == NULL) && (pathList.getLength() == 0))
273 return -1;
274
275 if (root != NULL)
276 root->ref();
277
278 // We need to replace all SoFile nodes with SoGroup.
279 // This is because the files referred to are probably 2.0 files.
280 // Better to just bring the data in right here and convert the
281 // entire scene graph once.
282 // We have to do this before we init our downgrader classes,
283 // or SoDB gets confused and tries to get our 1.0 classes to read
284 // 2.0 data during the SoFile::copyChildren() call.
285 if (root != NULL)
286 expandFileNodes(root);
287 else {
288 for (int i = 0; i < pathList.getLength(); i++) {
289 SoPath *path = pathList[i];
290 root = path->getHead();
291 expandFileNodes(root);
292 }
293 root = NULL;
294 }
295
296 // Prepare to write out down-graded scene
297 SoWriteAction wa;
298 SoOutput *out = wa.getOutput();
299
300 if (outfile != NULL)
301 out->openFile(outfile);
302
303 // Inventor V1.0 cannot handle "+" in node names, so use "_" instead.
304 if (targetVersion == 1.0)
305 SoBase::setInstancePrefix("_");
306
307 // NOTE: we cannot binary write, because the binary format
308 // for groups changed between 1.0 and 2.0, and for everything
309 // changed from 2.0 to 2.1. But writing/reading ascii works fine.
310
311 // This will point to the downgraded scene graph
312 SoNode *sceneGraph = NULL;
313
314 // Based on the target, intitialize any helper classes needed to do the downgrade
315 if (targetVersion == 1.0) {
316 // Init our 1.0 classes after reading is complete, since
317 // we'll be re-using class names when we go to write, and
318 // we don't want to confuse the reading mechanism which looks
319 // up classes by name.
320 SoEnvironmentV1::initClass();
321 SoPickStyleV1::initClass();
322 SoShapeHintsV1::initClass();
323 SoTextureCoordinatePlaneV1::initClass();
324 SoIndexedTriangleMeshV1::initClass();
325
326 // NOTES: we can do nothing for the following nodes:
327 // SoLightModel - v2.0 is a subset of v1.0
328 // SoMaterial - v2.0 shininess should be fine for v1.0
329 // SoTextureCoordinateEnvironment - v2.0 has no fields since it
330 // affects ALL coords (S and T), and v1.0 has only one field
331 // which defaults to ALL. So we don't have to do anything.
332
333 // The header string
334 out->setHeaderString("#Inventor V1.0 ascii");
335 }
336 else if (targetVersion == 2.0) {
337 // The header string
338 out->setHeaderString("#Inventor V2.0 ascii");
339 }
340
341 // Write the scene graph to our output file
342 if (root != NULL) {
343 // Downgrade to version 1.0 happens as a separate process to the SoWriteAction.
344 // This is because there are so many changes since 1.0 - we really have to go
345 // in and make changes to the scene.
346 if (targetVersion == 1.0)
347 sceneGraph = downgradeToV1(root);
348
349 // Downgrade to 2.0 will occur during the SoWriteAction traversal
350 // thanks to the saviour SoUnknownNode!
351 else if (targetVersion == 2.0)
352 sceneGraph = downgradeToV2(root);
353
354 sceneGraph->ref();
355
356 // Only write out the nodes we read in, i.e. the children of the root
357 if (sceneGraph->isOfType(SoGroup::getClassTypeId())) {
358 SoGroup *g = (SoGroup *) sceneGraph;
359 for (int i = 0; i < g->getNumChildren(); i++)
360 wa.apply(g->getChild(i));
361 }
362 else wa.apply(sceneGraph);
363
364 #if 0
365 SoWriteAction wa2;
366 wa2.getOutput()->setHeaderString(targetVersion == 2.0 ? "#Inventor V2.0 ascii" : "#Inventor V1.0 ascii");
367 wa2.apply(sceneGraph);
368 #endif
369
370 sceneGraph->unref();
371 }
372
373 // Or write the paths to our output file
374 else {
375 SoPathList newPathList;
376 for (int i = 0; i < pathList.getLength(); i++) {
377 SoPath *path = pathList[i];
378 path->ref();
379 SbPList pathIndices;
380
381 // Squirrel off the pathIndices so the path doesn't get ruined
382 // as we monkey with the scene graph
383 for (int j = 0; j < path->getLength(); j++)
384 pathIndices.append((void *) (unsigned long) path->getIndex(j));
385
386 if (targetVersion == 1.0)
387 sceneGraph = downgradeToV1(path->getHead());
388 else if (targetVersion == 2.0)
389 sceneGraph = downgradeToV2(path->getHead());
390
391 SoPath *newPath = recreatePath(pathIndices, sceneGraph);
392 newPathList.append(newPath);
393 path->unref();
394 }
395
396 wa.apply(newPathList);
397
398 #if 0
399 SoWriteAction wa2;
400 wa2.getOutput()->setHeaderString(targetVersion == 2.0 ? "#Inventor V2.0 ascii" : "#Inventor V1.0 ascii");
401 wa2.apply(newPathList);
402 #endif
403
404 }
405
406 out->closeFile();
407
408 return 0; // 0 means success
409 }
410