Example #1
0
        public void BulkLoadSplitsTreeProperly()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);
            tree.BulkLoad(points);

            Assert.Equal(points.Length * 2, tree.Count);
            Assert.Equal(4, tree.Root.Height);
        }
Example #2
0
        public void TestSearch_AfterBulkLoadWithSplitRoot()
        {
            int maxEntries = 4;
            var tree       = new RBush <Point>(maxEntries);

            int      numFirstSet      = maxEntries * maxEntries + 2;    // Split-root will occur twice.
            var      firstSet         = points.Take(numFirstSet);
            Envelope firstSetEnvelope =
                new Envelope(
                    firstSet.Min(p => p.Envelope.MinX),
                    firstSet.Min(p => p.Envelope.MinY),
                    firstSet.Max(p => p.Envelope.MaxX),
                    firstSet.Max(p => p.Envelope.MaxY));

            tree.BulkLoad(firstSet);

            int      numExtraPoints         = 5;
            var      extraPointsSet         = points.TakeLast(numExtraPoints);
            Envelope extraPointsSetEnvelope =
                new Envelope(
                    extraPointsSet.Min(p => p.Envelope.MinX),
                    extraPointsSet.Min(p => p.Envelope.MinY),
                    extraPointsSet.Max(p => p.Envelope.MaxX),
                    extraPointsSet.Max(p => p.Envelope.MaxY));

            foreach (var p in extraPointsSet)
            {
                tree.Insert(p);
            }

            Assert.Equal(extraPointsSet.OrderBy(x => x), tree.Search(extraPointsSetEnvelope).OrderBy(x => x));
        }
Example #3
0
        public void FindNeighborsThatSatisfyAGivenPredicate()
        {
            RBush <Box> bush = new RBush <Box>();

            bush.BulkLoad(richData);

            IEnumerable <Box> result = bush.KnnToPointSearch(2, 4, 1, b => b.Version < 5);

            if (result.Count() == 1)
            {
                Box item = result.First();
                if (item.Envelope.MinX == 3 && item.Envelope.MinY == 3 &&
                    item.Envelope.MaxX == 3 && item.Envelope.MaxY == 3 &&
                    item.Version == 2)
                {
                    //Test passes. Found the correct item
                }
                else
                {
                    //Could not find the correct item
                    Assert.True(false);
                }
            }
            else
            {
                //Could not find the correct item
                Assert.True(false);
            }
        }
Example #4
0
        public static IEnumerable <IPoint> Execute(IGeometry line1, IGeometry line2)
        {
            IDictionary <string, IPoint> unique = new Dictionary <string, IPoint>();
            IList <IPoint> intersections        = new List <IPoint>();

            RBush <Extensions.SpatialDataWrapper <Segment> > tree = new RBush <Extensions.SpatialDataWrapper <Segment> >();

            tree.BulkLoad(LineSegment.Execute(line2).Select(a => a.AsSpatialData <Segment>()));
            foreach (Segment segment in LineSegment.Execute(line1))
            {
                foreach (Extensions.SpatialDataWrapper <Segment> match in tree.Search(segment.BBox.AsEnvelope()))
                {
                    IPoint intersect = Intersects(segment.LineString, match.GetOriginalData <Segment>().LineString);
                    if (intersect != null)
                    {
                        var key = string.Join(",", intersect.X, intersect.Y);
                        if (!unique.ContainsKey(key))
                        {
                            unique.Add(key, intersect);
                            intersections.Add(intersect);
                        }
                    }
                }
            }

            return(intersections);
        }
