001: /*
002: * $RCSfile: PointArrayRetained.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:28 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035: import java.lang.Math;
036:
037: /**
038: * The PointArray object draws the array of vertices as individual points.
039: */
040:
041: class PointArrayRetained extends GeometryArrayRetained {
042:
043: PointArrayRetained() {
044: this .geoType = GEO_TYPE_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: int i = ((vertexFormat & GeometryArray.BY_REFERENCE) == 0 ? initialVertexIndex
054: : initialCoordIndex);
055: Point3d pnt = new Point3d();
056: int[] vtxIndexArr = new int[1];
057:
058: switch (pickShape.getPickType()) {
059: case PickShape.PICKRAY:
060: PickRay pickRay = (PickRay) pickShape;
061:
062: while (i < validVertexCount) {
063: vtxIndexArr[0] = i;
064: getVertexData(i++, pnt);
065: if (intersectPntAndRay(pnt, pickRay.origin,
066: pickRay.direction, sdist)) {
067: if (flags == 0) {
068: return true;
069: }
070: if (sdist[0] < minDist) {
071: minDist = sdist[0];
072: x = pnt.x;
073: y = pnt.y;
074: z = pnt.z;
075: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
076: storeInterestData(pickInfo, flags, geom,
077: geomIndex, vtxIndexArr, iPnt,
078: sdist[0]);
079: }
080: }
081: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
082: storeInterestData(pickInfo, flags, geom,
083: geomIndex, vtxIndexArr, iPnt, sdist[0]);
084: }
085: }
086: }
087: break;
088: case PickShape.PICKSEGMENT:
089: PickSegment pickSegment = (PickSegment) pickShape;
090: Vector3d dir = new Vector3d(pickSegment.end.x
091: - pickSegment.start.x, pickSegment.end.y
092: - pickSegment.start.y, pickSegment.end.z
093: - pickSegment.start.z);
094: while (i < validVertexCount) {
095: vtxIndexArr[0] = i;
096: getVertexData(i++, pnt);
097: if (intersectPntAndRay(pnt, pickSegment.start, dir,
098: sdist)
099: && (sdist[0] <= 1.0)) {
100: if (flags == 0) {
101: return true;
102: }
103: if (sdist[0] < minDist) {
104: minDist = sdist[0];
105: x = pnt.x;
106: y = pnt.y;
107: z = pnt.z;
108: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
109: storeInterestData(pickInfo, flags, geom,
110: geomIndex, vtxIndexArr, iPnt,
111: sdist[0]);
112: }
113: }
114: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
115: storeInterestData(pickInfo, flags, geom,
116: geomIndex, vtxIndexArr, iPnt, sdist[0]);
117: }
118: }
119: }
120: break;
121: case PickShape.PICKBOUNDINGBOX:
122: case PickShape.PICKBOUNDINGSPHERE:
123: case PickShape.PICKBOUNDINGPOLYTOPE:
124: Bounds bounds = ((PickBounds) pickShape).bounds;
125:
126: while (i < validVertexCount) {
127: vtxIndexArr[0] = i;
128: getVertexData(i++, pnt);
129: if (bounds.intersect(pnt)) {
130: if (flags == 0) {
131: return true;
132: }
133: sdist[0] = pickShape.distance(pnt);
134: if (sdist[0] < minDist) {
135: minDist = sdist[0];
136: x = pnt.x;
137: y = pnt.y;
138: z = pnt.z;
139: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
140: storeInterestData(pickInfo, flags, geom,
141: geomIndex, vtxIndexArr, iPnt,
142: sdist[0]);
143: }
144: }
145: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
146: storeInterestData(pickInfo, flags, geom,
147: geomIndex, vtxIndexArr, iPnt, sdist[0]);
148: }
149: }
150: }
151:
152: break;
153: case PickShape.PICKCYLINDER:
154: PickCylinder pickCylinder = (PickCylinder) pickShape;
155:
156: while (i < validVertexCount) {
157: vtxIndexArr[0] = i;
158: getVertexData(i++, pnt);
159: if (intersectCylinder(pnt, pickCylinder, sdist)) {
160: if (flags == 0) {
161: return true;
162: }
163: if (sdist[0] < minDist) {
164: minDist = sdist[0];
165: x = pnt.x;
166: y = pnt.y;
167: z = pnt.z;
168: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
169: storeInterestData(pickInfo, flags, geom,
170: geomIndex, vtxIndexArr, iPnt,
171: sdist[0]);
172: }
173: }
174: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
175: storeInterestData(pickInfo, flags, geom,
176: geomIndex, vtxIndexArr, iPnt, sdist[0]);
177: }
178: }
179: }
180: break;
181: case PickShape.PICKCONE:
182: PickCone pickCone = (PickCone) pickShape;
183:
184: while (i < validVertexCount) {
185: vtxIndexArr[0] = i;
186: getVertexData(i++, pnt);
187: if (intersectCone(pnt, pickCone, sdist)) {
188: if (flags == 0) {
189: return true;
190: }
191: if (sdist[0] < minDist) {
192: minDist = sdist[0];
193: x = pnt.x;
194: y = pnt.y;
195: z = pnt.z;
196: if ((flags & PickInfo.CLOSEST_GEOM_INFO) != 0) {
197: storeInterestData(pickInfo, flags, geom,
198: geomIndex, vtxIndexArr, iPnt,
199: sdist[0]);
200: }
201: }
202: if ((flags & PickInfo.ALL_GEOM_INFO) != 0) {
203: storeInterestData(pickInfo, flags, geom,
204: geomIndex, vtxIndexArr, iPnt, sdist[0]);
205: }
206: }
207: }
208: break;
209: case PickShape.PICKPOINT:
210: // Should not happen since API already check for this
211: throw new IllegalArgumentException(J3dI18N
212: .getString("PointArrayRetained0"));
213: default:
214: throw new RuntimeException(
215: "PickShape not supported for intersection");
216: }
217:
218: if (minDist < Double.MAX_VALUE) {
219: iPnt.x = x;
220: iPnt.y = y;
221: iPnt.z = z;
222: return true;
223: }
224: return false;
225: }
226:
227: boolean intersect(Point3d[] pnts) {
228: Point3d point = new Point3d();
229: int i = ((vertexFormat & GeometryArray.BY_REFERENCE) == 0 ? initialVertexIndex
230: : initialCoordIndex);
231:
232: switch (pnts.length) {
233: case 3: // Triangle
234: while (i < validVertexCount) {
235: getVertexData(i++, point);
236: if (intersectTriPnt(pnts[0], pnts[1], pnts[2], point)) {
237: return true;
238: }
239: }
240: break;
241: case 4: // Quad
242: while (i < validVertexCount) {
243: getVertexData(i++, point);
244: if (intersectTriPnt(pnts[0], pnts[1], pnts[2], point)
245: || intersectTriPnt(pnts[0], pnts[2], pnts[3],
246: point)) {
247: return true;
248: }
249: }
250: break;
251: case 2: // Line
252: double dist[] = new double[1];
253: Vector3d dir = new Vector3d();
254:
255: while (i < validVertexCount) {
256: getVertexData(i++, point);
257: dir.x = pnts[1].x - pnts[0].x;
258: dir.y = pnts[1].y - pnts[0].y;
259: dir.z = pnts[1].z - pnts[0].z;
260: if (intersectPntAndRay(point, pnts[0], dir, dist)
261: && (dist[0] <= 1.0)) {
262: return true;
263: }
264: }
265: break;
266: case 1: // Point
267: while (i < validVertexCount) {
268: getVertexData(i++, point);
269: if ((pnts[0].x == point.x) && (pnts[0].y == point.y)
270: && (pnts[0].z == point.z)) {
271: return true;
272: }
273: }
274: break;
275: }
276: return false;
277: }
278:
279: boolean intersect(Transform3D this ToOtherVworld,
280: GeometryRetained geom) {
281:
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(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: // the bounds argument is already transformed
298: boolean intersect(Bounds targetBound) {
299: int i = ((vertexFormat & GeometryArray.BY_REFERENCE) == 0 ? initialVertexIndex
300: : initialCoordIndex);
301: Point3d pnt = new Point3d();
302:
303: while (i < validVertexCount) {
304: getVertexData(i++, pnt);
305: if (targetBound.intersect(pnt)) {
306: return true;
307: }
308: }
309: return false;
310: }
311:
312: int getClassType() {
313: return POINT_TYPE;
314: }
315: }
|