📄 pickresult.java
字号:
/* * $RCSfile: PickResult.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.6 $ * $Date: 2007/02/09 17:20:26 $ * $State: Exp $ */package com.sun.j3d.utils.picking;import javax.vecmath.*;import javax.media.j3d.*;import java.util.ArrayList;import com.sun.j3d.utils.geometry.Primitive;import com.sun.j3d.internal.*;/** * Stores information about a pick hit. * Detailed information about the pick and each intersection of the PickShape * with the picked Node can be inquired. The PickResult is constructed with * basic information and more detailed information is generated as needed. The * additional information is only available if capability bits on the scene * graph Nodes are set properly; * <A HREF="PickTool.html#setCapabilities(javax.media.j3d.Node, int)"> * <code>PickTool.setCapabilties(Node, int)</code></A> * can * be used to ensure correct capabilites are set. Inquiring data which is not * available due to capabilties not being set will generate a * <code>CapabilityNotSet</code> exception. * <p> * A PickResult can be used to calculate intersections on Node which is not part * of a live scene graph using the constructor which takes a local to VWorld * transformation for the Node. * <p> * Pick hits on TriangleStrip primitives will store the triangle points in the * PickIntersection with * the verticies in counter-clockwise order. For triangles which start with * an odd numbered vertex this will be the the opposite of the * order of the points in the TriangleStrip. * This way the triangle in * the PickIntersection will display the same was as the triangle in the * strip. * <p> * If the Shape3D being picked has multiple geometry arrays, the arrays are * stored in the PickResult and referred to by a geometry index. * <p> * If the Shape3D refers to a CompressedGeometry, the geometry is decompressed * into an array of Shape3D nodes which can be inquired. The geometry * NodeComponents for the Shape3D nodes are stored and used as if the Shape3D * had multiple geometries. If there are multiple CompressedGeometries on the * Shape3D, the decompressed Shape3Ds and GeometryArrays will be stored * sequentially. * <p> * The intersection point for Morph nodes cannot be calculated using the * displayed geometry * due to limitations in the current Java3D core API (the current * geometry of the the Morph cannot be inquired). Instead * the geometry at index 0 in the Morph is used. This limitation may * be eliminated in a future release of Java3D. */ public class PickResult { /* OPEN ISSUES: -- getInterpolatedTextureCoordinates uses the depricated API faor getTextureCoordinate(), need to update. -- Bounds tests don't fill in any picking info. -- Can't do any intersections with the PickPoint shape. */ // Externally used constants /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>Shape3D</code> node from * the <code>SceneGraphPath</code>. */ public static final int SHAPE3D = 0x1; /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>Morph</code> node from * the <code>SceneGraphPath</code>. */ public static final int MORPH = 0x2; /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>Primitive</code> node from * the <code>SceneGraphPath</code>. */ public static final int PRIMITIVE = 0x4; /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>Link</code> node from * the <code>SceneGraphPath</code>. */ public static final int LINK = 0x8; /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>Group</code> node from * the <code>SceneGraphPath</code>. */ public static final int GROUP = 0x10; /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>TransformGroup</code> node from * the <code>SceneGraphPath</code>. */ public static final int TRANSFORM_GROUP = 0x20; /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>BranchGroup</code> node from * the <code>SceneGraphPath</code>. */ public static final int BRANCH_GROUP = 0x40; /** * Flag to pass to * <CODE>getNode(int)</CODE> * to return a * <code>Switch</code> node from * the <code>SceneGraphPath</code>. */ public static final int SWITCH = 0x80; /* =================== ATTRIBUTES ======================= */ static boolean debug = false; /** if true, find only the first intersection */ private boolean firstIntersectOnly = false; /** Stored SceneGraphPath */ private SceneGraphPath pickedSceneGraphPath = null; /** Picked node: shape3d, text3d, etc. */ private Node pickedNode = null; /** GeometryArray(s) of the picked node */ private GeometryArray[] geometryArrays = null; /** Shape3Ds from CompressedGeometry on the picked node */ private Shape3D[] compressGeomShape3Ds = null; /** Transform to World Coordinates */ private Transform3D localToVWorld = null; /** the pick shape to use for intersections */ private PickShape pickShape = null; /* data derived from the pick shape */ private int pickShapeType = -1; private Vector3d pickShapeDir = null; private Point3d pickShapeStart = null; private Point3d pickShapeEnd = null; private Bounds pickShapeBounds = null; static final Point3d zeroPnt = new Point3d(); /** ArrayList to store intersection results * Used in PickTool */ ArrayList intersections = null; // internal constants used for intersections static final double FUZZ = 1E-6; /* fuzziness factor used to determine if two lines are parallel */ static final int PICK_SHAPE_RAY = 1; static final int PICK_SHAPE_SEGMENT = 2; static final int PICK_SHAPE_POINT = 3; static final int PICK_SHAPE_BOUNDING_BOX = 4; static final int PICK_SHAPE_BOUNDING_SPHERE = 5; static final int PICK_SHAPE_BOUNDING_POLYTOPE = 6; static final int PICK_SHAPE_CYLINDER = 7; static final int PICK_SHAPE_CONE = 8; static final double EPS = 1.0e-13; /* =================== METHODS ======================= */ /** Default constructor. */ PickResult () { } /** Construct a PickResult using a SceneGraphPath @param sgp SceneGraphPath associated with this PickResult @param ps The pickShape to intersect against */ public PickResult (SceneGraphPath sgp, PickShape ps) { pickedSceneGraphPath = sgp; pickedNode = sgp.getObject(); localToVWorld = sgp.getTransform(); pickShape = ps; initPickShape(); } /** Construct a PickResult using the Node and localToVWorld transform @param pn The picked node. @param l2vw The local to VWorld transformation for the node @param ps The PickShape to intersect against @throws IllegalArgumentException If the node is not a Morph or Shape3D. */ public PickResult (Node pn, Transform3D l2vw, PickShape ps) { if ((pn instanceof Shape3D) || (pn instanceof Morph)) { pickedNode = pn; localToVWorld = l2vw; pickShape = ps; initPickShape(); } else { throw new IllegalArgumentException(); } } void initPickShape() { if(pickShape instanceof PickRay) { if (pickShapeStart == null) pickShapeStart = new Point3d(); if (pickShapeDir == null) pickShapeDir = new Vector3d(); ((PickRay) pickShape).get (pickShapeStart, pickShapeDir); pickShapeType = PICK_SHAPE_RAY; } else if (pickShape instanceof PickSegment) { if (pickShapeStart == null) pickShapeStart = new Point3d(); if (pickShapeEnd == null) pickShapeEnd = new Point3d(); if (pickShapeDir == null) pickShapeDir = new Vector3d(); ((PickSegment)pickShape).get(pickShapeStart, pickShapeEnd); pickShapeDir.set (pickShapeEnd.x - pickShapeStart.x, pickShapeEnd.y - pickShapeStart.y, pickShapeEnd.z - pickShapeStart.z); pickShapeType = PICK_SHAPE_SEGMENT; } else if (pickShape instanceof PickBounds) { pickShapeBounds = ((PickBounds) pickShape).get(); if ( pickShapeBounds instanceof BoundingBox ) pickShapeType = PICK_SHAPE_BOUNDING_BOX; else if( pickShapeBounds instanceof BoundingSphere ) pickShapeType = PICK_SHAPE_BOUNDING_SPHERE; else if( pickShapeBounds instanceof BoundingPolytope ) pickShapeType = PICK_SHAPE_BOUNDING_POLYTOPE; } else if(pickShape instanceof PickPoint) { throw new RuntimeException ("PickPoint doesn't make sense for geometry-based picking. Java 3D doesn't have spatial information of the surface. Should use PickBounds with BoundingSphere and set radius to a epsilon tolerance."); } else if (pickShape instanceof PickCylinder) { pickShapeType = PICK_SHAPE_CYLINDER; } else if (pickShape instanceof PickCone) { pickShapeType = PICK_SHAPE_CONE; } else { throw new RuntimeException("PickShape not supported for intersection"); } } /** Get the SceneGraphPath. This will be null if the non SceneGraphPath * constructor was used. */ public SceneGraphPath getSceneGraphPath() { /* Q: should this return a copy */ return pickedSceneGraphPath; } /** Get the localToVworld transform for the Node */ public Transform3D getLocalToVworld() { return localToVWorld; } /** Get the GeometryArray at index 0 for the picked node */ public GeometryArray getGeometryArray() { if (geometryArrays == null) { storeGeometry(); } return geometryArrays[0]; } /** Get the array of GeometryArrays for the picked node */ public GeometryArray[] getGeometryArrays() { if (geometryArrays == null) { storeGeometry(); } return geometryArrays; } /** Get the number of GeometryArrays for the picked node */ public int numGeometryArrays() { if (geometryArrays == null) { storeGeometry(); } return geometryArrays.length; } /** Get the number of Shape3Ds that came from decompressing a CompressedGeometry on the picked node. */ public int numCompressedGeometryShape3Ds() { if (geometryArrays == null) { storeGeometry(); } if (compressGeomShape3Ds == null) { return 0; } else { return compressGeomShape3Ds.length; } } /** Get the array of Shape3Ds that came from decompressing a CompressedGeometry on the picked node. */ public Shape3D[] getCompressedGeometryShape3Ds() { if (geometryArrays == null) { storeGeometry(); } if (compressGeomShape3Ds == null) { return null; } else { return compressGeomShape3Ds; } } /** Get the PickShape used for intersections */ public PickShape getPickShape() { return pickShape; } /** Set the PickResult to find only the first intersection of the PickShape * with the Node. The default is <code>false</code> (all intersections are * found) */ public void setFirstIntersectOnly(boolean flag) { firstIntersectOnly = flag; } /** Return the "first intersection only" value. */ public boolean getFirstPickEnable() { return firstIntersectOnly; } /** Returns the number of PickIntersections in the PickResult. @return the number of intersections */ public int numIntersections () { if (intersections == null) { generateIntersections(); } return intersections.size(); } /** Returns a specific PickIntersection object @param index the index number @return the PickIntersection referenced by the index number */ public PickIntersection getIntersection (int index) { if (intersections == null) { generateIntersections(); } return (PickIntersection) intersections.get (index); } /** Gets the PickIntersection in this PickResult that is closest to a point @param pt the point to use for distance calculations @return the closest PickIntersection object */ public PickIntersection getClosestIntersection (Point3d pt) { PickIntersection pi = null; PickIntersection curPi = null; Point3d curPt = null; double minDist = Double.MAX_VALUE; double curDist = 0.0; if (pt == null) return null; if (intersections == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -