📄 boundingpolytope.java
字号:
for(i=0;i<polytope.planes.length;i++) { planes[i].x = polytope.planes[i].x; planes[i].y = polytope.planes[i].y; planes[i].z = polytope.planes[i].z; planes[i].w = polytope.planes[i].w; mag[i] = polytope.mag[i]; } nVerts = polytope.nVerts; verts = new Point3d[nVerts]; for (k=0; k<nVerts; k++) { verts[k] = new Point3d(polytope.verts[k]); } boundsIsEmpty = boundsObject.boundsIsEmpty; boundsIsInfinite = boundsObject.boundsIsInfinite; } else { throw new IllegalArgumentException(J3dI18N.getString("BoundingPolytope2")); } } /** * Creates a copy of a polytope. * @return a new BoundingPolytope */ public Object clone() { return new BoundingPolytope(planes); } /** * Indicates whether the specified <code>bounds</code> object is * equal to this BoundingPolytope object. They are equal if the * specified <code>bounds</code> object is an instance of * BoundingPolytope and all of the data * members of <code>bounds</code> are equal to the corresponding * data members in this BoundingPolytope. * @param bounds the object with which the comparison is made. * @return true if this BoundingPolytope is equal to <code>bounds</code>; * otherwise false * * @since Java 3D 1.2 */ public boolean equals(Object bounds) { try { BoundingPolytope polytope = (BoundingPolytope)bounds; if (planes.length != polytope.planes.length) return false; for (int i = 0; i < planes.length; i++) if (!planes[i].equals(polytope.planes[i])) return false; return true; } catch (NullPointerException e) { return false; } catch (ClassCastException e) { return false; } } /** * Returns a hash code value for this BoundingPolytope object * based on the data values in this object. Two different * BoundingPolytope objects with identical data values (i.e., * BoundingPolytope.equals returns true) will return the same hash * code value. Two BoundingPolytope objects with different data * members may return the same hash code value, although this is * not likely. * @return a hash code value for this BoundingPolytope object. * * @since Java 3D 1.2 */ public int hashCode() { long bits = 1L; for (int i = 0; i < planes.length; i++) { bits = 31L * bits + HashCodeUtil.doubleToLongBits(planes[i].x); bits = 31L * bits + HashCodeUtil.doubleToLongBits(planes[i].y); bits = 31L * bits + HashCodeUtil.doubleToLongBits(planes[i].z); bits = 31L * bits + HashCodeUtil.doubleToLongBits(planes[i].w); } return (int) (bits ^ (bits >> 32)); } /** * Combines this bounding polytope with a bounding object so that the * resulting bounding polytope encloses the original bounding polytope and the * given bounds object. * @param boundsObject another bounds object */ public void combine(Bounds boundsObject) { BoundingSphere sphere; if((boundsObject == null) || (boundsObject.boundsIsEmpty) || (boundsIsInfinite)) return; if((boundsIsEmpty) || (boundsObject.boundsIsInfinite)) { this.set(boundsObject); return; } boundsIsEmpty = boundsObject.boundsIsEmpty; boundsIsInfinite = boundsObject.boundsIsInfinite; if( boundsObject.boundId == BOUNDING_SPHERE ) { sphere = (BoundingSphere)boundsObject; int i; double dis; for(i = 0; i < planes.length; i++){ dis = sphere.radius+ sphere.center.x*planes[i].x + sphere.center.y*planes[i].y + sphere.center.z * planes[i].z + planes[i].w; if( dis > 0.0 ) { planes[i].w += -dis; } } } else if( boundsObject instanceof BoundingBox){ BoundingBox b = (BoundingBox)boundsObject; if( !allocBoxVerts){ boxVerts = new Point3d[8]; for(int j=0;j<8;j++)boxVerts[j] = new Point3d(); allocBoxVerts = true; } boxVerts[0].set(b.lower.x, b.lower.y, b.lower.z ); boxVerts[1].set(b.lower.x, b.upper.y, b.lower.z ); boxVerts[2].set(b.upper.x, b.lower.y, b.lower.z ); boxVerts[3].set(b.upper.x, b.upper.y, b.lower.z ); boxVerts[4].set(b.lower.x, b.lower.y, b.upper.z ); boxVerts[5].set(b.lower.x, b.upper.y, b.upper.z ); boxVerts[6].set(b.upper.x, b.lower.y, b.upper.z ); boxVerts[7].set(b.upper.x, b.upper.y, b.upper.z ); this.combine(boxVerts); } else if(boundsObject.boundId == BOUNDING_POLYTOPE) { BoundingPolytope polytope = (BoundingPolytope)boundsObject; this.combine(polytope.verts); } else { throw new IllegalArgumentException(J3dI18N.getString("BoundingPolytope3")); } computeAllVerts(); } /** * Combines this bounding polytope with an array of bounding objects so that the * resulting bounding polytope encloses the original bounding polytope and the * given array of bounds object. * @param boundsObjects an array of bounds objects */ public void combine(Bounds[] boundsObjects) { int i=0; double dis; if( (boundsObjects == null) || (boundsObjects.length <= 0) || (boundsIsInfinite)) return; // find first non empty bounds object while( (i<boundsObjects.length) && ((boundsObjects[i]==null) || boundsObjects[i].boundsIsEmpty)) { i++; } if( i >= boundsObjects.length) return; // no non empty bounds so do not modify current bounds if(boundsIsEmpty) this.set(boundsObjects[i++]); if(boundsIsInfinite) return; for(;i<boundsObjects.length;i++) { if( boundsObjects[i] == null ); // do nothing else if( boundsObjects[i].boundsIsEmpty ); // do nothing else if( boundsObjects[i].boundsIsInfinite ) { this.set(boundsObjects[i]); break; // We're done; } else if( boundsObjects[i].boundId == BOUNDING_SPHERE ) { BoundingSphere sphere = (BoundingSphere)boundsObjects[i]; for(int j = 0; j < planes.length; j++){ dis = sphere.radius+ sphere.center.x*planes[j].x + sphere.center.y*planes[j].y + sphere.center.z* planes[j].z + planes[j].w; if( dis > 0.0 ) { planes[j].w += -dis; } } } else if( boundsObjects[i].boundId == BOUNDING_BOX){ BoundingBox b = (BoundingBox)boundsObjects[i]; if( !allocBoxVerts){ boxVerts = new Point3d[8]; for(int j=0;j<8;j++)boxVerts[j] = new Point3d(); allocBoxVerts = true; } boxVerts[0].set(b.lower.x, b.lower.y, b.lower.z ); boxVerts[1].set(b.lower.x, b.upper.y, b.lower.z ); boxVerts[2].set(b.upper.x, b.lower.y, b.lower.z ); boxVerts[3].set(b.upper.x, b.upper.y, b.lower.z ); boxVerts[4].set(b.lower.x, b.lower.y, b.upper.z ); boxVerts[5].set(b.lower.x, b.upper.y, b.upper.z ); boxVerts[6].set(b.upper.x, b.lower.y, b.upper.z ); boxVerts[7].set(b.upper.x, b.upper.y, b.upper.z ); this.combine(boxVerts); } else if(boundsObjects[i] instanceof BoundingPolytope) { BoundingPolytope polytope = (BoundingPolytope)boundsObjects[i]; this.combine(polytope.verts); } else { throw new IllegalArgumentException(J3dI18N.getString("BoundingPolytope4")); } computeAllVerts(); } } /** * Combines this bounding polytope with a point. * @param point a 3d point in space */ public void combine(Point3d point) { int i; double dis; if(boundsIsInfinite) { return; } if( boundsIsEmpty ){ planes = new Vector4d[6]; mag = new double[planes.length]; pDotN = new double[planes.length]; nVerts = 1; verts = new Point3d[nVerts]; verts[0] = new Point3d( point.x, point.y, point.z); for(i=0;i<planes.length;i++) { pDotN[i] = 0.0; } planes[0] = new Vector4d( 1.0, 0.0, 0.0, -point.x ); planes[1] = new Vector4d(-1.0, 0.0, 0.0, point.x ); planes[2] = new Vector4d( 0.0, 1.0, 0.0, -point.y ); planes[3] = new Vector4d( 0.0,-1.0, 0.0, point.y ); planes[4] = new Vector4d( 0.0, 0.0, 1.0, -point.z ); planes[5] = new Vector4d( 0.0, 0.0,-1.0, point.z ); mag[0] = 1.0; mag[1] = 1.0; mag[2] = 1.0; mag[3] = 1.0; mag[4] = 1.0; mag[5] = 1.0; centroid.x = point.x; centroid.y = point.y; centroid.z = point.z; boundsIsEmpty = false; boundsIsInfinite = false; } else { for(i = 0; i < planes.length; i++){ dis = point.x*planes[i].x + point.y*planes[i].y + point.z* planes[i].z + planes[i].w; if( dis > 0.0 ) { planes[i].w += -dis; } } computeAllVerts(); } } /** * Combines this bounding polytope with an array of points. * @param points an array of 3d points in space */ public void combine(Point3d[] points) { int i,j; double dis; if( boundsIsInfinite) { return; } if( boundsIsEmpty ){ planes = new Vector4d[6]; mag = new double[planes.length]; pDotN = new double[planes.length]; nVerts = points.length; verts = new Point3d[nVerts]; verts[0] = new Point3d( points[0].x, points[0].y, points[0].z); for(i=0;i<planes.length;i++) { pDotN[i] = 0.0; } planes[0] = new Vector4d( 1.0, 0.0, 0.0, -points[0].x ); planes[1] = new Vector4d(-1.0, 0.0, 0.0, points[0].x ); planes[2] = new Vector4d( 0.0, 1.0, 0.0, -points[0].y ); planes[3] = new Vector4d( 0.0,-1.0, 0.0, points[0].y ); planes[4] = new Vector4d( 0.0, 0.0, 1.0, -points[0].z ); planes[5] = new Vector4d( 0.0, 0.0,-1.0, points[0].z ); mag[0] = 1.0; mag[1] = 1.0; mag[2] = 1.0; mag[3] = 1.0; mag[4] = 1.0; mag[5] = 1.0; centroid.x = points[0].x; centroid.y = points[0].y; centroid.z = points[0].z; boundsIsEmpty = false; boundsIsInfinite = false; } for(j = 0; j < points.length; j++){ for(i = 0; i < planes.length; i++){ dis = points[j].x*planes[i].x + points[j].y*planes[i].y + points[j].z*planes[i].z + planes[i].w; if( dis > 0.0 ) { planes[i].w += -dis; } } } computeAllVerts(); } /** * Modifies the bounding polytope so that it bounds the volume * generated by transforming the given bounding object. * @param boundsObject the bounding object to be transformed * @param matrix a transformation matrix */ public void transform( Bounds boundsObject, Transform3D matrix) { if( boundsObject == null || boundsObject.boundsIsEmpty) { boundsIsEmpty = true; boundsIsInfinite = false; computeAllVerts(); return; } if(boundsObject.boundsIsInfinite) { this.set(boundsObject); return; } if( boundsObject.boundId == BOUNDING_SPHERE ) { BoundingSphere sphere = new BoundingSphere((BoundingSphere)boundsObject); sphere.transform(matrix); this.set(sphere); } else if( boundsObject.boundId == BOUNDING_BOX){ BoundingBox box = new BoundingBox( (BoundingBox)boundsObject); box.transform(matrix); this.set(box); } else if(boundsObject.boundId == BOUNDING_POLYTOPE) { BoundingPolytope polytope = new BoundingPolytope( (BoundingPolytope)boundsObject); polytope.transform(matrix); this.set(polytope); } else { throw new IllegalArgumentException(J3dI18N.getString("BoundingPolytope5")); } } /** * Transforms this bounding polytope by the given transformation matrix. * @param matrix a transformation matrix */ public void transform( Transform3D matrix) { if(boundsIsInfinite) return; int i; double invMag; Transform3D invTrans = new Transform3D(matrix); invTrans.invert(); invTrans.transpose(); for(i = 0; i < planes.length; i++){ planes[i].x = planes[i].x * mag[i]; planes[i].y = planes[i].y * mag[i]; planes[i].z = planes[i].z * mag[i]; planes[i].w = planes[i].w * mag[i]; invTrans.transform( planes[i] ); } for(i=0;i<planes.length;i++) { // normalize the plane normals mag[i] = Math.sqrt(planes[i].x*planes[i].x + planes[i].y*planes[i].y + planes[i].z*planes[i].z); invMag = 1.0/mag[i]; this.planes[i] = new Vector4d( planes[i].x*invMag, planes[i].y*invMag, planes[i].z*invMag, planes[i].w*invMag ); } for (i=0; i < verts.length; i++) { matrix.transform(verts[i]); } } /** * Test for intersection with a ray. * @param origin is a the starting point of the ray * @param direction is the direction of the ray * @param intersectPoint is a point defining the location of the intersection * @return true or false indicating if an intersection occured */ boolean intersect(Point3d origin, Vector3d direction, Point3d intersectPoint ) { double t,v0,vd,x,y,z,invMag; double dx, dy, dz; int i; if( boundsIsEmpty ) { return false; } if( boundsIsInfinite ) { intersectPoint.x = origin.x; intersectPoint.y = origin.y; intersectPoint.z = origin.z; return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -