001: /*
002: * $RCSfile: IndexedPointArrayRetained.java,v $
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.7 $
028: * $Date: 2008/02/28 20:17:24 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035: import java.lang.Math;
036:
037: /**
038: * The IndexedPointArray object draws the array of vertices as individual points.
039: */
040:
041: class IndexedPointArrayRetained extends IndexedGeometryArrayRetained {
042:
043: IndexedPointArrayRetained() {
044: this .geoType = GEO_TYPE_INDEXED_POINT_SET;
045: }
046:
047: boolean intersect(PickShape pickShape, PickInfo pickInfo,
048: int flags, Point3d iPnt, GeometryRetained geom,
049: int geomIndex) {
050: double sdist[] = new double[1];
051: double minDist = Double.MAX_VALUE;
052: double x = 0, y = 0, z = 0;
053: Point3d pnt = new Point3d();
054: int[] vtxIndexArr = new int[1];
055:
056: int i = ((vertexFormat & GeometryArray.BY_REFERENCE) == 0 ? initialVertexIndex
057: : initialCoordIndex);
058:
059: switch (pickShape.getPickType()) {
060: case PickShape.PICKRAY:
061: PickRay pickRay = (PickRay) pickShape;
062:
063: while (i < validVertexCount) {
064: vtxIndexArr[0] = indexCoord[i];
065: getVertexData(indexCoord[i++], pnt);
066: if (intersectPntAndRay(pnt, pickRay.origin,
067: pickRay.direction, sdist)) {
068: if (flags == 0) {
069: return true;
070: }
071: if (sdist[0] < minDist) {
072: minDist = sdist[0];
073: x = pnt.x;
074: y = pnt.y;
075: z = pnt.z;
076: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
077: storeInterestData(pickInfo, flags, geom,
078: geomIndex, vtxIndexArr, iPnt,
079: sdist[0]);
080: }
081: }
082: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
083: storeInterestData(pickInfo, flags, geom,
084: geomIndex, vtxIndexArr, iPnt, sdist[0]);
085: }
086: }
087: }
088: break;
089: case PickShape.PICKSEGMENT:
090: PickSegment pickSegment = (PickSegment) pickShape;
091: Vector3d dir = new Vector3d(pickSegment.end.x
092: - pickSegment.start.x, pickSegment.end.y
093: - pickSegment.start.y, pickSegment.end.z
094: - pickSegment.start.z);
095:
096: while (i < validVertexCount) {
097: vtxIndexArr[0] = indexCoord[i];
098: getVertexData(indexCoord[i++], pnt);
099: if (intersectPntAndRay(pnt, pickSegment.start, dir,
100: sdist)
101: && (sdist[0] <= 1.0)) {
102: if (flags == 0) {
103: return true;
104: }
105: if (sdist[0] < minDist) {
106: minDist = sdist[0];
107: x = pnt.x;
108: y = pnt.y;
109: z = pnt.z;
110: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
111: storeInterestData(pickInfo, flags, geom,
112: geomIndex, vtxIndexArr, iPnt,
113: sdist[0]);
114: }
115: }
116: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
117: storeInterestData(pickInfo, flags, geom,
118: geomIndex, vtxIndexArr, iPnt, sdist[0]);
119: }
120: }
121: }
122: break;
123: case PickShape.PICKBOUNDINGBOX:
124: case PickShape.PICKBOUNDINGSPHERE:
125: case PickShape.PICKBOUNDINGPOLYTOPE:
126: Bounds bounds = ((PickBounds) pickShape).bounds;
127:
128: while (i < validVertexCount) {
129: vtxIndexArr[0] = indexCoord[i];
130: getVertexData(indexCoord[i++], pnt);
131: if (bounds.intersect(pnt)) {
132: if (flags == 0) {
133: return true;
134: }
135: sdist[0] = pickShape.distance(pnt);
136: if (sdist[0] < minDist) {
137: minDist = sdist[0];
138: x = pnt.x;
139: y = pnt.y;
140: z = pnt.z;
141: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
142: storeInterestData(pickInfo, flags, geom,
143: geomIndex, vtxIndexArr, iPnt,
144: sdist[0]);
145: }
146: }
147: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
148: storeInterestData(pickInfo, flags, geom,
149: geomIndex, vtxIndexArr, iPnt, sdist[0]);
150: }
151: }
152: }
153: break;
154: case PickShape.PICKCYLINDER:
155: PickCylinder pickCylinder = (PickCylinder) pickShape;
156:
157: while (i < validVertexCount) {
158: vtxIndexArr[0] = indexCoord[i];
159: getVertexData(indexCoord[i++], pnt);
160: if (intersectCylinder(pnt, pickCylinder, sdist)) {
161: if (flags == 0) {
162: return true;
163: }
164: if (sdist[0] < minDist) {
165: minDist = sdist[0];
166: x = pnt.x;
167: y = pnt.y;
168: z = pnt.z;
169: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
170: storeInterestData(pickInfo, flags, geom,
171: geomIndex, vtxIndexArr, iPnt,
172: sdist[0]);
173: }
174: }
175: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
176: storeInterestData(pickInfo, flags, geom,
177: geomIndex, vtxIndexArr, iPnt, sdist[0]);
178: }
179: }
180: }
181: break;
182: case PickShape.PICKCONE:
183: PickCone pickCone = (PickCone) pickShape;
184:
185: while (i < validVertexCount) {
186: vtxIndexArr[0] = indexCoord[i];
187: getVertexData(indexCoord[i++], pnt);
188: if (intersectCone(pnt, pickCone, sdist)) {
189: if (flags == 0) {
190: return true;
191: }
192: if (sdist[0] < minDist) {
193: minDist = sdist[0];
194: x = pnt.x;
195: y = pnt.y;
196: z = pnt.z;
197: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
198: storeInterestData(pickInfo, flags, geom,
199: geomIndex, vtxIndexArr, iPnt,
200: sdist[0]);
201: }
202: }
203: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
204: storeInterestData(pickInfo, flags, geom,
205: geomIndex, vtxIndexArr, iPnt, sdist[0]);
206: }
207: }
208: }
209: break;
210: case PickShape.PICKPOINT:
211: // Should not happen since API already check for this
212: throw new IllegalArgumentException(J3dI18N
213: .getString("IndexedPointArrayRetained0"));
214: default:
215: throw new RuntimeException(
216: "PickShape not supported for intersection");
217: }
218:
219: if (minDist < Double.MAX_VALUE) {
220: iPnt.x = x;
221: iPnt.y = y;
222: iPnt.z = z;
223: return true;
224: }
225: return false;
226: }
227:
228: boolean intersect(Point3d[] pnts) {
229: Point3d point = new Point3d();
230: int i = ((vertexFormat & GeometryArray.BY_REFERENCE) == 0 ? initialVertexIndex
231: : initialCoordIndex);
232:
233: switch (pnts.length) {
234: case 3: // Triangle
235: while (i < validVertexCount) {
236: getVertexData(indexCoord[i++], point);
237: if (intersectTriPnt(pnts[0], pnts[1], pnts[2], point)) {
238: return true;
239: }
240: }
241: break;
242: case 4: // Quad
243: while (i < validVertexCount) {
244: getVertexData(indexCoord[i++], point);
245: if (intersectTriPnt(pnts[0], pnts[1], pnts[2], point)
246: || intersectTriPnt(pnts[0], pnts[2], pnts[3],
247: point)) {
248: return true;
249: }
250: }
251: break;
252: case 2: // Line
253: double dist[] = new double[1];
254: Vector3d dir = new Vector3d();
255:
256: while (i < validVertexCount) {
257: getVertexData(indexCoord[i++], point);
258: dir.x = pnts[1].x - pnts[0].x;
259: dir.y = pnts[1].y - pnts[0].y;
260: dir.z = pnts[1].z - pnts[0].z;
261: if (intersectPntAndRay(point, pnts[0], dir, dist)
262: && (dist[0] <= 1.0)) {
263: return true;
264: }
265: }
266: break;
267: case 1: // Point
268: while (i < validVertexCount) {
269: getVertexData(indexCoord[i++], point);
270: if ((pnts[0].x == point.x) && (pnts[0].y == point.y)
271: && (pnts[0].z == point.z)) {
272: return true;
273: }
274: }
275: break;
276: }
277: return false;
278: }
279:
280: boolean intersect(Transform3D this ToOtherVworld,
281: GeometryRetained geom) {
282: Point3d[] pnt = new Point3d[1];
283: int i = ((vertexFormat & GeometryArray.BY_REFERENCE) == 0 ? initialVertexIndex
284: : initialCoordIndex);
285: pnt[0] = new Point3d();
286:
287: while (i < validVertexCount) {
288: getVertexData(indexCoord[i++], pnt[0]);
289: this ToOtherVworld.transform(pnt[0]);
290: if (geom.intersect(pnt)) {
291: return true;
292: }
293: }
294: return false;
295:
296: }
297:
298: // the bounds argument is already transformed
299: boolean intersect(Bounds targetBound) {
300: int i = ((vertexFormat & GeometryArray.BY_REFERENCE) == 0 ? initialVertexIndex
301: : initialCoordIndex);
302: Point3d pnt = new Point3d();
303:
304: while (i < validVertexCount) {
305: getVertexData(indexCoord[i++], pnt);
306: if (targetBound.intersect(pnt)) {
307: return true;
308: }
309: }
310: return false;
311: }
312:
313: int getClassType() {
314: return POINT_TYPE;
315: }
316: }
|