?? intersect.java
字號:
* should be allocated by the user. * @return <code>true</code> if the segment intersects the point, * <code>false</code> if the segment does not intersect the object. */ public static boolean segmentAndPoint( PickSegment segment, Point3d pnt, double dist[] ) { Point3d start = new Point3d(); Point3d end = new Point3d(); Vector3d direction = new Vector3d(); segment.get(start, end); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; if((rayAndPoint(pnt, start, direction, dist)==true) && (dist[0] <= 1.0)) return true; return false; } /** * Return true if point intersects with segment and the distance, from * the start of segment to the intersection point, is stored in dist[0]. * * @param segment The segment that is used in intersection test. * @param pnt The point that is used in intersection test. * @param dist On return dist[0] contains the distance between segment's start and the point * intersection, if exist. * @return true if segment intersects point, else return false. */ public static boolean segmentAndPoint( PickSegment segment, Point3f pnt, double dist[] ) { Point3d start = new Point3d(); Point3d end = new Point3d(); Vector3d direction = new Vector3d(); segment.get(start, end); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; if((rayAndPoint(new Point3d(pnt), start, direction, dist)==true) && (dist[0] <= 1.0)) return true; return false; } /** * Determines if the <code>PickPoint</code> and <code>Point3d</code> * objects intersect. * * @param point The PickPoint that is used in the intersection test. * @param pnt The Point3d that is used in intersection test. * @return <code>true</code> if the PickPoint and Point3d objects * intersect, <code>false</code> if the do not intersect. */ public static boolean pointAndPoint( PickPoint point, Point3d pnt) { Point3d location = new Point3d(); point.get(location); if ((location.x == pnt.x) && (location.y == pnt.y) && (location.z == pnt.z)) return true; return false; } /** * Return true if pnt intersects with point. * * @param point The point that is used in intersection test. * @param pnt The point that is used in intersection test. * @return true if point intersects pnt, else return false. */ public static boolean pointAndPoint( PickPoint point, Point3f pnt) { Point3d location = new Point3d(); point.get(location); if(((float) location.x == pnt.x) && ((float) location.y == pnt.y) && ((float) location.z == pnt.z)) return true; return false; } /** * Determines if the <code>PickRay</code> and Line * objects intersect. * The line is defined as <code>coordinates[index]</code> to * <code>coordinates[index+1]</code>. * * @param ray The ray that is used in the intersection test. * @param coordinates An array holding the line data. * @param dist On return dist[0] contains the distance between ray's origin and the point of * intersection, if it exists. The dist array * should be allocated by the user. * @return <code>true</code> if the ray intersects the line, * <code>false</code> if the ray does not intersect the object. */ public static boolean rayAndLine(PickRay ray, Point3d coordinates[], int index, double dist[] ) { Point3d origin = new Point3d(); Vector3d direction = new Vector3d(); if((coordinates.length - index) < 2) throw new RuntimeException(J3dUtilsI18N.getString("Intersect11")); ray.get(origin, direction); Point3d start = coordinates[index++]; Point3d end = coordinates[index]; return lineAndRay( start, end, origin, direction, dist ); } /** * Return true if line intersects with ray and the distance, from * the origin of ray to the intersection point, is stored in dist[0]. * The line is defined by coordinates[index] to coordinates[index+1] * * @param ray The ray that is used in intersection test. * @param coordinates an array of vertices. * @param index the vertex index * @param dist On return dist[0] contains the distance between ray's origin and the point intersection, if * exist. * @return true if ray intersects line, else return false. */ public static boolean rayAndLine(PickRay ray, Point3f coordinates[], int index, double dist[] ) { Point3d origin = new Point3d(); Vector3d direction = new Vector3d(); if((coordinates.length - index) < 2) throw new RuntimeException(J3dUtilsI18N.getString("Intersect11")); ray.get(origin, direction); Point3d start = new Point3d(coordinates[index++]); Point3d end = new Point3d(coordinates[index]); return lineAndRay( start, end, origin, direction, dist ); } /** * Determines if the <code>PickSegment</code> and Line * objects intersect. * The line is defined as <code>coordinates[index]</code> to * <code>coordinates[index+1]</code>. * * @param segment The segment that is used in the intersection test. * @param coordinates An array holding the line data. * @param dist On return dist[0] contains the distance between segment's origin and the point of * intersection, if it exists. The dist array * should be allocated by the user. * @return <code>true</code> if the segment intersects the line, * <code>false</code> if the segment does not intersect the object. */ public static boolean segmentAndLine(PickSegment segment, Point3d coordinates[], int index, double dist[] ) { Point3d start = new Point3d(); Point3d end = new Point3d(); Vector3d direction = new Vector3d(); if((coordinates.length - index) < 2) throw new RuntimeException(J3dUtilsI18N.getString("Intersect13")); segment.get(start, end); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; Point3d startpnt = coordinates[index++]; Point3d endpnt = coordinates[index]; if(lineAndRay(startpnt, endpnt, start, direction, dist)==true) if(dist[0] <= 1.0) return true; return false; } /** * Return true if line intersects with segment and the distance, from * the start of segment to the intersection point, is stored in dist[0]. * The line is defined by coordinates[index] to coordinates[index+1] * * @param segment The segment that is used in intersection test. * @param coordinates an array of vertices. * @param index the vertex index * @param dist On return dist[0] contains the distance between segment's start and the point * intersection, if exist. * @return true if segment intersects line, else return false. */ public static boolean segmentAndLine(PickSegment segment, Point3f coordinates[], int index, double dist[] ) { Point3d start = new Point3d(); Point3d end = new Point3d(); Vector3d direction = new Vector3d(); if((coordinates.length - index) < 2) throw new RuntimeException(J3dUtilsI18N.getString("Intersect13")); segment.get(start, end); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; Point3d startpnt = new Point3d(coordinates[index++]); Point3d endpnt = new Point3d(coordinates[index]); if(lineAndRay(startpnt, endpnt, start, direction, dist)==true) if(dist[0] <= 1.0) return true; return false; } /** * Determines if the <code>PickPoint</code> and Line * objects intersect. * The line is defined as <code>coordinates[index]</code> to * <code>coordinates[index+1]</code>. * * @param point The point that is used in the intersection test. * @param coordinates An array holding the line data. * @return <code>true</code> if the the point intersects the line, * <code>false</code> if the the point does not intersect the object. */ public static boolean pointAndLine(PickPoint point, Point3d coordinates[], int index ) { if((coordinates.length - index) < 2) throw new RuntimeException(J3dUtilsI18N.getString("Intersect13")); double dist[] = new double[1]; Point3d start = coordinates[index++]; Point3d end = coordinates[index]; Point3d location = new Point3d(); Vector3d direction = new Vector3d(); point.get(location); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; if ((rayAndPoint(location, start, direction, dist)==true) && (dist[0] <= 1.0)) return true; return false; } /** * Return true if line intersects with point. * The line is defined by coordinates[index] to coordinates[index+1] * * @param point The point that is used in intersection test. * @param coordinates an array of vertices. * @param index the vertex index * @return true if point intersects line, else return false. */ public static boolean pointAndLine(PickPoint point, Point3f coordinates[], int index ) { if((coordinates.length - index) < 2) throw new RuntimeException(J3dUtilsI18N.getString("Intersect13")); double dist[] = new double[1]; Point3d start = new Point3d(coordinates[index++]); Point3d end = new Point3d(coordinates[index]); Point3d location = new Point3d(); Vector3d direction = new Vector3d(); point.get(location); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; if((rayAndPoint(location, start, direction, dist)==true) && (dist[0] <= 1.0)) return true; return false; } /** * Return true if point is on the inside of halfspace test. The * halfspace is * partition by the plane of triangle or quad. * */ private static boolean pointAndPoly( Point3d coordinates[], PickPoint point) { Vector3d vec0 = new Vector3d(); // Edge vector from point 0 to point 1; Vector3d vec1 = new Vector3d(); // Edge vector from point 0 to point 2 or 3; Vector3d pNrm = new Vector3d(); double absNrmX, absNrmY, absNrmZ, pD = 0.0; Vector3d tempV3d = new Vector3d(); double pNrmDotrDir = 0.0; double tempD; int i, j; // Compute plane normal. for(i=0; i<coordinates.length-1;) { vec0.x = coordinates[i+1].x - coordinates[i].x; vec0.y = coordinates[i+1].y - coordinates[i].y; vec0.z = coordinates[i+1].z - coordinates[i++].z; if(vec0.length() > 0.0) break; } for(j=i; j<coordinates.length-1; j++) { vec1.x = coordinates[j+1].x - coordinates[j].x; vec1.y = coordinates[j+1].y - coordinates[j].y; vec1.z = coordinates[j+1].z - coordinates[j].z; if(vec1.length() > 0.0) break; } if(j == (coordinates.length-1)) { // System.out.println("(1) Degenerated polygon."); return false; // Degenerated polygon. } /* System.out.println("Ray orgin : " + ray.origin + " dir " + ray.direction); System.out.println("Triangle/Quad :"); for(i=0; i<coordinates.length; i++) System.out.println("P" + i + " " + coordinates[i]); */ pNrm.cross(vec0,vec1); if(pNrm.length() == 0.0) { // System.out.println("(2) Degenerated polygon."); return false; // Degenerated polygon. } // Compute plane D. tempV3d.set((Tuple3d) coordinates[0]); pD = pNrm.dot(tempV3d); Point3d location = new Point3d(); point.get(location); tempV3d.set((Tuple3d) location); if((pD - pNrm.dot(tempV3d)) == 0.0 ) return true; return false; } private static boolean lineAndRay(Point3d start, Point3d end, Point3d ori, Vector3d dir, double dist[] ) { double m00, m01, m10, m11; double mInv00, mInv01, mInv10, mInv11; double dmt, t, s, tmp1, tmp2; Vector3d lDir; lDir = new Vector3d(end.x - start.x, end.y - start.y, end.z - start.z); m00 = lDir.x; m01 = -dir.x; m10 = lDir.y; m11 = -dir.y; // Get the determinant. dmt = (m00 * m11) - (m10 * m01); if(dmt==0.0) // No solution, hence no intersect. return false; // Find the inverse. tmp1 = 1/dmt; mInv00 = tmp1 * m11; mInv01 = tmp1 * (-m01); mInv10 = tmp1 * (-m10); mInv11 = tmp1 * m00; tmp1 = ori.x - start.x; tmp2 = ori.y - start.y; t = mInv00 * tmp1 + mInv01 * tmp2; s = mInv10 * tmp1 + mInv11 * tmp2; if(s<0.0) // Before the origin of ray. return false; if((t<0)||(t>1.0)) // Before or after the end points of line. return false; tmp1 = ori.z + s * dir.z; tmp2 = start.z + t * lDir.z; if((tmp1 < (tmp2 - Double.MIN_VALUE)) || (tmp1 > (tmp2 + Double.MIN_VALUE))) return false; dist[0] = s; return true; } private static boolean rayAndPoint( Point3d pnt, Point3d ori, Vector3d dir, double dist[] ) { int flag = 0; double temp; if(dir.x != 0.0) { flag = 0; dist[0] = (pnt.x - ori.x)/dir.x; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -