001: /*
002: * $RCSfile: PickRay.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.5 $
028: * $Date: 2008/02/28 20:17:28 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035:
036: /**
037: * PickRay is an infinite ray pick shape. It can
038: * be used as an argument to the picking methods in BranchGroup and Locale.
039: *
040: * @see BranchGroup#pickAll
041: * @see Locale#pickAll
042: */
043: public final class PickRay extends PickShape {
044:
045: Point3d origin;
046: Vector3d direction;
047:
048: /**
049: * Constructs an empty PickRay. The origin and direction of the
050: * ray are initialized to (0,0,0).
051: */
052: public PickRay() {
053: origin = new Point3d();
054: direction = new Vector3d();
055: }
056:
057: /**
058: * Constructs an infinite ray pick shape from the specified
059: * parameters.
060: * @param origin the origin of the ray.
061: * @param direction the direction of the ray.
062: */
063: public PickRay(Point3d origin, Vector3d direction) {
064: this .origin = new Point3d(origin);
065: this .direction = new Vector3d(direction);
066: }
067:
068: /**
069: * Sets the parameters of this PickRay to the specified values.
070: * @param origin the origin of the ray.
071: * @param direction the direction of the ray.
072: */
073: public void set(Point3d origin, Vector3d direction) {
074: this .origin.x = origin.x;
075: this .origin.y = origin.y;
076: this .origin.z = origin.z;
077: this .direction.x = direction.x;
078: this .direction.y = direction.y;
079: this .direction.z = direction.z;
080: }
081:
082: /**
083: * Retrieves the parameters from this PickRay.
084: * @param origin the Point3d object into which the origin will be copied.
085: * @param direction the Vector3d object into which the direction
086: * will be copied.
087: */
088: public void get(Point3d origin, Vector3d direction) {
089: origin.x = this .origin.x;
090: origin.y = this .origin.y;
091: origin.z = this .origin.z;
092: direction.x = this .direction.x;
093: direction.y = this .direction.y;
094: direction.z = this .direction.z;
095: }
096:
097: /**
098: * Return true if shape intersect with bounds.
099: * The point of intersection is stored in pickPos.
100: */
101: final boolean intersect(Bounds bounds, Point4d pickPos) {
102: return bounds.intersect(origin, direction, pickPos);
103: }
104:
105: // Only use within J3D.
106: // Return a new PickRay that is the transformed (t3d) of this pickRay.
107: PickShape transform(Transform3D t3d) {
108:
109: Point3d end = new Point3d();
110:
111: PickRay newPR = new PickRay(origin, direction);
112:
113: end.x = origin.x + direction.x;
114: end.y = origin.y + direction.y;
115: end.z = origin.z + direction.z;
116:
117: t3d.transform(newPR.origin);
118: t3d.transform(end);
119:
120: newPR.direction.x = end.x - newPR.origin.x;
121: newPR.direction.y = end.y - newPR.origin.y;
122: newPR.direction.z = end.z - newPR.origin.z;
123: newPR.direction.normalize();
124:
125: return newPR;
126: }
127:
128: Point3d getStartPoint() {
129: return origin;
130: }
131:
132: int getPickType() {
133: return PICKRAY;
134: }
135: }
|