public void Test_S2LatLngRect_CellOps() { // Contains(S2Cell), MayIntersect(S2Cell), Intersects(S2Cell) // Special cases. TestCellOps(S2LatLngRect.Empty, S2Cell.FromFacePosLevel(3, 0, 0), 0); TestCellOps(S2LatLngRect.Full, S2Cell.FromFacePosLevel(2, 0, 0), 4); TestCellOps(S2LatLngRect.Full, S2Cell.FromFacePosLevel(5, 0, 25), 4); // This rectangle includes the first quadrant of face 0. It's expanded // slightly because cell bounding rectangles are slightly conservative. S2LatLngRect r4 = RectFromDegrees(-45.1, -45.1, 0.1, 0.1); TestCellOps(r4, S2Cell.FromFacePosLevel(0, 0, 0), 3); TestCellOps(r4, S2Cell.FromFacePosLevel(0, 0, 1), 4); TestCellOps(r4, S2Cell.FromFacePosLevel(1, 0, 1), 0); // This rectangle intersects the first quadrant of face 0. S2LatLngRect r5 = RectFromDegrees(-10, -45, 10, 0); TestCellOps(r5, S2Cell.FromFacePosLevel(0, 0, 0), 3); TestCellOps(r5, S2Cell.FromFacePosLevel(0, 0, 1), 3); TestCellOps(r5, S2Cell.FromFacePosLevel(1, 0, 1), 0); // Rectangle consisting of a single point. TestCellOps(RectFromDegrees(4, 4, 4, 4), S2Cell.FromFace(0), 3); // Rectangles that intersect the bounding rectangle of a face // but not the face itself. TestCellOps(RectFromDegrees(41, -87, 42, -79), S2Cell.FromFace(2), 1); TestCellOps(RectFromDegrees(-41, 160, -40, -160), S2Cell.FromFace(5), 1); // This is the leaf cell at the top right hand corner of face 0. // It has two angles of 60 degrees and two of 120 degrees. S2Cell cell0tr = new(new S2Point(1 + 1e-12, 1, 1)); _ = cell0tr.GetRectBound(); S2LatLng v0 = new(cell0tr.VertexRaw(0)); TestCellOps(RectFromDegrees(v0.Lat().GetDegrees() - 1e-8, v0.Lng().GetDegrees() - 1e-8, v0.Lat().GetDegrees() - 2e-10, v0.Lng().GetDegrees() + 1e-10), cell0tr, 1); // Rectangles that intersect a face but where no vertex of one region // is contained by the other region. The first one passes through // a corner of one of the face cells. TestCellOps(RectFromDegrees(-37, -70, -36, -20), S2Cell.FromFace(5), 2); // These two intersect like a diamond and a square. S2Cell cell202 = S2Cell.FromFacePosLevel(2, 0, 2); S2LatLngRect bound202 = cell202.GetRectBound(); TestCellOps(RectFromDegrees(bound202.Lo().Lat().GetDegrees() + 3, bound202.Lo().Lng().GetDegrees() + 3, bound202.Hi().Lat().GetDegrees() - 3, bound202.Hi().Lng().GetDegrees() - 3), cell202, 2); }
public void Test_S2Cell_GetMaxDistanceToEdge() { // Test an edge for which its antipode crosses the cell. Validates both the // standard and brute force implementations for this case. S2Cell cell = S2Cell.FromFacePosLevel(0, 0, 20); S2Point a = -S2.Interpolate(2.0, cell.Center(), cell.Vertex(0)); S2Point b = -S2.Interpolate(2.0, cell.Center(), cell.Vertex(2)); S1ChordAngle actual = cell.MaxDistance(a, b); S1ChordAngle expected = GetMaxDistanceToEdgeBruteForce(cell, a, b); Assert2.Near(expected.Radians(), S1ChordAngle.Straight.Radians(), S2.DoubleError); Assert2.Near(actual.Radians(), S1ChordAngle.Straight.Radians(), S2.DoubleError); }
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 Test_S2RegionEncodeDecodeTest_S2Cell() { S2Cell cell_from_point = new(new S2Point(1, 2, 3)); S2Cell cell_from_latlng = new(S2LatLng.FromDegrees(39.0, -120.0)); S2Cell cell_from_face_pos_lvl = S2Cell.FromFacePosLevel(3, 0x12345678, S2.kMaxCellLevel - 4); S2Cell cell_from_from_face = S2Cell.FromFace(0); var cell = TestEncodeDecode(kEncodedCellFromPoint, cell_from_point); Assert.Equal(cell_from_point, cell); cell = TestEncodeDecode(kEncodedCellFromLatLng, cell_from_latlng); Assert.Equal(cell_from_latlng, cell); cell = TestEncodeDecode(kEncodedCellFromFacePosLevel, cell_from_face_pos_lvl); Assert.Equal(cell_from_face_pos_lvl, cell); cell = TestEncodeDecode(kEncodedCellFace0, cell_from_from_face); Assert.Equal(cell_from_from_face, cell); }
public void testCells() { // For each cube face, we construct some cells on // that face and some caps whose positions are relative to that face, // and then check for the expected intersection/containment results. // The distance from the center of a face to one of its vertices. var kFaceRadius = Math.Atan(S2.Sqrt2); for (var face = 0; face < 6; ++face) { // The cell consisting of the entire face. var rootCell = S2Cell.FromFacePosLevel(face, (byte)0, 0); // A leaf cell at the midpoint of the v=1 edge. var edgeCell = new S2Cell(S2Projections.FaceUvToXyz(face, 0, 1 - EPS)); // A leaf cell at the u=1, v=1 corner. var cornerCell = new S2Cell(S2Projections.FaceUvToXyz(face, 1 - EPS, 1 - EPS)); // Quick check for full and empty caps. Assert.True(S2Cap.Full.Contains(rootCell)); Assert.True(!S2Cap.Empty.MayIntersect(rootCell)); // Check intersections with the bounding caps of the leaf cells that are // adjacent to 'corner_cell' along the Hilbert curve. Because this corner // is at (u=1,v=1), the curve stays locally within the same cube face. var first = cornerCell.Id.Previous.Previous.Previous; var last = cornerCell.Id.Next.Next.Next.Next; for (var id = first; id < last; id = id.Next) { var cell = new S2Cell(id); JavaAssert.Equal(cell.CapBound.Contains(cornerCell), id.Equals(cornerCell.Id)); JavaAssert.Equal( cell.CapBound.MayIntersect(cornerCell), id.Parent.Contains(cornerCell.Id)); } var antiFace = (face + 3) % 6; // Opposite face. for (var capFace = 0; capFace < 6; ++capFace) { // A cap that barely contains all of 'cap_face'. var center = S2Projections.GetNorm(capFace); var covering = S2Cap.FromAxisAngle(center, S1Angle.FromRadians(kFaceRadius + EPS)); JavaAssert.Equal(covering.Contains(rootCell), capFace == face); JavaAssert.Equal(covering.MayIntersect(rootCell), capFace != antiFace); JavaAssert.Equal(covering.Contains(edgeCell), center.DotProd(edgeCell.Center) > 0.1); JavaAssert.Equal(covering.Contains(edgeCell), covering.MayIntersect(edgeCell)); JavaAssert.Equal(covering.Contains(cornerCell), capFace == face); JavaAssert.Equal( covering.MayIntersect(cornerCell), center.DotProd(cornerCell.Center) > 0); // A cap that barely intersects the edges of 'cap_face'. var bulging = S2Cap.FromAxisAngle(center, S1Angle.FromRadians(S2.PiOver4 + EPS)); Assert.True(!bulging.Contains(rootCell)); JavaAssert.Equal(bulging.MayIntersect(rootCell), capFace != antiFace); JavaAssert.Equal(bulging.Contains(edgeCell), capFace == face); JavaAssert.Equal(bulging.MayIntersect(edgeCell), center.DotProd(edgeCell.Center) > 0.1); Assert.True(!bulging.Contains(cornerCell)); Assert.True(!bulging.MayIntersect(cornerCell)); // A singleton cap. var singleton = S2Cap.FromAxisAngle(center, S1Angle.FromRadians(0)); JavaAssert.Equal(singleton.MayIntersect(rootCell), capFace == face); Assert.True(!singleton.MayIntersect(edgeCell)); Assert.True(!singleton.MayIntersect(cornerCell)); } } }
public void testBasic() { // Most of the S2LatLngRect methods have trivial implementations that // use the R1Interval and S1Interval classes, so most of the testing // is done in those unit tests. // Test basic properties of empty and full caps. var empty = S2LatLngRect.Empty; var full = S2LatLngRect.Full; assertTrue(empty.IsValid); assertTrue(empty.IsEmpty); assertTrue(full.IsValid); assertTrue(full.IsFull); // assertTrue various constructors and accessor methods. var d1 = rectFromDegrees(-90, 0, -45, 180); assertDoubleNear(d1.LatLo.Degrees, -90); assertDoubleNear(d1.LatHi.Degrees, -45); assertDoubleNear(d1.LngLo.Degrees, 0); assertDoubleNear(d1.LngHi.Degrees, 180); assertTrue(d1.Lat.Equals(new R1Interval(-S2.PiOver2, -S2.PiOver4))); assertTrue(d1.Lng.Equals(new S1Interval(0, S2.Pi))); // FromCenterSize() assertTrue( S2LatLngRect.FromCenterSize(S2LatLng.FromDegrees(80, 170), S2LatLng.FromDegrees(40, 60)) .ApproxEquals(rectFromDegrees(60, 140, 90, -160))); assertTrue(S2LatLngRect .FromCenterSize(S2LatLng.FromDegrees(10, 40), S2LatLng.FromDegrees(210, 400)).IsFull); assertTrue( S2LatLngRect.FromCenterSize(S2LatLng.FromDegrees(-90, 180), S2LatLng.FromDegrees(20, 50)) .ApproxEquals(rectFromDegrees(-90, 155, -80, -155))); // FromPoint(), FromPointPair() assertEquals(S2LatLngRect.FromPoint(d1.Lo), new S2LatLngRect(d1.Lo, d1.Lo)); assertEquals( S2LatLngRect.FromPointPair(S2LatLng.FromDegrees(-35, -140), S2LatLng.FromDegrees(15, 155)), rectFromDegrees(-35, 155, 15, -140)); assertEquals( S2LatLngRect.FromPointPair(S2LatLng.FromDegrees(25, -70), S2LatLng.FromDegrees(-90, 80)), rectFromDegrees(-90, -70, 25, 80)); // GetCenter(), GetVertex(), Contains(S2LatLng), InteriorContains(S2LatLng). var eqM180 = S2LatLng.FromRadians(0, -S2.Pi); var northPole = S2LatLng.FromRadians(S2.PiOver2, 0); var r1 = new S2LatLngRect(eqM180, northPole); assertEquals(r1.Center, S2LatLng.FromRadians(S2.PiOver4, -S2.PiOver2)); assertEquals(r1.GetVertex(0), S2LatLng.FromRadians(0, S2.Pi)); assertEquals(r1.GetVertex(1), S2LatLng.FromRadians(0, 0)); assertEquals(r1.GetVertex(2), S2LatLng.FromRadians(S2.PiOver2, 0)); assertEquals(r1.GetVertex(3), S2LatLng.FromRadians(S2.PiOver2, S2.Pi)); assertTrue(r1.Contains(S2LatLng.FromDegrees(30, -45))); assertTrue(!r1.Contains(S2LatLng.FromDegrees(30, 45))); assertTrue(!r1.InteriorContains(eqM180) && !r1.InteriorContains(northPole)); assertTrue(r1.Contains(new S2Point(0.5, -0.3, 0.1))); assertTrue(!r1.Contains(new S2Point(0.5, 0.2, 0.1))); // Make sure that GetVertex() returns vertices in CCW order. for (var i = 0; i < 4; ++i) { var lat = S2.PiOver4 * (i - 2); var lng = S2.PiOver2 * (i - 2) + 0.2; var r = new S2LatLngRect(new R1Interval(lat, lat + S2.PiOver4), new S1Interval( Math.IEEERemainder(lng, 2 * S2.Pi), Math.IEEERemainder(lng + S2.PiOver2, 2 * S2.Pi))); for (var k = 0; k < 4; ++k) { assertTrue( S2.SimpleCcw(r.GetVertex((k - 1) & 3).ToPoint(), r.GetVertex(k).ToPoint(), r.GetVertex((k + 1) & 3).ToPoint())); } } // Contains(S2LatLngRect), InteriorContains(S2LatLngRect), // Intersects(), InteriorIntersects(), Union(), Intersection(). // // Much more testing of these methods is done in s1interval_unittest // and r1interval_unittest. var r1Mid = rectFromDegrees(45, -90, 45, -90); var reqM180 = new S2LatLngRect(eqM180, eqM180); var rNorthPole = new S2LatLngRect(northPole, northPole); testIntervalOps(r1, r1Mid, "TTTT", r1, r1Mid); testIntervalOps(r1, reqM180, "TFTF", r1, reqM180); testIntervalOps(r1, rNorthPole, "TFTF", r1, rNorthPole); assertTrue(r1.Equals(rectFromDegrees(0, -180, 90, 0))); testIntervalOps(r1, rectFromDegrees(-10, -1, 1, 20), "FFTT", rectFromDegrees(-10, -180, 90, 20), rectFromDegrees(0, -1, 1, 0)); testIntervalOps(r1, rectFromDegrees(-10, -1, 0, 20), "FFTF", rectFromDegrees(-10, -180, 90, 20), rectFromDegrees(0, -1, 0, 0)); testIntervalOps(r1, rectFromDegrees(-10, 0, 1, 20), "FFTF", rectFromDegrees(-10, -180, 90, 20), rectFromDegrees(0, 0, 1, 0)); testIntervalOps(rectFromDegrees(-15, -160, -15, -150), rectFromDegrees(20, 145, 25, 155), "FFFF", rectFromDegrees(-15, 145, 25, -150), empty); testIntervalOps(rectFromDegrees(70, -10, 90, -140), rectFromDegrees(60, 175, 80, 5), "FFTT", rectFromDegrees(60, -180, 90, 180), rectFromDegrees(70, 175, 80, 5)); // assertTrue that the intersection of two rectangles that overlap in // latitude // but not longitude is valid, and vice versa. testIntervalOps(rectFromDegrees(12, 30, 60, 60), rectFromDegrees(0, 0, 30, 18), "FFFF", rectFromDegrees(0, 0, 60, 60), empty); testIntervalOps(rectFromDegrees(0, 0, 18, 42), rectFromDegrees(30, 12, 42, 60), "FFFF", rectFromDegrees(0, 0, 42, 60), empty); // AddPoint() var p = S2LatLngRect.Empty; p = p.AddPoint(S2LatLng.FromDegrees(0, 0)); p = p.AddPoint(S2LatLng.FromRadians(0, -S2.PiOver2)); p = p.AddPoint(S2LatLng.FromRadians(S2.PiOver4, -S2.Pi)); p = p.AddPoint(new S2Point(0, 0, 1)); assertTrue(p.Equals(r1)); // Expanded() assertTrue( rectFromDegrees(70, 150, 80, 170).Expanded(S2LatLng.FromDegrees(20, 30)).ApproxEquals( rectFromDegrees(50, 120, 90, -160))); assertTrue(S2LatLngRect.Empty.Expanded(S2LatLng.FromDegrees(20, 30)).IsEmpty); assertTrue(S2LatLngRect.Full.Expanded(S2LatLng.FromDegrees(20, 30)).IsFull); assertTrue( rectFromDegrees(-90, 170, 10, 20).Expanded(S2LatLng.FromDegrees(30, 80)).ApproxEquals( rectFromDegrees(-90, -180, 40, 180))); // ConvolveWithCap() var llr1 = new S2LatLngRect(S2LatLng.FromDegrees(0, 170), S2LatLng.FromDegrees(0, -170)) .ConvolveWithCap(S1Angle.FromDegrees(15)); var llr2 = new S2LatLngRect(S2LatLng.FromDegrees(-15, 155), S2LatLng.FromDegrees(15, -155)); assertTrue(llr1.ApproxEquals(llr2)); llr1 = new S2LatLngRect(S2LatLng.FromDegrees(60, 150), S2LatLng.FromDegrees(80, 10)) .ConvolveWithCap(S1Angle.FromDegrees(15)); llr2 = new S2LatLngRect(S2LatLng.FromDegrees(45, -180), S2LatLng.FromDegrees(90, 180)); assertTrue(llr1.ApproxEquals(llr2)); // GetCapBound(), bounding cap at center is smaller: assertTrue(new S2LatLngRect(S2LatLng.FromDegrees(-45, -45), S2LatLng.FromDegrees(45, 45)).CapBound.ApproxEquals(S2Cap.FromAxisHeight(new S2Point(1, 0, 0), 0.5))); // GetCapBound(), bounding cap at north pole is smaller: assertTrue(new S2LatLngRect(S2LatLng.FromDegrees(88, -80), S2LatLng.FromDegrees(89, 80)).CapBound.ApproxEquals(S2Cap.FromAxisAngle(new S2Point(0, 0, 1), S1Angle.FromDegrees(2)))); // GetCapBound(), longitude span > 180 degrees: assertTrue( new S2LatLngRect(S2LatLng.FromDegrees(-30, -150), S2LatLng.FromDegrees(-10, 50)).CapBound .ApproxEquals(S2Cap.FromAxisAngle(new S2Point(0, 0, -1), S1Angle.FromDegrees(80)))); // Contains(S2Cell), MayIntersect(S2Cell), Intersects(S2Cell) // Special cases. testCellOps(empty, S2Cell.FromFacePosLevel(3, (byte)0, 0), 0); testCellOps(full, S2Cell.FromFacePosLevel(2, (byte)0, 0), 4); testCellOps(full, S2Cell.FromFacePosLevel(5, (byte)0, 25), 4); // This rectangle includes the first quadrant of face 0. It's expanded // slightly because cell bounding rectangles are slightly conservative. var r4 = rectFromDegrees(-45.1, -45.1, 0.1, 0.1); testCellOps(r4, S2Cell.FromFacePosLevel(0, (byte)0, 0), 3); testCellOps(r4, S2Cell.FromFacePosLevel(0, (byte)0, 1), 4); testCellOps(r4, S2Cell.FromFacePosLevel(1, (byte)0, 1), 0); // This rectangle intersects the first quadrant of face 0. var r5 = rectFromDegrees(-10, -45, 10, 0); testCellOps(r5, S2Cell.FromFacePosLevel(0, (byte)0, 0), 3); testCellOps(r5, S2Cell.FromFacePosLevel(0, (byte)0, 1), 3); testCellOps(r5, S2Cell.FromFacePosLevel(1, (byte)0, 1), 0); // Rectangle consisting of a single point. testCellOps(rectFromDegrees(4, 4, 4, 4), S2Cell.FromFacePosLevel(0, (byte)0, 0), 3); // Rectangles that intersect the bounding rectangle of a face // but not the face itself. testCellOps(rectFromDegrees(41, -87, 42, -79), S2Cell.FromFacePosLevel(2, (byte)0, 0), 1); testCellOps(rectFromDegrees(-41, 160, -40, -160), S2Cell.FromFacePosLevel(5, (byte)0, 0), 1); { // This is the leaf cell at the top right hand corner of face 0. // It has two angles of 60 degrees and two of 120 degrees. var cell0tr = new S2Cell(new S2Point(1 + 1e-12, 1, 1)); var bound0tr = cell0tr.RectBound; var v0 = new S2LatLng(cell0tr.GetVertexRaw(0)); testCellOps( rectFromDegrees(v0.Lat.Degrees - 1e-8, v0.Lng.Degrees - 1e-8, v0.Lat.Degrees - 2e-10, v0.Lng.Degrees + 1e-10), cell0tr, 1); } // Rectangles that intersect a face but where no vertex of one region // is contained by the other region. The first one passes through // a corner of one of the face cells. testCellOps(rectFromDegrees(-37, -70, -36, -20), S2Cell.FromFacePosLevel(5, (byte)0, 0), 2); { // These two intersect like a diamond and a square. var cell202 = S2Cell.FromFacePosLevel(2, (byte)0, 2); var bound202 = cell202.RectBound; testCellOps( rectFromDegrees(bound202.Lo.Lat.Degrees + 3, bound202.Lo.Lng.Degrees + 3, bound202.Hi.Lat.Degrees - 3, bound202.Hi.Lng.Degrees - 3), cell202, 2); } }
public void testSubdivide() { for (var face = 0; face < 6; ++face) { testSubdivide(S2Cell.FromFacePosLevel(face, (byte)0, 0)); } // The maximum edge *ratio* is the ratio of the longest edge of any cell to // the shortest edge of any cell at the same level (and similarly for the // maximum diagonal ratio). // // The maximum edge *aspect* is the maximum ratio of the longest edge of a // cell to the shortest edge of that same cell (and similarly for the // maximum diagonal aspect). Console .WriteLine("Level Area Edge Diag Approx Average\n"); Console .WriteLine(" Ratio Ratio Aspect Ratio Aspect Min Max Min Max\n"); for (var i = 0; i <= S2CellId.MaxLevel; ++i) { var s = levelStats[i]; if (s.count > 0) { s.avgArea /= s.count; s.avgWidth /= s.count; s.avgEdge /= s.count; s.avgDiag /= s.count; s.avgAngleSpan /= s.count; } Console.WriteLine( "%5d %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f\n", i, s.maxArea / s.minArea, s.maxEdge / s.minEdge, s.maxEdgeAspect, s.maxDiag / s.minDiag, s.maxDiagAspect, s.minApproxRatio, s.maxApproxRatio, S2Cell.AverageArea(i) / s.maxArea, S2Cell .AverageArea(i) / s.minArea); } // Now check the validity of the S2 length and area metrics. for (var i = 0; i <= S2CellId.MaxLevel; ++i) { var s = levelStats[i]; if (s.count == 0) { continue; } Console.WriteLine( "Level {0} - metric (error/actual : error/tolerance)\n", i); // The various length calculations are only accurate to 1e-15 or so, // so we need to allow for this amount of discrepancy with the theoretical // minimums and maximums. The area calculation is accurate to about 1e-15 // times the cell width. testMinMaxAvg("area", i, s.count, 1e-15 * s.minWidth, s.minArea, s.maxArea, s.avgArea, S2Projections.MinArea, S2Projections.MaxArea, S2Projections.AvgArea); testMinMaxAvg("width", i, s.count, 1e-15, s.minWidth, s.maxWidth, s.avgWidth, S2Projections.MinWidth, S2Projections.MaxWidth, S2Projections.AvgWidth); testMinMaxAvg("edge", i, s.count, 1e-15, s.minEdge, s.maxEdge, s.avgEdge, S2Projections.MinEdge, S2Projections.MaxEdge, S2Projections.AvgEdge); testMinMaxAvg("diagonal", i, s.count, 1e-15, s.minDiag, s.maxDiag, s.avgDiag, S2Projections.MinDiag, S2Projections.MaxDiag, S2Projections.AvgDiag); testMinMaxAvg("angle span", i, s.count, 1e-15, s.minAngleSpan, s.maxAngleSpan, s.avgAngleSpan, S2Projections.MinAngleSpan, S2Projections.MaxAngleSpan, S2Projections.AvgAngleSpan); // The aspect ratio calculations are ratios of lengths and are therefore // less accurate at higher subdivision levels. Assert.True(s.maxEdgeAspect <= S2Projections.MaxEdgeAspect + 1e-15 * (1 << i)); Assert.True(s.maxDiagAspect <= S2Projections.MaxDiagAspect + 1e-15 * (1 << i)); } }