예제 #1
0
 public void TestEndpointIntersection()
 {
     i.ComputeIntersection(new Coordinate(100, 100), new Coordinate(10, 100),
                           new Coordinate(100, 10), new Coordinate(100, 100));
     Assert.IsTrue(i.HasIntersection);
     Assert.AreEqual(1, i.IntersectionNum);
 }
예제 #2
0
        public static Geometry SegmentIntersection(Geometry g1, Geometry g2)
        {
            var pt1 = g1.Coordinates;
            var pt2 = g2.Coordinates;
            var ri  = new RobustLineIntersector();

            ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
            switch (ri.IntersectionNum)
            {
            case 0:
                // no intersection => return empty point
                return(g1.Factory.CreatePoint((Coordinate)null));

            case 1:
                // return point
                return(g1.Factory.CreatePoint(ri.GetIntersection(0)));

            case 2:
                // return line
                return(g1.Factory.CreateLineString(
                           new Coordinate[] {
                    ri.GetIntersection(0),
                    ri.GetIntersection(1)
                }));
            }
            return(null);
        }
예제 #3
0
        public static bool SegmentIntersects(Geometry g1, Geometry g2)
        {
            var pt1 = g1.Coordinates;
            var pt2 = g2.Coordinates;
            var ri  = new RobustLineIntersector();

            ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
            return(ri.HasIntersection);
        }
예제 #4
0
        /// <summary>
        /// Computes an intersection point between two segments, if there is one.
        /// There may be 0, 1 or many intersection points between two segments.
        /// If there are 0, null is returned. If there is 1 or more, a single one
        /// is returned (chosen at the discretion of the algorithm).  If
        /// more information is required about the details of the intersection,
        /// the {RobustLineIntersector} class should be used.
        /// </summary>
        /// <param name="line"></param>
        /// <returns> An intersection point, or <c>null</c> if there is none.</returns>
        public virtual Coordinate Intersection(ILineSegmentBase line)
        {
            LineIntersector li = new RobustLineIntersector();

            li.ComputeIntersection(P0, P1, new Coordinate(line.P0), new Coordinate(line.P1));
            if (li.HasIntersection)
            {
                return(li.GetIntersection(0));
            }
            return(null);
        }
예제 #5
0
        /// <summary>
        /// Computes an intersection point between two segments, if there is one.
        /// There may be 0, 1 or many intersection points between two segments.
        /// If there are 0, null is returned. If there is 1 or more, a single one
        /// is returned (chosen at the discretion of the algorithm).  If
        /// more information is required about the details of the intersection,
        /// the {RobustLineIntersector} class should be used.
        /// </summary>
        /// <param name="line">A line segment</param>
        /// <returns> An intersection point, or <c>null</c> if there is none.</returns>
        /// <see cref="RobustLineIntersector"/>
        public Coordinate Intersection(LineSegment line)
        {
            LineIntersector li = new RobustLineIntersector();

            li.ComputeIntersection(_p0, _p1, line._p0, line._p1);
            if (li.HasIntersection)
            {
                return(li.GetIntersection(0));
            }
            return(null);
        }
예제 #6
0
        public void TestCollinear4()
        {
            var i  = new RobustLineIntersector();
            var p1 = new Coordinate(30, 10);
            var p2 = new Coordinate(20, 10);
            var q1 = new Coordinate(10, 10);
            var q2 = new Coordinate(30, 10);

            i.ComputeIntersection(p1, p2, q1, q2);
            Assert.AreEqual(LineIntersector.CollinearIntersection, i.IntersectionNum);
            Assert.IsTrue(i.HasIntersection);
        }
        private void CheckIntersectionDir(LineSegment line1, LineSegment line2, Coordinate pt)
        {
            LineIntersector li = new RobustLineIntersector();

            li.ComputeIntersection(
                line1.P0, line1.P1,
                line2.P0, line2.P1);
            Assert.That(li.IntersectionNum, Is.EqualTo(1));
            var actual = li.GetIntersection(0);

            CheckEqualXYZ(pt, actual);
        }
예제 #8
0
        public void TestCollinear3()
        {
            RobustLineIntersector i  = new RobustLineIntersector();
            Coordinate            p1 = new Coordinate(10, 10);
            Coordinate            p2 = new Coordinate(20, 10);
            Coordinate            q1 = new Coordinate(15, 10);
            Coordinate            q2 = new Coordinate(30, 10);

            i.ComputeIntersection(p1, p2, q1, q2);
            Assert.AreEqual(RobustLineIntersector.Collinear, i.IntersectionNum);
            Assert.IsTrue(!i.IsProper);
            Assert.IsTrue(i.HasIntersection);
        }
예제 #9
0
        public void Test2Lines()
        {
            var i  = new RobustLineIntersector();
            var p1 = new Coordinate(10, 10);
            var p2 = new Coordinate(20, 20);
            var q1 = new Coordinate(20, 10);
            var q2 = new Coordinate(10, 20);
            var x  = new Coordinate(15, 15);

            i.ComputeIntersection(p1, p2, q1, q2);
            Assert.AreEqual(LineIntersector.PointIntersection, i.IntersectionNum);
            Assert.AreEqual(1, i.IntersectionNum);
            Assert.AreEqual(x, i.GetIntersection(0));
            Assert.IsTrue(i.IsProper);
            Assert.IsTrue(i.HasIntersection);
        }
예제 #10
0
        public void Test2Lines()
        {
            RobustLineIntersector i  = new RobustLineIntersector();
            Coordinate            p1 = new Coordinate(10, 10);
            Coordinate            p2 = new Coordinate(20, 20);
            Coordinate            q1 = new Coordinate(20, 10);
            Coordinate            q2 = new Coordinate(10, 20);
            Coordinate            x  = new Coordinate(15, 15);

            i.ComputeIntersection(p1, p2, q1, q2);
            Assert.AreEqual(RobustLineIntersector.DoIntersect, i.IntersectionNum);
            Assert.AreEqual(1, i.IntersectionNum);
            Assert.AreEqual(x, i.GetIntersection(0));
            Assert.IsTrue(i.IsProper);
            Assert.IsTrue(i.HasIntersection);
        }
예제 #11
0
        /// <summary>
        /// Computes an intersection point between two segments, if there is one.
        /// </summary>
        /// <param name="">line
        /// </param>
        /// <returns> an intersection point, or null if there is none
        /// </returns>
        /// <remarks>
        /// There may be 0, 1 or many intersection points between two segments.
        /// If there are 0, null is returned. If there is 1 or more, a single one
        /// is returned (chosen at the discretion of the algorithm).  If
        /// more information is required about the details of the intersection,
        /// the <see cref="RobustLineIntersector"/> class should be used.
        /// </remarks>
        public Coordinate Intersection(LineSegment line)
        {
            if (line == null)
            {
                throw new ArgumentNullException("line");
            }

            LineIntersector li = new RobustLineIntersector();

            li.ComputeIntersection(p0, p1, line.p0, line.p1);
            if (li.HasIntersection)
            {
                return(li.GetIntersection(0));
            }

            return(null);
        }