Example #5
0
        public void BasicRemoveTest()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);

            var len = points.Length;

            tree.Delete(points[0]);
            tree.Delete(points[1]);
            tree.Delete(points[2]);

            tree.Delete(points[len - 1]);
            tree.Delete(points[len - 2]);
            tree.Delete(points[len - 3]);

            var shouldFindPoints = points
                                   .Skip(3).Take(len - 6)
                                   .OrderBy(x => x)
                                   .ToList();
            var foundPoints = tree.Search()
                              .OrderBy(x => x)
                              .ToList();

            Assert.Equal(shouldFindPoints, foundPoints);
            Assert.Equal(shouldFindPoints.Count, tree.Count);
            Assert.Equal(
                shouldFindPoints.Aggregate(Envelope.EmptyBounds, (e, p) => e.Extend(p.Envelope)),
                tree.Envelope);
        }
        public void FindsNNeighbors1()
        {
            RBush <Box> bush = new RBush <Box>();

            bush.BulkLoad(boxes);
            IEnumerable <Box> result = bush.KnnToLineSegmentSearch(6362, 3343.5, 6360.75, 3344, 10);

            Box[] mustBeReturned = Box.CreateBoxes(new double[, ]
            {
                { 6359.0, 3343.0, 6362.5, 3345.0 }, { 6357.0, 3342.0, 6363.5, 3343.0 }, { 6360.5, 3340.0, 6361.5, 3342.0 },
                { 6364.0, 3342.0, 6365.0, 3344.5 }, { 6362.0, 3340.5, 6363.0, 3341.5 }, { 6358.5, 3337.5, 6360.5, 3341.5 },
                { 6361.0, 3339.0, 6363.0, 3340.0 }, { 6364.5, 3340.5, 6368.0, 3341.0 }, { 6363.5, 3337.5, 6365.0, 3340.0 },
                { 6367.0, 3342.0, 6368.0, 3344.0 },
            });
            Assert.True(mustBeReturned.Length == result.Count());


            Assert.True(result.ElementAt(0).CompareTo(mustBeReturned[0]) == 0);
            Assert.True(result.ElementAt(1).CompareTo(mustBeReturned[1]) == 0);
            Assert.True(result.ElementAt(2).CompareTo(mustBeReturned[2]) == 0);
            Assert.True(result.ElementAt(3).CompareTo(mustBeReturned[3]) == 0 ||
                        result.ElementAt(3).CompareTo(mustBeReturned[4]) == 0);
            Assert.True(result.ElementAt(4).CompareTo(mustBeReturned[3]) == 0 ||
                        result.ElementAt(4).CompareTo(mustBeReturned[4]) == 0);
            Assert.True(result.ElementAt(5).CompareTo(mustBeReturned[5]) == 0);

            Assert.True(result.ElementAt(6).CompareTo(mustBeReturned[6]) == 0);
            Assert.True(result.ElementAt(7).CompareTo(mustBeReturned[7]) == 0);
            Assert.True(result.ElementAt(8).CompareTo(mustBeReturned[8]) == 0);
            Assert.True(result.ElementAt(9).CompareTo(mustBeReturned[9]) == 0);
        }
        public void FindNeighborsThatSatisfyAGivenPredicate()
        {
            RBush <Box> bush = new RBush <Box>();

            bush.BulkLoad(richData);

            IEnumerable <Box> result = bush.KnnToLineSegmentSearch(6386.5, 3349.5, 6393, 3353.5, 1, b => b.Version > 2);

            if (result.Count() == 1)
            {
                Box item = result.First();
                if (item.Envelope.MinX == 6393.8 && item.Envelope.MinY == 3354.0 &&
                    item.Envelope.MaxX == 6395.3 && item.Envelope.MaxY == 3356.0 &&
                    item.Version == 3)
                {
                    //Test passes. Found the correct item
                }
                else
                {
                    Assert.True(false, "Could not find the correct item");
                }
            }
            else
            {
                Assert.True(false, "Could not find the correct item");
            }
        }
Example #8
0
        public void RunTestGroup(int maxEntries, int numberOfItems)
        {
            RBush <Item> rBush = new RBush <Item>(maxEntries);

            double             spaceScale = 50;
            IEnumerable <Item> items      = ItemListGenerator.GenerateItems(numberOfItems, spaceScale);

            Console.WriteLine("maxEntries = " + maxEntries);

            // Tests:

            RunTest("BulkLoad", numberOfItems, () => { rBush.BulkLoad(items); });

            RunTest("Search OLD", numberOfItems, () => { rBush.Search_Old(); });
            RunTest("Search NEW", numberOfItems, () => { rBush.Search(); });

            RunTest("Search envelope (Inf. bounds) OLD", numberOfItems, () => { rBush.Search_Old(Envelope.InfiniteBounds); });
            RunTest("Search envelope (Inf. bounds) NEW", numberOfItems, () => { rBush.Search(Envelope.InfiniteBounds); });

            RunTest("Iterate through IEnumerable [for comparison]", numberOfItems, () => { foreach (Item i in items)
                                                                                           {
                                                                                           }
                    });

            Console.ReadLine();
        }
Example #9
0
        public void BasicRemoveTest()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);

            var len = points.Length;

            tree.Delete(points[0]);
            tree.Delete(points[1]);
            tree.Delete(points[2]);

            tree.Delete(points[len - 1]);
            tree.Delete(points[len - 2]);
            tree.Delete(points[len - 3]);

            var shouldFindPoints = points
                                   .Skip(3).Take(len - 6)
                                   .OrderBy(x => x)
                                   .ToList();
            var foundPoints = tree.Search()
                              .OrderBy(x => x)
                              .ToList();

            Assert.Equal(shouldFindPoints, foundPoints);
        }
