/// <summary> /// Tests for intersection between a <see cref="Ray"/> and a <see cref="Triangle"/>. /// </summary> /// <param name="ray">A <see cref="Ray"/> instance.</param> /// <param name="triangle">A <see cref="Triangle"/> instance.</param> /// <returns>An <see cref="IntersectionPair"/> instance containing the intersection information.</returns> /// <remarks> /// For information about the algorithm visit http://www.acm.org/jgt/papers/MollerTrumbore97/ /// </remarks> public static IntersectionPair Intersects(Ray ray, Triangle triangle) { // Find the vectors for the 2 edges sharing trangle.Point0 Vector3F edge1 = triangle.Point1 - triangle.Point0; Vector3F edge2 = triangle.Point2 - triangle.Point0; // Begin calculating determinant - also used to calc the U parameter. Vector3F pvec = Vector3F.CrossProduct(ray.Direction, edge2); float det = Vector3F.Dot(edge1, pvec); // If determinant is zero the ray lies in plane of the triangle if (MathFunctions.ApproxEquals(det, 0.0f)) { return new IntersectionPair(false, Vector3F.Zero); } float invDet = 1.0f / det; // Calculate the distance from triangle.Point0 to the ray origin Vector3F tvec = ray.Origin - triangle.Point0; // Calculate U parameter and test bounds float u = Vector3F.Dot(tvec, pvec) * invDet; if ((u < 0.0f) || u > 1.0f) return new IntersectionPair(false, Vector3F.Zero); // Prepare to test the V parameter Vector3F qvec = Vector3F.CrossProduct(tvec, edge1); // Calculate V parameter and test bounds float v = Vector3F.Dot(ray.Direction, qvec) * invDet; if ((v < 0.0f) || v > 1.0f) return new IntersectionPair(false, Vector3F.Zero); // The ray intersects the triangle // Calculate the distance from the ray origin to the intersection point. //t = Vector3F.Dot(edge2, qvec) * invDet; return new IntersectionPair(true, triangle.FromBarycentric(u,v)); }
/// <summary> /// Initializes a new instance of the <see cref="Triangle"/> class using a given Triangle. /// </summary> /// <param name="t">A <see cref="Triangle"/> instance.</param> public Triangle(Triangle t) { _p0 = t._p0; _p1 = t._p1; _p2 = t._p2; }