//#endif //#endfor instanced to '3Df' #endregion #region Triangle-Triangle Intersection #endregion #region Triangle-Ray Intersection //#foreach instanced to '2Dd' //#endfor instanced to '2Dd' //#foreach instanced to '2Df' //#endfor instanced to '2Df' //#foreach instanced to '3Dd' //#ifdef 3D /// <summary> /// Intersect triangle and ray, fast barycentric coordinate version. /// </summary> /// <param name="t">Triangle.</param> /// <param name="Normal">Possibly precomputed normal.</param> /// <param name="r">The ray.</param> /// <param name="p">The output collision, valid if true is returned.</param> /// <returns>Intersection result</returns> public static bool Intersect(Triangle3d t, Ray3d r, double maxDist, out Vector3d p) { Vector3d Normal = t.Normal; // We compute distance of ray of plane. double d = -((r.Origin - t.A) * Normal) / (r.Direction * Normal); // We exit quickly if intersection occurs behind ray, or if intersection does not // exist. if (d < 0.0 || d >= maxDist || double.IsNaN(d)) { p = Vector3d.Zero; return(false); } // We now compute the actual point on the plane. p = r.Origin + d * r.Direction; // We have to determine if point lies inside triangle. We do it using barycentric // coordinates. We solve the system with two unknowns. Vector2d uv = t.GetBarycentric(p); // Is it inside. if (Triangle3d.IsBaryCentricInside(uv)) { return(true); } return(false); }
//#endif //#endfor instanced to '3Df' #endregion #region Triangle-Line Intersection //#foreach instanced to '3Dd' //#ifdef 3D /// <summary> /// Triangle-Line intersection helper (only 3D version). /// </summary> /// <param name="t">The triangle.</param> /// <param name="r">The line.</param> /// <param name="p">Point of intersection.</param> /// <returns>Does intersection exist.</returns> private static bool Intersect(Triangle3d t, Line3d r, out Vector3d p) { Vector3d Normal = t.Normal; // We compute distance of line of plane. double d = -((r.A - t.A) * Normal) / (r.Direction * Normal); // We now compute the actual point on the plane. p = r.A + d * r.Direction; // We have to determine if point lies inside triangle. We do it using barycentric // coordinates. We solve the system with two unknowns. Vector2d uv = t.GetBarycentric(p); // Is it inside. return(Triangle3d.IsBaryCentricInside(uv)); }
//#endfor instanced to '3Df' #endregion #region LineSeg-Triangle Intersection //#foreach instanced to '3Dd' //#ifdef 3D /// <summary> /// A line-triangle intersection. /// </summary> /// <remarks>Only point is returned. In case of line segment lying on top of triangle, one /// point is only returned.</remarks> /// <param name="line">The line segment</param> /// <param name="triangle">The triangle</param> /// <param name="point">The intersection point.</param> /// <returns>Does intersection occur</returns> private static bool Intersect(LineSegment3d line, Triangle3d triangle, out Vector3d point) { // We compute distance of ray of plane. double d = -((line.A - triangle.A) * triangle.Normal) / (line.Direction * triangle.Normal); // We exit quickly if intersection does not occur on line. if (d < 0.0 || d > 1.0) { point = Vector3d.Zero; return(false); } // We now compute the actual point on the plane. point = line.A + d * line.Direction; // We have to determine if point lies inside triangle. We do it using barycentric // coordinates. We solve the system with two unknowns. Vector2d uv = triangle.GetBarycentric(point); // Is it inside. return(Triangle3d.IsBaryCentricInside(uv)); }