Пример #1
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 (segment.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;

            IntrLine2Triangle2.TriangleLineRelations(segment.Center, segment.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;
                IntrLine2Triangle2.GetInterval(segment.Center, segment.Direction, triangle, dist, sign, ref param);

                Intersector1 intr = new Intersector1(param[0], param[1], -segment.Extent, +segment.Extent);
                intr.Find();

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

            Result = (Type != IntersectionType.Empty) ?
                     IntersectionResult.Intersects : IntersectionResult.NoIntersection;
            return(Result == IntersectionResult.Intersects);
        }
Пример #2
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 (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;
                Intersector1 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);
        }