001: /*
002: * $RCSfile: IntersectInfoBehavior.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:48 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.picking;
046:
047: import java.awt.*;
048: import java.awt.event.*;
049: import java.util.*;
050: import javax.media.j3d.*;
051: import javax.vecmath.*;
052: import com.sun.j3d.utils.pickfast.*;
053: import com.sun.j3d.utils.geometry.*;
054:
055: /**
056: * Class: IntersectInfoBehavior
057: *
058: * Description: Used to respond to mouse pick and drag events
059: * in the 3D window. Displays information about the pick.
060: *
061: * Version: 1.0
062: *
063: */
064: public class IntersectInfoBehavior extends Behavior {
065:
066: float size;
067: PickCanvas pickCanvas;
068: PickInfo[] pickInfoArr;
069: Appearance oldlook, redlookwf, redlook, greenlook, bluelook;
070: Node oldNode = null;
071: GeometryArray oldGeom = null;
072: Color3f redColor = new Color3f(1.0f, 0.0f, 0.0f);
073: TransformGroup[] sphTrans = new TransformGroup[6];
074: Sphere[] sph = new Sphere[6];
075: Transform3D spht3 = new Transform3D();
076:
077: public IntersectInfoBehavior(Canvas3D canvas3D,
078: BranchGroup branchGroup, float size) {
079: pickCanvas = new PickCanvas(canvas3D, branchGroup);
080: pickCanvas.setTolerance(5.0f);
081: pickCanvas.setMode(PickInfo.PICK_GEOMETRY);
082: pickCanvas.setFlags(PickInfo.LOCAL_TO_VWORLD
083: | PickInfo.CLOSEST_GEOM_INFO);
084: this .size = size;
085: // Create an Appearance.
086: redlook = new Appearance();
087: Color3f objColor = new Color3f(0.5f, 0.0f, 0.0f);
088: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
089: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
090: redlook.setMaterial(new Material(objColor, black, objColor,
091: white, 50.0f));
092: redlook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
093:
094: redlookwf = new Appearance();
095: redlookwf.setMaterial(new Material(objColor, black, objColor,
096: white, 50.0f));
097: PolygonAttributes pa = new PolygonAttributes();
098: pa.setPolygonMode(pa.POLYGON_LINE);
099: pa.setCullFace(pa.CULL_NONE);
100: redlookwf.setPolygonAttributes(pa);
101:
102: oldlook = new Appearance();
103: objColor = new Color3f(1.0f, 1.0f, 1.0f);
104: oldlook.setMaterial(new Material(objColor, black, objColor,
105: white, 50.0f));
106:
107: greenlook = new Appearance();
108: objColor = new Color3f(0.0f, 0.8f, 0.0f);
109: greenlook.setMaterial(new Material(objColor, black, objColor,
110: white, 50.0f));
111: bluelook = new Appearance();
112: objColor = new Color3f(0.0f, 0.0f, 0.8f);
113: bluelook.setMaterial(new Material(objColor, black, objColor,
114: white, 50.0f));
115: for (int i = 0; i < 6; i++) {
116: switch (i) {
117: case 0:
118: sph[i] = new Sphere(size * 1.15f, redlook);
119: break;
120: case 1:
121: sph[i] = new Sphere(size * 1.1f, greenlook);
122: break;
123: default:
124: sph[i] = new Sphere(size, bluelook);
125: break;
126: }
127: sph[i].setPickable(false);
128: sphTrans[i] = new TransformGroup();
129: sphTrans[i]
130: .setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
131: sphTrans[i]
132: .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
133:
134: // Add sphere, transform
135: branchGroup.addChild(sphTrans[i]);
136: sphTrans[i].addChild(sph[i]);
137: }
138: }
139:
140: public void initialize() {
141: wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
142: }
143:
144: public void processStimulus(Enumeration criteria) {
145: WakeupCriterion wakeup;
146: AWTEvent[] event;
147: int eventId;
148:
149: while (criteria.hasMoreElements()) {
150: wakeup = (WakeupCriterion) criteria.nextElement();
151: if (wakeup instanceof WakeupOnAWTEvent) {
152: event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
153: for (int i = 0; i < event.length; i++) {
154: eventId = event[i].getID();
155: if (eventId == MouseEvent.MOUSE_PRESSED) {
156: int x = ((MouseEvent) event[i]).getX();
157: int y = ((MouseEvent) event[i]).getY();
158: pickCanvas.setShapeLocation(x, y);
159:
160: pickInfoArr = pickCanvas.pickAllSorted();
161: // Use this to do picking benchmarks
162: /*
163: long start = System.currentTimeMillis();
164: for (int l=0;l<2;l++) {
165: if (l == 0) System.out.print ("BOUNDS: ");
166: if (l == 1) System.out.print ("GEOMETRY: ");
167:
168: for (int k=0;k<1000;k++) {
169: if (l == 0) {
170: pickCanvas.setMode(PickTool.BOUNDS);
171: pickResult = pickCanvas.pickAllSorted();
172: }
173: if (l == 1) {
174: pickCanvas.setMode(PickTool.GEOMETRY);
175: pickResult = pickCanvas.pickAllSorted();
176: }
177: }
178: long delta = System.currentTimeMillis() - start;
179: System.out.println ("\t"+delta+" ms / 1000 picks");
180: }
181: */
182: if (pickInfoArr != null) {
183:
184: // Get closest intersection results
185: Transform3D l2vw = pickInfoArr[0]
186: .getLocalToVWorld();
187: PickInfo.IntersectionInfo[] iInfoArr = pickInfoArr[0]
188: .getIntersectionInfos();
189: PickIntersection pi = new PickIntersection(
190: l2vw, iInfoArr[0]);
191:
192: // Safe to assume the return geometry is of GeometryArray type.
193: GeometryArray curGeomArray = (GeometryArray) iInfoArr[0]
194: .getGeometry();
195:
196: // Position sphere at intersection point
197: Vector3d v = new Vector3d();
198: Point3d intPt = pi.getPointCoordinatesVW();
199: v.set(intPt);
200: spht3.setTranslation(v);
201: sphTrans[0].setTransform(spht3);
202:
203: // Position sphere at closest vertex
204: Point3d closestVert = pi
205: .getClosestVertexCoordinatesVW();
206: v.set(closestVert);
207: spht3.setTranslation(v);
208: sphTrans[1].setTransform(spht3);
209:
210: Point3d[] ptw = pi
211: .getPrimitiveCoordinatesVW();
212: Point3d[] pt = pi.getPrimitiveCoordinates();
213: int[] coordidx = pi
214: .getPrimitiveCoordinateIndices();
215: Point3d ptcoord = new Point3d();
216: for (int k = 0; k < pt.length; k++) {
217: v.set(ptw[k]);
218: spht3.setTranslation(v);
219: sphTrans[k + 2].setTransform(spht3);
220: }
221:
222: // Get interpolated color (if available)
223: Color4f iColor4 = null;
224: Color3f iColor = null;
225: Vector3f iNormal = null;
226:
227: if (curGeomArray != null) {
228: int vf = curGeomArray.getVertexFormat();
229:
230: if (((vf & (GeometryArray.COLOR_3 | GeometryArray.COLOR_4)) != 0)
231: && (null != (iColor4 = pi
232: .getPointColor()))) {
233: iColor = new Color3f(iColor4.x,
234: iColor4.y, iColor4.z);
235:
236: // Change the point's color
237: redlook
238: .setMaterial(new Material(
239: iColor,
240: new Color3f(0.0f,
241: 0.0f, 0.0f),
242: iColor,
243: new Color3f(1.0f,
244: 1.0f, 1.0f),
245: 50.0f));
246: }
247: if (((vf & GeometryArray.NORMALS) != 0)
248: && (null != (iNormal = pi
249: .getPointNormal()))) {
250: System.out
251: .println("Interpolated normal: "
252: + iNormal);
253: }
254: }
255:
256: System.out.println("=============");
257: System.out
258: .println("Coordinates of intersection pt:"
259: + intPt);
260: System.out
261: .println("Coordinates of vertices: ");
262: for (int k = 0; k < pt.length; k++) {
263: System.out.println(k + ":" + ptw[k].x
264: + " " + ptw[k].y + " "
265: + ptw[k].z);
266: }
267: System.out.println("Closest vertex: "
268: + closestVert);
269: if (iColor != null) {
270: System.out
271: .println("Interpolated color: "
272: + iColor);
273: }
274: if (iNormal != null) {
275: System.out
276: .println("Interpolated normal: "
277: + iNormal);
278: }
279: }
280: }
281: }
282: }
283: }
284: wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
285: }
286: }
|