public void testGetLengthCentroid()
        {
            // Construct random great circles and divide them randomly into segments.
            // Then make sure that the length and centroid are correct. Note that
            // because of the way the centroid is computed, it does not matter how
            // we split the great circle into segments.

            for (var i = 0; i < 100; ++i)
            {
                // Choose a coordinate frame for the great circle.
                var x = randomPoint();
                var y = S2Point.Normalize(S2Point.CrossProd(x, randomPoint()));
                var z = S2Point.Normalize(S2Point.CrossProd(x, y));

                var vertices = new List <S2Point>();
                for (double theta = 0; theta < 2 * S2.Pi; theta += Math.Pow(rand.NextDouble(), 10))
                {
                    var p = (x * Math.Cos(theta)) + (y * Math.Sin(theta));
                    if (vertices.Count == 0 || !p.Equals(vertices[vertices.Count - 1]))
                    {
                        vertices.Add(p);
                    }
                }
                // Close the circle.
                vertices.Add(vertices[0]);
                var line   = new S2Polyline(vertices);
                var length = line.ArcLengthAngle;
                assertTrue(Math.Abs(length.Radians - 2 * S2.Pi) < 2e-14);
            }
        }
        public void testBasic()
        {
            var vertices = new List <S2Point>();
            var empty    = new S2Polyline(vertices);

            assertEquals(empty.RectBound, S2LatLngRect.Empty);
        }
        public void testEqualsAndHashCode()
        {
            var vertices = new List <S2Point>();

            vertices.Add(new S2Point(1, 0, 0));
            vertices.Add(new S2Point(0, 1, 0));
            vertices.Add(S2Point.Normalize(new S2Point(0, 1, 1)));
            vertices.Add(new S2Point(0, 0, 1));


            var line1 = new S2Polyline(vertices);
            var line2 = new S2Polyline(vertices);

            checkEqualsAndHashCodeMethods(line1, line2, true);

            var moreVertices = new List <S2Point>(vertices);

            moreVertices.RemoveAt(0);

            var line3 = new S2Polyline(moreVertices);

            checkEqualsAndHashCodeMethods(line1, line3, false);
            checkEqualsAndHashCodeMethods(line1, null, false);
            checkEqualsAndHashCodeMethods(line1, "", false);
        }
Exemple #4
0
 public static string ToDebugString(this S2Polyline polyline)
 {
     if (polyline.NumVertices() > 0)
     {
         return(AppendVertices(polyline.VerticesClone(), polyline.NumVertices()));
     }
     return("");
 }
Exemple #5
0
 // Add a polyline to the input geometry.
 public void AddPolyline(S2Polyline polyline)
 {
     bound_ = bound_.Union(polyline.GetRectBound());
     for (int i = 0; i < polyline.NumVertices(); ++i)
     {
         points_.Add(polyline.Vertex(i));
     }
 }
Exemple #6
0
    public void Test_MakePolyline_ValidInput()
    {
        Assert.True(MakePolyline("-20:150, -20:151, -19:150", out var polyline));
        var expected = new S2Polyline(new[] {
            S2LatLng.FromDegrees(-20, 150).ToPoint(),
            S2LatLng.FromDegrees(-20, 151).ToPoint(),
            S2LatLng.FromDegrees(-19, 150).ToPoint(),
        });

        Assert.True(polyline == expected);
    }
Exemple #7
0
    // As above, but does not Debug.Assert-fail on invalid input. Returns true if
    // conversion is successful.
    public static bool MakePolyline(string str, out S2Polyline?polyline, S2Debug override_ = S2Debug.ALLOW)
    {
        polyline = null;
        var vertices = new List <S2Point>();

        if (!ParsePoints(str, vertices))
        {
            return(false);
        }
        polyline = new S2Polyline(vertices.ToArray(), override_);
        return(true);
    }
        public void testMayIntersect()
        {
            var vertices = new List <S2Point>();

            vertices.Add(S2Point.Normalize(new S2Point(1, -1.1, 0.8)));
            vertices.Add(S2Point.Normalize(new S2Point(1, -0.8, 1.1)));
            var line = new S2Polyline(vertices);

            for (var face = 0; face < 6; ++face)
            {
                var cell = S2Cell.FromFacePosLevel(face, (byte)0, 0);
                assertEquals(line.MayIntersect(cell), (face & 1) == 0);
            }
        }
