예제 #1
0
        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 (line.Direction.IsNormalized == false || segment.Direction.IsNormalized == false)
            {
                Type   = IntersectionType.Empty;
                Result = IntersectionResult.InvalidQuery;
                return(false);
            }

            Vector2d s = Vector2d.Zero;

            Type = IntrLine2Line2.Classify(line.Origin, line.Direction,
                                           segment.Center, segment.Direction,
                                           dotThresh, ref s);

            if (Type == IntersectionType.Point)
            {
                // Test whether the line-line intersection is on the segment.
                if (Math.Abs(s[1]) <= segment.Extent + intervalThresh)
                {
                    Quantity  = 1;
                    Point     = line.Origin + s[0] * line.Direction;
                    Parameter = s[0];
                }
                else
                {
                    Quantity = 0;
                    Type     = IntersectionType.Empty;
                }
            }
            else if (Type == IntersectionType.Line)
            {
                Type     = IntersectionType.Segment;
                Quantity = int.MaxValue;
            }
            else
            {
                Quantity = 0;
            }

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

            return(Result == IntersectionResult.Intersects);
        }
        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);
        }