📄 pickintersection.java
字号:
/** Get the distance from the PickShape start point to the intersection point @return the distance to the intersection point, if available. */ public double getDistance () { return distance; } /** Set the distance to intersection point @param dist the distance to the intersection point */ void setDistance (double dist) { distance = dist; } /** Set VWorld coordinates of the picked primtive @param coords */ void setPrimitiveCoordinatesVW (Point3d [] coords) { primitiveCoordinatesVW = new Point3d [coords.length]; System.arraycopy (coords, 0, primitiveCoordinatesVW, 0, coords.length); } /** Get VWorld coordinates of the intersected primitive @return an array of Point3d's for the primitive that was picked */ public Point3d[] getPrimitiveCoordinatesVW () { return primitiveCoordinatesVW; } /** Set vertex indices of primitive's vertices @param verts array of coordinate indices */ void setVertexIndices (int [] verts) { primitiveVertexIndices = new int [verts.length]; System.arraycopy (verts, 0, primitiveVertexIndices, 0, verts.length); } /** Get vertex indices of the intersected primitive @return an array which contains the list of indices */ public int [] getPrimitiveVertexIndices () { return primitiveVertexIndices; } /** Returns the index of the intersected GeometryArray into the geometry arrays in the PickResult */ public int getGeometryArrayIndex() { return geomIndex; } /* ================================================================== */ /* Derived Data: GeometryArray */ /* ================================================================== */ /** Returns the GeometryArray for the intersection */ public GeometryArray getGeometryArray() { 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))); } return geom; } /** Returns true if the geometry is indexed */ public boolean geometryIsIndexed() { GeometryArray ga = getGeometryArray(); if (iGeom != null) { return true; } else { return false; } } /* ================================================================== */ /* Derived Data: Closest Vertex */ /* ================================================================== */ /** Get coordinates of closest vertex (local) @return the coordinates of the vertex closest to the intersection point */ public Point3d getClosestVertexCoordinates () { // System.out.println("closestVertexCoordinates " + closestVertexCoordinates); if (closestVertexCoordinates == null) { int vertexIndex = getClosestVertexIndex(); int vformat = geom.getVertexFormat(); int val; int[] indices = getPrimitiveCoordinateIndices(); if ((vformat & GeometryArray.BY_REFERENCE) == 0) { closestVertexCoordinates = new Point3d(); geom.getCoordinate(indices[vertexIndex], closestVertexCoordinates); /* System.out.println("PickIntersection : closestVertexCoordinates " + closestVertexCoordinates + " vertexIndex " + vertexIndex); */ } else { if ((vformat & GeometryArray.INTERLEAVED) == 0) { double[] doubleData = geom.getCoordRefDouble(); // If data was set as float then .. if (doubleData == null) { float[] floatData = geom.getCoordRefFloat(); if (floatData == null) { Point3f[] p3fData = geom.getCoordRef3f(); if (p3fData == null) { Point3d[] p3dData = geom.getCoordRef3d(); closestVertexCoordinates = new Point3d(p3dData[indices[vertexIndex]].x, p3dData[indices[vertexIndex]].y, p3dData[indices[vertexIndex]].z); } else { closestVertexCoordinates = new Point3d(p3fData[indices[vertexIndex]].x, p3fData[indices[vertexIndex]].y, p3fData[indices[vertexIndex]].z); } } else { val = indices[vertexIndex] * 3; // for x,y,z closestVertexCoordinates = new Point3d(floatData[val], floatData[val+1], floatData[val+2]); } } else { val = indices[vertexIndex] * 3; // for x,y,z closestVertexCoordinates = new Point3d(doubleData[val], doubleData[val+1], doubleData[val+2]); } } else { float[] floatData = geom.getInterleavedVertices(); int offset = getInterleavedVertexOffset(geom); int stride = offset + 3; // for the vertices . val = stride * indices[vertexIndex]+offset; closestVertexCoordinates = new Point3d(floatData[val], floatData[val+1], floatData[val+2]); } } } return closestVertexCoordinates; } /** Get coordinates of closest vertex (world) @return the coordinates of the vertex closest to the intersection point */ public Point3d getClosestVertexCoordinatesVW () { if (closestVertexCoordinatesVW == null) { int vertexIndex = getClosestVertexIndex(); Point3d[] coordinatesVW = getPrimitiveCoordinatesVW(); closestVertexCoordinatesVW = coordinatesVW[vertexIndex]; } return closestVertexCoordinatesVW; } /** Get index of closest vertex @return the index of the closest vertex */ public int getClosestVertexIndex () { if (closestVertexIndex == -1) { storeClosestVertex(); } return closestVertexIndex; } /** Calculates and stores the closest vertex information */ void storeClosestVertex () { if (closestVertexIndex == -1) { double maxDist = Double.MAX_VALUE; double curDist = Double.MAX_VALUE; int closestIndex = -1; /* System.out.println("primitiveCoordinatesVW.length " + primitiveCoordinatesVW.length); */ for (int i=0;i<primitiveCoordinatesVW.length;i++) { curDist = pointCoordinatesVW.distance (primitiveCoordinatesVW[i]); /* System.out.println("pointCoordinatesVW " + pointCoordinatesVW); System.out.println("primitiveCoordinatesVW[" + i + "] " + primitiveCoordinatesVW[i]); System.out.println("curDist " + curDist); */ if (curDist < maxDist) { closestIndex = i; maxDist = curDist; } } closestVertexIndex = closestIndex; } } /* ================================================================== */ /* Derived Data: Primitive */ /* ================================================================== */ /** Get the coordinates indices for the intersected primitive. For a non-indexed primitive, this will be the same as the primitive vertex indices @return an array indices */ public int[] getPrimitiveCoordinateIndices () { if (primitiveCoordinateIndices == null) { if (geometryIsIndexed()) { primitiveCoordinateIndices = new int[primitiveVertexIndices.length]; for (int i = 0; i < primitiveVertexIndices.length; i++) { primitiveCoordinateIndices[i] = iGeom.getCoordinateIndex(primitiveVertexIndices[i]); } } else { primitiveCoordinateIndices = primitiveVertexIndices; } } return primitiveCoordinateIndices; } /** Get the local coordinates intersected primitive @return an array of Point3d's for the primitive that was intersected */ public Point3d[] getPrimitiveCoordinates () { if (primitiveCoordinates == null) { primitiveCoordinates = new Point3d[primitiveVertexIndices.length]; int[] indices = getPrimitiveCoordinateIndices(); int vformat = geom.getVertexFormat(); int val; // System.out.println("PickIntersection : indices.length - " + indices.length); if ((vformat & GeometryArray.BY_REFERENCE) == 0) { for (int i = 0; i < indices.length; i++) { primitiveCoordinates[i] = new Point3d(); // System.out.println("PickIntersection : indices["+i+"] = " + indices[i]); geom.getCoordinate(indices[i], primitiveCoordinates[i]); } } else { if ((vformat & GeometryArray.INTERLEAVED) == 0) { double[] doubleData = geom.getCoordRefDouble(); // If data was set as float then .. if (doubleData == null) { float[] floatData = geom.getCoordRefFloat(); if (floatData == null) { Point3f[] p3fData = geom.getCoordRef3f(); if (p3fData == null) { Point3d[] p3dData = geom.getCoordRef3d(); for (int i = 0; i < indices.length; i++) { primitiveCoordinates[i] = new Point3d(p3dData[indices[i]].x, p3dData[indices[i]].y, p3dData[indices[i]].z); } } else { for (int i = 0; i < indices.length; i++) { primitiveCoordinates[i] = new Point3d(p3fData[indices[i]].x, p3fData[indices[i]].y, p3fData[indices[i]].z); } } } else { for (int i = 0; i < indices.length; i++) { val = indices[i] * 3; primitiveCoordinates[i] = new Point3d(floatData[val], floatData[val+1], floatData[val+2]); } } } else { for (int i = 0; i < indices.length; i++) { val = indices[i] * 3; primitiveCoordinates[i] = new Point3d(doubleData[val], doubleData[val+1], doubleData[val+2]); } } } else { float[] floatData = geom.getInterleavedVertices(); int offset = getInterleavedVertexOffset(geom); int stride = offset + 3; // for the vertices . for (int i = 0; i < indices.length; i++) { val = stride * indices[i]+offset; primitiveCoordinates[i] = new Point3d(floatData[val], floatData[val+1], floatData[val+2]); } } } } return primitiveCoordinates; } int getInterleavedVertexOffset(GeometryArray geo) { int offset = 0; int vformat = geo.getVertexFormat(); if ((vformat & GeometryArray.COLOR_3) == GeometryArray.COLOR_3) { offset += 3; } else if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4){ offset += 4; } if ((vformat & GeometryArray.NORMALS) != 0) offset += 3; if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) { offset += 2 * geo.getTexCoordSetCount(); } else if ((vformat & GeometryArray.TEXTURE_COORDINATE_3) == GeometryArray.TEXTURE_COORDINATE_3) { offset += 3 * geo.getTexCoordSetCount(); } return offset; } int getInterleavedStride(GeometryArray geo) { int offset = 3; // Add 3 for vertices int vformat = geo.getVertexFormat(); if ((vformat & GeometryArray.COLOR_3) == GeometryArray.COLOR_3) { offset += 3; } else if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4){ offset += 4; } if ((vformat & GeometryArray.NORMALS) != 0) offset += 3; if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) { offset += 2 * geo.getTexCoordSetCount(); } else if ((vformat & GeometryArray.TEXTURE_COORDINATE_3) == GeometryArray.TEXTURE_COORDINATE_3) { offset += 3 * geo.getTexCoordSetCount(); } return offset; } int getInterleavedColorOffset(GeometryArray geo) { int offset = 0; int vformat = geo.getVertexFormat(); if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) { offset += 2 * geo.getTexCoordSetCount(); } else if ((vformat & GeometryArray.TEXTURE_COORDINATE_3) == GeometryArray.TEXTURE_COORDINATE_3) { offset += 3 * geo.getTexCoordSetCount(); } return offset; } int getInterleavedNormalOffset(GeometryArray geo) { int offset = 0; int vformat = geo.getVertexFormat(); if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) { offset += 2 * geo.getTexCoordSetCount(); } else if ((vformat & GeometryArray.TEXTURE_COORDINATE_3) == GeometryArray.TEXTURE_COORDINATE_3) { offset += 3 * geo.getTexCoordSetCount(); } if ((vformat & GeometryArray.COLOR_3) == GeometryArray.COLOR_3) { offset += 3; } else if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4){ offset += 4; } return offset; } /** Get the normal indices for the intersected primitive. For a non-indexed primitive, this will be the same as the primitive vertex indices If the geometry array does not contain normals this will return null @return an array indices */ public int[] getPrimitiveNormalIndices () { if (hasNormals && (primitiveNormalIndices == null)) { if (geometryIsIndexed()) { primitiveNormalIndices = new int[primitiveVertexIndices.length]; for (int i = 0; i < primitiveVertexIndices.length; i++) { primitiveNormalIndices[i] = iGeom.getNormalIndex(primitiveVertexIndices[i]); } } else { primitiveNormalIndices = primitiveVertexIndices; } } return primitiveNormalIndices; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -