📄 pickintersection.java
字号:
/* * $RCSfile: PickIntersection.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.5 $ * $Date: 2007/03/02 21:27:15 $ * $State: Exp $ */package com.sun.j3d.utils.picking;import javax.vecmath.*;import javax.media.j3d.*;import com.sun.j3d.utils.geometry.Primitive;/** * Holds information about an intersection of a PickShape with a Node * as part of a PickResult. Information about * the intersected geometry, intersected primitive, intersection point, and * closest vertex can be inquired. * <p> * The intersected geometry is indicated by an index into the list of * geometry arrays on the PickResult. It can also be inquired from this * object. * <p> * The intersected primitive indicates which primitive out of the GeometryArray * was intersected (where the primitive is a point, line, triangle or quad, * not a * <code>com.sun.j3d.utils.geometry.Primitive)</code>. * For example, the intersection would indicate which triangle out of a * triangle strip was intersected. * The methods which return primitive data will have one value if the primitive * is * a point, two values if the primitive is a line, three values if the primitive * is a triangle and four values if the primitive is quad. * <p> * The primitive's VWorld coordinates are saved when then intersection is * calculated. The local coordinates, normal, color and texture coordinates * for the primitive can also be inquired if they are present and readable. * <p> * The intersection point is the location on the primitive which intersects the * pick shape closest to the center of the pick shape. The intersection point's * location in VWorld coordinates is saved when the intersection is calculated. * The local coordinates, normal, color and texture coordiantes of at the * intersection can be interpolated if they are present and readable. * <p> * The closest vertex is the vertex of the primitive closest to the intersection * point. The vertex index, VWorld coordinates and local coordinates of the * closest vertex can be inquired. The normal, color and texture coordinate * of the closest vertex can be inquired from the geometry array: * <p><blockquote><pre> * Vector3f getNormal(PickIntersection pi, int vertexIndex) { * int index; * Vector3d normal = new Vector3f(); * GeometryArray ga = pickIntersection.getGeometryArray(); * if (pickIntersection.geometryIsIndexed()) { * index = ga.getNormalIndex(vertexIndex); * } else { * index = vertexIndex; * } * ga.getNormal(index, normal); * return normal; * } * </pre></blockquote> * <p> * The color, normal * and texture coordinate information for the intersected primitive and the * intersection point * can be inquired * the geometry includes them and the corresponding READ capibility bits are * set. * <A HREF="PickTool.html#setCapabilities(javax.media.j3d.Node, int)"> * <code>PickTool.setCapabilties(Node, int)</code></A> * can be used to set the capability bits * to allow this data to be inquired. */public class PickIntersection { /* OPEN ISSUES: -- Tex coordinates always use texCoordSet == 0. */ /* =================== ATTRIBUTES ======================= */ // init by constructor: /** PickResult for intersection is part of */ PickResult pickResult = null; // init by intersection: /** Distance between start point and intersection point (see comment above)*/ double distance = -1; /** index of GeometryArray in PickResult */ int geomIndex = 0; /** Indices of the intersected primitive */ int[] primitiveVertexIndices = null; /** VWorld coordinates of intersected primitive */ Point3d[] primitiveCoordinatesVW = null; /** VWorld Coordinates of the intersection point */ Point3d pointCoordinatesVW = null; // Derived data // Geometry GeometryArray geom = null; IndexedGeometryArray iGeom = null; boolean hasNormals = false; boolean hasColors = false; boolean hasTexCoords = false; // Primitive /* indices for the different data types */ int[] primitiveCoordinateIndices; int[] primitiveNormalIndices; int[] primitiveColorIndices; int[] primitiveTexCoordIndices; /* Local coordinates of the intersected primitive */ Point3d[] primitiveCoordinates = null; /* Normals of the intersected primitive */ Vector3f[] primitiveNormals = null; /* Colors of the intersected primitive */ Color4f[] primitiveColors = null; /* TextureCoordinates of the intersected primitive */ TexCoord3f[] primitiveTexCoords = null; // Intersection point /** Local Coordinates of the intersection point */ Point3d pointCoordinates = null; /** Normal at the intersection point */ Vector3f pointNormal = null; /** Color at the intersection point */ Color4f pointColor = null; /** TexCoord at the intersection point */ TexCoord3f pointTexCoord = null; // Closest Vertex /** Index of the closest vertex */ int closestVertexIndex = -1; /** Coordinates of the closest vertex */ Point3d closestVertexCoordinates = null; /** Coordinates of the closest vertex (World coordinates) */ Point3d closestVertexCoordinatesVW = null; /** Weight factors for interpolation, values correspond to vertex indices, * sum == 1 */ double[] interpWeights; static final boolean debug = false; // Axis constants static final int X_AXIS = 1; static final int Y_AXIS = 2; static final int Z_AXIS = 3; // Tolerance for numerical stability static final double TOL = 1.0e-5; /* =================== METHODS ======================= */ /** Constructor @param pickResult The pickResult this intersection is part of. */ PickIntersection (PickResult pr, GeometryArray geomArr) { // pr can't be null. pickResult = pr; geom = geomArr; if (geom == null) { GeometryArray[] ga = pickResult.getGeometryArrays(); geom = ga[geomIndex]; } if (geom instanceof IndexedGeometryArray) { iGeom = (IndexedGeometryArray)geom; } int vertexFormat = geom.getVertexFormat(); hasColors = (0 != (vertexFormat & (GeometryArray.COLOR_3 | GeometryArray.COLOR_4))); hasNormals = (0 != (vertexFormat & GeometryArray.NORMALS)); hasTexCoords = (0 != (vertexFormat & (GeometryArray.TEXTURE_COORDINATE_2 | GeometryArray.TEXTURE_COORDINATE_3))); } /** String representation of this object */ public String toString () { String rt = new String ("PickIntersection: "); rt += " pickResult = "+pickResult + "\n"; rt += " geomIndex = "+geomIndex + "\n"; if (distance != -1) rt += " dist:"+distance + "\n"; if (pointCoordinates != null) rt += " pt:" + pointCoordinates + "\n"; if (pointCoordinatesVW != null) rt += " ptVW:" + pointCoordinatesVW + "\n"; if (primitiveCoordinateIndices != null) { rt += " prim coordinate ind:" + "\n"; for (int i=0;i<primitiveCoordinateIndices.length;i++) { rt += " "+primitiveCoordinateIndices[i] + "\n"; } } if (primitiveColorIndices != null) { rt += " prim color ind:" + "\n"; for (int i=0;i<primitiveColorIndices.length;i++) { rt += " "+primitiveColorIndices[i] + "\n"; } } if (primitiveNormalIndices != null) { rt += " prim normal ind:" + "\n"; for (int i=0;i<primitiveNormalIndices.length;i++) { rt += " "+primitiveNormalIndices[i] + "\n"; } } if (primitiveTexCoordIndices != null) { rt += " prim texture ind:" + "\n"; for (int i=0;i<primitiveTexCoordIndices.length;i++) { rt += " "+primitiveTexCoordIndices[i] + "\n"; } } if (closestVertexCoordinates != null) { rt += " clos. vert:" + closestVertexCoordinates + "\n"; } if (closestVertexCoordinatesVW != null) { rt += " clos. vert:" + closestVertexCoordinatesVW + "\n"; } if (closestVertexIndex != -1) { rt += " clos. vert. ind.:" + closestVertexIndex + "\n"; } return rt; } /* Call only by PickResult */ String toString2 () { String rt = new String ("PickIntersection: "); // rt += " pickResult = "+pickResult; rt += " geomIndex = "+geomIndex + "\n"; if (distance != -1) rt += " dist:"+distance + "\n"; if (pointCoordinates != null) rt += " pt:" + pointCoordinates + "\n"; if (pointCoordinatesVW != null) rt += " ptVW:" + pointCoordinatesVW + "\n"; if (primitiveCoordinateIndices != null) { rt += " prim coordinate ind:" + "\n"; for (int i=0;i<primitiveCoordinateIndices.length;i++) { rt += " "+primitiveCoordinateIndices[i] + "\n"; } } if (primitiveColorIndices != null) { rt += " prim color ind:" + "\n"; for (int i=0;i<primitiveColorIndices.length;i++) { rt += " "+primitiveColorIndices[i] + "\n"; } } if (primitiveNormalIndices != null) { rt += " prim normal ind:" + "\n"; for (int i=0;i<primitiveNormalIndices.length;i++) { rt += " "+primitiveNormalIndices[i] + "\n"; } } if (primitiveTexCoordIndices != null) { rt += " prim texture ind:" + "\n"; for (int i=0;i<primitiveTexCoordIndices.length;i++) { rt += " "+primitiveTexCoordIndices[i] + "\n"; } } if (closestVertexCoordinates != null) { rt += " clos. vert:" + closestVertexCoordinates + "\n"; } if (closestVertexCoordinatesVW != null) { rt += " clos. vert:" + closestVertexCoordinatesVW + "\n"; } if (closestVertexIndex != -1) { rt += " clos. vert. ind.:" + closestVertexIndex + "\n"; } return rt; } /** Gets the PickResult this intersection is part of */ PickResult getPickResult() { return pickResult; } /** Sets the geom index into the pick result */ void setGeomIndex(int gi) { if (geomIndex != gi) { GeometryArray[] ga = pickResult.getGeometryArrays(); geom = ga[gi]; if (geom instanceof IndexedGeometryArray) { iGeom = (IndexedGeometryArray)geom; } int vertexFormat = geom.getVertexFormat(); hasColors = (0 != (vertexFormat & (GeometryArray.COLOR_3 | GeometryArray.COLOR_4))); hasNormals = (0 != (vertexFormat & GeometryArray.NORMALS)); hasTexCoords = (0 != (vertexFormat & (GeometryArray.TEXTURE_COORDINATE_2 | GeometryArray.TEXTURE_COORDINATE_3))); } geomIndex = gi; } /** Sets the coordinates of the intersection point (world coordinates). @param pt the coordinates */ void setPointCoordinatesVW (Point3d pt) { if (pointCoordinatesVW == null) { pointCoordinatesVW = new Point3d (); } pointCoordinatesVW.x = pt.x; pointCoordinatesVW.y = pt.y; pointCoordinatesVW.z = pt.z; } /** Returns the coordinates of the intersection point (world coordinates), if available. @return coordinates of the point */ public Point3d getPointCoordinatesVW() { return pointCoordinatesVW; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -