summaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/CompileState.java
blob: fdf777e70d9e42688dce4cc7614afef5ead7ed32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package javax.media.j3d;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/**
 * The CompileState holds information used during a compile.  It is
 * passed to each SceneGraphObject (SGO) during the compile.  Each SGO
 * modifies the CompileState as necessary and passes the CompileState
 * to its children (if any).
 *
 * The CompileState currently has two functions: appearance mapping
 * and shape merging.
 *
 * Appearance mapping maintains a list of the unique appearances seen
 * during the compile.  getAppearance() is used to turn multiple,
 * equivalent and static appearances into a single shared appearance.
 *
 * The shape mergings collects shapes that are potentially mergable
 * during a compile.  The shapes are sorted into a Map of Lists of
 * shapes, using the shape's appearance as the key.  After a subtree
 * is traversed, the shapes are merged and added to the Group.
 */

class CompileState {
// Appearance mapping stuff:
private final HashMap<AppearanceRetained, AppearanceRetained> knownAppearances;
    int numShapes = 0;

// Shape merging stuff:
private final HashMap<AppearanceRetained, Vector<Shape3DRetained>> shapeLists;
    int	numMergeSets = 0;
    int	numMergeShapes = 0;

    static final int BOUNDS_READ		= 0x00001;
    static final int GEOMETRY_READ		= 0x00002;


    // scene graph flattening

    boolean keepTG = false;	// used to force the immediate transform
				// group to stay around

    boolean needNormalsTransform = false;  // true if the current transform
		 			   // needs to push down normals
					   // transform to geometries

				// the current static transform group
    TransformGroupRetained staticTransform = null;

				// parent group
    GroupRetained parentGroup = null;

    int numTransformGroups = 0;
    int numStaticTransformGroups = 0;
    int numMergedTransformGroups = 0;
    int numGroups = 0;
    int numMergedGroups = 0;
    int numShapesWSharedGeom = 0;
    int numShapesWStaticTG = 0;
    int numLinks = 0;
    int numSwitches = 0;
    int numOrderedGroups = 0;
    int numMorphs = 0;

CompileState() {
	knownAppearances = new HashMap<AppearanceRetained, AppearanceRetained>();
	shapeLists = new HashMap<AppearanceRetained, Vector<Shape3DRetained>>();
}

    // Appearance mapping:
/**
 * Returns an unique appearance which equals app. If appearance does not equal
 * any previously found, the appearance will be added to the known appearances
 * and be returned. If the apperance equals a previously known appearance, then
 * the prevously known apperance will be returned
 */
AppearanceRetained getAppearance(AppearanceRetained app) {
	// see if the appearance has allready been classified
	if (app.map == this && app.mapAppearance != null) {
		return app.mapAppearance;
	}

	// check if this appearance equals one one in the Map
	AppearanceRetained retval = knownAppearances.get(app);
	if (retval == null) {
		// not found, put this appearance in the map
		knownAppearances.put(app, app);
		retval = app;
	}

	// cache this result on the appearance in case it appears again
	app.map = this;
	app.mapAppearance = retval;

	return retval;
}

    void addShape(Shape3DRetained shape) {
	if (parentGroup != null) {
	    // sort the shapes into lists with equivalent appearances
		Vector<Shape3DRetained> list = shapeLists.get(shape.appearance);
		if (list == null) {
			list = new Vector<Shape3DRetained>();
			shapeLists.put(shape.appearance, list);
		}
	    // Add the shape to the list only if at its parent level
	    // no children can be added or removed ..
	    // Look for the first non-null geometry, there should be atleast
	    // one otherwise it wouldn't come here
	    GeometryRetained geometry = null;
	    int i = 0;
	    while (geometry == null && i < shape.geometryList.size()) {
			geometry = shape.geometryList.get(i);
		i++;
	    }
	    if (shape.parent instanceof GroupRetained && ((GroupRetained)shape.parent).isStaticChildren() && geometry.geoType < GeometryArrayRetained.GEO_TYPE_RASTER) {
		list.add(shape);
	    }

	}
    }


    void printStats() {
	System.err.println("numTransformGroups= " + numTransformGroups);
	System.err.println("numStaticTransformGroups= " + numStaticTransformGroups);
	System.err.println("numMergedTransformGroups= " + numMergedTransformGroups);
	System.err.println("numGroups= " + numGroups);
	System.err.println("numMergedGroups= " + numMergedGroups);
	System.err.println("numShapes= " + numShapes);
	System.err.println("numShapesWStaticTG= " + numShapesWStaticTG);
	System.err.println("numMergeShapes= " + numMergeShapes);
	System.err.println("numMergeSets= " + numMergeSets);
	System.err.println("numLinks= " + numLinks);
	System.err.println("numSwitches= " + numSwitches);
	System.err.println("numOrderedGroups= " + numOrderedGroups);
	System.err.println("numMorphs= " + numMorphs);
    }

