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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* Copyright 2000-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.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
import javax.vecmath.Point3d;
/**
* A leaf node that holds a merged shapes in compile mode
*/
class Shape3DCompileRetained extends Shape3DRetained {
int numShapes = 0;
// Each element in the arraylist is an array of geometries for a
// particular merged shape
ArrayList<ArrayList<Geometry>> geometryInfo = null;
Object[] srcList = null;
Shape3DCompileRetained(Shape3DRetained[] shapes, int nShapes, int compileFlags) {
int i, j;
// Merged list, only merged if geometry is mergeable
ArrayList<GeometryArrayRetained>[] mergedList = new ArrayList[GeometryRetained.GEO_TYPE_GEOMETRYARRAY + 1];
// Sorted list of separate geometry by geoType
ArrayList<GeometryArrayRetained>[] separateList = new ArrayList[GeometryRetained.GEO_TYPE_GEOMETRYARRAY + 1];
// Assign the num of shapes
numShapes = nShapes;
srcList = new Object[nShapes];
if (nShapes > 0) {
boundsAutoCompute = shapes[0].boundsAutoCompute;
source = shapes[0].source;
}
// Remove the null that was added by Shape3DRetained constructor
geometryList.remove(0);
// Assign the fields for this compile shape
boundsAutoCompute = shapes[0].boundsAutoCompute;
isPickable = shapes[0].isPickable;
isCollidable = shapes[0].isCollidable;
appearanceOverrideEnable = shapes[0].appearanceOverrideEnable;
appearance = shapes[0].appearance;
collisionBound = shapes[0].collisionBound;
localBounds = shapes[0].localBounds;
if ((compileFlags & CompileState.GEOMETRY_READ) != 0)
geometryInfo = new ArrayList<ArrayList<Geometry>>();
for (i = 0; i < nShapes; i++) {
Shape3DRetained shape = shapes[i];
((Shape3D)shape.source).id = i;
shape.source.retained = this;
srcList[i] = shape.source;
// If the transform has been pushd down
// to the shape, don't merge its geometry with other shapes
// geometry
// Put it in a separate list sorted by geo_type
// Have to handle shape.isPickable
for (j = 0; j < shape.geometryList.size(); j++) {
GeometryArrayRetained geo = (GeometryArrayRetained)shape.geometryList.get(j);
if (geo == null)
continue;
if (shape.willRemainOpaque(geo.geoType) && geo.isMergeable()) {
if (mergedList[geo.geoType] == null) {
mergedList[geo.geoType] = new ArrayList<GeometryArrayRetained>();
}
mergedList[geo.geoType].add(geo);
}
else {
// Keep a sorted list based on geoType;
if (separateList[geo.geoType] == null) {
separateList[geo.geoType] = new ArrayList<GeometryArrayRetained>();
}
// add it to the geometryList separately
separateList[geo.geoType].add(geo);
}
}
// Point to the geometryList's source, so the
// retained side will be garbage collected
if ((compileFlags & CompileState.GEOMETRY_READ) != 0) {
ArrayList<Geometry> sList = new ArrayList<Geometry>();
for (j = 0; j < shape.geometryList.size(); j++) {
GeometryRetained g = shape.geometryList.get(j);
if (g != null)
sList.add((Geometry)g.source);
else
sList.add(null);
}
geometryInfo.add(sList);
}
}
// Now, merged the mergelist and separate list based on geoType,
// this enables dlist optmization
for (i = 1; i <= GeometryRetained.GEO_TYPE_GEOMETRYARRAY; i++) {
switch (i) {
case GeometryArrayRetained.GEO_TYPE_QUAD_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new QuadArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_TRI_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new TriangleArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_POINT_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new PointArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_LINE_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new LineArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_TRI_STRIP_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new TriangleStripArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_TRI_FAN_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new TriangleFanArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_LINE_STRIP_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new LineStripArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_INDEXED_QUAD_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new IndexedQuadArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_INDEXED_TRI_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new IndexedTriangleArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_INDEXED_POINT_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new IndexedPointArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_INDEXED_LINE_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i], new IndexedLineArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_INDEXED_TRI_STRIP_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i],
new IndexedTriangleStripArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_INDEXED_TRI_FAN_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i],
new IndexedTriangleFanArrayRetained());
addSeparateList(separateList[i]);
break;
case GeometryArrayRetained.GEO_TYPE_INDEXED_LINE_STRIP_SET:
if (mergedList[i] != null)
addMergedList(mergedList[i],
new IndexedLineStripArrayRetained());
addSeparateList(separateList[i]);
break;
}
}
}
private void addMergedList(ArrayList<GeometryArrayRetained> glist,
GeometryArrayRetained cgeo) {
cgeo.setCompiled(glist);
geometryList.add(cgeo);
cgeo.setSource(((SceneGraphObjectRetained) glist.get(0)).source);
}
private void addSeparateList(ArrayList<GeometryArrayRetained> glist) {
if (glist == null)
return;
for (int k = 0; k < glist.size(); k++) {
geometryList.add(glist.get(k));
}
}
@Override
Bounds getCollisionBounds(int childIndex) {
return collisionBound;
}
@Override
int numGeometries(int childIndex) {
return geometryInfo.get(childIndex).size();
}
@Override
Geometry getGeometry(int i, int childIndex) {
return geometryInfo.get(childIndex).get(i);
}
@Override
Enumeration<Geometry> getAllGeometries(int childIndex) {
ArrayList<Geometry> geoInfo = geometryInfo.get(childIndex);
Vector<Geometry> geomList = new Vector<Geometry>();
for (int i = 0; i < geoInfo.size(); i++) {
geomList.add(geoInfo.get(i));
}
return geomList.elements();
}
Bounds getBounds(int childIndex) {
if (!boundsAutoCompute)
return super.getBounds();
ArrayList<Geometry> glist = geometryInfo.get(childIndex);
if (glist == null)
return null;
BoundingBox bbox = new BoundingBox((Bounds)null);
for (int i = 0; i < glist.size(); i++) {
Geometry g = glist.get(i);
if (g == null)
continue;
GeometryRetained geometry = (GeometryRetained)g.retained;
if (geometry.geoType == GeometryRetained.GEO_TYPE_NONE)
continue;
geometry.computeBoundingBox();
synchronized (geometry.geoBounds) {
bbox.combine(geometry.geoBounds);
}
}
return bbox;
}
/**
* Check if the geometry component of this shape node under path
* intersects with the pickRay.
* @return true if intersected else false. If return is true, dist
* contains the closest
* distance of intersection.
* @exception IllegalArgumentException if <code>path</code> is
* invalid.
*/
@Override
boolean intersect(SceneGraphPath path,
PickShape pickShape, double[] dist) {
int flags;
PickInfo pickInfo = new PickInfo();
Transform3D localToVworld = path.getTransform();
if (localToVworld == null) {
throw new IllegalArgumentException(J3dI18N.getString("Shape3DRetained3"));
}
pickInfo.setLocalToVWorldRef( localToVworld);
Shape3D shape = (Shape3D) path.getObject();
// Get the geometries for this shape only, since the compiled
// geomtryList contains several shapes
ArrayList<Geometry> glist = geometryInfo.get(shape.id);
// System.err.println("Shape3DCompileRetained.intersect() : ");
if (dist == null) {
// System.err.println(" no dist request ....");
return intersect(pickInfo, pickShape, 0, glist);
}
flags = PickInfo.CLOSEST_DISTANCE;
if (intersect(pickInfo, pickShape, flags, glist)) {
dist[0] = pickInfo.getClosestDistance();
return true;
}
return false;
}
boolean intersect(PickInfo pickInfo, PickShape pickShape, int flags,
ArrayList<Geometry> geometryList) {
Transform3D localToVworld = pickInfo.getLocalToVWorldRef();
Transform3D t3d = new Transform3D();
t3d.invert(localToVworld);
PickShape newPS = pickShape.transform(t3d);
int geomListSize = geometryList.size();
GeometryRetained geometry;
if (((flags & PickInfo.CLOSEST_INTERSECTION_POINT) == 0) &&
((flags & PickInfo.CLOSEST_DISTANCE) == 0) &&
((flags & PickInfo.CLOSEST_GEOM_INFO) == 0) &&
((flags & PickInfo.ALL_GEOM_INFO) == 0)) {
for (int i=0; i < geomListSize; i++) {
geometry = (GeometryRetained) geometryList.get(i).retained;
if (geometry != null) {
if (geometry.mirrorGeometry != null) {
geometry = geometry.mirrorGeometry;
}
// Need to modify this method
// if (geometry.intersect(newPS, null, null)) {
if (geometry.intersect(newPS, null, 0, null, null, 0)) {
return true;
}
}
}
}
else {
double distance;
double minDist = Double.POSITIVE_INFINITY;
Point3d closestIPnt = new Point3d();
Point3d iPnt = new Point3d();
Point3d iPntVW = new Point3d();
for (int i=0; i < geomListSize; i++) {
geometry = (GeometryRetained) geometryList.get(i).retained;
if (geometry != null) {
if (geometry.mirrorGeometry != null) {
geometry = geometry.mirrorGeometry;
}
if (geometry.intersect(newPS, pickInfo, flags, iPnt, geometry, i)) {
iPntVW.set(iPnt);
localToVworld.transform(iPntVW);
distance = pickShape.distance(iPntVW);
if (minDist > distance) {
minDist = distance;
closestIPnt.set(iPnt);
}
}
}
}
if (minDist < Double.POSITIVE_INFINITY) {
if ((flags & PickInfo.CLOSEST_DISTANCE) != 0) {
pickInfo.setClosestDistance(minDist);
}
if((flags & PickInfo.CLOSEST_INTERSECTION_POINT) != 0) {
pickInfo.setClosestIntersectionPoint(closestIPnt);
}
return true;
}
}
return false;
}
}
|