Exemple #9
0
 private static void AddPolylineWithLabels(S2Polyline polyline, EdgeType edge_type,
                                           Int32 label_begin, S2Builder builder, EdgeLabelMap edge_label_map)
 {
     for (int i = 0; i + 1 < polyline.NumVertices(); ++i)
     {
         Int32 label = label_begin + i;
         builder.SetLabel(label);
         // With undirected edges, reverse the direction of every other input edge.
         int dir = edge_type == EdgeType.DIRECTED ? 1 : (i & 1);
         builder.AddEdge(polyline.Vertex(i + (1 - dir)), polyline.Vertex(i + dir));
         S2Point key = polyline.Vertex(i) + polyline.Vertex(i + 1);
         edge_label_map[key].Add(label);
     }
 }
        public void Test_IndexedS2PolylineLayer_AddsShape()
        {
            S2Builder           builder = new(new Options());
            MutableS2ShapeIndex index   = new();

            builder.StartLayer(new IndexedS2PolylineLayer(index));
            string polyline_str = "0:0, 0:10";

            builder.AddPolyline(MakePolylineOrDie(polyline_str));
            Assert.True(builder.Build(out _));
            Assert.Equal(1, index.NumShapeIds());
            S2Polyline polyline = ((S2Polyline.Shape)
                                   index.Shape(0)).Polyline;

            Assert.Equal(polyline_str, polyline.ToDebugString());
        }
        public void testInterpolate()
        {
            var vertices = new List <S2Point>();

            vertices.Add(new S2Point(1, 0, 0));
            vertices.Add(new S2Point(0, 1, 0));
            vertices.Add(S2Point.Normalize(new S2Point(0, 1, 1)));
            vertices.Add(new S2Point(0, 0, 1));
            var line = new S2Polyline(vertices);

            assertEquals(line.Interpolate(-0.1), vertices[0]);
            assertTrue(S2.ApproxEquals(
                           line.Interpolate(0.1), S2Point.Normalize(new S2Point(1, Math.Tan(0.2 * S2.Pi / 2), 0))));
            assertTrue(S2.ApproxEquals(line.Interpolate(0.25), S2Point.Normalize(new S2Point(1, 1, 0))));

            assertTrue(S2.ApproxEquals(line.Interpolate(0.5), vertices[1]));
            assertTrue(S2.ApproxEquals(line.Interpolate(0.75), vertices[2]));
            assertTrue(S2.ApproxEquals(line.Interpolate(1.1), vertices[3]));
        }
        public void testProject()
        {
            var latLngs = new List <S2Point>();

            latLngs.Add(S2LatLng.FromDegrees(0, 0).ToPoint());
            latLngs.Add(S2LatLng.FromDegrees(0, 1).ToPoint());
            latLngs.Add(S2LatLng.FromDegrees(0, 2).ToPoint());
            latLngs.Add(S2LatLng.FromDegrees(1, 2).ToPoint());
            var line = new S2Polyline(latLngs);

            var     edgeIndex = -1;
            S2Point testPoint = default(S2Point);

            testPoint = S2LatLng.FromDegrees(0.5, -0.5).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                           line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 0).ToPoint()));
            assertEquals(0, edgeIndex);

            testPoint = S2LatLng.FromDegrees(0.5, 0.5).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                           line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 0.5).ToPoint()));
            assertEquals(0, edgeIndex);

            testPoint = S2LatLng.FromDegrees(0.5, 1).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                           line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 1).ToPoint()));
            assertEquals(0, edgeIndex);

            testPoint = S2LatLng.FromDegrees(-0.5, 2.5).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                           line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 2).ToPoint()));
            assertEquals(1, edgeIndex);

            testPoint = S2LatLng.FromDegrees(2, 2).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                           line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(1, 2).ToPoint()));
            assertEquals(2, edgeIndex);
        }
Exemple #13
0
        public void Test_S2RegionEncodeDecodeTest_S2Polyline()
        {
            var vertices       = Array.Empty <S2Point>();
            var polyline_empty = new S2Polyline(vertices);
            var polyline_semi  = new S2Polyline(new[]
            {
                S2LatLng.FromDegrees(0, 0),
                S2LatLng.FromDegrees(0, 90),
                S2LatLng.FromDegrees(0, 180),
            });
            var polyline_3segments = MakePolylineOrDie("0:0, 0:10, 10:20, 20:30");

            var polyline = TestEncodeDecode(kEncodedPolylineEmpty, polyline_empty);

            Assert.True(polyline_empty == polyline);
            polyline = TestEncodeDecode(kEncodedPolylineSemiEquator, polyline_semi);
            Assert.True(polyline_semi == polyline);
            polyline = TestEncodeDecode(kEncodedPolyline3Segments, polyline_3segments);
            Assert.True(polyline_3segments == polyline);
        }
        public void testEqualsAndHashCode()
        {
            var vertices = new List<S2Point>();
            vertices.Add(new S2Point(1, 0, 0));
            vertices.Add(new S2Point(0, 1, 0));
            vertices.Add(S2Point.Normalize(new S2Point(0, 1, 1)));
            vertices.Add(new S2Point(0, 0, 1));


            var line1 = new S2Polyline(vertices);
            var line2 = new S2Polyline(vertices);

            checkEqualsAndHashCodeMethods(line1, line2, true);

            var moreVertices = new List<S2Point>(vertices);
            moreVertices.RemoveAt(0);

            var line3 = new S2Polyline(moreVertices);

            checkEqualsAndHashCodeMethods(line1, line3, false);
            checkEqualsAndHashCodeMethods(line1, null, false);
            checkEqualsAndHashCodeMethods(line1, "", false);
        }
        public void testGetLengthCentroid()
        {
            // Construct random great circles and divide them randomly into segments.
            // Then make sure that the length and centroid are correct. Note that
            // because of the way the centroid is computed, it does not matter how
            // we split the great circle into segments.

            for (var i = 0; i < 100; ++i)
            {
                // Choose a coordinate frame for the great circle.
                var x = randomPoint();
                var y = S2Point.Normalize(S2Point.CrossProd(x, randomPoint()));
                var z = S2Point.Normalize(S2Point.CrossProd(x, y));

                var vertices = new List<S2Point>();
                for (double theta = 0; theta < 2*S2.Pi; theta += Math.Pow(rand.NextDouble(), 10))
                {
                    var p = (x * Math.Cos(theta)) + (y * Math.Sin(theta));
                    if (vertices.Count == 0 || !p.Equals(vertices[vertices.Count - 1]))
                    {
                        vertices.Add(p);
                    }
                }
                // Close the circle.
                vertices.Add(vertices[0]);
                var line = new S2Polyline(vertices);
                var length = line.ArcLengthAngle;
                assertTrue(Math.Abs(length.Radians - 2*S2.Pi) < 2e-14);
            }
        }
 public void testBasic()
 {
     var vertices = new List<S2Point>();
     var empty = new S2Polyline(vertices);
     assertEquals(empty.RectBound, S2LatLngRect.Empty);
 }
        public void testProject()
        {
            var latLngs = new List<S2Point>();
            latLngs.Add(S2LatLng.FromDegrees(0, 0).ToPoint());
            latLngs.Add(S2LatLng.FromDegrees(0, 1).ToPoint());
            latLngs.Add(S2LatLng.FromDegrees(0, 2).ToPoint());
            latLngs.Add(S2LatLng.FromDegrees(1, 2).ToPoint());
            var line = new S2Polyline(latLngs);

            var edgeIndex = -1;
            S2Point testPoint = default(S2Point);

            testPoint = S2LatLng.FromDegrees(0.5, -0.5).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 0).ToPoint()));
            assertEquals(0, edgeIndex);

            testPoint = S2LatLng.FromDegrees(0.5, 0.5).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 0.5).ToPoint()));
            assertEquals(0, edgeIndex);

            testPoint = S2LatLng.FromDegrees(0.5, 1).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 1).ToPoint()));
            assertEquals(0, edgeIndex);

            testPoint = S2LatLng.FromDegrees(-0.5, 2.5).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(0, 2).ToPoint()));
            assertEquals(1, edgeIndex);

            testPoint = S2LatLng.FromDegrees(2, 2).ToPoint();
            edgeIndex = line.GetNearestEdgeIndex(testPoint);
            assertTrue(S2.ApproxEquals(
                line.ProjectToEdge(testPoint, edgeIndex), S2LatLng.FromDegrees(1, 2).ToPoint()));
            assertEquals(2, edgeIndex);
        }
 public void testMayIntersect()
 {
     var vertices = new List<S2Point>();
     vertices.Add(S2Point.Normalize(new S2Point(1, -1.1, 0.8)));
     vertices.Add(S2Point.Normalize(new S2Point(1, -0.8, 1.1)));
     var line = new S2Polyline(vertices);
     for (var face = 0; face < 6; ++face)
     {
         var cell = S2Cell.FromFacePosLevel(face, (byte)0, 0);
         assertEquals(line.MayIntersect(cell), (face & 1) == 0);
     }
 }
        public void testInterpolate()
        {
            var vertices = new List<S2Point>();
            vertices.Add(new S2Point(1, 0, 0));
            vertices.Add(new S2Point(0, 1, 0));
            vertices.Add(S2Point.Normalize(new S2Point(0, 1, 1)));
            vertices.Add(new S2Point(0, 0, 1));
            var line = new S2Polyline(vertices);

            assertEquals(line.Interpolate(-0.1), vertices[0]);
            assertTrue(S2.ApproxEquals(
                line.Interpolate(0.1), S2Point.Normalize(new S2Point(1, Math.Tan(0.2*S2.Pi/2), 0))));
            assertTrue(S2.ApproxEquals(line.Interpolate(0.25), S2Point.Normalize(new S2Point(1, 1, 0))));

            assertTrue(S2.ApproxEquals(line.Interpolate(0.5), vertices[1]));
            assertTrue(S2.ApproxEquals(line.Interpolate(0.75), vertices[2]));
            assertTrue(S2.ApproxEquals(line.Interpolate(1.1), vertices[3]));
        }
Exemple #20
0
    public void Test_EmptyPolyline()
    {
        var polyline = new S2Polyline();

        Assert.Equal("", polyline.ToDebugString());
    }