public bool Find()
        {
            if (Result != IntersectionResult.NotComputed)
            {
                return(Result == IntersectionResult.Intersects);
            }

            // [RMS] if either segment direction is not a normalized vector,
            //   results are garbage, so fail query
            if (segment1.Direction.IsNormalized == false || segment2.Direction.IsNormalized == false)
            {
                Type   = IntersectionType.Empty;
                Result = IntersectionResult.InvalidQuery;
                return(false);
            }


            Vector2d s = Vector2d.Zero;

            Type = IntrLine2Line2.Classify(segment1.Center, segment1.Direction,
                                           segment2.Center, segment2.Direction,
                                           dotThresh, ref s);

            if (Type == IntersectionType.Point)
            {
                // Test whether the line-line intersection is on the segments.
                if (Math.Abs(s[0]) <= segment1.Extent + intervalThresh &&
                    Math.Abs(s[1]) <= segment2.Extent + intervalThresh)
                {
                    Quantity   = 1;
                    Point0     = segment1.Center + s[0] * segment1.Direction;
                    Parameter0 = s[0];
                }
                else
                {
                    Quantity = 0;
                    Type     = IntersectionType.Empty;
                }
            }
            else if (Type == IntersectionType.Line)
            {
                // Compute the location of segment1 endpoints relative to segment0.
                Vector2d diff = segment2.Center - segment1.Center;
                double   t1   = segment1.Direction.Dot(diff);
                double   tmin = t1 - segment2.Extent;
                double   tmax = t1 + segment2.Extent;
                var      calc = new Intersector1(-segment1.Extent, segment1.Extent, tmin, tmax);
                calc.Find();
                Quantity = calc.NumIntersections;
                if (Quantity == 2)
                {
                    Type       = IntersectionType.Segment;
                    Parameter0 = calc.GetIntersection(0);
                    Point0     = segment1.Center +
                                 Parameter0 * segment1.Direction;
                    Parameter1 = calc.GetIntersection(1);
                    Point1     = segment1.Center +
                                 Parameter1 * segment1.Direction;
                }
                else if (Quantity == 1)
                {
                    Type       = IntersectionType.Point;
                    Parameter0 = calc.GetIntersection(0);
                    Point0     = segment1.Center +
                                 Parameter0 * segment1.Direction;
                }
                else
                {
                    Type = IntersectionType.Empty;
                }
            }
            else
            {
                Quantity = 0;
            }

            Result = (Type != IntersectionType.Empty) ?
                     IntersectionResult.Intersects : IntersectionResult.NoIntersection;

            // [RMS] for debugging...
            //sanity_check();

            return(Result == IntersectionResult.Intersects);
        }
Пример #2
0
        public bool Find()
        {
            if (Result != IntersectionResult.NotComputed)
            {
                return(Result == IntersectionResult.Intersects);
            }

            // [RMS] if either line direction is not a normalized vector,
            //   results are garbage, so fail query
            if (line.Direction.IsNormalized == false)
            {
                Type   = IntersectionType.Empty;
                Result = IntersectionResult.InvalidQuery;
                return(false);
            }

            Vector3d dist = Vector3d.Zero;
            Vector3i sign = Vector3i.Zero;
            int      positive = 0, negative = 0, zero = 0;

            TriangleLineRelations(line.Origin, line.Direction, triangle,
                                  ref dist, ref sign, ref positive, ref negative, ref zero);

            if (positive == 3 || negative == 3)
            {
                // No intersections.
                Quantity = 0;
                Type     = IntersectionType.Empty;
            }
            else
            {
                Vector2d param = Vector2d.Zero;
                GetInterval(line.Origin, line.Direction, triangle, dist, sign, ref param);

                var intr = new Intersector1(param[0], param[1], -double.MaxValue, +double.MaxValue);
                intr.Find();

                Quantity = intr.NumIntersections;
                if (Quantity == 2)
                {
                    // Segment intersection.
                    Type   = IntersectionType.Segment;
                    Param0 = intr.GetIntersection(0);
                    Point0 = line.Origin + Param0 * line.Direction;
                    Param1 = intr.GetIntersection(1);
                    Point1 = line.Origin + Param1 * line.Direction;
                }
                else if (Quantity == 1)
                {
                    // Point intersection.
                    Type   = IntersectionType.Point;
                    Param0 = intr.GetIntersection(0);
                    Point0 = line.Origin + Param0 * line.Direction;
                }
                else
                {
                    // No intersections.
                    Type = IntersectionType.Empty;
                }
            }

            Result = (Type != IntersectionType.Empty) ?
                     IntersectionResult.Intersects : IntersectionResult.NoIntersection;
            return(Result == IntersectionResult.Intersects);
        }