@Test public void testIntersectRayTriangle() { Vector3 rayStart = new Vector3(0, 0, 0); Vector3 rayEnd = new Vector3(1, 1, 1); Vector3 t1 = new Vector3(1, 0, 0); Vector3 t2 = new Vector3(0, 1, 0); Vector3 t3 = new Vector3(0, 0, 1); Vector3 hitPoint = new Vector3(); Boolean result = Intersector.intersectRayTriangle(rayStart, rayEnd, t1, t2, t3, hitPoint); assertTrue(result); assertEquals("hitPoint.x", 1.0 / 3.0, hitPoint.x, 1e-14); assertEquals("hitPoint.y", 1.0 / 3.0, hitPoint.x, 1e-14); assertEquals("hitPoint.z", 1.0 / 3.0, hitPoint.x, 1e-14); }
public static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint) { Vector3 rayDir = Vector3.subtractAndCreate(rayEnd, rayStart); rayDir.normalize(); p.set(t1, t2, t3); if (!intersectRayPlane(rayStart, rayEnd, p, i)) return false; v0.subtractAndSet(t3, t1); v1.subtractAndSet(t2, t1); v2.subtractAndSet(i, t1); double dot00 = v0.dot(v0); double dot01 = v0.dot(v1); double dot02 = v0.dot(v2); double dot11 = v1.dot(v1); double dot12 = v1.dot(v2); double denom = dot00 * dot11 - dot01 * dot01; if (denom == 0) return false; double u = (dot11 * dot02 - dot01 * dot12) / denom; double v = (dot00 * dot12 - dot01 * dot02) / denom; if (u >= 0 && v >= 0 && u + v <= 1) { if (hitPoint != null) hitPoint.setAll(i); return true; } else return false; }
Intersector { public static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint) { Vector3 rayDir = Vector3.subtractAndCreate(rayEnd, rayStart); rayDir.normalize(); p.set(t1, t2, t3); if (!intersectRayPlane(rayStart, rayEnd, p, i)) return false; v0.subtractAndSet(t3, t1); v1.subtractAndSet(t2, t1); v2.subtractAndSet(i, t1); double dot00 = v0.dot(v0); double dot01 = v0.dot(v1); double dot02 = v0.dot(v2); double dot11 = v1.dot(v1); double dot12 = v1.dot(v2); double denom = dot00 * dot11 - dot01 * dot01; if (denom == 0) return false; double u = (dot11 * dot02 - dot01 * dot12) / denom; double v = (dot00 * dot12 - dot01 * dot02) / denom; if (u >= 0 && v >= 0 && u + v <= 1) { if (hitPoint != null) hitPoint.setAll(i); return true; } else return false; } }
Intersector { public static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint) { Vector3 rayDir = Vector3.subtractAndCreate(rayEnd, rayStart); rayDir.normalize(); p.set(t1, t2, t3); if (!intersectRayPlane(rayStart, rayEnd, p, i)) return false; v0.subtractAndSet(t3, t1); v1.subtractAndSet(t2, t1); v2.subtractAndSet(i, t1); double dot00 = v0.dot(v0); double dot01 = v0.dot(v1); double dot02 = v0.dot(v2); double dot11 = v1.dot(v1); double dot12 = v1.dot(v2); double denom = dot00 * dot11 - dot01 * dot01; if (denom == 0) return false; double u = (dot11 * dot02 - dot01 * dot12) / denom; double v = (dot00 * dot12 - dot01 * dot02) / denom; if (u >= 0 && v >= 0 && u + v <= 1) { if (hitPoint != null) hitPoint.setAll(i); return true; } else return false; }  }
Intersector { public static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint) { Vector3 rayDir = Vector3.subtractAndCreate(rayEnd, rayStart); rayDir.normalize(); p.set(t1, t2, t3); if (!intersectRayPlane(rayStart, rayEnd, p, i)) return false; v0.subtractAndSet(t3, t1); v1.subtractAndSet(t2, t1); v2.subtractAndSet(i, t1); double dot00 = v0.dot(v0); double dot01 = v0.dot(v1); double dot02 = v0.dot(v2); double dot11 = v1.dot(v1); double dot12 = v1.dot(v2); double denom = dot00 * dot11 - dot01 * dot01; if (denom == 0) return false; double u = (dot11 * dot02 - dot01 * dot12) / denom; double v = (dot00 * dot12 - dot01 * dot02) / denom; if (u >= 0 && v >= 0 && u + v <= 1) { if (hitPoint != null) hitPoint.setAll(i); return true; } else return false; }  static boolean intersectRayPlane(Vector3 rayStart, Vector3 rayEnd, Plane plane, Vector3 hitPoint); static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint); static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint); }
Intersector { public static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint) { Vector3 rayDir = Vector3.subtractAndCreate(rayEnd, rayStart); rayDir.normalize(); p.set(t1, t2, t3); if (!intersectRayPlane(rayStart, rayEnd, p, i)) return false; v0.subtractAndSet(t3, t1); v1.subtractAndSet(t2, t1); v2.subtractAndSet(i, t1); double dot00 = v0.dot(v0); double dot01 = v0.dot(v1); double dot02 = v0.dot(v2); double dot11 = v1.dot(v1); double dot12 = v1.dot(v2); double denom = dot00 * dot11 - dot01 * dot01; if (denom == 0) return false; double u = (dot11 * dot02 - dot01 * dot12) / denom; double v = (dot00 * dot12 - dot01 * dot02) / denom; if (u >= 0 && v >= 0 && u + v <= 1) { if (hitPoint != null) hitPoint.setAll(i); return true; } else return false; }  static boolean intersectRayPlane(Vector3 rayStart, Vector3 rayEnd, Plane plane, Vector3 hitPoint); static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint); static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint);  }