📄 pickobject.java
字号:
/* * $RCSfile: PickObject.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.4 $ * $Date: 2007/02/09 17:20:13 $ * $State: Exp $ *//*New Base level methods:ISSUE : How about PickPoint and PickSegment ?DONE : PickShape generatePickRay(int x, int y) SceneGraphPath[] pickAll(int x, int y) SceneGraphPath[] pickAllSorted(int x, int y) SceneGraphPath pickAny(int x, int y) SceneGraphPath pickClosest(int x, int y) Node getPickedNode(SceneGraphPath, flag) Node getPickedNode(SceneGraphPath, flag, int count) where flag can be any combo of: Group, Morph, Primitive, Shape3D, TransformGroup, SwitchTODO : SceneGraphPath[] pickGeomAll(int x, int y) SceneGraphPath[] pickGeomAllSorted(int x, int y) SceneGraphPath pickGeomAny(int x, int y) SceneGraphPath pickGeomClosest(int x, int y) bool intersect(SceneGraphPath, PickShape) Eventually: getClosestVtx(ScenGraphPath, PickShape)Misc: Mouse should stay on top of object it is dragging */package com.sun.j3d.utils.behaviors.picking;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.media.j3d.*;import com.sun.j3d.utils.geometry.Primitive;import javax.vecmath.*;/* * Contains methods to aid in picking. A PickObject is created * for a given Canvas3D and a BranchGroup. SceneGraphObjects * under the specified BranchGroup can then be checked to determine * if they have been picked. *//** * @deprecated As of Java 3D version 1.2, replaced by * <code>com.sun.j3d.utils.picking.PickCanvas</code> * * @see com.sun.j3d.utils.picking.PickCanvas */public class PickObject extends Object { // Have to rethink what to support. Is this complete. /** * A flag to indicate to the pickNode method to return a * <code>Shape3D</code> node from * a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int SHAPE3D = 0x1; /** * A flag to indicate to the pickNode method to return a * <code>Morph</code> node from * a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int MORPH = 0x2; /** * A flag to indicate to the pickNode method to return a * <code>Primitive</code> node * from a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int PRIMITIVE = 0x4; /** * A flag to indicate to the pickNode method to return a * <code>Link</code> node from * a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int LINK = 0x8; /** * A flag to indicate to the pickNode method to return a * <code>Group</code> node from * a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int GROUP = 0x10; /** * A flag to indicate to the pickNode method to return a * <code>TransformGroup</code> * node from a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int TRANSFORM_GROUP = 0x20; /** * A flag to indicate to the pickNode method to return a * <code>BranchGroup</code> * node from a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int BRANCH_GROUP = 0x40; /** * A flag to indicate to the pickNode method to return a * <code>Switch</code> node from * a given <code>SceneGraphPath</code>. * * @see PickObject#pickNode */ public static final int SWITCH = 0x80; /** * Set this flag if you want to pick by geometry. */ public static final int USE_GEOMETRY = 0x100; /** * Set this flag if you want to pick by bounds. */ public static final int USE_BOUNDS = 0x200; BranchGroup pickRoot; Canvas3D canvas; Point3d origin = new Point3d(); Vector3d direction = new Vector3d(); PickRay pickRay = new PickRay(); SceneGraphPath sceneGraphPath = null; SceneGraphPath sceneGraphPathArr[] = null; int pickBy; // To pick by Bounds or Geometry. static final boolean debug = false; /** * Creates a PickObject. * @param c Current J3D canvas. * @param root The portion of the scenegraph for which picking is to occur * on. It has to be a <code>BranchGroup</code>. * * @see BranchGroup * @see Canvas3D */ public PickObject(Canvas3D c, BranchGroup root) { pickRoot = root; canvas = c; } /** * Creates a PickRay that starts at the viewer position and points into * the scene in the direction of (xpos, ypos) specified in window space. * * @param xpos The value along the x-axis. * @param ypos The value along the y-axis. * @return A PickShape object that is the constructed PickRay. */ public PickShape generatePickRay(int xpos, int ypos) { Transform3D motion=new Transform3D(); Point3d eyePosn = new Point3d(); Point3d mousePosn = new Point3d(); Vector3d mouseVec=new Vector3d(); canvas.getCenterEyeInImagePlate(eyePosn); canvas.getPixelLocationInImagePlate(xpos,ypos,mousePosn); if (canvas.getView().getProjectionPolicy() == View.PARALLEL_PROJECTION) { // Correct for the parallel projection: keep the eye's z // coordinate, but make x,y be the same as the mouse, this // simulates the eye being at "infinity" eyePosn.x = mousePosn.x; eyePosn.y = mousePosn.y; } canvas.getImagePlateToVworld(motion); if (debug) { System.out.println("mouse position " + xpos + " " + ypos); System.out.println("before, mouse " + mousePosn + " eye " + eyePosn); } motion.transform(eyePosn); motion.transform(mousePosn); mouseVec.sub(mousePosn, eyePosn); mouseVec.normalize(); if (debug) { System.out.println(motion + "\n"); System.out.println("after, mouse " + mousePosn + " eye " + eyePosn + " mouseVec " + mouseVec); } pickRay.set(eyePosn, mouseVec); return (PickShape) pickRay; } /** * Returns an array referencing all the items that are pickable below the * <code>BranchGroup</code> (specified in the PickObject constructor) that * intersect with a ray that starts at the * viewer position and points into the scene in the direction of (xpos, ypos) * specified in window space. The resultant array is unordered. * * @param xpos The value along the x-axis. * @param ypos The value along the y-axis. * @return The array of SceneGraphPath objects that contain Objects that * were picked * If no pickable object is found <code>null</code> is returned.. * * @see SceneGraphPath */ public SceneGraphPath[] pickAll(int xpos, int ypos) { pickRay = (PickRay) generatePickRay(xpos, ypos); sceneGraphPathArr = pickRoot.pickAll(pickRay); return sceneGraphPathArr; } /** * Returns a sorted array of references to all the Pickable items below the * <code>BranchGroup</code> (specified in the PickObject constructor) that * intersect with the ray that starts at the viewer * position and points into the scene in the direction of (xpos, ypos) * in the window space. * Element [0] references the item closest to viewer. * * @param xpos The value along the x-axis. * @param ypos The value along the y-axis. * @return A sorted arrayof SceneGraphPath objects that contain Objects that * were picked. The array is sorted from closest to farthest from the * viewer * If no pickable object is found <code>null</code> is returned.. * * @see SceneGraphPath */ public SceneGraphPath[] pickAllSorted(int xpos, int ypos) { pickRay = (PickRay) generatePickRay(xpos, ypos); sceneGraphPathArr = pickRoot.pickAllSorted(pickRay); return sceneGraphPathArr; } /** * Returns a reference to any item that is Pickable below the specified * <code>BranchGroup</code> (specified in the PickObject constructor) which * intersects with the ray that starts at the viewer * position and points into the scene in the direction of (xpos, ypos) in * window space. * * @param xpos The value along the x-axis. * @param ypos The value along the y-axis. * @return A SceneGraphPath of an object that was picked. This is not * guarenteed to return the same result for multiple picks * If no pickable object is found <code>null</code> is returned.. * * @see SceneGraphPath */ public SceneGraphPath pickAny(int xpos, int ypos) { pickRay = (PickRay) generatePickRay(xpos, ypos); sceneGraphPath = pickRoot.pickAny(pickRay); return sceneGraphPath; } /** * Returns a reference to the item that is closest to the viewer and is * Pickable below the <code>BranchGroup</code> (specified in the PickObject * constructor) which intersects with the ray that starts at * the viewer position and points into the scene in the direction of * (xpos, ypos) in the window space. * * @param xpos The value along the x-axis. * @param ypos The value along the y-axis. * @return A SceneGraphPath which contains the closest pickable object. * If no pickable object is found, <code>null</code> is returned. * * @see SceneGraphPath */ public SceneGraphPath pickClosest(int xpos, int ypos) { pickRay = (PickRay) generatePickRay(xpos, ypos); sceneGraphPath = pickRoot.pickClosest(pickRay); return sceneGraphPath; } /** * Returns an array referencing all the items that are pickable below the * <code>BranchGroup</code> (specified in the PickObject constructor) that * intersect with a ray that starts at the * viewer position and points into the scene in the direction of (xpos, ypos) * specified in window space. The resultant array is unordered. * * @param xpos The value along the x-axis. * @param ypos The value along the y-axis. * @param flag Specifys picking by Geometry or Bounds. * @return The array of SceneGraphPath objects that contain Objects that * were picked * If no pickable object is found <code>null</code> is returned.. * * @see SceneGraphPath */ public SceneGraphPath[] pickAll(int xpos, int ypos, int flag) { if(flag == USE_BOUNDS) { return pickAll(xpos, ypos); } else if(flag == USE_GEOMETRY) { return pickGeomAll(xpos, ypos); } else return null; } /** * Returns a sorted array of references to all the Pickable items below the * <code>BranchGroup</code> (specified in the PickObject constructor) that * intersect with the ray that starts at the viewer * position and points into the scene in the direction of (xpos, ypos) * in the window space. * Element [0] references the item closest to viewer. * * @param xpos The value along the x-axis. * @param ypos The value along the y-axis. * @param flag Specifys picking by Geometry or Bounds. * @return A sorted arrayof SceneGraphPath objects that contain Objects that * were picked. The array is sorted from closest to farthest from the * viewer * If no pickable object is found <code>null</code> is returned.. * * @see SceneGraphPath */ public SceneGraphPath[] pickAllSorted(int xpos, int ypos, int flag) { if(flag == USE_BOUNDS) { return pickAllSorted(xpos, ypos); } else if(flag == USE_GEOMETRY) { return pickGeomAllSorted(xpos, ypos); } else return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -