internal static bool AnyIntersections(PlanarChainGraph <double> graph)
        {
            if (graph == null)
            {
                return(false);
            }
            SegmentGroup <double>        activeEdges = new SegmentGroup <double>();
            IEnumerator <Node <double> > points      = graph.GetEnumerator(); //walk through the points in xy sorted order

            Node <double> nd;
            LeakyResizableArray <Edge <double> > localEdges;

            int           localCt;
            int           activeCt = 0;
            Edge <double> localEdge;
            LineIntersectionResult <double> intersects;
            Point2 <double> localStart;
            Point2 <double> localEnd;
            Point2 <double> activeStart;
            Point2 <double> activeEnd;

            //new point event in the moving front
            while (points.MoveNext())
            {
                nd         = points.Current;
                localEdges = nd.Edges; //edges connected to this point

                localCt = (int)localEdges.Count;

                //compute intersections with other edges in the scan area
                for (int i = 0; i < localCt; i++)
                {
                    localEdge  = localEdges.Data[i];
                    localStart = localEdge.Start.Point;
                    localEnd   = localEdge.End.Point;
                    activeCt   = activeEdges.Edges.Count;

                    foreach (Edge <double> activeEdge in activeEdges.Edges)
                    {
                        if (object.ReferenceEquals(localEdge, activeEdge))
                        {
                            continue; //can't have a "full" match -- this is an exiting segment
                        }
                        activeStart = activeEdge.Start.Point;
                        activeEnd   = activeEdge.End.Point;

                        intersects = Coordinate2Utils.GetIntersection(localStart.X, localStart.Y, localEnd.X, localEnd.Y, activeStart.X, activeStart.Y, activeEnd.X, activeEnd.Y);
                        if (intersects.IntersectionType != LineIntersectionType.NoIntersection)
                        {
                            if (object.ReferenceEquals(localEdge.Previous, activeEdge) || object.ReferenceEquals(localEdge.Next, activeEdge))
                            {
                                continue; // this is adjacent segments in a chain/ring
                            }
                            return(true);
                        }
                    }
                }
                //remove all exiting segments and add all starting segments
                //Action gets called exactly twice per edge -- once to add it, once to remove it
                for (int i = 0; i < localCt; i++)
                {
                    activeEdges.Action(localEdges.Data[i]);
                }
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// Computes whether a ring defined by an array of {@link Coordinate}s is oriented counter-clockwise.
        /// <ul><li>The list of points is assumed to have the first and last points equal.
        /// <li>This will handle coordinate lists which contain repeated points.</ul>
        /// This algorithm is <b>only</b> guaranteed to work with valid rings.
        /// If the ring is invalid (e.g. self-crosses or touches), the computed result may not be correct.
        /// </summary>
        /// <param name="ring"></param>
        /// <returns></returns>
        public static bool IsCCW(Point2 <double>[] ring)
        {
            if (ring.Length < 3)
            {
                return(false);
            }
            int nPts = ring.Length - 1;

            // find highest point
            Point2 <double> hiPt    = ring[0];
            int             hiIndex = 0;

            for (int i = 1; i < ring.Length; i++)
            {
                Point2 <double> p = ring[i];
                if (p.Y > hiPt.Y)
                {
                    hiPt    = p;
                    hiIndex = i;
                }
            }

            // find distinct point before highest point
            int iPrev = hiIndex;

            do
            {
                iPrev = iPrev - 1;
                if (iPrev < 0)
                {
                    iPrev = nPts;
                }
            } while (ring[iPrev].Equals(hiPt) && iPrev != hiIndex);

            // find distinct point after highest point
            int iNext = hiIndex;

            do
            {
                iNext = (iNext + 1) % nPts;
            } while (ring[iNext].Equals(hiPt) && iNext != hiIndex);

            Point2 <double> prev = ring[iPrev];
            Point2 <double> next = ring[iNext];

            // This check catches cases where the ring contains an A-B-A configuration of points.
            // This can happen if the ring does not contain 3 distinct points (including the case where the input array has fewer than 4 elements), or it contains coincident line segments.
            if (prev.Equals(hiPt) || next.Equals(hiPt) || prev.Equals(next))
            {
                return(false);
            }

            int disc = Coordinate2Utils.OrientationIndex(prev.X, prev.Y, hiPt.X, hiPt.Y, next.X, next.Y);

            // If disc is exactly 0, lines are collinear.  There are two possible cases:
            //  (1) the lines lie along the x axis in opposite directions (is handled by checking if next is left of prev ==> CCW)
            //  (2) the lines lie on top of one another (will never happen if the ring is valid, so don't check for it)
            bool isCCW = false;

            if (disc == 0) //poly is CCW if prev x is right of next x
            {
                isCCW = (prev.X > next.X);
            }
            else // if area is positive, points are ordered CCW
            {
                isCCW = (disc > 0);
            }
            return(isCCW);
        }
Example #3
0
        public static LineSegment2IntersectionResult <double> ComputeIntersection(Point2 <double> p1, Point2 <double> p2, Point2 <double> q1, Point2 <double> q2)
        {
            if (p1 == null || p2 == null || q1 == null || q2 == null)
            {
                return(null);
            }

            //quick rejection
            if (!Coordinate2Utils.EnvelopesIntersect(p1, p2, q1, q2))
            {
                return(new LineSegment2IntersectionResult <double>());
            }
            // for each endpoint, compute which side of the other segment it lies
            // if both endpoints lie on the same side of the other segment, the segments do not intersect
            int Pq1 = Coordinate2Utils.OrientationIndex(p1, p2, q1);
            int Pq2 = Coordinate2Utils.OrientationIndex(p1, p2, q2);

            if ((Pq1 > 0 && Pq2 > 0) || (Pq1 < 0 && Pq2 < 0))
            {
                return(new LineSegment2IntersectionResult <double>());
            }

            int Qp1 = Coordinate2Utils.OrientationIndex(q1, q2, p1);
            int Qp2 = Coordinate2Utils.OrientationIndex(q1, q2, p2);

            if ((Qp1 > 0 && Qp2 > 0) || (Qp1 < 0 && Qp2 < 0))
            {
                return(new LineSegment2IntersectionResult <double>());
            }
            //end quick rejection

            if (Pq1 == 0 && Pq2 == 0 && Qp1 == 0 && Qp2 == 0) //collinear intersection
            {
                bool p1q1p2 = Coordinate2Utils.PointInEnvelope(p1, p2, q1);
                bool p1q2p2 = Coordinate2Utils.PointInEnvelope(p1, p2, q2);
                bool q1p1q2 = Coordinate2Utils.PointInEnvelope(q1, q2, p1);
                bool q1p2q2 = Coordinate2Utils.PointInEnvelope(q1, q2, p2);

                if (p1q1p2 && p1q2p2)
                {
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q1, q2));
                }
                if (q1p1q2 && q1p2q2)
                {
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, p1, p2));
                }
                if (p1q1p2 && q1p1q2)
                {
                    if (q1.Equals(p1) && !p1q2p2 && !q1p2q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q1));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q1, p1));
                }
                if (p1q1p2 && q1p2q2)
                {
                    if (q1.Equals(p2) && !p1q2p2 && !q1p1q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q1));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q1, p2));
                }
                if (p1q2p2 && q1p1q2)
                {
                    if (q2.Equals(p1) && !p1q1p2 && !q1p2q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q2));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q2, p1));
                }
                if (p1q2p2 && q1p2q2)
                {
                    if (q2.Equals(p2) && !p1q1p2 && !q1p1q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q2));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q2, p2));
                }
                return(new LineSegment2IntersectionResult <double>());
            }//end collinear

            // At this point we know that there is a single intersection point (since the lines are not collinear).
            // Check if the intersection is an endpoint. If it is, copy the endpoint as the intersection point. Copying the point rather than computing it
            // ensures the point has the exact value, which is important for robustness. It is sufficient to simply check for an endpoint which is on
            // the other line, since at this point we know that the inputLines must intersect.
            if (Pq1 == 0 || Pq2 == 0 || Qp1 == 0 || Qp2 == 0)
            {
                // Check for two equal endpoints.  This is done explicitly rather than by the orientation tests below in order to improve robustness.
                if (p1.Equals(q1) || p1.Equals(q2))
                {
                    return(new LineSegment2IntersectionResult <double>(p1));
                }
                else if (p2.Equals(q1) || p2.Equals(q2))
                {
                    return(new LineSegment2IntersectionResult <double>(p2));
                }
                // Now check to see if any endpoint lies on the interior of the other segment.
                else if (Pq1 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(q1));
                }
                else if (Pq2 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(q2));
                }
                else if (Qp1 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(p1));
                }
                else if (Qp2 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(p2));
                }
            } //end exact at endpoint

            //intersectWNormalization
            Coordinate2 <double> n1     = new Coordinate2 <double>(p1);
            Coordinate2 <double> n2     = new Coordinate2 <double>(p2);
            Coordinate2 <double> n3     = new Coordinate2 <double>(q1);
            Coordinate2 <double> n4     = new Coordinate2 <double>(q2);
            Coordinate2 <double> normPt = new Coordinate2 <double>();

            Coordinate2Utils.NormalizeToEnvCentre(n1, n2, n3, n4, normPt);

            //safeHCoordinateIntersection
            Coordinate2 <double> intPt = null;
            // unrolled computation
            double px = n1.Y - n2.Y;
            double py = n2.X - n1.X;
            double pw = n1.X * n2.Y - n2.X * n1.Y;

            double qx = n3.Y - n4.Y;
            double qy = n4.X - n3.X;
            double qw = n3.X * n4.Y - n4.X * n3.Y;

            double x = py * qw - qy * pw;
            double y = qx * pw - px * qw;
            double w = px * qy - qx * py;

            double xInt = x / w;
            double yInt = y / w;

            if (double.IsNaN(xInt) || double.IsNaN(yInt) || double.IsInfinity(xInt) || double.IsInfinity(yInt))
            {
                intPt = MeanNearest(n1, n2, n3, n4);
                xInt  = intPt.X + normPt.X;
                yInt  = intPt.Y + normPt.Y;
                return(new LineSegment2IntersectionResult <double>(p1.Factory.ConstructPoint(xInt, yInt)));
            }
            else
            {
                xInt += normPt.X;
                yInt += normPt.Y;
                intPt = new Coordinate2 <double>(xInt, yInt);
            }
            //End safeHCoordinateIntersection
            //End intersectWNormalization

            if (!(Coordinate2Utils.PointInEnvelope(p1, p2, intPt) || Coordinate2Utils.PointInEnvelope(q1, q2, intPt)))
            {
                intPt = MeanNearest(p1, p2, q1, q2);
            }

            return(new LineSegment2IntersectionResult <double>(p1.Factory.ConstructPoint(intPt.X, intPt.Y)));
        }