    void doShapeMerge() {

	//	System.err.println("doShapeMerge, shapeList = "+shapeLists);
	if (shapeLists != null) {
	    // loop over the shapes in each list, creating a single shape
	    // for each.  Add the shape to the group
		Collection<Vector<Shape3DRetained>> lists = shapeLists.values();
		Iterator<Vector<Shape3DRetained>> listIterator = lists.iterator();
	    Shape3DRetained mergeShape;
	    GeometryRetained firstGeo;
	    int num = 0;
	    int compileFlags = 0;

	    while (listIterator.hasNext()) {
			Vector<Shape3DRetained> curList = listIterator.next();
		int numShapes = curList.size();
		Shape3DRetained[] shapes = new Shape3DRetained[numShapes];
		curList.copyInto(shapes);
		Shape3DRetained[] toBeMergedShapes = new Shape3DRetained[numShapes];
		for (int i = 0; i < numShapes; i++) {
		    if (shapes[i] == null) {
			continue;
		    }
		    firstGeo = null;
		    num = 0;
		    // Get the first non-null geometry
		    while (firstGeo == null && num < shapes[i].geometryList.size()) {
					firstGeo = shapes[i].geometryList.get(num);
			num++;
		    }

		    if (firstGeo != null && firstGeo instanceof GeometryArrayRetained) {
			int numMerge = 0;
			mergeShape = shapes[i];
			GeometryArrayRetained mergeGeo = (GeometryArrayRetained)firstGeo;

			toBeMergedShapes[numMerge++] = mergeShape;
			// Determine if all mergeable shapes have the same boundsCompute
			// and collisionBounds set the same way
			compileFlags = getCompileFlags(mergeShape);
			for (int j = i+1; j < numShapes; j++) {
			    if (shapes[j] == null) {
				continue;
			    }
			    firstGeo = null;
			    num = 0;
			    // Get the first non-null geometry
			    while (firstGeo == null && num < shapes[j].geometryList.size()) {
							firstGeo = shapes[j].geometryList.get(num);
				num++;
			    }

			    // There is a non-null geometry for this shape ..
			    if (firstGeo != null &&
				shapes[j].isEquivalent(mergeShape) &&
				firstGeo.isEquivalenceClass(mergeGeo) &&
				((GeometryArrayRetained)firstGeo).vertexFormat == mergeGeo.vertexFormat) {
				    // got one to merge, add shapes to merge,
				    toBeMergedShapes[numMerge++] = shapes[j];

				    compileFlags |= getCompileFlags(shapes[j]);

				    // remove from shapes
				    shapes[j] = null;
			    }
			}
			if (numMerge > 1) {

			    // remove the shapes from its parent before merge
			    // They all should
			    GroupRetained group = (GroupRetained)toBeMergedShapes[0].parent;
			    Shape3DRetained s;
			    for (int n = 0; n < numMerge; n++) {
				s = toBeMergedShapes[n];
				boolean found = false;
				int numChilds = group.numChildren();
				for (int k = 0; (k < numChilds && !found); k++) {
				    if (group.getChild(k).retained == s) {
					found = true;
					group.removeChild(k);
				    }
				}
				if (!found) {
				    System.err.println("ShapeSet.add(): Can't remove " +
						       "shape from parent, can't find shape!");
				}

			    }

			    mergeShape = new Shape3DCompileRetained(toBeMergedShapes, numMerge, compileFlags);

			    if (J3dDebug.devPhase && J3dDebug.debug) {
				if (J3dDebug.doDebug(J3dDebug.compileState, J3dDebug.LEVEL_3)) {
				    System.err.println("Dest is "+ parentGroup);
				    System.err.println("Compile Shape "+mergeShape);
				    System.err.println(mergeShape.geometryList.size()+" geoemtryList");
								for (int j = 0; j < mergeShape.geometryList.size(); j++) {
									GeometryRetained geo = mergeShape.geometryList.get(j);
									if (geo != null)
										System.err.println("\t Geo_type = " + geo.geoType);
								}

				    System.err.println(numMerge+" Shapes were merged ");
				    for (int j = 0; j < numMerge; j++) {
					System.err.println("\t" + toBeMergedShapes[j]);
				    }
				}
			    }

			    // Set the source to one of the merged shape's source
			    mergeShape.setSource(toBeMergedShapes[0].source);
			    numMergeSets++;
			    numMergeShapes += numMerge ;
			    parentGroup.addChild((Node)mergeShape.source);
			}
		    }
		    // add the shape to the dest
		}
	    }
	}

	// Clear the shapelists for the next merge
	shapeLists.clear();

    }


    int getCompileFlags(Shape3DRetained shape) {
	int cflag = 0;

	// If allow intersect is turned on , then geometry is readable
	if (shape.allowIntersect() ||
	    shape.source.getCapability(Shape3D.ALLOW_GEOMETRY_READ)||
	    (shape.boundsAutoCompute &&
	     shape.source.getCapability(Shape3D.ALLOW_BOUNDS_READ)))
	    cflag |= GEOMETRY_READ;

	return cflag;

    }

}