Example #1
0
        private static void TestIntervalOps(R2Rect x, R2Rect y, string expected_rexion,
                                            R2Rect expected_union, R2Rect expected_intersection)
        {
            // Test all of the interval operations on the given pair of intervals.
            // "expected_rexion" is a sequence of "T" and "F" characters corresponding
            // to the expected results of Contains(), InteriorContains(), Intersects(),
            // and InteriorIntersects() respectively.

            Assert.Equal(expected_rexion[0] == 'T', x.Contains(y));
            Assert.Equal(expected_rexion[1] == 'T', x.InteriorContains(y));
            Assert.Equal(expected_rexion[2] == 'T', x.Intersects(y));
            Assert.Equal(expected_rexion[3] == 'T', x.InteriorIntersects(y));

            Assert.Equal(x.Union(y) == x, x.Contains(y));
            Assert.Equal(!x.Intersection(y).IsEmpty(), x.Intersects(y));

            Assert.Equal(expected_union, x.Union(y));
            Assert.Equal(expected_intersection, x.Intersection(y));

            R2Rect r = x;

            r = r.AddRect(y);
            Assert.Equal(expected_union, r);
            if (y.GetSize() == new R2Point(0, 0))
            {
                r = x;
                r = r.AddPoint(y.Lo());
                Assert.Equal(expected_union, r);
            }
        }
Example #2
0
        public void Test_S2PaddedCell_ShrinkToFit()
        {
            const int kIters = 1000;

            for (int iter = 0; iter < kIters; ++iter)
            {
                // Start with the desired result and work backwards.
                S2CellId result    = S2Testing.GetRandomCellId();
                R2Rect   result_uv = result.BoundUV();
                R2Point  size_uv   = result_uv.GetSize();

                // Find the biggest rectangle that fits in "result" after padding.
                // (These calculations ignore numerical errors.)
                double max_padding = 0.5 * Math.Min(size_uv[0], size_uv[1]);
                double padding     = max_padding * S2Testing.Random.RandDouble();
                R2Rect max_rect    = result_uv.Expanded(-padding);

                // Start with a random subset of the maximum rectangle.
                R2Point a = new(SampleInterval(max_rect[0]), SampleInterval(max_rect[1]));
                R2Point b = new(SampleInterval(max_rect[0]), SampleInterval(max_rect[1]));
                if (!result.IsLeaf())
                {
                    // If the result is not a leaf cell, we must ensure that no child of
                    // "result" also satisfies the conditions of ShrinkToFit().  We do this
                    // by ensuring that "rect" intersects at least two children of "result"
                    // (after padding).
                    int    axis   = S2Testing.Random.Uniform(2);
                    double center = result.CenterUV()[axis];

                    // Find the range of coordinates that are shared between child cells
                    // along that axis.
                    R1Interval shared = new(center - padding, center + padding);
                    double     mid    = SampleInterval(shared.Intersection(max_rect[axis]));
                    a = a.SetAxis(axis, SampleInterval(new R1Interval(max_rect[axis].Lo, mid)));
                    b = b.SetAxis(axis, SampleInterval(new R1Interval(mid, max_rect[axis].Hi)));
                }
                R2Rect rect = R2Rect.FromPointPair(a, b);

                // Choose an arbitrary ancestor as the S2PaddedCell.
                S2CellId initial_id = result.Parent(S2Testing.Random.Uniform(result.Level() + 1));
                Assert.Equal(result, new S2PaddedCell(initial_id, padding).ShrinkToFit(rect));
            }
        }