Debian Codegraph @

apps/tools/ivfix/IfCollector.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 #include <Inventor/SoLists.h>
38 #include <Inventor/actions/SoSearchAction.h>
39 #include <Inventor/elements/SoModelMatrixElement.h>
40 #include <Inventor/nodekits/SoBaseKit.h>
41 #include <Inventor/nodes/SoCamera.h>
42 #include <Inventor/nodes/SoClipPlane.h>
43 #include <Inventor/nodes/SoComplexity.h>
44 #include <Inventor/nodes/SoCoordinate3.h>
45 #include <Inventor/nodes/SoDrawStyle.h>
46 #include <Inventor/nodes/SoEnvironment.h>
47 #include <Inventor/nodes/SoFile.h>
48 #include <Inventor/nodes/SoFont.h>
49 #include <Inventor/nodes/SoGroup.h>
50 #include <Inventor/nodes/SoInfo.h>
51 #include <Inventor/nodes/SoLOD.h>
52 #include <Inventor/nodes/SoLabel.h>
53 #include <Inventor/nodes/SoLevelOfDetail.h>
54 #include <Inventor/nodes/SoLight.h>
55 #include <Inventor/nodes/SoLightModel.h>
56 #include <Inventor/nodes/SoMaterial.h>
57 #include <Inventor/nodes/SoMaterialBinding.h>
58 #include <Inventor/nodes/SoNormal.h>
59 #include <Inventor/nodes/SoNormalBinding.h>
60 #include <Inventor/nodes/SoPointSet.h>
61 #include <Inventor/nodes/SoProfile.h>
62 #include <Inventor/nodes/SoProfileCoordinate2.h>
63 #include <Inventor/nodes/SoShape.h>
64 #include <Inventor/nodes/SoShapeHints.h>
65 #include <Inventor/nodes/SoTexture2.h>
66 #include <Inventor/nodes/SoTexture2Transform.h>
67 #include <Inventor/nodes/SoTextureCoordinate2.h>
68 #include <Inventor/nodes/SoTextureCoordinateBinding.h>
69 #include <Inventor/nodes/SoTransformation.h>
70 #include <Inventor/nodes/SoVertexProperty.h>
71 #include <Inventor/nodes/SoWWWAnchor.h>
72 #include <Inventor/nodes/SoWWWInline.h>
73
74 #include "IfAssert.h"
75 #include "IfCollector.h"
76 #include "IfShape.h"
77 #include "IfTypes.h"
78
79 /////////////////////////////////////////////////////////////////////////////
80 //
81 // Constructor.
82 //
83 /////////////////////////////////////////////////////////////////////////////
84
85 IfCollector::IfCollector()
86 {
87 }
88
89 /////////////////////////////////////////////////////////////////////////////
90 //
91 // Destructor.
92 //
93 /////////////////////////////////////////////////////////////////////////////
94
95 IfCollector::~IfCollector()
96 {
97 }
98
99 /////////////////////////////////////////////////////////////////////////////
100 //
101 // Finds all shapes in the given scene, creating a list of IfShape
102 // instances. The number of shapes is returned.
103 //
104 /////////////////////////////////////////////////////////////////////////////
105
106 int
107 IfCollector::collect(SoNode *sceneRoot, IfShape *&shapeList,
108 SbBool doTexCoords)
109 {
110 doingTexCoords = doTexCoords;
111
112 // These are all the types that we consider to be "shapes"; in
113 // other words, we do not want to go below these nodes when
114 // processing a scene.
115 SoTypeList shapeTypes;
116 IfTypes::getShapeTypes(&shapeTypes);
117
118 // To find all the shapes in the scene, we apply an
119 // SoCallbackAction to the root. We would like to use an
120 // SoSearchAction, which is much faster, but we have to search for
121 // a variety of different node types, and we don't want to keep
122 // searching once we have found one. If we used an SoSearchAction,
123 // we would have to apply it once for each type and then somehow
124 // merge the returned lists of paths.
125
126 SoCallbackAction cba;
127 int i;
128 for (i = 0; i < shapeTypes.getLength(); i++)
129 cba.addPreCallback(shapeTypes[i], storePathCB, this);
130 cba.apply(sceneRoot);
131
132 // Determine the number of shapes
133 int numShapes = pathsToShapes.getLength();
134 if (numShapes == 0) {
135 shapeList = NULL;
136 return 0;
137 }
138
139 // Allocate an array of IfShape instances of the correct size
140 shapeList = new IfShape[numShapes];
141
142 // For each shape, fill in the corresponding IfShape instance
143 for (i = 0; i < numShapes; i++)
144 collectShape(pathsToShapes[i], &shapeList[i]);
145
146 return numShapes;
147 }
148
149 /////////////////////////////////////////////////////////////////////////////
150 //
151 // Stores a path to a shape found in the scene
152 //
153 /////////////////////////////////////////////////////////////////////////////
154
155 void
156 IfCollector::storePath(SoCallbackAction *cba)
157 {
158 pathsToShapes.append(cba->getCurPath()->copy());
159 }
160
161 /////////////////////////////////////////////////////////////////////////////
162 //
163 // Collects properties for a single shape.
164 //
165 /////////////////////////////////////////////////////////////////////////////
166
167 void
168 IfCollector::collectShape(const SoPath *pathToShape, IfShape *shape)
169 {
170 // Find and remember the shape we're looking for. Note that if
171 // there is a special group (such as an Array or MultipleCopy) node
172 // in the path, we have to consider that to be the shape, since we
173 // don't want to ignore its special properties
174 currentShape = shape;
175
176 const SoFullPath *fullPath = (const SoFullPath *) pathToShape;
177 for (int i = 0; i < fullPath->getLength(); i++) {
178 SoNode *node = fullPath->getNode(i);
179 SoType type = node->getTypeId();
180
181 // File, LOD, line- and point-based shape nodes, and some others
182 // are handled specially, since we want to make sure they are
183 // never flattened
184 if (IfTypes::isOpaqueGroupType(type) ||
185 IfTypes::isUnflattenableShape(type)) {
186 ASSERT(currentShape->shape == NULL);
187 currentShape->shape = node;
188 currentShape->shape->ref();
189 currentShape->dontFlatten = TRUE;
190 break;
191 }
192
193 // Handle nodes that should be considered shapes:
194 if (IfTypes::isShape(type)) {
195 ASSERT(currentShape->shape == NULL);
196 currentShape->shape = node;
197 currentShape->shape->ref();
198 break;
199 }
200 }
201
202 // Set up a callback to invoke at each node encountered
203 SoCallbackAction ca;
204 ca.addPreCallback(SoNode::getClassTypeId(), storeNodeCB, this);
205
206 // Examine each node that affects the shape
207 ca.apply((SoPath *) pathToShape);
208
209 // If the shape is derived from SoVertexShape and has a non-NULL
210 // vertexProperty field, this has to be handled specially
211 if (shape->shape->isOfType(SoVertexShape::getClassTypeId())) {
212 SoVertexShape *vs = (SoVertexShape *) shape->shape;
213 SoVertexProperty *vp =
214 (SoVertexProperty *) vs->vertexProperty.getValue();
215 if (vp != NULL) {
216 // Copy the properties from the SoVertexProperty node
217 // into separate property nodes
218 handleVertexProperty(shape, vp);
219
220 // Create a copy of the shape that does not have an
221 // SoVertexProperty node in it. Otherwise, the primitive
222 // generation code for the shape can get confused.
223 SoNode *shapeCopy = shape->shape->copy();
224 shapeCopy->ref();
225 ASSERT(shapeCopy->isOfType(SoVertexShape::getClassTypeId()));
226 SoVertexShape *vsCopy = (SoVertexShape *) shapeCopy;
227 vsCopy->vertexProperty = NULL;
228 shape->shape->unref();
229 shape->shape = shapeCopy;
230 }
231 }
232 }
233
234 /////////////////////////////////////////////////////////////////////////////
235 //
236 // Stores a node encountered during traversal of a path to a shape.
237 //
238 /////////////////////////////////////////////////////////////////////////////
239
240 SoCallbackAction::Response
241 IfCollector::storeNode(SoCallbackAction *cba, SoNode *node)
242 {
243 //////////////////////////////////////////////////////////////////
244 //
245 // Make sure we stop when we hit the shape we are collecting. If
246 // we do hit the shape, also get the current matrix from the
247 // state.
248 //
249
250 if (node == currentShape->shape) {
251 currentShape->transform = SoModelMatrixElement::get(cba->getState());
252 return SoCallbackAction::PRUNE;
253 }
254
255 // Get the type of the node
256 SoType nodeType = node->getTypeId();
257
258 // Based on the type of the node, store the appropriate pointer to
259 // it in the IfShape instance
260
261 #define IS_OF_TYPE(classname) \
262 nodeType.isDerivedFrom(classname::getClassTypeId())
263
264 #define SET_TO_NULL(field) \
265 if (currentShape->field != NULL) \
266 currentShape->field->unref(); \
267 currentShape->field = NULL
268
269 #define SET_TO_NODE(field, classname) \
270 if (currentShape->field != NULL) \
271 currentShape->field->unref(); \
272 currentShape->field = (classname *) node; \
273 currentShape->field->ref()
274
275 #define ADD_TO_LIST(field) \
276 if (currentShape->field == NULL) \
277 currentShape->field = new SoNodeList; \
278 currentShape->field->append(node)
279
280 #define DO_TYPE(classname, field) \
281 else if (IS_OF_TYPE(classname)) { \
282 SET_TO_NODE(field, classname); \
283 }
284
285 #define DO_LIST(classname, field) \
286 else if (IS_OF_TYPE(classname)) { \
287 ADD_TO_LIST(field); \
288 }
289
290 //////////////////////////////////////////////////////////////////
291 //
292 // Special cases:
293 //
294
295 // Skip over any shapes, groups, node kits, labels, info, and
296 // transformations
297 if (IS_OF_TYPE(SoShape) ||
298 IS_OF_TYPE(SoGroup) ||
299 IS_OF_TYPE(SoBaseKit) ||
300 IS_OF_TYPE(SoNodeKitListPart) ||
301 IS_OF_TYPE(SoLabel) ||
302 IS_OF_TYPE(SoInfo) ||
303 IS_OF_TYPE(SoTransformation))
304 ; // Do nothing
305
306 // These node types are kept in lists:
307 DO_LIST(SoClipPlane, clipPlanes)
308 DO_LIST(SoLight, lights)
309 DO_LIST(SoProfile, profiles)
310
311 // Special handling of texture transform nodes. If we are NOT
312 // producing texture coordinates, we need to save and output these
313 // transforms, so they are added to the "other" list.
314 else if (IS_OF_TYPE(SoTexture2Transform)) {
315 if (doingTexCoords) {
316 ADD_TO_LIST(textureTransforms);
317 }
318 else {
319 ADD_TO_LIST(other);
320 }
321 }
322
323 // Vertex property nodes are handled specially
324 else if (IS_OF_TYPE(SoVertexProperty))
325 handleVertexProperty(currentShape, (SoVertexProperty *) node);
326
327 // Materials have to be copied. We may be playing around with
328 // their values later on, so we want to make sure that changing a
329 // material does not affect other objects. (This is especially
330 // important if fixing is done on several objects in the same
331 // scene graph.)
332 else if (IS_OF_TYPE(SoMaterial)) {
333 if (currentShape->material != NULL)
334 currentShape->material->unref();
335 currentShape->material = (SoMaterial *) node->copy();
336 currentShape->material->ref();
337 }
338
339 //////////////////////////////////////////////////////////////////
340 //
341 // Typical cases:
342 //
343
344 DO_TYPE(SoCamera, camera)
345 DO_TYPE(SoComplexity, complexity)
346 DO_TYPE(SoCoordinate3, coords)
347 DO_TYPE(SoDrawStyle, drawStyle)
348 DO_TYPE(SoEnvironment, environment)
349 DO_TYPE(SoFont, font)
350 DO_TYPE(SoLightModel, lightModel)
351 DO_TYPE(SoMaterialBinding, materialBinding)
352 DO_TYPE(SoNormal, normals)
353 DO_TYPE(SoNormalBinding, normalBinding)
354 DO_TYPE(SoProfileCoordinate2, profileCoords)
355 DO_TYPE(SoShapeHints, shapeHints)
356 DO_TYPE(SoTexture2, texture)
357 DO_TYPE(SoTextureCoordinate2, texCoords)
358 DO_TYPE(SoTextureCoordinateBinding, texCoordBinding)
359
360 //////////////////////////////////////////////////////////////////
361 //
362 // Anything else:
363 //
364
365 else {
366 ADD_TO_LIST(other);
367 }
368
369 return SoCallbackAction::CONTINUE;
370
371 #undef IS_OF_TYPE
372 #undef SET_TO_NULL
373 #undef SET_TO_NODE
374 #undef ADD_TO_LIST
375 #undef DO_TYPE
376 #undef DO_LIST
377 }
378
379 /////////////////////////////////////////////////////////////////////////////
380 //
381 // Handles properties stored in a vertexProperty node.
382 //
383 /////////////////////////////////////////////////////////////////////////////
384
385 void
386 IfCollector::handleVertexProperty(IfShape *shape, SoVertexProperty *vp)
387 {
388 int num;
389
390 // Coordinates
391 num = vp->vertex.getNum();
392 if (num > 0) {
393 if (shape->coords != NULL)
394 shape->coords->unref();
395 shape->coords = new SoCoordinate3;
396 shape->coords->ref();
397 shape->coords->point.setValues(0, num, vp->vertex.getValues(0));
398 }
399
400 // Normals
401 num = vp->normal.getNum();
402 if (num > 0) {
403 if (shape->normals != NULL)
404 shape->normals->unref();
405 shape->normals = new SoNormal;
406 shape->normals->ref();
407 shape->normals->vector.setValues(0, num, vp->normal.getValues(0));
408
409 // Steal normal binding
410 shape->normalBinding = new SoNormalBinding;
411 shape->normalBinding->ref();
412 shape->normalBinding->value = vp->normalBinding.getValue();
413 }
414
415 // Colors
416 num = vp->orderedRGBA.getNum();
417 if (num > 0) {
418 SoMaterial *oldMaterial = shape->material;
419 if (oldMaterial != NULL) {
420 shape->material = (SoMaterial *) oldMaterial->copy();
421 oldMaterial->unref();
422 }
423 else
424 shape->material = new SoMaterial;
425 shape->material->ref();
426
427 // Unpack the colors
428 shape->material->diffuseColor.setNum(num);
429 shape->material->transparency.setNum(num);
430 SbColor *dc = shape->material->diffuseColor.startEditing();
431 float *tr = shape->material->transparency.startEditing();
432 const uint32_t *pc = vp->orderedRGBA.getValues(0);
433
434 for (int i = 0; i < num; i++)
435 dc[i].setPackedValue(pc[i], tr[i]);
436
437 shape->material->diffuseColor.finishEditing();
438 shape->material->transparency.finishEditing();
439
440 // Steal material binding
441 shape->materialBinding = new SoMaterialBinding;
442 shape->materialBinding->ref();
443 shape->materialBinding->value = vp->materialBinding.getValue();
444 }
445
446 // Texture coordinates
447 num = vp->texCoord.getNum();
448 if (num > 0) {
449 if (shape->texCoords != NULL)
450 shape->texCoords->unref();
451 shape->texCoords = new SoTextureCoordinate2;
452 shape->texCoords->ref();
453 shape->texCoords->point.setValues(0, num, vp->texCoord.getValues(0));
454 }
455 }