/// <summary> /// Intersection test. /// </summary> /// <remarks>Test does not handle parallel lines.</remarks> /// <param name="one">The first line segment.</param> /// <param name="other">The second line segment.</param> /// <param name="t1">The interpolation for first line segment, only if intersects is true.</param> /// <param name="t2">The interpolation for second line segment, only if intersects is true.</param> /// <returns>Do lines intersect.</returns> public static bool Intersect(LineSegment3f one, LineSegment3f other, out float t1, out float t2) { t1 = t2 = 0.0f; // We solve 2x2 system and then check if it is ok for the third argument. Vector2f r = LinearSolver.SolveSystem( new Matrix2x2f(one.Direction.X, -other.Direction.X, one.Direction.Y, -other.Direction.Y), new Vector2f(other.A.X - one.A.X, other.A.Y - one.A.Y)); // If system has the solution, it must be in range [0,1]. if (r.X < 0.0f || r.X > 0.0f) { return(false); } if (r.Y < 0.0f || r.Y > 0.0f) { return(false); } // We check if the last line satisfies. if (!Vector3f.NearEqual(one.Sample(r.X), other.Sample(r.Y))) { return(false); } // We copy interpolation. t1 = r.X; t2 = r.Y; return(true); }
/// <summary> /// Intersection test. /// </summary> /// <remarks>Test does not handle parallel lines.</remarks> /// <param name="one">The first line segment.</param> /// <param name="other">The second line segment.</param> /// <param name="result">The resulting intersection point, only if it returns true.</param> /// <returns>Do lines intersect.</returns> public static bool Intersect(LineSegment3f one, LineSegment3f other, out Vector3f result) { float t1, t2; if (!Intersect(one, other, out t1, out t2)) { result = Vector3f.Zero; return(false); } else { result = one.Sample(t1); return(true); } }
//#endif //#endfor instanced to '3Dd' //#foreach instanced to '3Df' //#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(LineSegment3f line, Triangle3f triangle, out Vector3f point) { // We compute distance of ray of plane. float d = -((line.A - triangle.A) * triangle.Normal) / (line.Direction * triangle.Normal); // We exit quickly if intersection does not occur on line. if (d < 0.0f || d > 1.0f) { point = Vector3f.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. Vector2f uv = triangle.GetBarycentric(point); // Is it inside. return(Triangle3f.IsBaryCentricInside(uv)); }
//#endfor instanced to '3Dd' //#foreach instanced to '3Df' /// <summary> /// Pure intersection test. /// </summary> /// <remarks>Test does not handle parallel lines.</remarks> /// <param name="one">The first line segment.</param> /// <param name="other">The second line segment.</param> /// <returns>Do lines intersect.</returns> public static bool Intersect(LineSegment3f one, LineSegment3f other) { float t1, t2; return(Intersect(one, other, out t1, out t2)); }