Ejemplo n.º 1
0
        public void RTree_Range_Search_Test()
        {
            var nodeCount      = 1000;
            var randomPolygons = new HashSet <Polygon>();

            for (int i = 0; i < nodeCount; i++)
            {
                randomPolygons.Add(getRandomPointOrPolygon());
            }
            var order = 5;
            var tree  = new RTree(order);

            foreach (var polygon in randomPolygons)
            {
                tree.Insert(polygon);
            }

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            var searchRectangle = getRandomPointOrPolygon().GetContainingRectangle();

            var expectedIntersections = randomPolygons.Where(x => RectangleIntersection.FindIntersection(searchRectangle, x.GetContainingRectangle()) != null).ToList();
            var actualIntersections   = tree.RangeSearch(searchRectangle);

            Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count);
        }
Ejemplo n.º 2
0
        public void RTree_Stress_Test()
        {
            var nodeCount      = 10000;
            var randomPolygons = new HashSet <Polygon>();

            for (int i = 0; i < nodeCount; i++)
            {
                randomPolygons.Add(getRandomPointOrPolygon());
            }

            var order = 5;
            var tree  = new RTree(order);

            foreach (var polygon in randomPolygons)
            {
                tree.Insert(polygon);
            }

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            foreach (var polygon in randomPolygons)
            {
                tree.Delete(polygon);
            }
        }
Ejemplo n.º 3
0
        public void RTree_Insertion_Test()
        {
            var nodeCount      = 1000;
            var randomPolygons = new HashSet <Polygon>();

            for (int i = 0; i < nodeCount; i++)
            {
                randomPolygons.Add(getRandomPointOrPolygon());
            }
            var order = 5;
            var tree  = new RTree(order);

            int j = 0;

            foreach (var polygon in randomPolygons)
            {
                tree.Insert(polygon);

                //IEnumerable test
                Assert.AreEqual(tree.Count, tree.Count());
                //height should be similar to that of B+ tree.
                //https://en.wikipedia.org/wiki/B-tree#Best_case_and_worst_case_heights
                var theoreticalMaxHeight = Math.Ceiling(Math.Log((j + 2) / 2, (int)Math.Ceiling((double)order / 2))) + 1;

                var actualMaxHeight = tree.Root.Height;
                Assert.AreEqual(verifyHeightUniformityAndReturnHeight(tree.Root, order), actualMaxHeight);
                Assert.IsTrue(actualMaxHeight <= theoreticalMaxHeight);
                j++;

                Assert.IsTrue(tree.Exists(polygon));
            }

            Assert.AreEqual(j, tree.Count);
        }
Ejemplo n.º 4
0
        public void RTree_Deletion_Test()
        {
            var nodeCount      = 1000;
            var randomPolygons = new HashSet <Polygon>();

            for (int i = 0; i < nodeCount; i++)
            {
                randomPolygons.Add(getRandomPointOrPolygon());
            }
            var order = 5;
            var tree  = new RTree(order);

            foreach (var polygon in randomPolygons)
            {
                tree.Insert(polygon);
            }

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            int j = randomPolygons.Count;

            foreach (var polygon in randomPolygons)
            {
                tree.Delete(polygon);
                Assert.IsFalse(tree.Exists(polygon));

                j--;

                if (j > 0)
                {
                    var actualMaxHeight = tree.Root.Height;
                    Assert.AreEqual(verifyHeightUniformityAndReturnHeight(tree.Root, order), actualMaxHeight);
                    Assert.AreEqual(j, tree.Count);
                }
            }
        }
Ejemplo n.º 5
0
        public void RTreeCanBeRecoveredFromDisk()
        {
            var  filename = Path.GetTempFileName();
            long indexId  = 0;

            // create the new storage manager
            using (var storageManager = new DiskStorageManager(filename))
            {
                var options = new RTreeOptions()
                {
                    Dimensions      = 2,
                    EnsureTightMBRs = true,
                    TreeVariant     = RTreeVariant.RStar,
                    FillFactor      = 0.9
                };

                storageManager.DatafileSuffix  = "data";
                storageManager.IndexfileSuffix = "index";
                storageManager.Overwrite       = true;
                storageManager.PageSize        = 1024;

                using (var rtree = new RTree <byte[]>(options, storageManager))
                {
                    rtree.Add(new Circle(new Point(-8, 2), 4), DataHelpers.GenerateSomeBytes(0x123021));
                    rtree.Add(new Circle(new Point(0, 2), 4), DataHelpers.GenerateSomeBytes(0x723321));
                    rtree.Add(new Circle(new Point(8, 2), 4), DataHelpers.GenerateSomeBytes(0x325721));
                    rtree.Add(new Circle(new Point(-4, -4), 0), DataHelpers.GenerateSomeBytes(0x452910));
                    rtree.Add(new Circle(new Point(4, -4), 0), DataHelpers.GenerateSomeBytes(0x7290900));

                    // sanity check
                    Assert.Equal(5, rtree.Count());
                    indexId = rtree.Options.IndexIdentifier;
                }
            }

            // recycle the storage manager
            using (var storageManager = new DiskStorageManager(filename))
            {
                storageManager.DatafileSuffix  = "data";
                storageManager.IndexfileSuffix = "index";
                storageManager.Overwrite       = false;
                storageManager.PageSize        = 1024;

                var options = new RTreeOptions(indexId)
                {
                    Dimensions      = 2,
                    EnsureTightMBRs = true,
                    TreeVariant     = RTreeVariant.RStar,
                    FillFactor      = 0.9
                };

                using (var rtree = new RTree <byte[]>(options, storageManager))
                {
                    // make sure my objects are still there
                    Assert.Equal(5, rtree.Count());

                    // make sure they are still correct
                    var intersection = rtree.IntersectsWith(new Circle(new Point(4, -4), 4));
                    Assert.Equal(3, intersection.Count());
                }
            }
        }