aboutsummaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/PickCylinderSegment.java
blob: 9d33626a630b18832415ca1a54de55113d05fe73 (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
/*
 * Copyright 1999-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 javax.vecmath.Point3d;
import javax.vecmath.Point4d;

/**
 * PickCylinderSegment is a finite cylindrical segment pick shape.  It can
 * be used as an argument to the picking methods in BranchGroup and Locale.
 *
 * @see BranchGroup#pickAll
 * @see Locale#pickAll
 *
 * @since Java 3D 1.2
 */

public final class PickCylinderSegment extends PickCylinder {

    Point3d end;

    /**
     * Constructs an empty PickCylinderSegment.
     * The origin and end points of the cylindrical segment are
     * initialized to (0,0,0).  The radius is initialized
     * to 0.
     */
    public PickCylinderSegment() {
	this.end = new Point3d();
    }

    /**
     * Constructs a finite cylindrical segment pick shape from the specified
     * parameters.
     * @param origin the origin point of the cylindrical segment.
     * @param end the end point of the cylindrical segment.
     * @param radius the radius of the cylindrical segment.
     */
    public PickCylinderSegment(Point3d origin, Point3d end, double radius) {
	this.origin = new Point3d(origin);
	this.end = new Point3d(end);
	this.radius = radius;
	calcDirection(); // calculate direction, based on start and end
    }

    /**
     * Sets the parameters of this PickCylinderSegment to the specified values.
     * @param origin the origin point of the cylindrical segment.
     * @param end the end point of the cylindrical segment.
     * @param radius the radius of the cylindrical segment.
     */
    public void set(Point3d origin, Point3d end, double radius) {
	this.origin.set(origin);
	this.end.set(end);
	this.radius = radius;
	calcDirection(); // calculate direction, based on start and end
    }

    /** Calculates the direction for this PickCylinderSegment, based on start
      and end points.
      */
    private void calcDirection() {
	this.direction.x = end.x - origin.x;
	this.direction.y = end.y - origin.y;
	this.direction.z = end.z - origin.z;
    }

    /**
     * Gets the end point of this PickCylinderSegment.
     * @param end the Point3d object into which the end point
     * will be copied.
     */
    public void getEnd(Point3d end) {
	end.set(this.end);
    }

    /**
     * Returns true if shape intersect with bounds.
     * The point of intersection is stored in pickPos.
     * @param bounds the bounds object to check
     * @param pickPos the location of the point of intersection (not used for
     * method. Provided for compatibility).
     */
    @Override
    final boolean intersect(Bounds bounds, Point4d pickPos) {
	Point4d iPnt = new Point4d();

	//
	// ================ BOUNDING SPHERE ================
	//
	if (bounds instanceof BoundingSphere) {
	    Point3d sphCenter = ((BoundingSphere)bounds).getCenter();
	    double sphRadius = ((BoundingSphere)bounds).getRadius();
	    double sqDist = Utils.ptToSegSquare(sphCenter, origin, end, null);
	    if (sqDist <= (sphRadius+radius)*(sphRadius+radius)) {
		return true;
	    }
	    return false; // we are too far to intersect
	}
	//
	// ================ BOUNDING BOX ================
	//
	else if (bounds instanceof BoundingBox) {
	    // Calculate radius of BoundingBox
	    Point3d lower = new Point3d();
	    ((BoundingBox)bounds).getLower (lower);

	    Point3d center = ((BoundingBox)bounds).getCenter ();

	    double temp = (center.x - lower.x + radius);
	    double boxRadiusSquared = temp*temp;
	    temp = (center.y - lower.y + radius);
	    boxRadiusSquared += temp*temp;
	    temp = (center.z - lower.z + radius);
	    boxRadiusSquared += temp*temp;

	    // First, see if cylinder is too far away from BoundingBox
	    double sqDist = Utils.ptToSegSquare(center, origin, end, null);
	    if (sqDist > boxRadiusSquared) {
		return false; // we are too far to intersect
	    }
	    else if (sqDist < (radius*radius)) {
		return true; // center is in cylinder
	    }

	    // Then, see if ray intersects
	    if (((BoundingBox)bounds).intersect (origin, end, iPnt)) {
		return true;
	    }

	    // Ray does not intersect, test for distance with each edge
	    Point3d upper = new Point3d();
	    ((BoundingBox)bounds).getUpper (upper);

	    Point3d[][] edges = {
		// Top horizontal 4
		{upper, new Point3d (lower.x, upper.y, upper.z)},
		{new Point3d(lower.x, upper.y, upper.z), new Point3d(lower.x, lower.y, upper.z)},
		{new Point3d(lower.x, lower.y, upper.z), new Point3d(upper.x, lower.y, upper.z)},
		{new Point3d(upper.x, lower.y, upper.z), upper},
		// Bottom horizontal 4
		{lower, new Point3d(lower.x, upper.y, lower.z)},
		{new Point3d(lower.x, upper.y, lower.z), new Point3d(upper.x, upper.y, lower.z)},
		{new Point3d(upper.x, upper.y, lower.z), new Point3d(upper.x, lower.y, lower.z)},
		{new Point3d(upper.x, lower.y, lower.z), lower},
		// Vertical 4
		{lower, new Point3d(lower.x, lower.y, upper.z)},
		{new Point3d(lower.x, upper.y, lower.z), new Point3d(lower.x, upper.y, upper.z)},
		{new Point3d(upper.x, upper.y, lower.z), new Point3d(upper.x, upper.y, upper.z)},
		{new Point3d(upper.x, lower.y, lower.z), new Point3d(upper.x, lower.y, upper.z)}
	    };
	    for (int i=0;i<edges.length;i++) {
		//	System.err.println ("Testing edge: "+edges[i][0]+" - "+edges[i][1]);
		double distToEdge =
		    Utils.segmentToSegment (origin, end,
					       edges[i][0], edges[i][1], null, null, null);
		if (distToEdge <= radius*radius) {
		    //	  System.err.println ("Intersects!");
		    return true;
		}
	    }
	    return false; // Not close enough
	}
	//
	// ================ BOUNDING POLYTOPE ================
	//
	else if (bounds instanceof BoundingPolytope) {
	    int i, j;

	    // First, check to see if we are too far to intersect the polytope's
	    // bounding sphere
	    Point3d sphCenter = new Point3d();
	    BoundingSphere bsphere = new BoundingSphere (bounds);

	    bsphere.getCenter (sphCenter);
	    double sphRadius = bsphere.getRadius();

	    double sqDist = Utils.ptToSegSquare(sphCenter, origin, end, null);
	    if (sqDist > (sphRadius+radius)*(sphRadius+radius)) {
		return false; // we are too far to intersect
	    }

	    // Now check to see if ray intersects with polytope
	    if (bounds.intersect (origin, direction, iPnt)) {
		return true;
	    }

	    // Now check distance to edges. Since we don't know a priori how
	    // the polytope is structured, we will cycle through. We discard edges
	    // when their center is not on the polytope surface.
	    BoundingPolytope ptope = (BoundingPolytope)bounds;
	    Point3d midpt = new Point3d();
	    double distToEdge;
	    for (i=0;i<ptope.nVerts;i++) {
		for (j=i;i<ptope.nVerts;i++) {
		    // XXXX: make BoundingPolytope.pointInPolytope available to package
		    // scope
		    midpt.x = (ptope.verts[i].x + ptope.verts[j].x) * 0.5;
		    midpt.y = (ptope.verts[i].y + ptope.verts[j].y) * 0.5;
		    midpt.z = (ptope.verts[i].z + ptope.verts[j].z) * 0.5;

		    if (! PickCylinder.pointInPolytope (ptope,
							midpt.x, midpt.y, midpt.z)) {
			continue;
		    }
		    distToEdge =
			Utils.segmentToSegment (origin, end,
						   ptope.verts[i], ptope.verts[j], null, null, null);
		    if (distToEdge <= radius*radius) {
			return true;
		    }
		}
	    }
	    return false;
	}
	/*
	else {
	    throw new RuntimeException("intersect method not implemented");
	}
	*/
	return false;
    }

    // Only use within J3D.
    // Return a new PickCylinderSegment that is the transformed (t3d) of this
    // pickCylinderSegment.
    @Override
    PickShape transform(Transform3D t3d) {

	PickCylinderSegment newPCS = new PickCylinderSegment();

	newPCS.origin.x = origin.x;
	newPCS.origin.y = origin.y;
	newPCS.origin.z = origin.z;
	newPCS.radius = radius * t3d.getScale();
	newPCS.end.x = end.x;
	newPCS.end.y = end.y;
	newPCS.end.z = end.z;

	t3d.transform(newPCS.origin);
	t3d.transform(newPCS.end);

	newPCS.direction.x = newPCS.end.x - newPCS.origin.x;
	newPCS.direction.y = newPCS.end.y - newPCS.origin.y;
	newPCS.direction.z = newPCS.end.z - newPCS.origin.z;
	newPCS.direction.normalize();

	return newPCS;
    }

}