📄 boundingpolytope.java
字号:
invMag = 1.0/Math.sqrt(direction.x*direction.x + direction.y*direction.y + direction.z*direction.z); dx = direction.x*invMag; dy = direction.y*invMag; dz = direction.z*invMag; // compute intersection point of ray and each plane then test if point is in polytope for(i=0;i<planes.length;i++) { vd = planes[i].x*dx + planes[i].y*dy + planes[i].z*dz; v0 = -(planes[i].x*origin.x + planes[i].y*origin.y + planes[i].z*origin.z + planes[i].w); if(vd != 0.0) { // ray is parallel to plane t = v0/vd; if( t >= 0.0) { // plane is behind origin x = origin.x + dx*t; // compute intersection point y = origin.y + dy*t; z = origin.z + dz*t; if( pointInPolytope(x,y,z) ) { intersectPoint.x = x; intersectPoint.y = y; intersectPoint.z = z; return true; // ray intersects a face of polytope } } } } return false; } /** * 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 position is a point defining the location of the pick w= distance to pick * @return true or false indicating if an intersection occured */ boolean intersect(Point3d origin, Vector3d direction, Point4d position ) { double t,v0,vd,x,y,z,invMag; double dx, dy, dz; int i,j; if( boundsIsEmpty ) { return false; } if( boundsIsInfinite ) { position.x = origin.x; position.y = origin.y; position.z = origin.z; position.w = 0.0; return true; } invMag = 1.0/Math.sqrt(direction.x*direction.x + direction.y* direction.y + direction.z*direction.z); dx = direction.x*invMag; dy = direction.y*invMag; dz = direction.z*invMag; for(i=0;i<planes.length;i++) { vd = planes[i].x*dx + planes[i].y*dy + planes[i].z*dz; v0 = -(planes[i].x*origin.x + planes[i].y*origin.y + planes[i].z*origin.z + planes[i].w); // System.err.println("v0="+v0+" vd="+vd); if(vd != 0.0) { // ray is parallel to plane t = v0/vd; if( t >= 0.0) { // plane is behind origin x = origin.x + dx*t; // compute intersection point y = origin.y + dy*t; z = origin.z + dz*t; // System.err.println("t="+t+" point="+x+" "+y+" "+z); if( pointInPolytope(x,y,z) ) { position.x = x; position.y = y; position.z = z; position.w = t; return true; // ray intersects a face of polytope } } } } return false; } /** * Test for intersection with a point * @param point is the pick point * @param position is a point defining the location of the pick w= distance to pick * @return true or false indicating if an intersection occured */ boolean intersect(Point3d point, Point4d position ) { int i; if( boundsIsEmpty ) { return false; } if( boundsIsInfinite ) { position.x = point.x; position.y = point.y; position.z = point.z; position.w = 0.0; return true; } for(i = 0; i < this.planes.length; i++){ if(( point.x*this.planes[i].x + point.y*this.planes[i].y + point.z*this.planes[i].z + planes[i].w ) > 0.0 ) return false; } return true; } /** * Test for intersection with a segment * @param start is a point defining the start of the line segment * @param end is a point defining the end of the line segment * @param position is a point defining the location of the pick w= distance to pick * @return true or false indicating if an intersection occured */ boolean intersect( Point3d start, Point3d end, Point4d position ) { double t,v0,vd,x,y,z; int i,j; //System.err.println("line segment intersect : planes.length " + planes.length); if( boundsIsEmpty ) { return false; } if( boundsIsInfinite ) { position.x = start.x; position.y = start.y; position.z = start.z; position.w = 0.0; return true; } Point3d direction = new Point3d(); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; for(i=0;i<planes.length;i++) { vd = planes[i].x*direction.x + planes[i].y*direction.y + planes[i].z*direction.z; v0 = -(planes[i].x*start.x + planes[i].y*start.y + planes[i].z*start.z + planes[i].w); // System.err.println("v0="+v0+" vd="+vd); if(vd != 0.0) { // ray is parallel to plane t = v0/vd; // System.err.println("t is " + t); if( t >= 0.0) { // plane is behind start x = start.x + direction.x*t; // compute intersection point y = start.y + direction.y*t; z = start.z + direction.z*t; // System.err.println("t="+t+" point="+x+" "+y+" "+z); if( pointInPolytope(x,y,z) ) { // if((t*t) > (end.x-start.x)*(end.x-start.x) + // (end.y-start.y)*(end.y-start.y) + // (end.z-start.z)*(end.z-start.z)) { if(t <= 1.0) { position.x = x; position.y = y; position.z = z; position.w = t; return true; // ray intersects a face of polytope } } } } } return false; } /** * Test for intersection with a ray. * @param origin the starting point of the ray * @param direction the direction of the ray * @return true or false indicating if an intersection occured */ public boolean intersect(Point3d origin, Vector3d direction ) { // compute intersection point of ray and each plane then test if point is in polytope double t,v0,vd,x,y,z; int i,j; if( boundsIsEmpty ) { return false; } if( boundsIsInfinite ) { return true; } for(i=0;i<planes.length;i++) { vd = planes[i].x*direction.x + planes[i].y*direction.y + planes[i].z*direction.z; v0 = -(planes[i].x*origin.x + planes[i].y*origin.y + planes[i].z*origin.z + planes[i].w); if(vd != 0.0) { // ray is parallel to plane t = v0/vd; if( t >= 0.0) { // plane is behind origin x = origin.x + direction.x*t; // compute intersection point y = origin.y + direction.y*t; z = origin.z + direction.z*t; if( pointInPolytope(x,y,z) ) { return true; // ray intersects a face of polytope } else { // System.err.println("point outside polytope"); } } } } return false; } /** * Tests whether the bounding polytope is empty. A bounding polytope is * empty if it is null (either by construction or as the result of * a null intersection) or if its volume is negative. A bounding polytope * with a volume of zero is <i>not</i> empty. * @return true if the bounding polytope is empty; * otherwise, it returns false */ public boolean isEmpty() { // if nVerts > 0 after computeAllVerts(), that means // there is some intersection between 3 planes. return (boundsIsEmpty || (nVerts <= 0)); } /** * Test for intersection with a point. * @param point a Point defining a position in 3-space * @return true or false indicating if an intersection occured */ public boolean intersect(Point3d point ) { int i; if( boundsIsEmpty ) { return false; } if( boundsIsInfinite ) { return true; } for(i = 0; i < this.planes.length; i++){ if(( point.x*this.planes[i].x + point.y*this.planes[i].y + point.z*this.planes[i].z + planes[i].w ) > 0.0 ) return false; } return true; } /** * Test for intersection with another bounds object. * @param boundsObject another bounds object * @return true or false indicating if an intersection occured */ boolean intersect(Bounds boundsObject, Point4d position) { return intersect(boundsObject); } /** * Test for intersection with another bounds object. * @param boundsObject another bounds object * @return true or false indicating if an intersection occured */ public boolean intersect(Bounds boundsObject) { if( boundsObject == null ) { return false; } if( boundsIsEmpty || boundsObject.boundsIsEmpty ) { return false; } if( boundsIsInfinite || boundsObject.boundsIsInfinite ) { return true; } if( boundsObject.boundId == BOUNDING_SPHERE ) { return intersect_ptope_sphere( this, (BoundingSphere)boundsObject); } else if( boundsObject.boundId == BOUNDING_BOX){ return intersect_ptope_abox( this, (BoundingBox)boundsObject); } else if(boundsObject.boundId == BOUNDING_POLYTOPE) { return intersect_ptope_ptope( this, (BoundingPolytope)boundsObject); } else { throw new IllegalArgumentException(J3dI18N.getString("BoundingPolytope6")); } } /** * Test for intersection with another bounds object. * @param boundsObjects an array of bounding objects * @return true or false indicating if an intersection occured */ public boolean intersect(Bounds[] boundsObjects) { double distsq, radsq; BoundingSphere sphere; int i; if( boundsObjects == null || boundsObjects.length <= 0 ) { return false; } if( boundsIsEmpty ) { return false; } for(i = 0; i < boundsObjects.length; i++){ if( boundsObjects[i] == null || boundsObjects[i].boundsIsEmpty) ; else if( boundsIsInfinite || boundsObjects[i].boundsIsInfinite ) { return true; // We're done here. } if( boundsObjects[i].boundId == BOUNDING_SPHERE ) { sphere = (BoundingSphere)boundsObjects[i]; radsq = sphere.radius; radsq *= radsq; distsq = sphere.center.distanceSquared(sphere.center); if (distsq < radsq) { return true; } } else if(boundsObjects[i].boundId == BOUNDING_BOX){ if( this.intersect(boundsObjects[i])) return true; } else if(boundsObjects[i].boundId == BOUNDING_POLYTOPE) { if( this.intersect(boundsObjects[i])) return true; } else { throw new IllegalArgumentException(J3dI18N.getString("BoundingPolytope7")); } } return false; } /** * Test for intersection with another bounds object. * @param boundsObject another bounds object * @param newBoundPolytope the new bounding polytope, which is the intersection of * the boundsObject and this BoundingPolytope * @return true or false indicating if an intersection occured */ public boolean intersect(Bounds boundsObject, BoundingPolytope newBoundPolytope) { int i; if((boundsObject == null) || boundsIsEmpty || boundsObject.boundsIsEmpty ) { newBoundPolytope.boundsIsEmpty = true; newBoundPolytope.boundsIsInfinite = false; newBoundPolytope.computeAllVerts(); return false; } if(boundsIsInfinite && (!boundsObject.boundsIsInfinite)) { newBoundPolytope.set(boundsObject); return true; } else if((!boundsIsInfinite) && boundsObject.boundsIsInfinite) { newBoundPolytope.set(this); return true; } else if(boundsIsInfinite && boundsObject.boundsIsInfinite) { newBoundPolytope.set(this); return true; } BoundingBox tbox = new BoundingBox(); // convert sphere to box if( boundsObject.boundId == BOUNDING_SPHERE ) { BoundingSphere sphere = (BoundingSphere)boundsObject; if( this.intersect( sphere)) { BoundingBox sbox = new BoundingBox( sphere ); // convert sphere to box BoundingBox pbox = new BoundingBox( this ); // convert polytope to box pbox.intersect(sbox, tbox); // insersect two boxes newBoundPolytope.set( tbox ); return true; } } else if( boundsObject.boundId == BOUNDING_BOX){ BoundingBox box = (BoundingBox)boundsObject; if( this.intersect( box)) { BoundingBox pbox = new BoundingBox( this ); // convert polytope to box pbox.intersect(box, tbox); // insersect two boxes newBoundPolytope.set( tbox ); return true; } } else if(boundsObject.boundId == BOUNDING_POLYTOPE) { BoundingPolytope polytope = (BoundingPolytope)boundsObject; if( this.intersect( polytope)) { Vector4d newPlanes[] = new Vector4d[planes.length + polytope.planes.length]; for(i=0;i<planes.length;i++) { newPlanes[i] = new Vector4d(planes[i]); } for(i=0;i<polytope.planes.length;i++) { newPlanes[planes.length + i] = new Vector4d(polytope.planes[i]); } BoundingPolytope newPtope= new BoundingPolytope( newPlanes ); newBoundPolytope.set(newPtope); return true; } } else { throw new IllegalArgumentException(J3dI18N.getString("BoundingPolytope8")); } newBoundPolytope.boundsIsEmpty = true; newBoundPolytope.boundsIsInfinite = false; newBoundPolytope.computeAllVerts();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -