public void Test_S2CellUnion_FromBeginEnd() { // Since FromMinMax() is implemented in terms of FromBeginEnd(), we // focus on test cases that generate an empty range. S2CellId initial_id = S2CellId.FromFace(3); // Test an empty range before the minimum S2CellId. S2CellUnion cell_union = new(new List <S2CellId> { initial_id }); S2CellId id_begin = S2CellId.Begin(S2.kMaxCellLevel); cell_union.InitFromBeginEnd(id_begin, id_begin); Assert.True(cell_union.IsEmpty()); // Test an empty range after the maximum S2CellId. cell_union = new S2CellUnion(new List <S2CellId> { initial_id }); S2CellId id_end = S2CellId.End(S2.kMaxCellLevel); cell_union.InitFromBeginEnd(id_end, id_end); Assert.True(cell_union.IsEmpty()); // Test the full sphere. cell_union = S2CellUnion.FromBeginEnd(id_begin, id_end); Assert.Equal(6, cell_union.Size()); foreach (S2CellId id in cell_union) { Assert.True(id.IsFace()); } }
public void Test_S2CellUnion_S2CellIdConstructor() { var face1_id = S2CellId.FromFace(1); var face1_union = new S2CellUnion(new List <S2CellId> { face1_id }); Assert.Equal(1, face1_union.Size()); Assert.Equal(face1_id, face1_union.CellId(0)); }
public void Test_S2ShapeIndexBufferedRegion_PointZeroRadius() { // Test that buffering a point using a zero radius produces a non-empty // covering. (This requires using "less than or equal to" distance tests.) var index = MakeIndexOrDie("34:25 # #"); var region = new S2ShapeIndexBufferedRegion(index, S1ChordAngle.Zero); var coverer = new S2RegionCoverer(); S2CellUnion covering = coverer.GetCovering(region); Assert.Equal(1, covering.Size()); foreach (S2CellId id in covering) { Assert.True(id.IsLeaf()); } }
public void Test_S2ShapeIndexBufferedRegion_FullPolygon() { // Test buffering an S2ShapeIndex that contains a full polygon. var index = MakeIndexOrDie("# # full"); var radius = new S1ChordAngle(S1Angle.FromDegrees(2)); var region = new S2ShapeIndexBufferedRegion(index, radius); var coverer = new S2RegionCoverer(); S2CellUnion covering = coverer.GetCovering(region); Assert.Equal(6, covering.Size()); foreach (S2CellId id in covering) { Assert.True(id.IsFace()); } }
public void Test_S2ShapeIndexBufferedRegion_FullAfterBuffering() { // Test a region that becomes the full polygon after buffering. var index = MakeIndexOrDie("0:0 | 0:90 | 0:180 | 0:-90 | 90:0 | -90:0 # #"); var radius = new S1ChordAngle(S1Angle.FromDegrees(60)); var region = new S2ShapeIndexBufferedRegion(index, radius); var coverer = new S2RegionCoverer(); coverer.Options_.MaxCells = 1000; S2CellUnion covering = coverer.GetCovering(region); Assert.Equal(6, covering.Size()); foreach (S2CellId id in covering) { Assert.True(id.IsFace()); } }
public void Test_S2CellUnion_Normalize() { // Try a bunch of random test cases, and keep track of average // statistics for normalization (to see if they agree with the // analysis above). double in_sum = 0, out_sum = 0; const int kIters = 2000; for (int i = 0; i < kIters; ++i) { var input = new List <S2CellId>(); var expected = new List <S2CellId>(); AddCells(S2CellId.None, false, input, expected); in_sum += input.Count; out_sum += expected.Count; var cellunion = new S2CellUnion(input); Assert.Equal(expected.Count, cellunion.Size()); for (var j = 0; j < expected.Count; ++j) { Assert.Equal(expected[j], cellunion.CellId(j)); } // Test GetCapBound(). var cap = cellunion.GetCapBound(); foreach (var id in cellunion) { Assert.True(cap.Contains(new S2Cell(id))); } // Test Contains(S2CellId) and Intersects(S2CellId). foreach (var input_id in input) { Assert.True(cellunion.Contains(input_id)); Assert.True(cellunion.Contains(input_id.ToPoint())); Assert.True(cellunion.Intersects(input_id)); if (!input_id.IsFace()) { Assert.True(cellunion.Intersects(input_id.Parent())); if (input_id.Level() > 1) { Assert.True(cellunion.Intersects(input_id.Parent().Parent())); Assert.True(cellunion.Intersects(input_id.Parent(0))); } } if (!input_id.IsLeaf()) { Assert.True(cellunion.Contains(input_id.ChildBegin())); Assert.True(cellunion.Intersects(input_id.ChildBegin())); Assert.True(cellunion.Contains(input_id.ChildEnd().Prev())); Assert.True(cellunion.Intersects(input_id.ChildEnd().Prev())); Assert.True(cellunion.Contains(input_id.ChildBegin(S2.kMaxCellLevel))); Assert.True(cellunion.Intersects(input_id.ChildBegin(S2.kMaxCellLevel))); } } foreach (var expected_id in expected) { if (!expected_id.IsFace()) { Assert.True(!cellunion.Contains(expected_id.Parent())); Assert.True(!cellunion.Contains(expected_id.Parent(0))); } } // Test Contains(S2CellUnion), Intersects(S2CellUnion), Union(), // Intersection(), and Difference(). var x = new List <S2CellId>(); var y = new List <S2CellId>(); var x_or_y = new List <S2CellId>(); var x_and_y = new List <S2CellId>(); foreach (var input_id in input) { var in_x = rnd.OneIn(2); var in_y = rnd.OneIn(2); if (in_x) { x.Add(input_id); } if (in_y) { y.Add(input_id); } if (in_x || in_y) { x_or_y.Add(input_id); } } var xcells = new S2CellUnion(x); var ycells = new S2CellUnion(y); var x_or_y_expected = new S2CellUnion(x_or_y); var x_or_y_cells = xcells.Union(ycells); Assert.True(x_or_y_cells == x_or_y_expected); // Compute the intersection of "x" with each cell of "y", // check that this intersection is correct, and append the // results to x_and_y_expected. foreach (var yid in ycells) { var ucells = xcells.Intersection(yid); foreach (var xid in xcells) { if (xid.Contains(yid)) { Assert.True(ucells.Size() == 1 && ucells.CellId(0) == yid); } else if (yid.Contains(xid)) { Assert.True(ucells.Contains(xid)); } } foreach (var uid in ucells) { Assert.True(xcells.Contains(uid)); Assert.True(yid.Contains(uid)); } x_and_y.AddRange(ucells); } var x_and_y_expected = new S2CellUnion(x_and_y); var x_and_y_cells = xcells.Intersection(ycells); Assert.True(x_and_y_cells == x_and_y_expected); var x_minus_y_cells = xcells.Difference(ycells); var y_minus_x_cells = ycells.Difference(xcells); Assert.True(xcells.Contains(x_minus_y_cells)); Assert.True(!x_minus_y_cells.Intersects(ycells)); Assert.True(ycells.Contains(y_minus_x_cells)); Assert.True(!y_minus_x_cells.Intersects(xcells)); Assert.True(!x_minus_y_cells.Intersects(y_minus_x_cells)); var diff_intersection_union = x_minus_y_cells.Union(y_minus_x_cells).Union(x_and_y_cells); Assert.True(diff_intersection_union == x_or_y_cells); var test = new List <S2CellId>(); var dummy = new List <S2CellId>(); AddCells(S2CellId.None, false, test, dummy); foreach (var test_id in test) { var contains = false; var intersects = false; foreach (var expected_id in expected) { if (expected_id.Contains(test_id)) { contains = true; } if (expected_id.Intersects(test_id)) { intersects = true; } } Assert.Equal(contains, cellunion.Contains(test_id)); Assert.Equal(intersects, cellunion.Intersects(test_id)); } } _logger.WriteLine($"avg in {in_sum / kIters:2f}, avg out {out_sum / kIters:2f}"); }