Debian Codegraph @

apps/tools/ivfix/IfWeeder.c++

raw symbols SCIP
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/actions/SoSearchAction.h>cxx . . $ `<file>/lib/database/include/Inventor/actions/SoSearchAction.h`/
38 #include <Inventor/nodes/SoMaterial.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoMaterial.h`/
39 #include <Inventor/nodes/SoSeparator.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoSeparator.h`/
40 #include <Inventor/nodes/SoIndexedFaceSet.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoIndexedFaceSet.h`/
41 #include <Inventor/nodes/SoIndexedTriangleStripSet.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoIndexedTriangleStripSet.h`/
42 #include <Inventor/nodes/SoVertexProperty.h>cxx . . $ `<file>/lib/database/include/Inventor/nodes/SoVertexProperty.h`/
43
44 #include "IfAssert.h"cxx . . $ `<file>/apps/tools/ivfix/IfAssert.h`/
45 #include "IfShape.h"cxx . . $ `<file>/apps/tools/ivfix/IfShape.h`/
46 #include "IfTypes.h"cxx . . $ `<file>/apps/tools/ivfix/IfTypes.h`/
47 #include "IfWeeder.h"cxx . . $ `<file>/apps/tools/ivfix/IfWeeder.h`/
48
49 /////////////////////////////////////////////////////////////////////////////
50 //
51 // Constructor.
52 //
53 /////////////////////////////////////////////////////////////////////////////
54
55 IfWeedercxx . . $ IfWeeder#::IfWeedercxx . . $ IfWeeder#IfWeeder(49f6e7a06ebc5aa8).()
56 {
57 }
58
59 /////////////////////////////////////////////////////////////////////////////
60 //
61 // Destructor.
62 //
63 /////////////////////////////////////////////////////////////////////////////
64
65 IfWeedercxx . . $ IfWeeder#::~cxx . . $ IfWeeder#`~IfWeeder`(49f6e7a06ebc5aa8).IfWeedercxx . . $ IfWeeder#()
66 {
67 }
68
69 /////////////////////////////////////////////////////////////////////////////
70 //
71 // This weeds out unnecessary items from the given fixed scene graph.
72 //
73 /////////////////////////////////////////////////////////////////////////////
74
75 void
76 IfWeedercxx . . $ IfWeeder#::weedcxx . . $ IfWeeder#weed(da6c7544ccee32bd).(SoNodecxx . . $ SoNode# *root)
77 {
78 // Weed out duplicate and unused materials from all SoMaterial
79 // nodes in the graph
80 weedMaterialscxx . . $ IfWeeder#weedMaterials(da6c7544ccee32bd).(root);
81 }
82
83 /////////////////////////////////////////////////////////////////////////////
84 //
85 // This type is used by weedMaterials() to determine material-shape
86 // dependencies.
87 //
88 /////////////////////////////////////////////////////////////////////////////
89
90 struct IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# {
91 SoMaterialcxx . . $ SoMaterial# *materialcxx . . $ IfWeederMaterialEntry#material.; // Pointer to material
92 SoNodeListcxx . . $ SoNodeList# shapescxx . . $ IfWeederMaterialEntry#shapes.; // IfShapes that use material
93 SbBoolcxx . . $ SbBool# canWeedcxx . . $ IfWeederMaterialEntry#canWeed.; // TRUE if material can be weeded
94 };
95
96 /////////////////////////////////////////////////////////////////////////////
97 //
98 // This weeds out values in SoMaterial nodes that are not used by any
99 // shape. It also removes duplicate material values.
100 //
101 /////////////////////////////////////////////////////////////////////////////
102
103 void
104 IfWeedercxx . . $ IfWeeder#::weedMaterialscxx . . $ IfWeeder#weedMaterials(da6c7544ccee32bd).(SoNodecxx . . $ SoNode# *root)
105 {
106 // Find all SoMaterial nodes in the scene graph and determine
107 // which shape nodes are affected by each one. Note that because
108 // materials may be multiply instanced, this is a little more
109 // complicated than just looking at each subgraph with a
110 // material. The resulting materials are stored in the
111 // materialList, which is a list of pointers to
112 // IfWeederMaterialEntry instances.
113 findMaterialsAndShapescxx . . $ IfWeeder#findMaterialsAndShapes(da6c7544ccee32bd).(root);
114
115 // Weed each material in the resulting list
116 int i;
117 for (i = 0; i < materialListcxx . . $ IfWeeder#materialList.->getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); i++) {
118 IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *entry = (IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *)
119 (*materialListcxx . . $ IfWeeder#materialList.)[cxx . . $ SbPList#`operator[]`(5f9a4f363ac2f591).i];
120
121 if (entry->canWeedcxx . . $ IfWeederMaterialEntry#canWeed.)
122 weedMaterialcxx . . $ IfWeeder#weedMaterial(162fd4b29d361d31).(root, entry);
123 }
124
125 // Clean up
126 for (i = 0; i < materialListcxx . . $ IfWeeder#materialList.->getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); i++)
127 delete (IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *) (*materialListcxx . . $ IfWeeder#materialList.)[cxx . . $ SbPList#`operator[]`(5f9a4f363ac2f591).i];
128 delete materialListcxx . . $ IfWeeder#materialList.;
129 }
130
131 /////////////////////////////////////////////////////////////////////////////
132 //
133 // Finds all materials in the given graph and the shapes that depend
134 // on them.
135 //
136 /////////////////////////////////////////////////////////////////////////////
137
138 void
139 IfWeedercxx . . $ IfWeeder#::findMaterialsAndShapescxx . . $ IfWeeder#findMaterialsAndShapes(da6c7544ccee32bd).(SoNodecxx . . $ SoNode# *root)
140 {
141 // Since we know the structure of the given scene graph (which is
142 // after fixing has occurred), we can be efficient here. Just
143 // search for all materials in the scene. For each material, the
144 // shapes affected by it must be under the separator that is the
145 // material's parent node. So just search for all shapes under
146 // that separator, making sure that the path to the shape comes
147 // after the material.
148
149 // First, create a dictionary so we can tell when we've found a
150 // multiple instance of a material
151 SbDictcxx . . $ SbDict# materialDictcxx . . $ SbDict#SbDict(d4f767463ce0a6b3).;
152
153 // Search for all materials in the scene
154 SoSearchActioncxx . . $ SoSearchAction# sacxx . . $ SoSearchAction#SoSearchAction(49f6e7a06ebc5aa8).;
155 sa.setTypecxx . . $ SoSearchAction#setType(a368ce5ca5a50910).(SoMaterialcxx . . $ SoMaterial#::getClassTypeIdcxx . . $ SoMaterial#getClassTypeId(c00c150378d2b968).());
156 sa.setInterestcxx . . $ SoSearchAction#setInterest(b27440939ca13a5b).(SoSearchActioncxx . . $ SoSearchAction#::ALLcxx . . $ SoSearchAction#ALL.);
157 sa.applycxx . . $ SoAction#apply(da6c7544ccee32bd).(root);
158
159 // Set up another search action to find all shapes using a
160 // material. Note that we have to search for all node types that
161 // should be considered shapes.
162 SoSearchActioncxx . . $ SoSearchAction# sa2cxx . . $ SoSearchAction#SoSearchAction(49f6e7a06ebc5aa8).;
163 sa2.setInterestcxx . . $ SoSearchAction#setInterest(b27440939ca13a5b).(SoSearchActioncxx . . $ SoSearchAction#::ALLcxx . . $ SoSearchAction#ALL.);
164
165 // These are the shape types
166 SoTypeListcxx . . $ SoTypeList# shapeTypescxx . . $ SoTypeList#SoTypeList(49f6e7a06ebc5aa8).;
167 IfTypescxx . . $ IfTypes#::getShapeTypescxx . . $ IfTypes#getShapeTypes(5bda5d4fa9a25a2f).(&shapeTypes);
168
169 // Process each material, adding new ones to the list
170 materialListcxx . . $ IfWeeder#materialList. = new SbPListcxx . . $ SbPList#;
171 for (int i = 0; i < sa.getPathscxx . . $ SoSearchAction#getPaths(e5198181f61c9f38).().getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); i++) {
172
173 const SoPathcxx . . $ SoPath# *path = (const SoPathcxx . . $ SoPath# *) sa.getPathscxx . . $ SoSearchAction#getPaths(e5198181f61c9f38).()[cxx . . $ SoPathList#`operator[]`(81b2b9099fcfac9c).i];
174
175 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(path->getLength() > 1);
176 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(path->getTail()->getTypeId() == SoMaterial::getClassTypeId());
177
178 SoMaterialcxx . . $ SoMaterial# *material = (SoMaterialcxx . . $ SoMaterial# *) path->getTailcxx . . $ SoPath#getTail(b794963b450d51b3).();
179
180 // Add to the dictionary if necessary, or use the existing
181 // entry
182 void *entryPtr;
183 IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *entry;
184 if (materialDict.findcxx . . $ SbDict#find(a750591cf51bff5e).((unsigned long) material, entryPtr)) {
185 entry = (IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *) entryPtr;
186 if (! entry->canWeedcxx . . $ IfWeederMaterialEntry#canWeed.)
187 continue;
188 }
189 else {
190 entry = new IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry#;
191 entry->materialcxx . . $ IfWeederMaterialEntry#material. = material;
192 entry->canWeedcxx . . $ IfWeederMaterialEntry#canWeed. = TRUEcxx . . $ `lib/database/include/Inventor/SbBasic.h:80:12`!;
193 materialDict.entercxx . . $ SbDict#enter(9b3d2d56e494888).((unsigned long) material, entry);
194 materialListcxx . . $ IfWeeder#materialList.->appendcxx . . $ SbPList#append(5d22bdacc48458e8).(entry);
195 }
196
197 // If any node above the material in the path is an opaque
198 // group, we can't really weed this material
199 int j;
200 for (j = path->getLengthcxx . . $ SoPath#getLength(455f465bc33b4cdf).() - 2; j >= 0; j--) {
201 if (IfTypescxx . . $ IfTypes#::isOpaqueGroupTypecxx . . $ IfTypes#isOpaqueGroupType(34bb3eef6b2c25d5).(path->getNodecxx . . $ SoPath#getNode(fd675789997d09c2).(j)->getTypeIdcxx . . $ SoBase#getTypeId(70775439ff16cb15).())) {
202 entry->canWeedcxx . . $ IfWeederMaterialEntry#canWeed. = FALSEcxx . . $ `lib/database/include/Inventor/SbBasic.h:79:12`!;
203 break;
204 }
205 }
206 if (! entry->canWeedcxx . . $ IfWeederMaterialEntry#canWeed.)
207 continue;
208
209 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(path->getNodeFromTail(1)->
210 isOfType(SoSeparator::getClassTypeId()));
211
212 SoSeparatorcxx . . $ SoSeparator# *parent = (SoSeparatorcxx . . $ SoSeparator# *) path->getNodeFromTailcxx . . $ SoPath#getNodeFromTail(fd675789997d09c2).(1);
213 int materialIndex = path->getIndexFromTailcxx . . $ SoPath#getIndexFromTail(7ccb52b1bfe01045).(0);
214
215 // Find all shapes using the material, adding them to the list
216 // of shapes in the material's entry. Store all the paths to
217 // them in this list
218 SoPathListcxx . . $ SoPathList# pathsToShapescxx . . $ SoPathList#SoPathList(49f6e7a06ebc5aa8).;
219 for (int type = 0; type < shapeTypes.getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); type++) {
220 sa2.setTypecxx . . $ SoSearchAction#setType(a368ce5ca5a50910).(shapeTypes[cxx . . $ SoTypeList#`operator[]`(eb1adc4f4b160d08).type]);
221 sa2.applycxx . . $ SoAction#apply(da6c7544ccee32bd).(parent);
222 for (j = 0; j < sa2.getPathscxx . . $ SoSearchAction#getPaths(e5198181f61c9f38).().getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); j++)
223 pathsToShapes.appendcxx . . $ SoPathList#append(eed4dbbaf0eca0f1).(sa2.getPathscxx . . $ SoSearchAction#getPaths(e5198181f61c9f38).()[cxx . . $ SoPathList#`operator[]`(81b2b9099fcfac9c).j]);
224 }
225
226 for (j = 0; j < pathsToShapes.getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); j++) {
227
228 const SoPathcxx . . $ SoPath# *shapePath = (const SoPathcxx . . $ SoPath# *) pathsToShapes[cxx . . $ SoPathList#`operator[]`(81b2b9099fcfac9c).j];
229
230 // We can't weed the material at all if a shape other than
231 // the one we created is found
232 SoTypecxx . . $ SoType# tailType = shapePath->getTailcxx . . $ SoPath#getTail(b794963b450d51b3).()->getTypeIdcxx . . $ SoBase#getTypeId(70775439ff16cb15).();
233 if (tailType !=cxx . . $ SoType#`operator!=`(1fd66475a268b64e). SoIndexedTriangleStripSetcxx . . $ SoIndexedTriangleStripSet#::getClassTypeIdcxx . . $ SoIndexedTriangleStripSet#getClassTypeId(c00c150378d2b968).() &&
234 tailType !=cxx . . $ SoType#`operator!=`(1fd66475a268b64e). SoIndexedFaceSetcxx . . $ SoIndexedFaceSet#::getClassTypeIdcxx . . $ SoIndexedFaceSet#getClassTypeId(c00c150378d2b968).()) {
235 entry->canWeedcxx . . $ IfWeederMaterialEntry#canWeed. = FALSEcxx . . $ `lib/database/include/Inventor/SbBasic.h:79:12`!;
236 break;
237 }
238
239 // Make sure the shape comes after the material and does
240 // not get its materials from a vertex property
241 // node.
242 else if (shapePath->getIndexcxx . . $ SoPath#getIndex(7ccb52b1bfe01045).(1) > materialIndex) {
243 SoIndexedShapecxx . . $ SoIndexedShape# *is = (SoIndexedShapecxx . . $ SoIndexedShape# *) shapePath->getTailcxx . . $ SoPath#getTail(b794963b450d51b3).();
244
245 // ??? If the shape's materialIndex field has the
246 // ??? default value, we assume that it might have to
247 // ??? access all the material values. To check, we would
248 // ??? have to look at the coordIndex values if the
249 // ??? material binding is not OVERALL. This change could
250 // ??? not be done in time for the release. See bug 311071.
251 if (is->materialIndexcxx . . $ SoIndexedShape#materialIndex..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).() == 1 &&
252 is->materialIndexcxx . . $ SoIndexedShape#materialIndex.[cxx . . $ SoMFInt32#`operator[]`(7ccb52b1bfe01045).0] < 0) {
253 entry->canWeedcxx . . $ IfWeederMaterialEntry#canWeed. = FALSEcxx . . $ `lib/database/include/Inventor/SbBasic.h:79:12`!;
254 break;
255 }
256
257 SoVertexPropertycxx . . $ SoVertexProperty# *vp =
258 (SoVertexPropertycxx . . $ SoVertexProperty# *) is->vertexPropertycxx . . $ SoVertexShape#vertexProperty..getValuecxx . . $ SoSFNode#getValue(b794963b450d51b3).();
259 if (vp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`! || vp->orderedRGBAcxx . . $ SoVertexProperty#orderedRGBA..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).() == 0)
260 entry->shapescxx . . $ IfWeederMaterialEntry#shapes..appendcxx . . $ SoNodeList#append(da6c7544ccee32bd).(shapePath->getTailcxx . . $ SoPath#getTail(b794963b450d51b3).());
261 }
262 }
263 }
264 }
265
266 /////////////////////////////////////////////////////////////////////////////
267 //
268 // Weeds the material in the given entry.
269 //
270 /////////////////////////////////////////////////////////////////////////////
271
272 void
273 IfWeedercxx . . $ IfWeeder#::weedMaterialcxx . . $ IfWeeder#weedMaterial(162fd4b29d361d31).(SoNodecxx . . $ SoNode# *root, IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *entry)
274 {
275 // If the material affects no shapes at all, get rid of it. This
276 // can happen when vertex property nodes are used.
277 if (entry->shapescxx . . $ IfWeederMaterialEntry#shapes..getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).() == 0) {
278 SoSearchActioncxx . . $ SoSearchAction# sacxx . . $ SoSearchAction#SoSearchAction(49f6e7a06ebc5aa8).;
279 sa.setNodecxx . . $ SoSearchAction#setNode(da6c7544ccee32bd).(entry->materialcxx . . $ IfWeederMaterialEntry#material.);
280 sa.setInterestcxx . . $ SoSearchAction#setInterest(b27440939ca13a5b).(SoSearchActioncxx . . $ SoSearchAction#::ALLcxx . . $ SoSearchAction#ALL.);
281 sa.applycxx . . $ SoAction#apply(da6c7544ccee32bd).(root);
282 for (int i = 0; i < sa.getPathscxx . . $ SoSearchAction#getPaths(e5198181f61c9f38).().getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); i++) {
283 SoPathcxx . . $ SoPath# *path = sa.getPathscxx . . $ SoSearchAction#getPaths(e5198181f61c9f38).()[cxx . . $ SoPathList#`operator[]`(81b2b9099fcfac9c).i];
284 SoSeparatorcxx . . $ SoSeparator# *parent = (SoSeparatorcxx . . $ SoSeparator# *) path->getNodeFromTailcxx . . $ SoPath#getNodeFromTail(fd675789997d09c2).(1);
285 int index = path->getIndexFromTailcxx . . $ SoPath#getIndexFromTail(7ccb52b1bfe01045).(0);
286 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(parent->isOfType(SoSeparator::getClassTypeId()));
287 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(parent->getChild(index) == entry->material);
288 parent->removeChildcxx . . $ SoGroup#removeChild(d4f767463ce0a6b3).(index);
289 }
290 }
291
292 // Remove all material values from the material node that are
293 // not used by any dependent shapes. Adjust the indices in the
294 // dependent shapes accordingly.
295 removeDuplicateMaterialscxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).(entry);
296
297 // Now remove all material values that are not used by any
298 // dependent shapes. Again, adjust the indices in the dependent
299 // shapes.
300 removeUnusedMaterialscxx . . $ IfWeeder#removeUnusedMaterials(f9faf8de3a996eee).(entry);
301 }
302
303 /////////////////////////////////////////////////////////////////////////////
304 //
305 // Finds and removes duplicate materials, adjusting material
306 // indices in all dependent shapes.
307 //
308 /////////////////////////////////////////////////////////////////////////////
309
310 void
311 IfWeedercxx . . $ IfWeeder#::removeDuplicateMaterialscxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).(IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *entry)
312 {
313 ////////////////////////////////////////////
314 //
315 // To find duplicate materials, we create a hash table of material
316 // values. Each entry in the hash table contains an index into the
317 // material node's values. The entries in the table use the
318 // material values as the hash key, so that duplicate materials
319 // will be in the same slot in the table. If we find a duplicate,
320 // we store that fact in a table of index duplicates. For example,
321 // if material index 32 is the same as material index 21, we store
322 // 21 in entry 32 in the duplicates array. When we are done with
323 // the hash table, the duplicates array will tell us which
324 // material values we need to keep in the material node and also
325 // how to adjust the material indices in the SoIndexedShapes.
326 //
327
328 // Instances of this structure is stored in the hash table.
329 struct IfWeederMaterialInfocxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo# {
330 int indexcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#index.; // Index of material value
331 IfWeederMaterialInfocxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo# *nextcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#next.; // For making lists in the hash table
332 };
333
334 // Access the fields of the material node for faster referencing
335 curMaterialcxx . . $ IfWeeder#curMaterial. = entry->materialcxx . . $ IfWeederMaterialEntry#material.;
336 ambicxx . . $ IfWeeder#ambi. = curMaterialcxx . . $ IfWeeder#curMaterial.->ambientColorcxx . . $ SoMaterial#ambientColor..getValuescxx . . $ SoMFColor#getValues(9dd468d3f2917510).(0);
337 diffcxx . . $ IfWeeder#diff. = curMaterialcxx . . $ IfWeeder#curMaterial.->diffuseColorcxx . . $ SoMaterial#diffuseColor..getValuescxx . . $ SoMFColor#getValues(9dd468d3f2917510).(0);
338 speccxx . . $ IfWeeder#spec. = curMaterialcxx . . $ IfWeeder#curMaterial.->specularColorcxx . . $ SoMaterial#specularColor..getValuescxx . . $ SoMFColor#getValues(9dd468d3f2917510).(0);
339 emiscxx . . $ IfWeeder#emis. = curMaterialcxx . . $ IfWeeder#curMaterial.->emissiveColorcxx . . $ SoMaterial#emissiveColor..getValuescxx . . $ SoMFColor#getValues(9dd468d3f2917510).(0);
340 shincxx . . $ IfWeeder#shin. = curMaterialcxx . . $ IfWeeder#curMaterial.->shininesscxx . . $ SoMaterial#shininess..getValuescxx . . $ SoMFFloat#getValues(bab6c6b0b673bcf).(0);
341 trancxx . . $ IfWeeder#tran. = curMaterialcxx . . $ IfWeeder#curMaterial.->transparencycxx . . $ SoMaterial#transparency..getValuescxx . . $ SoMFFloat#getValues(bab6c6b0b673bcf).(0);
342
343 int numMaterials = curMaterialcxx . . $ IfWeeder#curMaterial.->diffuseColorcxx . . $ SoMaterial#diffuseColor..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).();
344
345 // Create an array of structures to insert into the table
346 IfWeederMaterialInfocxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo# *infos = new IfWeederMaterialInfocxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#[numMaterials];
347
348 // Create an array to hold the duplicate info
349 int *duplicates = new int [numMaterials];
350
351 // Create a hash table of a reasonable size
352 SbDictcxx . . $ SbDict# *hashTable = new SbDictcxx . . $ SbDict#SbDict(d4f767463ce0a6b3).(1 + numMaterials / 4);
353
354 // Add each material index to the hash table, looking for
355 // duplicates
356 int i, numMatches = 0;
357
358 for (i = 0; i < numMaterials; i++) {
359
360 // Assume that there is no duplicate. We'll overwrite this if
361 // we find one below
362 duplicates[i] = -1;
363
364 // Prepare an info instance to insert
365 infos[i].indexcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#index. = i;
366 infos[i].nextcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
367
368 // Compute a hash value for the material based on the material
369 // values
370 uint32_tcxx . . $ uint32_t# hashKey = computeMaterialHashKeycxx . . $ IfWeeder#computeMaterialHashKey(46cc46841198e427).(i);
371
372 // See if there is anything in the hash table with that key
373 void *entry;
374 if (hashTable->findcxx . . $ SbDict#find(a750591cf51bff5e).(hashKey, entry)) {
375 // Look for an exact match
376 IfWeederMaterialInfocxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo# *info, *prevInfo = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
377 for (info = (IfWeederMaterialInfocxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo# *) entry;
378 info != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; info = info->nextcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#next.) {
379
380 // If there's a match, record the duplicate
381 if (isSameMaterialcxx . . $ IfWeeder#isSameMaterial(9b79fb6aee4c0440).(info->indexcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#index., i)) {
382 duplicates[i] = info->indexcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#index.;
383 numMatches++;
384 break;
385 }
386
387 prevInfo = info;
388 }
389
390 // If we didn't find a duplicate, add a new entry to the
391 // end of the list
392 if (duplicates[i] == -1) {
393 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(prevInfo != NULL);
394 prevInfo->nextcxx . . $ IfWeeder#removeDuplicateMaterials(f9faf8de3a996eee).IfWeederMaterialInfo#next. = &infos[i];
395 }
396 }
397
398 // If there was nothing in the table for this key, add a new entry
399 else
400 hashTable->entercxx . . $ SbDict#enter(9b3d2d56e494888).(hashKey, &infos[i]);
401 }
402
403 // We're done with the hash table
404 delete hashTable;
405 delete [] infos;
406
407 // Now we can adjust the material indices in the shapes affected
408 // by the material. Don't bother doing this unless we weeded at
409 // least one material.
410 if (numMatches > 0) {
411
412 for (i = 0; i < entry->shapescxx . . $ IfWeederMaterialEntry#shapes..getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); i++) {
413
414 // Adjust material indices to use only original materials,
415 // not duplicates
416 SoIndexedShapecxx . . $ SoIndexedShape# *iShape = (SoIndexedShapecxx . . $ SoIndexedShape# *) entry->shapescxx . . $ IfWeederMaterialEntry#shapes.[cxx . . $ SoNodeList#`operator[]`(fd675789997d09c2).i];
417 int num = iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).();
418 int32_tcxx . . $ int32_t# *m = iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..startEditingcxx . . $ SoMFInt32#startEditing(a7741f82f5fc3640).();
419 for (int i = 0; i < num; i++) {
420
421 // If the material index is off the end, cycle it
422 if (m[i] >= numMaterials)
423 m[i] %= numMaterials;
424
425 // Now check for duplicates
426 if (m[i] >= 0 && duplicates[m[i]] != -1)
427 m[i] = duplicates[m[i]];
428 }
429 iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..finishEditingcxx . . $ SoMFInt32#finishEditing(49f6e7a06ebc5aa8).();
430 }
431 }
432
433 delete [] duplicates;
434 }
435
436 /////////////////////////////////////////////////////////////////////////////
437 //
438 // Computes a hash key for the material value with the given index.
439 //
440 /////////////////////////////////////////////////////////////////////////////
441
442 uint32_tcxx . . $ uint32_t#
443 IfWeedercxx . . $ IfWeeder#::computeMaterialHashKeycxx . . $ IfWeeder#computeMaterialHashKey(46cc46841198e427).(int index)
444 {
445 // Just use the diffuse color to compute a key, since it's most
446 // likely to vary between materials
447
448 // Since we care only about exact matches, we can use any hash
449 // function we like here. Just pick one that distributes the
450 // materials in the range 0-10000. We can do this because we know
451 // the color values range from 0 to 1.
452
453 return (uint32_tcxx . . $ uint32_t#) (diffcxx . . $ IfWeeder#diff.[index][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).0] * 8871 +
454 diffcxx . . $ IfWeeder#diff.[index][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).1] * 1104 +
455 diffcxx . . $ IfWeeder#diff.[index][cxx . . $ SbVec3f#`operator[]`(c60a38c584b9a5cc).2] * 25);
456 }
457
458 /////////////////////////////////////////////////////////////////////////////
459 //
460 // Returns TRUE if the two material values with the given indices
461 // are the same.
462 //
463 /////////////////////////////////////////////////////////////////////////////
464
465 SbBoolcxx . . $ SbBool#
466 IfWeedercxx . . $ IfWeeder#::isSameMaterialcxx . . $ IfWeeder#isSameMaterial(9b79fb6aee4c0440).(int i1, int i2)
467 {
468 // Trivial case of same indices
469 if (i1 == i2)
470 return TRUEcxx . . $ `lib/database/include/Inventor/SbBasic.h:80:12`!;
471
472 // Diffuse colors always have to be the same
473 if (diffcxx . . $ IfWeeder#diff.[i1] !=cxx . . $ `operator!=`(648f52454fd9f27). diffcxx . . $ IfWeeder#diff.[i2])
474 return FALSEcxx . . $ `lib/database/include/Inventor/SbBasic.h:79:12`!;
475
476 #define TEST_FIELDcxx . . $ `apps/tools/ivfix/IfWeeder.c++:476:9`!(FIELD,VAR) \
477 if (curMaterial->FIELD.getNum() > 1 && VAR[i1] != VAR[i2]) \
478 return FALSEcxx . . $ `lib/database/include/Inventor/SbBasic.h:79:12`!
479
480 // The rest depends on what varies
481 TEST_FIELDcxx . . $ `apps/tools/ivfix/IfWeeder.c++:476:9`!(ambientColor, ambi);
482 TEST_FIELDcxx . . $ `apps/tools/ivfix/IfWeeder.c++:476:9`!(specularColor, spec);
483 TEST_FIELDcxx . . $ `apps/tools/ivfix/IfWeeder.c++:476:9`!(emissiveColor, emis);
484 TEST_FIELDcxx . . $ `apps/tools/ivfix/IfWeeder.c++:476:9`!(shininess, shin);
485 TEST_FIELDcxx . . $ `apps/tools/ivfix/IfWeeder.c++:476:9`!(transparency, tran);
486
487 #undef TEST_FIELDcxx . . $ `apps/tools/ivfix/IfWeeder.c++:476:9`!
488
489 // If we got here, they're the same
490 return TRUEcxx . . $ `lib/database/include/Inventor/SbBasic.h:80:12`!;
491 }
492
493 /////////////////////////////////////////////////////////////////////////////
494 //
495 // Removes any unused material values and updates the indices in all
496 // dependent shapes.
497 //
498 /////////////////////////////////////////////////////////////////////////////
499
500 void
501 IfWeedercxx . . $ IfWeeder#::removeUnusedMaterialscxx . . $ IfWeeder#removeUnusedMaterials(f9faf8de3a996eee).(IfWeederMaterialEntrycxx . . $ IfWeederMaterialEntry# *entry)
502 {
503 SoMaterialcxx . . $ SoMaterial# *material = entry->materialcxx . . $ IfWeederMaterialEntry#material.;
504 int numMaterials = material->diffuseColorcxx . . $ SoMaterial#diffuseColor..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).();
505 if (numMaterials == 0)
506 return;
507
508 ////////////////////////////////////////////
509 //
510 // Scan the lists of material indices in each shape to see which
511 // (and how many) materials are used. Store the results in an
512 // array of flags and a counter.
513
514 int i, numUsed = 0;
515 SbBoolcxx . . $ SbBool# *materialUsed = new SbBoolcxx . . $ SbBool#[numMaterials];
516 for (i = 0; i < numMaterials; i++)
517 materialUsed[i] = FALSEcxx . . $ `lib/database/include/Inventor/SbBasic.h:79:12`!;
518 for (i = 0; i < entry->shapescxx . . $ IfWeederMaterialEntry#shapes..getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); i++) {
519 SoIndexedShapecxx . . $ SoIndexedShape# *iShape = (SoIndexedShapecxx . . $ SoIndexedShape# *) entry->shapescxx . . $ IfWeederMaterialEntry#shapes.[cxx . . $ SoNodeList#`operator[]`(fd675789997d09c2).i];
520 int numIndices = iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).();
521 const int32_tcxx . . $ int32_t# *oldIndices = iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..getValuescxx . . $ SoMFInt32#getValues(7547fac9af83ee5e).(0);
522 if (numIndices == 1) {
523
524 // If there is only one material index, that means the
525 // shape uses overall material binding, so we can set the
526 // materialIndex field to the default value and skip the
527 // shape.
528 if (! materialUsed[0]) {
529 materialUsed[0] = TRUEcxx . . $ `lib/database/include/Inventor/SbBasic.h:80:12`!;
530 numUsed++;
531 }
532 iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex. =cxx . . $ SoMFInt32#`operator=`(7864480464b09eea). -1;
533 iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..setDefaultcxx . . $ SoField#setDefault(d4f767463ce0a6b3).(TRUEcxx . . $ `lib/database/include/Inventor/SbBasic.h:80:12`!);
534 continue;
535 }
536 for (int j = 0; j < numIndices; j++) {
537 int mi = oldIndices[j];
538 // The material index should have already been brought
539 // into the correct range
540 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(mi < numMaterials);
541 if (mi >= 0 && ! materialUsed[mi]) {
542 materialUsed[mi] = TRUEcxx . . $ `lib/database/include/Inventor/SbBasic.h:80:12`!;
543 numUsed++;
544 }
545 }
546 }
547
548 // If all materials are used, stop
549 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(numUsed <= numMaterials);
550 if (numUsed == numMaterials) {
551 delete [] materialUsed;
552 return;
553 }
554
555 ////////////////////////////////////////////
556 //
557 // Set up an array of correspondences from old material indices
558 // to new ones. Store -1 for those materials that are not used.
559
560 int *materialIndex = new int[numMaterials];
561 int curNewIndex = 0;
562 for (i = 0; i < numMaterials; i++) {
563 if (materialUsed[i])
564 materialIndex[i] = curNewIndex++;
565 else
566 materialIndex[i] = -1;
567 }
568 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(curNewIndex == numUsed);
569
570 ////////////////////////////////////////////
571 //
572 // Update the material indices in the shapes
573
574 for (i = 0; i < entry->shapescxx . . $ IfWeederMaterialEntry#shapes..getLengthcxx . . $ SbPList#getLength(455f465bc33b4cdf).(); i++) {
575 SoIndexedShapecxx . . $ SoIndexedShape# *iShape = (SoIndexedShapecxx . . $ SoIndexedShape# *) entry->shapescxx . . $ IfWeederMaterialEntry#shapes.[cxx . . $ SoNodeList#`operator[]`(fd675789997d09c2).i];
576 int numIndices = iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).();
577 if (numIndices == 1) {
578
579 // If there is only one material index and its value is 0
580 // or default, that means the shape uses overall material
581 // binding, so we can skip the shape.
582 if (iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..isDefaultcxx . . $ SoField#isDefault(455f465bc33b4cdf).() ||
583 iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex.[cxx . . $ SoMFInt32#`operator[]`(7ccb52b1bfe01045).0] == 0)
584 continue;
585
586 numIndices = iShape->coordIndexcxx . . $ SoIndexedShape#coordIndex..getNumcxx . . $ SoMField#getNum(455f465bc33b4cdf).();
587 iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..setValuescxx . . $ SoMFInt32#setValues(4ba7fc99d2e6f458).(0, numIndices,
588 iShape->coordIndexcxx . . $ SoIndexedShape#coordIndex..getValuescxx . . $ SoMFInt32#getValues(7547fac9af83ee5e).(0));
589 }
590 int32_tcxx . . $ int32_t# *newIndices = iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..startEditingcxx . . $ SoMFInt32#startEditing(a7741f82f5fc3640).();
591 for (i = 0; i < numIndices; i++) {
592 int mi = newIndices[i];
593 if (mi >= 0) {
594 // The material index should have already been brought
595 // into the correct range
596 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(mi < numMaterials);
597 newIndices[i] = materialIndex[mi];
598 ASSERTcxx . . $ `apps/tools/ivfix/IfAssert.h:54:9`!(newIndices[i] >= 0);
599 }
600 }
601 iShape->materialIndexcxx . . $ SoIndexedShape#materialIndex..finishEditingcxx . . $ SoMFInt32#finishEditing(49f6e7a06ebc5aa8).();
602 }
603
604 ////////////////////////////////////////////
605 //
606 // Now we need to compress the material values themselves. Save
607 // the existing fields, then copy the needed values into the real
608 // fields. Do each field that has more than 1 value.
609 //
610
611 SoMFColorcxx . . $ SoMFColor# saveColorscxx . . $ SoMFColor#SoMFColor(49f6e7a06ebc5aa8).;
612 SoMFFloatcxx . . $ SoMFFloat# saveFloatscxx . . $ SoMFFloat#SoMFFloat(49f6e7a06ebc5aa8).;
613 saveColors.setContainercxx . . $ SoField#setContainer(1c5659d1d94c054f).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
614 saveFloats.setContainercxx . . $ SoField#setContainer(1c5659d1d94c054f).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
615
616 #define COMPRESS_COLORcxx . . $ `apps/tools/ivfix/IfWeeder.c++:616:9`!(FIELD) \
617 if (material->FIELD.getNum() > 1) { \
618 saveColors = material->FIELD; \
619 material->FIELD.setNum(numUsed); \
620 const SbColor *oldColors = saveColors.getValues(0); \
621 SbColor *newColors = material->FIELD.startEditing(); \
622 curNewIndex = 0; \
623 for (i = 0; i < numMaterials; i++) \
624 if (materialUsed[i]) \
625 newColors[curNewIndex++] = oldColors[i]; \
626 material->FIELD.finishEditing(); \
627 }
628
629 #define COMPRESS_FLOATcxx . . $ `apps/tools/ivfix/IfWeeder.c++:629:9`!(FIELD) \
630 if (material->FIELD.getNum() > 1) { \
631 saveFloats = material->FIELD; \
632 material->FIELD.setNum(numUsed); \
633 const float *oldFloats = saveFloats.getValues(0); \
634 float *newFloats = material->FIELD.startEditing(); \
635 curNewIndex = 0; \
636 for (i = 0; i < numMaterials; i++) \
637 if (materialUsed[i]) \
638 newFloats[curNewIndex++] = oldFloats[i]; \
639 material->FIELD.finishEditing(); \
640 }
641
642 COMPRESS_COLORcxx . . $ `apps/tools/ivfix/IfWeeder.c++:616:9`!(ambientColor);
643 COMPRESS_COLORcxx . . $ `apps/tools/ivfix/IfWeeder.c++:616:9`!(diffuseColor);
644 COMPRESS_COLORcxx . . $ `apps/tools/ivfix/IfWeeder.c++:616:9`!(specularColor);
645 COMPRESS_COLORcxx . . $ `apps/tools/ivfix/IfWeeder.c++:616:9`!(emissiveColor);
646 COMPRESS_FLOATcxx . . $ `apps/tools/ivfix/IfWeeder.c++:629:9`!(shininess);
647 COMPRESS_FLOATcxx . . $ `apps/tools/ivfix/IfWeeder.c++:629:9`!(transparency);
648
649 // Clean up
650 delete [] materialUsed;
651 delete [] materialIndex;
652
653 #undef COMPRESS_COLORcxx . . $ `apps/tools/ivfix/IfWeeder.c++:616:9`!
654 #undef COMPRESS_FLOATcxx . . $ `apps/tools/ivfix/IfWeeder.c++:629:9`!
655 }