Example #1
0
        /// <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(LineSegment3d one, LineSegment3d other,
                                     out double t1, out double t2)
        {
            t1 = t2 = 0.0;

            // We solve 2x2 system and then check if it is ok for the third argument.
            Vector2d r = LinearSolver.SolveSystem(
                new Matrix2x2d(one.Direction.X, -other.Direction.X,
                               one.Direction.Y, -other.Direction.Y),
                new Vector2d(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.0 || r.X > 0.0)
            {
                return(false);
            }
            if (r.Y < 0.0 || r.Y > 0.0)
            {
                return(false);
            }

            // We check if the last line satisfies.
            if (!Vector3d.NearEqual(one.Sample(r.X), other.Sample(r.Y)))
            {
                return(false);
            }

            // We copy interpolation.
            t1 = r.X;
            t2 = r.Y;
            return(true);
        }
Example #2
0
        /// <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(LineSegment3d one, LineSegment3d other, out Vector3d result)
        {
            double t1, t2;

            if (!Intersect(one, other, out t1, out t2))
            {
                result = Vector3d.Zero;
                return(false);
            }
            else
            {
                result = one.Sample(t1);
                return(true);
            }
        }
Example #3
0
        //#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));
        }
Example #4
0
        //#endfor instanced to '2Df'

        //#foreach instanced to '3Dd'


        /// <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(LineSegment3d one, LineSegment3d other)
        {
            double t1, t2;

            return(Intersect(one, other, out t1, out t2));
        }