Example #10
0
        public void DeleteAndSearchTest()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);

            var len = points.Length;

            // Delete an existent point.
            bool resultExistent           = tree.Delete(points[0]);
            var  shouldFindPointsExistent = points
                                            .Skip(1)
                                            .OrderBy(x => x)
                                            .ToList();
            var foundPointsExistent = tree.Search()
                                      .OrderBy(x => x)
                                      .ToList();

            // Try to delete a non-existent point.
            bool resultNonExistent           = tree.Delete(new Point(1245, 1233, 1245, 1233));
            var  shouldFindPointsNonExistent = shouldFindPointsExistent;
            var  foundPointsNonExistent      = tree.Search()
                                               .OrderBy(x => x)
                                               .ToList();

            Assert.True(resultExistent);
            Assert.Equal(shouldFindPointsExistent, foundPointsExistent);
            Assert.False(resultNonExistent);
            Assert.Equal(shouldFindPointsNonExistent, foundPointsNonExistent);
        }
Example #11
0
        public void NonExistentItemCanBeDeleted()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);

            tree.Delete(new Point(13, 13, 13, 13));
        }
Example #12
0
        public void DoesNotThrowIfRequestingTooManyItems()
        {
            var bush = new RBush <Box>();

            bush.BulkLoad(boxes);

            bush.Knn(1000, 40, 40);
        }
Example #13
0
        public void DoesNotThrowIfRequestingTooManyItemsForMaxDistance()
        {
            var bush = new RBush <Box>();

            bush.BulkLoad(boxes);

            bush.Knn(1000, 40, 40, maxDistance: 10);
        }
Example #14
0
        public void SearchReturnsEmptyResultIfNothingFound()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);

            Assert.Equal(new Point[] { }, tree.Search(new Envelope(200, 200, 210, 210)));
        }
Example #15
0
        public void NonExistentItemCanBeDeleted()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);

            tree.Delete(new Point(13, 13, 13, 13));
            Assert.Equal(points.Length, tree.Count);
        }
Example #16
0
        public void BulkLoadAfterDeleteTest2()
        {
            var pts       = GetPoints(20);
            var ptsDelete = pts.Take(4);
            var tree      = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(pts);

            foreach (var item in ptsDelete)
            {
                tree.Delete(item);
            }

            tree.BulkLoad(ptsDelete);

            Assert.Equal(pts.Count, tree.Search().Count);
            Assert.Equal(pts.OrderBy(x => x).ToList(), tree.Search().OrderBy(x => x).ToList());
        }
Example #17
0
        public void BulkLoadTestData()
        {
            var tree = new RBush <Point>();

            tree.BulkLoad(points);

            Assert.Equal(points.Length, tree.Count);
            Assert.Equal(points.OrderBy(x => x).ToList(), tree.Search().OrderBy(x => x).ToList());
        }
Example #18
0
        public void ClearWorks()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);
            tree.Clear();

            Assert.Equal(0, tree.Count);
            Assert.Empty(tree.Root.children);
        }
Example #19
0
        public void TestSearchAfterBulkLoadWithSplitRoot()
        {
            var maxEntries = 4;
            var tree       = new RBush <Point>(maxEntries);

            var numFirstSet = maxEntries * maxEntries + 2;              // Split-root will occur twice.
            var firstSet    = points.Take(numFirstSet);

            tree.BulkLoad(firstSet);

            var numExtraPoints         = 5;
            var extraPointsSet         = points.Skip(points.Length - numExtraPoints);
            var extraPointsSetEnvelope =
                extraPointsSet.Aggregate(Envelope.EmptyBounds, (e, p) => e.Extend(p.Envelope));

            tree.BulkLoad(extraPointsSet);

            // first 10 entries and last 5 entries are completely mutually exclusive
            // so searching the bounds of the new set should only return the new set exactly
            Assert.Equal(extraPointsSet.OrderBy(x => x), tree.Search(extraPointsSetEnvelope).OrderBy(x => x));
        }
Example #20
0
        public void BulkLoadMergesTreesProperly()
        {
            var smaller = GetPoints(10);
            var tree1   = new RBush <Point>(maxEntries: 4);

            tree1.BulkLoad(smaller);
            tree1.BulkLoad(points);

            var tree2 = new RBush <Point>(maxEntries: 4);

            tree2.BulkLoad(points);
            tree2.BulkLoad(smaller);

            Assert.True(tree1.Count == tree2.Count);
            Assert.True(tree1.Root.Height == tree2.Root.Height);

            var allPoints = points.Concat(smaller).OrderBy(x => x).ToList();

            Assert.Equal(allPoints, tree1.Search().OrderBy(x => x).ToList());
            Assert.Equal(allPoints, tree2.Search().OrderBy(x => x).ToList());
        }