예제 #12
0
        public static Geometry SegmentIntersectionDd(Geometry g1, Geometry g2)
        {
            var pt1 = g1.Coordinates;
            var pt2 = g2.Coordinates;

            // first check if there actually is an intersection
            var ri = new RobustLineIntersector();

            ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
            if (!ri.HasIntersection)
            {
                // no intersection => return empty point
                return(g1.Factory.CreatePoint((Coordinate)null));
            }

            var intPt = CGAlgorithmsDD.Intersection(pt1[0], pt1[1], pt2[0], pt2[1]);

            return(g1.Factory.CreatePoint(intPt));
        }
        public void CheckInputNotAltered(Coordinate[] pt, int scaleFactor)
        {
            // save input points
            Coordinate[] savePt = new Coordinate[4];
            for (int i = 0; i < 4; i++)
            {
                savePt[i] = new Coordinate(pt[i]);
            }

            LineIntersector li = new RobustLineIntersector();

            li.PrecisionModel = new PrecisionModel(scaleFactor);
            li.ComputeIntersection(pt[0], pt[1], pt[2], pt[3]);

            // check that input points are unchanged
            for (int i = 0; i < 4; i++)
            {
                Assert.AreEqual(savePt[i], pt[i], "Input point " + i + " was altered - ");
            }
        }
        /// <summary>
        /// Check that intersection of segment defined by points in pt array
        /// is equal to the expectedIntPt value (up to the given distanceTolerance)
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="expectedIntersectionNum"></param>
        /// <param name="expectedIntPt">the expected intersection points (maybe null if not tested)</param>
        /// <param name="distanceTolerance">tolerance to use for equality test</param>
        private void CheckIntersection(Coordinate[] pt,
                                       int expectedIntersectionNum,
                                       Coordinate[] expectedIntPt,
                                       double distanceTolerance)
        {
            LineIntersector li = new RobustLineIntersector();

            li.ComputeIntersection(pt[0], pt[1], pt[2], pt[3]);

            int intNum = li.IntersectionNum;

            Assert.AreEqual(expectedIntersectionNum, intNum, "Number of intersections not as expected");

            if (expectedIntPt != null)
            {
                Assert.AreEqual(intNum, expectedIntPt.Length, "Wrong number of expected int pts provided");
                // test that both points are represented here
                if (intNum == 1)
                {
                    CheckIntPoints(expectedIntPt[0], li.GetIntersection(0), distanceTolerance);
                }
                else if (intNum == 2)
                {
                    CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);
                    CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);

                    if (!(Equals(expectedIntPt[0], li.GetIntersection(0), distanceTolerance) ||
                          Equals(expectedIntPt[0], li.GetIntersection(1), distanceTolerance)))
                    {
                        CheckIntPoints(expectedIntPt[0], li.GetIntersection(0), distanceTolerance);
                        CheckIntPoints(expectedIntPt[0], li.GetIntersection(1), distanceTolerance);
                    }
                    else if (!(Equals(expectedIntPt[1], li.GetIntersection(0), distanceTolerance) ||
                               Equals(expectedIntPt[1], li.GetIntersection(1), distanceTolerance)))
                    {
                        CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);
                        CheckIntPoints(expectedIntPt[1], li.GetIntersection(1), distanceTolerance);
                    }
                }
            }
        }
        private void CheckIntersectionDir(LineSegment line1, LineSegment line2, Coordinate p1, Coordinate p2)
        {
            LineIntersector li = new RobustLineIntersector();

            li.ComputeIntersection(
                line1.P0, line1.P1,
                line2.P0, line2.P1);

            Assert.That(li.IntersectionNum, Is.EqualTo(2));

            var actual1 = li.GetIntersection(0);
            var actual2 = li.GetIntersection(1);

            // normalize actual results
            if (actual1.CompareTo(actual2) > 0)
            {
                actual1 = li.GetIntersection(1);
                actual2 = li.GetIntersection(0);
            }

            CheckEqualXYZ(p1, actual1);
            CheckEqualXYZ(p2, actual2);
        }
예제 #16
0
        private void AddIntersections()
        {
            var seqA = GetVertices(_geomA);
            var seqB = GetVertices(_geomB);

            // NTS-specific: these arrays were only written to, never read from.
            ////bool[] isIntersected0 = new bool[seqA.Count - 1];
            ////bool[] isIntersected1 = new bool[seqB.Count - 1];

            bool isCCWA = Orientation.IsCCW(seqA);
            bool isCCWB = Orientation.IsCCW(seqB);

            // Compute rays for all intersections
            var li = new RobustLineIntersector();

            for (int i = 0; i < seqA.Count - 1; i++)
            {
                var a0 = seqA.GetCoordinate(i);
                var a1 = seqA.GetCoordinate(i + 1);

                if (isCCWA)
                {
                    // flip segment orientation
                    (a0, a1) = (a1, a0);
                }

                for (int j = 0; j < seqB.Count - 1; j++)
                {
                    var b0 = seqB.GetCoordinate(j);
                    var b1 = seqB.GetCoordinate(j + 1);

                    if (isCCWB)
                    {
                        // flip segment orientation
                        (b0, b1) = (b1, b0);
                    }

                    li.ComputeIntersection(a0, a1, b0, b1);
                    if (li.HasIntersection)
                    {
                        // NTS-specific: these arrays were only written to, never read from.
                        ////isIntersected0[i] = true;
                        ////isIntersected1[j] = true;

                        /*
                         * With both rings oriented CW (effectively)
                         * There are two situations for segment intersections:
                         *
                         * 1) A entering B, B exiting A => rays are IP-A1:R, IP-B0:L
                         * 2) A exiting B, B entering A => rays are IP-A0:L, IP-B1:R
                         * (where :L/R indicates result is to the Left or Right).
                         *
                         * Use full edge to compute direction, for accuracy.
                         */
                        var intPt = li.GetIntersection(0);

                        bool isAenteringB = OrientationIndex.CounterClockwise == Orientation.Index(a0, a1, b1);

                        if (isAenteringB)
                        {
                            _area += EdgeRay.AreaTerm(intPt, a0, a1, true);
                            _area += EdgeRay.AreaTerm(intPt, b1, b0, false);
                        }
                        else
                        {
                            _area += EdgeRay.AreaTerm(intPt, a1, a0, false);
                            _area += EdgeRay.AreaTerm(intPt, b0, b1, true);
                        }
                    }
                }
            }
        }