001: /*
002: * $RCSfile: GeometrySizeProcessor.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.3 $
041: * $Date: 2007/02/09 17:17:02 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.utils.scenegraph.traverser;
046:
047: import java.util.HashSet;
048: import java.util.Enumeration;
049: import javax.media.j3d.*;
050:
051: /**
052: * Count the number of Geometry primitives in all the Shape3D nodes supplied by
053: * TreeScan
054: *
055: * The number of Triangles, Quads, Points and Lines are available
056: */
057: public class GeometrySizeProcessor implements ProcessNodeInterface {
058:
059: private long triangleCount;
060: private long quadCount;
061: private long lineCount;
062: private long pointCount;
063:
064: private long vertexInStrips;
065: private long vertexInArrays;
066:
067: private int[] triangleStripDistribution;
068: private int[] triangleFanDistribution;
069:
070: private long stripCount;
071:
072: private int shape3Dcount;
073:
074: private HashSet appearanceSet = new HashSet();
075:
076: /** Creates new GeometrySizeProcessor */
077: public GeometrySizeProcessor() {
078: triangleCount = 0;
079: pointCount = 0;
080: quadCount = 0;
081: lineCount = 0;
082: vertexInStrips = 0;
083: vertexInArrays = 0;
084: stripCount = 0;
085: shape3Dcount = 0;
086: triangleStripDistribution = new int[15];
087: triangleFanDistribution = new int[15];
088:
089: for (int i = 0; i < triangleStripDistribution.length; i++) {
090: triangleStripDistribution[i] = 0;
091: triangleFanDistribution[i] = 0;
092: }
093:
094: appearanceSet.clear();
095: }
096:
097: /**
098: * Process the Shape3D node, increment triangleCount by the number
099: * of triangles in the Geometry of the node
100: * @param node must be a Shape3D node
101: */
102: public boolean processNode(javax.media.j3d.Node node) {
103: if (!javax.media.j3d.Shape3D.class.isAssignableFrom(node
104: .getClass()))
105: throw new RuntimeException("Node class "
106: + node.getClass().getName()
107: + " is not a (subclass) of Shape3D as expected");
108:
109: shape3Dcount++;
110: appearanceSet.add(((Shape3D) node).getAppearance());
111:
112: Enumeration e = ((Shape3D) node).getAllGeometries();
113:
114: while (e.hasMoreElements()) {
115: Geometry geom = (Geometry) e.nextElement();
116:
117: if (geom instanceof GeometryStripArray) {
118: int numStrips = ((GeometryStripArray) geom)
119: .getNumStrips();
120: int vertexCounts[] = new int[numStrips];
121: ((GeometryStripArray) geom)
122: .getStripVertexCounts(vertexCounts);
123: for (int i = 0; i < numStrips; i++)
124: vertexInStrips += vertexCounts[i];
125:
126: stripCount += numStrips;
127:
128: if (geom instanceof TriangleFanArray) {
129: for (int i = 0; i < numStrips; i++) {
130: int c = vertexCounts[i] - 2;
131: storeTriangleFanSize(c);
132: triangleCount += c;
133: }
134: } else if (geom instanceof TriangleStripArray) {
135: for (int i = 0; i < numStrips; i++) {
136: int c = vertexCounts[i] - 2;
137: storeTriangleStripSize(c);
138: triangleCount += c;
139: }
140: } else if (geom instanceof LineStripArray) {
141: for (int i = 0; i < numStrips; i++)
142: lineCount += vertexCounts[i] - 1;
143: } else
144: unsupportedGeom(geom);
145: } else if (geom instanceof IndexedGeometryStripArray) {
146: int numStrips = ((IndexedGeometryStripArray) geom)
147: .getNumStrips();
148: int vertexCounts[] = new int[numStrips];
149: ((IndexedGeometryStripArray) geom)
150: .getStripIndexCounts(vertexCounts);
151: for (int i = 0; i < numStrips; i++)
152: vertexInStrips += vertexCounts[i];
153:
154: stripCount += numStrips;
155:
156: if (geom instanceof IndexedTriangleFanArray) {
157: for (int i = 0; i < numStrips; i++) {
158: int c = vertexCounts[i] - 2;
159: triangleCount += c;
160: storeTriangleFanSize(c);
161: }
162: } else if (geom instanceof IndexedTriangleStripArray) {
163: for (int i = 0; i < numStrips; i++) {
164: int c = vertexCounts[i] - 2;
165: triangleCount += c;
166: storeTriangleStripSize(c);
167: }
168: } else if (geom instanceof IndexedLineStripArray) {
169: for (int i = 0; i < numStrips; i++)
170: lineCount += vertexCounts[i] - 1;
171: } else
172: unsupportedGeom(geom);
173: } else if (geom instanceof IndexedGeometryArray) {
174: if (geom instanceof IndexedTriangleArray) {
175: triangleCount += ((IndexedGeometryArray) geom)
176: .getIndexCount() / 3;
177: vertexInArrays += ((IndexedGeometryArray) geom)
178: .getIndexCount();
179: } else if (geom instanceof IndexedQuadArray) {
180: quadCount += ((IndexedGeometryArray) geom)
181: .getIndexCount() / 4;
182: vertexInArrays += ((IndexedGeometryArray) geom)
183: .getIndexCount();
184: } else if (geom instanceof IndexedLineArray) {
185: lineCount += ((IndexedGeometryArray) geom)
186: .getIndexCount() / 2;
187: vertexInArrays += ((IndexedGeometryArray) geom)
188: .getIndexCount();
189: } else if (geom instanceof IndexedPointArray) {
190: pointCount += ((IndexedGeometryArray) geom)
191: .getIndexCount();
192: vertexInArrays += ((IndexedGeometryArray) geom)
193: .getIndexCount();
194: } else
195: unsupportedGeom(geom);
196: } else if (geom instanceof LineArray) {
197: lineCount += ((LineArray) geom).getVertexCount() / 2;
198: vertexInArrays += ((LineArray) geom).getVertexCount();
199: } else if (geom instanceof TriangleArray) {
200: triangleCount += ((TriangleArray) geom)
201: .getVertexCount() / 3;
202: vertexInArrays += ((TriangleArray) geom)
203: .getVertexCount();
204: } else if (geom instanceof QuadArray) {
205: quadCount += ((QuadArray) geom).getVertexCount() / 4;
206: vertexInArrays += ((QuadArray) geom).getVertexCount();
207: } else if (geom instanceof PointArray) {
208: pointCount += ((PointArray) geom).getVertexCount();
209: vertexInArrays += ((PointArray) geom).getVertexCount();
210: } else
211: unsupportedGeom(geom);
212: }
213: return true;
214: }
215:
216: /**
217: * Keep a record of the distribution of Triangle strip lengths
218: *
219: * @param size number of Triangles in the strip
220: */
221: private void storeTriangleStripSize(int size) {
222: if (size < 10)
223: triangleStripDistribution[size]++;
224: else if (size < 20)
225: triangleStripDistribution[10]++;
226: else if (size < 50)
227: triangleStripDistribution[11]++;
228: else if (size < 100)
229: triangleStripDistribution[12]++;
230: else if (size < 1000) {
231: triangleStripDistribution[13]++;
232: } else
233: triangleStripDistribution[14]++;
234: }
235:
236: /**
237: * Keep a record of the distribution of Triangle strip lengths
238: *
239: * @param size number of Triangles in the strip
240: */
241: private void storeTriangleFanSize(int size) {
242: if (size < 10)
243: triangleFanDistribution[size]++;
244: else if (size < 20)
245: triangleFanDistribution[10]++;
246: else if (size < 50)
247: triangleFanDistribution[11]++;
248: else if (size < 100)
249: triangleFanDistribution[12]++;
250: else if (size < 1000) {
251: triangleFanDistribution[13]++;
252: } else
253: triangleFanDistribution[14]++;
254: }
255:
256: /**
257: * Display the Unsupported Geometry error message to stderr
258: */
259: private void unsupportedGeom(Geometry geom) {
260: if (geom == null)
261: return;
262: else
263: System.err
264: .println("GeometrySizeProcessor: Unsupported Geometry Type "
265: + geom.getClass().getName());
266: }
267:
268: /** Getter for property lineCount.
269: * @return Value of property lineCount.
270: */
271: public long getLineCount() {
272: return lineCount;
273: }
274:
275: /** Setter for property lineCount.
276: * @param lineCount New value of property lineCount.
277: */
278: public void setLineCount(long lineCount) {
279: this .lineCount = lineCount;
280: }
281:
282: /** Getter for property quadCount.
283: * @return Value of property quadCount.
284: */
285: public long getQuadCount() {
286: return quadCount;
287: }
288:
289: /** Setter for property quadCount.
290: * @param quadCount New value of property quadCount.
291: */
292: public void setQuadCount(long quadCount) {
293: this .quadCount = quadCount;
294: }
295:
296: /** Getter for property pointCount.
297: * @return Value of property pointCount.
298: */
299: public long getPointCount() {
300: return pointCount;
301: }
302:
303: /** Setter for property pointCount.
304: * @param pointCount New value of property pointCount.
305: */
306: public void setPointCount(long pointCount) {
307: this .pointCount = pointCount;
308: }
309:
310: /** Getter for property triangleCount.
311: * @return Value of property triangleCount.
312: */
313: public long getTriangleCount() {
314: return triangleCount;
315: }
316:
317: /** Setter for property triangleCount.
318: * @param triangleCount New value of property triangleCount.
319: */
320: public void setTriangleCount(long triangleCount) {
321: this .triangleCount = triangleCount;
322: }
323:
324: /**
325: * Return the triangle strip size distributions
326: *
327: * Each element of the array represents the following Strip size
328: *
329: * element strip size
330: * 0
331: * 1 1
332: * 2 2
333: * 3 3
334: * 4 4
335: * 5 5
336: * 6 6
337: * 7 7
338: * 8 8
339: * 9 9
340: * 10 10-19
341: * 11 20-49
342: * 12 50-99
343: * 13 100-999
344: * 14 > 1000
345: */
346: public int[] getTriangleStripDistribution() {
347: return triangleStripDistribution;
348: }
349:
350: /**
351: * Return the triangle fan size distributions
352: *
353: * Each element of the array represents the following Strip size
354: *
355: * element strip size
356: * 0
357: * 1 1
358: * 2 2
359: * 3 3
360: * 4 4
361: * 5 5
362: * 6 6
363: * 7 7
364: * 8 8
365: * 9 9
366: * 10 10-19
367: * 11 20-49
368: * 12 50-99
369: * 13 100-999
370: * 14 > 1000
371: */
372: public int[] getTriangleFanDistribution() {
373: return triangleFanDistribution;
374: }
375:
376: public int getShape3DCount() {
377: return shape3Dcount;
378: }
379:
380: public void setShape3DCount(int count) {
381: shape3Dcount = count;
382: }
383:
384: public int getAppearanceCount() {
385: return appearanceSet.size();
386: }
387:
388: /**
389: * Return the percentage of Vertex which are in strips
390: */
391: public float getStripPercentage() {
392: if ((vertexInStrips + vertexInArrays) == 0)
393: return 0;
394: else
395: return ((float) vertexInStrips)
396: / (vertexInStrips + vertexInArrays) * 100f;
397: }
398:
399: /**
400: * Return the average length of the Strips
401: */
402: public float getAverageStripLength() {
403: if (stripCount == 0)
404: return 0.0f;
405: else
406: return (float) vertexInStrips / (float) stripCount;
407: }
408: }
|