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