Example #21
0
        public void FindsNNeighbors()
        {
            var bush = new RBush <Box>();

            bush.BulkLoad(boxes);
            var result   = bush.Knn(10, 40, 40);
            var expected = boxes
                           .OrderBy(b => b.DistanceTo(40, 40))
                           .Take(10)
                           .ToList();

            Assert.Equal(expected, result);
        }
Example #22
0
        public void FindAllNeighborsForMaxDistance()
        {
            RBush <Box> bush = new RBush <Box>();

            bush.BulkLoad(boxes);

            IEnumerable <Box> result = bush.KnnToPointSearch(40, 40, 0, maxDist: 10);

            foreach (Box resBox in result)
            {
                Assert.True(CalcBoxDist(resBox, 40, 40) <= 10);
            }
        }
        /// <summary>
        /// Method used to split all the bounding boxes into a predefined config based smaller buckets to be able to parallely searched
        /// </summary>
        private void SplitToBuckets()
        {
            int subListSize      = BoundingBoxes.Count / MaxConcurrentEngines;
            int elementsIterated = 0;

            while (elementsIterated < BoundingBoxes.Count)
            {
                int remainingElements = Math.Min(BoundingBoxes.Count - elementsIterated, subListSize);
                var newBush           = new RBush <BoundingBox>();
                newBush.BulkLoad((BoundingBoxes as List <BoundingBox>).GetRange(elementsIterated, remainingElements));
                BoxesEnginesBuckets.Add(newBush);
                elementsIterated += subListSize;
            }
        }
Example #24
0
        public void FindAllNeighborsForMaxDistance()
        {
            var bush = new RBush <Box>();

            bush.BulkLoad(boxes);

            var result   = bush.Knn(0, 40, 40, maxDistance: 10);
            var expected = boxes
                           .Where(b => b.DistanceTo(40, 40) <= 10)
                           .OrderBy(b => b.DistanceTo(40, 40))
                           .ToList();

            Assert.Equal(expected, result);
        }
Example #25
0
        public void DoesNotThrowIfRequestingTooManyItems()
        {
            RBush <Box> bush = new RBush <Box>();

            bush.BulkLoad(boxes);

            try
            {
                IEnumerable <Box> result = bush.KnnSearch(40, 40, 1000);
            }
            catch (Exception ex)
            {
                Assert.Fail("Expected no exception, but got: " + ex.Message);
            }
        }
Example #26
0
        private void RebuildDynamicIndex()
        {
            RTreeDynamic.Clear();
            foreach (var body in Bodies)
            {
                body.Project(Time);
                body.Envelope = new Envelope(
                    body.Position.X - body.Size,
                    body.Position.Y - body.Size,
                    body.Position.X + body.Size,
                    body.Position.Y + body.Size);
            }

            RTreeDynamic.BulkLoad(Bodies.Where(b => !b.IsStatic));
        }
Example #27
0
        public void TreeEnvelopeTest()
        {
            var tree = new RBush <Point>(maxEntries: 4);

            tree.BulkLoad(points);

            Envelope envelope =
                new Envelope(
                    points.Min(p => p.Envelope.MinX),
                    points.Min(p => p.Envelope.MinY),
                    points.Max(p => p.Envelope.MaxX),
                    points.Max(p => p.Envelope.MaxY));

            Assert.Equal(envelope, tree.Envelope);
        }
Example #28
0
        public void FindNeighborsThatSatisfyAGivenPredicate()
        {
            var bush = new RBush <Box>();

            bush.BulkLoad(richData);

            var result   = bush.Knn(1, 2, 4, predicate: b => b.Version < 5);
            var expected = richData
                           .Where(b => b.Version < 5)
                           .OrderBy(b => b.DistanceTo(2, 4))
                           .Take(1)
                           .ToList();

            Assert.Equal(expected, result);
        }
Example #29
0
        public void DoesNotThrowIfRequestingTooManyItems()
        {
            RBush <Box> bush = new RBush <Box>();

            bush.BulkLoad(boxes);

            try
            {
                IEnumerable <Box> result = bush.KnnToPointSearch(40, 40, 1000);
            }
            catch (Exception ex)
            {
                //Expected no exception
                Assert.True(false);
            }
        }
Example #30
0
        public void TestBulk()
        {
            var tree = new RBush <Point>();

            tree.BulkLoad(missingEnvelopeTestData);

            var envelope = new Envelope(
                minX: 34.73274678,
                minY: 31.87729923,
                maxX: 34.73274678,
                maxY: 31.87729923);

            Assert.Equal(
                expected: tree.Search().Where(p => p.Envelope.Intersects(envelope)).Count(),
                actual: tree.Search(envelope).Count);
        }