├── README.md
├── .gitattributes
├── .github
├── FUNDING.yml
├── dependabot.yml
└── workflows
│ ├── build.yml
│ ├── release.yml
│ └── release.signed.yml
├── RBush
├── ISpatialData.cs
├── ISpatialIndex.cs
├── ArgumentNullException.cs
├── ISpatialDatabase.cs
├── RBush.csproj
├── RBush.Node.cs
├── RBushExtensions.cs
├── Envelope.cs
├── RBush.cs
└── RBush.Utilities.cs
├── RBush.Test
├── .editorconfig
├── RBush.Test.csproj
├── Point.cs
├── KnnTests.cs
└── RBushTests.cs
├── Directory.Build.props
├── Directory.Packages.props
├── LICENSE
├── RBush.sln
├── .gitignore
└── .editorconfig
/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/viceroypenguin/RBush/HEAD/README.md
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [viceroypenguin]
4 |
--------------------------------------------------------------------------------
/RBush/ISpatialData.cs:
--------------------------------------------------------------------------------
1 | namespace RBush;
2 |
3 | ///
4 | /// Exposes an that describes the
5 | /// bounding box of current object.
6 | ///
7 | public interface ISpatialData
8 | {
9 | ///
10 | /// The bounding box of the current object.
11 | ///
12 | ref readonly Envelope Envelope { get; }
13 | }
14 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | # To get started with Dependabot version updates, you'll need to specify which
2 | # package ecosystems to update and where the package manifests are located.
3 | # Please see the documentation for all configuration options:
4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5 |
6 | version: 2
7 | updates:
8 | - package-ecosystem: "nuget"
9 | directory: "/"
10 | schedule:
11 | interval: monthly
12 | rebase-strategy: auto
13 | ignore:
14 | - dependency-name: "*"
15 | update-types: ["version-update:semver-minor"]
16 |
--------------------------------------------------------------------------------
/RBush.Test/.editorconfig:
--------------------------------------------------------------------------------
1 | [*.cs]
2 |
3 | dotnet_diagnostic.CS1573.severity = none # CS1573: Missing XML comment for parameter
4 | dotnet_diagnostic.CS1591.severity = none # CS1591: Missing XML comment for publicly visible type or member
5 | dotnet_diagnostic.CS1712.severity = none # CS1712: Type parameter has no matching typeparam tag in the XML comment (but other type parameters do)
6 |
7 | dotnet_diagnostic.CA1707.severity = none # CA1707: Identifiers should not contain underscores
8 | dotnet_diagnostic.CA1814.severity = none
9 | dotnet_diagnostic.CA1822.severity = none # CA1822: Mark members as static
10 |
--------------------------------------------------------------------------------
/RBush.Test/RBush.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 | false
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | latest
4 |
5 | enable
6 | $(WarningsAsErrors);nullable;
7 |
8 | enable
9 |
10 | latest-all
11 | true
12 |
13 | true
14 |
15 |
16 |
17 | true
18 | true
19 | true
20 | opencover
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | workflow_dispatch:
5 | push:
6 | branches:
7 | - 'master'
8 | paths-ignore:
9 | - '**/readme.md'
10 | pull_request:
11 | types: [opened, synchronize, reopened]
12 |
13 | jobs:
14 | build:
15 |
16 | runs-on: ubuntu-latest
17 |
18 | steps:
19 | - uses: actions/checkout@v4
20 |
21 | - name: Setup .NET
22 | uses: actions/setup-dotnet@v4
23 | with:
24 | dotnet-version: |
25 | 8.0.x
26 |
27 | - name: Restore dependencies
28 | run: dotnet restore
29 | - name: Build
30 | run: dotnet build -c Release --no-restore
31 | - name: Test
32 | run: dotnet test -c Release --no-build --logger GitHubActions
33 |
34 | - name: Upload coverage reports to Codecov with GitHub Action
35 | uses: codecov/codecov-action@v4
36 | with:
37 | token: ${{ secrets.CODECOV_TOKEN }}
38 |
--------------------------------------------------------------------------------
/Directory.Packages.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | true
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/RBush.Test/Point.cs:
--------------------------------------------------------------------------------
1 | namespace RBush.Test;
2 |
3 | internal sealed class Point(double minX, double minY, double maxX, double maxY) : ISpatialData, IEquatable
4 | {
5 | private readonly Envelope _envelope =
6 | new(
7 | MinX: minX,
8 | MinY: minY,
9 | MaxX: maxX,
10 | MaxY: maxY
11 | );
12 |
13 | public ref readonly Envelope Envelope => ref _envelope;
14 |
15 | public bool Equals(Point? other) =>
16 | other != null
17 | && Envelope.Equals(other.Envelope);
18 |
19 | public override bool Equals(object? obj) =>
20 | Equals(obj as Point);
21 |
22 | public override int GetHashCode() =>
23 | _envelope.GetHashCode();
24 |
25 | public double DistanceTo(double x, double y) =>
26 | Envelope.DistanceTo(x, y);
27 |
28 | public static Point[] CreatePoints(double[,] data) =>
29 | Enumerable.Range(0, data.GetLength(0))
30 | .Select(i => new Point(
31 | minX: data[i, 0],
32 | minY: data[i, 1],
33 | maxX: data[i, 2],
34 | maxY: data[i, 3]))
35 | .ToArray();
36 | }
37 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Release
2 |
3 | on:
4 | workflow_dispatch:
5 | push:
6 | tags:
7 | - '**'
8 |
9 | permissions:
10 | contents: write
11 |
12 | jobs:
13 | release:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | steps:
18 | - uses: actions/checkout@v4
19 |
20 | - name: Setup .NET
21 | uses: actions/setup-dotnet@v4
22 | with:
23 | dotnet-version: |
24 | 8.0.x
25 |
26 | - name: Restore dependencies
27 | run: dotnet restore
28 | - name: Build
29 | run: dotnet build -c Release --no-restore
30 |
31 | - name: Package
32 | run: dotnet pack -c Release --no-build --property:PackageOutputPath=../nupkgs
33 | - name: Push to Nuget
34 | run: dotnet nuget push "./nupkgs/*.nupkg" --source "https://api.nuget.org/v3/index.json" --api-key ${{ secrets.NUGETPUBLISHKEY }}
35 |
36 | - name: Create Release
37 | uses: ncipollo/release-action@v1
38 | with:
39 | generateReleaseNotes: 'true'
40 | makeLatest: 'true'
41 |
--------------------------------------------------------------------------------
/RBush/ISpatialIndex.cs:
--------------------------------------------------------------------------------
1 | namespace RBush;
2 |
3 | ///
4 | /// Provides the base interface for the abstraction of
5 | /// an index to find points within a bounding box.
6 | ///
7 | /// The type of elements in the index.
8 | public interface ISpatialIndex
9 | {
10 | ///
11 | /// Get all of the elements within the current .
12 | ///
13 | ///
14 | /// A list of every element contained in the .
15 | ///
16 | IReadOnlyList Search();
17 |
18 | ///
19 | /// Get all of the elements from this
20 | /// within the bounding box.
21 | ///
22 | /// The area for which to find elements.
23 | ///
24 | /// A list of the points that are within the bounding box
25 | /// from this .
26 | ///
27 | IReadOnlyList Search(in Envelope boundingBox);
28 | }
29 |
--------------------------------------------------------------------------------
/.github/workflows/release.signed.yml:
--------------------------------------------------------------------------------
1 | name: Release
2 |
3 | on:
4 | workflow_dispatch:
5 | push:
6 | tags:
7 | - '**'
8 |
9 | permissions:
10 | contents: write
11 |
12 | jobs:
13 | release:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | steps:
18 | - uses: actions/checkout@v4
19 |
20 | - name: Setup .NET
21 | uses: actions/setup-dotnet@v4
22 | with:
23 | dotnet-version: |
24 | 8.0.x
25 |
26 | - name: Materialize Signing Key
27 | id: write_sign_key_file
28 | uses: timheuer/base64-to-file@v1
29 | with:
30 | fileName: 'MyKeys.snk'
31 | encodedString: ${{ secrets.SIGNING_KEY }}
32 |
33 | - name: Restore dependencies
34 | run: dotnet restore
35 | - name: Build
36 | run: dotnet build -c Release --no-restore
37 |
38 | - name: Package
39 | run: dotnet pack -c Release --no-build --property:PackageOutputPath=../nupkgs
40 | - name: Push to Nuget
41 | run: dotnet nuget push "./nupkgs/*.nupkg" --source "https://api.nuget.org/v3/index.json" --api-key ${{ secrets.NUGETPUBLISHKEY }}
42 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 viceroypenguin
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/RBush/ArgumentNullException.cs:
--------------------------------------------------------------------------------
1 | #if !NET6_0_OR_GREATER
2 | #pragma warning disable IDE0005 // Using directive is unnecessary.
3 | global using ArgumentNullException = RBush.ArgumentNullException;
4 | #pragma warning restore IDE0005 // Using directive is unnecessary.
5 |
6 | using System.ComponentModel;
7 | using System.Diagnostics.CodeAnalysis;
8 | using System.Runtime.CompilerServices;
9 |
10 | namespace RBush;
11 |
12 | [Browsable(false)]
13 | [ExcludeFromCodeCoverage]
14 | internal static class ArgumentNullException
15 | {
16 | /// Throws an if is null.
17 | /// The reference type argument to validate as non-null.
18 | /// The name of the parameter with which corresponds.
19 | public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
20 | {
21 | if (argument is null)
22 | Throw(paramName);
23 | }
24 |
25 | [DoesNotReturn]
26 | private static void Throw(string? paramName) =>
27 | throw new System.ArgumentNullException(paramName);
28 | }
29 | #endif
30 |
--------------------------------------------------------------------------------
/RBush/ISpatialDatabase.cs:
--------------------------------------------------------------------------------
1 | namespace RBush;
2 |
3 | ///
4 | /// Provides the base interface for the abstraction for
5 | /// an updateable data store of elements on a 2-d plane.
6 | ///
7 | /// The type of elements in the index.
8 | public interface ISpatialDatabase : ISpatialIndex
9 | {
10 | ///
11 | /// Adds an object to the
12 | ///
13 | ///
14 | /// The object to be added to .
15 | ///
16 | void Insert(T item);
17 |
18 | ///
19 | /// Removes an object from the .
20 | ///
21 | ///
22 | /// The object to be removed from the .
23 | ///
24 | /// indicating whether the item was removed.
25 | bool Delete(T item);
26 |
27 | ///
28 | /// Removes all elements from the .
29 | ///
30 | void Clear();
31 |
32 | ///
33 | /// Adds all of the elements from the collection to the .
34 | ///
35 | ///
36 | /// A collection of items to add to the .
37 | ///
38 | ///
39 | /// For multiple items, this method is more performant than
40 | /// adding items individually via .
41 | ///
42 | void BulkLoad(IEnumerable items);
43 | }
44 |
--------------------------------------------------------------------------------
/RBush/RBush.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net47;netstandard2.0;net8.0
5 | true
6 |
7 |
8 |
9 | RBush
10 | Spatial Index data structure; used to make it easier to find data points on a two dimensional plane.
11 |
12 | viceroypenguin
13 | .NET R-Tree Algorithm tree search spatial index
14 | Copyright © 2017-2024 Turning Code, LLC (and others)
15 |
16 | MIT
17 | readme.md
18 |
19 | true
20 | https://github.com/viceroypenguin/RBush
21 | git
22 |
23 | true
24 |
25 |
26 |
27 | true
28 | ../MyKeys.snk
29 | RBush.Signed
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | minor
43 | preview.0
44 | v
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/RBush/RBush.Node.cs:
--------------------------------------------------------------------------------
1 | namespace RBush;
2 |
3 | public partial class RBush
4 | {
5 | ///
6 | /// A node in an R-tree data structure containing other nodes
7 | /// or elements of type .
8 | ///
9 | public class Node : ISpatialData
10 | {
11 | private Envelope _envelope;
12 |
13 | internal Node(List items, int height)
14 | {
15 | Height = height;
16 | Items = items;
17 | ResetEnvelope();
18 | }
19 |
20 | internal void Add(ISpatialData node)
21 | {
22 | Items.Add(node);
23 | _envelope = Envelope.Extend(node.Envelope);
24 | }
25 |
26 | internal void Remove(ISpatialData node)
27 | {
28 | _ = Items.Remove(node);
29 | ResetEnvelope();
30 | }
31 |
32 | internal void RemoveRange(int index, int count)
33 | {
34 | Items.RemoveRange(index, count);
35 | ResetEnvelope();
36 | }
37 |
38 | internal void ResetEnvelope()
39 | {
40 | _envelope = GetEnclosingEnvelope(Items);
41 | }
42 |
43 | internal readonly List Items;
44 |
45 | ///
46 | /// The descendent nodes or elements of a
47 | ///
48 | public IReadOnlyList Children => Items;
49 |
50 | ///
51 | /// The current height of a .
52 | ///
53 | ///
54 | /// A node containing individual elements has a of 1.
55 | ///
56 | public int Height { get; }
57 |
58 | ///
59 | /// Determines whether the current is a leaf node.
60 | ///
61 | public bool IsLeaf => Height == 1;
62 |
63 | ///
64 | /// Gets the bounding box of all of the descendents of the
65 | /// current .
66 | ///
67 | public ref readonly Envelope Envelope => ref _envelope;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/RBush.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.2.32616.157
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RBush", "RBush\RBush.csproj", "{24061D07-5EFA-4D72-9617-0C6671280FDF}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".root", ".root", "{6EA781FE-7457-4266-93E4-6C2751DCB4CF}"
9 | ProjectSection(SolutionItems) = preProject
10 | .editorconfig = .editorconfig
11 | .gitignore = .gitignore
12 | .github\dependabot.yml = .github\dependabot.yml
13 | Directory.Build.props = Directory.Build.props
14 | Directory.Packages.props = Directory.Packages.props
15 | README.md = README.md
16 | EndProjectSection
17 | EndProject
18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RBush.Test", "RBush.Test\RBush.Test.csproj", "{C2CDDC6C-046F-44C1-86F5-9FA742133895}"
19 | EndProject
20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{A51C2CD9-38D1-41E6-8603-986B2A4B08EE}"
21 | ProjectSection(SolutionItems) = preProject
22 | .github\workflows\build.yml = .github\workflows\build.yml
23 | .github\workflows\release.signed.yml = .github\workflows\release.signed.yml
24 | .github\workflows\release.yml = .github\workflows\release.yml
25 | EndProjectSection
26 | EndProject
27 | Global
28 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
29 | Debug|Any CPU = Debug|Any CPU
30 | Release|Any CPU = Release|Any CPU
31 | EndGlobalSection
32 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
33 | {24061D07-5EFA-4D72-9617-0C6671280FDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34 | {24061D07-5EFA-4D72-9617-0C6671280FDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
35 | {24061D07-5EFA-4D72-9617-0C6671280FDF}.Release|Any CPU.ActiveCfg = Release|Any CPU
36 | {24061D07-5EFA-4D72-9617-0C6671280FDF}.Release|Any CPU.Build.0 = Release|Any CPU
37 | {C2CDDC6C-046F-44C1-86F5-9FA742133895}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38 | {C2CDDC6C-046F-44C1-86F5-9FA742133895}.Debug|Any CPU.Build.0 = Debug|Any CPU
39 | {C2CDDC6C-046F-44C1-86F5-9FA742133895}.Release|Any CPU.ActiveCfg = Release|Any CPU
40 | {C2CDDC6C-046F-44C1-86F5-9FA742133895}.Release|Any CPU.Build.0 = Release|Any CPU
41 | EndGlobalSection
42 | GlobalSection(SolutionProperties) = preSolution
43 | HideSolutionNode = FALSE
44 | EndGlobalSection
45 | GlobalSection(NestedProjects) = preSolution
46 | {A51C2CD9-38D1-41E6-8603-986B2A4B08EE} = {6EA781FE-7457-4266-93E4-6C2751DCB4CF}
47 | EndGlobalSection
48 | GlobalSection(ExtensibilityGlobals) = postSolution
49 | SolutionGuid = {94682DB2-A4CF-4B98-AA57-EBF3038CCD97}
50 | EndGlobalSection
51 | EndGlobal
52 |
--------------------------------------------------------------------------------
/RBush/RBushExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace RBush;
4 |
5 | ///
6 | /// Extension methods for the object.
7 | ///
8 | public static class RBushExtensions
9 | {
10 | [StructLayout(LayoutKind.Sequential)]
11 | private record struct ItemDistance(T Item, double Distance);
12 |
13 | ///
14 | /// Get the nearest neighbors to a specific point.
15 | ///
16 | /// The type of elements in the index.
17 | /// An index of points.
18 | /// The number of points to retrieve.
19 | /// The x-coordinate of the center point.
20 | /// The y-coordinate of the center point.
21 | /// The maximum distance of points to be considered "near"; optional.
22 | /// A function to test each element for a condition; optional.
23 | /// The list of up to elements nearest to the given point.
24 | public static IReadOnlyList Knn(
25 | this ISpatialIndex tree,
26 | int k,
27 | double x,
28 | double y,
29 | double? maxDistance = null,
30 | Func? predicate = null)
31 | where T : ISpatialData
32 | {
33 | ArgumentNullException.ThrowIfNull(tree);
34 |
35 | var items = maxDistance == null
36 | ? tree.Search()
37 | : tree.Search(
38 | new Envelope(
39 | MinX: x - maxDistance.Value,
40 | MinY: y - maxDistance.Value,
41 | MaxX: x + maxDistance.Value,
42 | MaxY: y + maxDistance.Value));
43 |
44 | var distances = items
45 | .Select(i => new ItemDistance(i, i.Envelope.DistanceTo(x, y)))
46 | .OrderBy(i => i.Distance)
47 | .AsEnumerable();
48 |
49 | if (maxDistance.HasValue)
50 | distances = distances.TakeWhile(i => i.Distance <= maxDistance.Value);
51 |
52 | if (predicate != null)
53 | distances = distances.Where(i => predicate(i.Item));
54 |
55 | if (k > 0)
56 | distances = distances.Take(k);
57 |
58 | return distances
59 | .Select(i => i.Item)
60 | .ToList();
61 | }
62 |
63 | ///
64 | /// Calculates the distance from the borders of an
65 | /// to a given point.
66 | ///
67 | /// The from which to find the distance
68 | /// The x-coordinate of the given point
69 | /// The y-coordinate of the given point
70 | /// The calculated Euclidean shortest distance from the to a given point.
71 | public static double DistanceTo(this in Envelope envelope, double x, double y)
72 | {
73 | var dX = AxisDistance(x, envelope.MinX, envelope.MaxX);
74 | var dY = AxisDistance(y, envelope.MinY, envelope.MaxY);
75 | return Math.Sqrt((dX * dX) + (dY * dY));
76 |
77 | static double AxisDistance(double p, double min, double max) =>
78 | p < min ? min - p :
79 | p > max ? p - max :
80 | 0;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/RBush/Envelope.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace RBush;
4 |
5 | ///
6 | /// A bounding envelope, used to identify the bounds of of the points within
7 | /// a particular node.
8 | ///
9 | /// The minimum X value of the bounding box.
10 | /// The minimum Y value of the bounding box.
11 | /// The maximum X value of the bounding box.
12 | /// The maximum Y value of the bounding box.
13 | [StructLayout(LayoutKind.Sequential)]
14 | public readonly record struct Envelope(
15 | double MinX,
16 | double MinY,
17 | double MaxX,
18 | double MaxY)
19 | {
20 | ///
21 | /// The calculated area of the bounding box.
22 | ///
23 | public double Area =>
24 | Math.Max(MaxX - MinX, 0) * Math.Max(MaxY - MinY, 0);
25 |
26 | ///
27 | /// Half of the linear perimeter of the bounding box
28 | ///
29 | public double Margin =>
30 | Math.Max(MaxX - MinX, 0) + Math.Max(MaxY - MinY, 0);
31 |
32 | ///
33 | /// Extends a bounding box to include another bounding box
34 | ///
35 | /// The other bounding box
36 | /// A new bounding box that encloses both bounding boxes.
37 | /// Does not affect the current bounding box.
38 | public Envelope Extend(in Envelope other) =>
39 | new(
40 | MinX: Math.Min(MinX, other.MinX),
41 | MinY: Math.Min(MinY, other.MinY),
42 | MaxX: Math.Max(MaxX, other.MaxX),
43 | MaxY: Math.Max(MaxY, other.MaxY));
44 |
45 | ///
46 | /// Intersects a bounding box to only include the common area
47 | /// of both bounding boxes
48 | ///
49 | /// The other bounding box
50 | /// A new bounding box that is the intersection of both bounding boxes.
51 | /// Does not affect the current bounding box.
52 | public Envelope Intersection(in Envelope other) =>
53 | new(
54 | MinX: Math.Max(MinX, other.MinX),
55 | MinY: Math.Max(MinY, other.MinY),
56 | MaxX: Math.Min(MaxX, other.MaxX),
57 | MaxY: Math.Min(MaxY, other.MaxY));
58 |
59 | ///
60 | /// Determines whether is contained
61 | /// within this bounding box.
62 | ///
63 | /// The other bounding box
64 | ///
65 | /// if is
66 | /// completely contained within this bounding box;
67 | /// otherwise.
68 | ///
69 | public bool Contains(in Envelope other) =>
70 | MinX <= other.MinX &&
71 | MinY <= other.MinY &&
72 | MaxX >= other.MaxX &&
73 | MaxY >= other.MaxY;
74 |
75 | ///
76 | /// Determines whether intersects
77 | /// this bounding box.
78 | ///
79 | /// The other bounding box
80 | ///
81 | /// if is
82 | /// intersects this bounding box in any way;
83 | /// otherwise.
84 | ///
85 | public bool Intersects(in Envelope other) =>
86 | MinX <= other.MaxX &&
87 | MinY <= other.MaxY &&
88 | MaxX >= other.MinX &&
89 | MaxY >= other.MinY;
90 |
91 | ///
92 | /// A bounding box that contains the entire 2-d plane.
93 | ///
94 | public static Envelope InfiniteBounds { get; } =
95 | new(
96 | MinX: double.NegativeInfinity,
97 | MinY: double.NegativeInfinity,
98 | MaxX: double.PositiveInfinity,
99 | MaxY: double.PositiveInfinity);
100 |
101 | ///
102 | /// An empty bounding box.
103 | ///
104 | public static Envelope EmptyBounds { get; } =
105 | new(
106 | MinX: double.PositiveInfinity,
107 | MinY: double.PositiveInfinity,
108 | MaxX: double.NegativeInfinity,
109 | MaxY: double.NegativeInfinity);
110 | }
111 |
--------------------------------------------------------------------------------
/RBush.Test/KnnTests.cs:
--------------------------------------------------------------------------------
1 | using Xunit;
2 |
3 | namespace RBush.Test;
4 |
5 | public class KnnTests
6 | {
7 | private static readonly Point[] s_points = Point.CreatePoints(
8 | new double[,]
9 | {
10 | {87,55,87,56}, {38,13,39,16}, {7,47,8,47}, {89,9,91,12}, {4,58,5,60}, {0,11,1,12}, {0,5,0,6}, {69,78,73,78},
11 | {56,77,57,81}, {23,7,24,9}, {68,24,70,26}, {31,47,33,50}, {11,13,14,15}, {1,80,1,80}, {72,90,72,91}, {59,79,61,83},
12 | {98,77,101,77}, {11,55,14,56}, {98,4,100,6}, {21,54,23,58}, {44,74,48,74}, {70,57,70,61}, {32,9,33,12}, {43,87,44,91},
13 | {38,60,38,60}, {62,48,66,50}, {16,87,19,91}, {5,98,9,99}, {9,89,10,90}, {89,2,92,6}, {41,95,45,98}, {57,36,61,40},
14 | {50,1,52,1}, {93,87,96,88}, {29,42,33,42}, {34,43,36,44}, {41,64,42,65}, {87,3,88,4}, {56,50,56,52}, {32,13,35,15},
15 | {3,8,5,11}, {16,33,18,33}, {35,39,38,40}, {74,54,78,56}, {92,87,95,90}, {12,97,16,98}, {76,39,78,40}, {16,93,18,95},
16 | {62,40,64,42}, {71,87,71,88}, {60,85,63,86}, {39,52,39,56}, {15,18,19,18}, {91,62,94,63}, {10,16,10,18}, {5,86,8,87},
17 | {85,85,88,86}, {44,84,44,88}, {3,94,3,97}, {79,74,81,78}, {21,63,24,66}, {16,22,16,22}, {68,97,72,97}, {39,65,42,65},
18 | {51,68,52,69}, {61,38,61,42}, {31,65,31,65}, {16,6,19,6}, {66,39,66,41}, {57,32,59,35}, {54,80,58,84}, {5,67,7,71},
19 | {49,96,51,98}, {29,45,31,47}, {31,72,33,74}, {94,25,95,26}, {14,7,18,8}, {29,0,31,1}, {48,38,48,40}, {34,29,34,32},
20 | {99,21,100,25}, {79,3,79,4}, {87,1,87,5}, {9,77,9,81}, {23,25,25,29}, {83,48,86,51}, {79,94,79,95}, {33,95,33,99},
21 | {1,14,1,14}, {33,77,34,77}, {94,56,98,59}, {75,25,78,26}, {17,73,20,74}, {11,3,12,4}, {45,12,47,12}, {38,39,39,39},
22 | {99,3,103,5}, {41,92,44,96}, {79,40,79,41}, {29,2,29,4},
23 | });
24 |
25 | [Test]
26 | public void FindsNNeighbors()
27 | {
28 | var bush = new RBush();
29 | bush.BulkLoad(s_points);
30 | var result = bush.Knn(10, 40, 40);
31 | var expected = s_points
32 | .OrderBy(b => b.DistanceTo(40, 40))
33 | .Take(10)
34 | .ToList();
35 | Assert.Equal(expected, result);
36 | }
37 |
38 | [Test]
39 | public void DoesNotThrowIfRequestingTooManyItems()
40 | {
41 | var bush = new RBush();
42 | bush.BulkLoad(s_points);
43 |
44 | _ = bush.Knn(1000, 40, 40);
45 | }
46 |
47 | ///
48 | /// This test is not correct in original javascript library
49 | ///
50 | [Test]
51 | public void FindAllNeighborsForMaxDistance()
52 | {
53 | var bush = new RBush();
54 | bush.BulkLoad(s_points);
55 |
56 | var result = bush.Knn(0, 40, 40, maxDistance: 10);
57 | var expected = s_points
58 | .Where(b => b.DistanceTo(40, 40) <= 10)
59 | .OrderBy(b => b.DistanceTo(40, 40))
60 | .ToList();
61 | Assert.Equal(expected, result);
62 | }
63 |
64 | [Test]
65 | public void FindNNeighborsForMaxDistance()
66 | {
67 | var bush = new RBush();
68 | bush.BulkLoad(s_points);
69 |
70 | var result = bush.Knn(1, 40, 40, maxDistance: 10);
71 | var expected = s_points
72 | .OrderBy(b => b.DistanceTo(40, 40))
73 | .Take(1)
74 | .ToList();
75 |
76 | Assert.Equal(expected, result);
77 | }
78 |
79 | [Test]
80 | public void DoesNotThrowIfRequestingTooManyItemsForMaxDistance()
81 | {
82 | var bush = new RBush();
83 | bush.BulkLoad(s_points);
84 |
85 | _ = bush.Knn(1000, 40, 40, maxDistance: 10);
86 | }
87 |
88 | private static readonly Point[] s_richData = Point.CreatePoints(
89 | new double[,]
90 | {
91 | { 1, 2, 1, 2 }, { 3, 3, 3, 3 }, { 5, 5, 5, 5 },
92 | { 4, 2, 4, 2 }, { 2, 4, 2, 4 }, { 5, 3, 5, 3 },
93 | });
94 |
95 | [Test]
96 | public void FindNeighborsThatSatisfyAGivenPredicate()
97 | {
98 | var bush = new RBush();
99 | bush.BulkLoad(s_richData);
100 |
101 | var result = bush.Knn(1, 2, 4, predicate: p => p.Envelope.MinX != 2);
102 | var expected = s_richData
103 | .Where(p => p.Envelope.MinX != 2)
104 | .OrderBy(b => b.DistanceTo(2, 4))
105 | .Take(1)
106 | .ToList();
107 | Assert.Equal(expected, result);
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 | *.vcxproj.filters
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | bld/
24 | [Bb]in/
25 | [Oo]bj/
26 | [Ll]og/
27 |
28 | # Visual Studio 2015 cache/options directory
29 | .vs/
30 | # Uncomment if you have tasks that create the project's static files in wwwroot
31 | #wwwroot/
32 |
33 | # MSTest test Results
34 | [Tt]est[Rr]esult*/
35 | [Bb]uild[Ll]og.*
36 |
37 | # NUNIT
38 | *.VisualState.xml
39 | TestResult.xml
40 |
41 | # Build Results of an ATL Project
42 | [Dd]ebugPS/
43 | [Rr]eleasePS/
44 | dlldata.c
45 |
46 | # .NET Core
47 | project.lock.json
48 | project.fragment.lock.json
49 | artifacts/
50 | **/Properties/launchSettings.json
51 |
52 | *_i.c
53 | *_p.c
54 | *_i.h
55 | *.ilk
56 | *.meta
57 | *.obj
58 | *.pch
59 | *.pdb
60 | *.pgc
61 | *.pgd
62 | *.rsp
63 | *.sbr
64 | *.tlb
65 | *.tli
66 | *.tlh
67 | *.tmp
68 | *.tmp_proj
69 | *.log
70 | *.vspscc
71 | *.vssscc
72 | .builds
73 | *.pidb
74 | *.svclog
75 | *.scc
76 |
77 | # Chutzpah Test files
78 | _Chutzpah*
79 |
80 | # Visual C++ cache files
81 | ipch/
82 | *.aps
83 | *.ncb
84 | *.opendb
85 | *.opensdf
86 | *.sdf
87 | *.cachefile
88 | *.VC.db
89 | *.VC.VC.opendb
90 |
91 | # Visual Studio profiler
92 | *.psess
93 | *.vsp
94 | *.vspx
95 | *.sap
96 |
97 | # TFS 2012 Local Workspace
98 | $tf/
99 |
100 | # Guidance Automation Toolkit
101 | *.gpState
102 |
103 | # ReSharper is a .NET coding add-in
104 | _ReSharper*/
105 | *.[Rr]e[Ss]harper
106 | *.DotSettings.user
107 |
108 | # JustCode is a .NET coding add-in
109 | .JustCode
110 |
111 | # TeamCity is a build add-in
112 | _TeamCity*
113 |
114 | # DotCover is a Code Coverage Tool
115 | *.dotCover
116 |
117 | # Visual Studio code coverage results
118 | *.coverage
119 | *.coveragexml
120 |
121 | # NCrunch
122 | _NCrunch_*
123 | .*crunch*.local.xml
124 | nCrunchTemp_*
125 |
126 | # MightyMoose
127 | *.mm.*
128 | AutoTest.Net/
129 |
130 | # Web workbench (sass)
131 | .sass-cache/
132 |
133 | # Installshield output folder
134 | [Ee]xpress/
135 |
136 | # DocProject is a documentation generator add-in
137 | DocProject/buildhelp/
138 | DocProject/Help/*.HxT
139 | DocProject/Help/*.HxC
140 | DocProject/Help/*.hhc
141 | DocProject/Help/*.hhk
142 | DocProject/Help/*.hhp
143 | DocProject/Help/Html2
144 | DocProject/Help/html
145 |
146 | # Click-Once directory
147 | publish/
148 |
149 | # Publish Web Output
150 | *.[Pp]ublish.xml
151 | *.azurePubxml
152 | # TODO: Comment the next line if you want to checkin your web deploy settings
153 | # but database connection strings (with potential passwords) will be unencrypted
154 | *.pubxml
155 | *.publishproj
156 |
157 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
158 | # checkin your Azure Web App publish settings, but sensitive information contained
159 | # in these scripts will be unencrypted
160 | PublishScripts/
161 |
162 | # NuGet Packages
163 | *.nupkg
164 | # The packages folder can be ignored because of Package Restore
165 | **/packages/*
166 | # except build/, which is used as an MSBuild target.
167 | !**/packages/build/
168 | # Uncomment if necessary however generally it will be regenerated when needed
169 | #!**/packages/repositories.config
170 | # NuGet v3's project.json files produces more ignoreable files
171 | *.nuget.props
172 | *.nuget.targets
173 |
174 | # Microsoft Azure Build Output
175 | csx/
176 | *.build.csdef
177 |
178 | # Microsoft Azure Emulator
179 | ecf/
180 | rcf/
181 |
182 | # Windows Store app package directories and files
183 | AppPackages/
184 | BundleArtifacts/
185 | Package.StoreAssociation.xml
186 | _pkginfo.txt
187 |
188 | # Visual Studio cache files
189 | # files ending in .cache can be ignored
190 | *.[Cc]ache
191 | # but keep track of directories ending in .cache
192 | !*.[Cc]ache/
193 |
194 | # Others
195 | ClientBin/
196 | ~$*
197 | *~
198 | *.dbmdl
199 | *.dbproj.schemaview
200 | *.jfm
201 | *.pfx
202 | *.snk
203 | *.publishsettings
204 | node_modules/
205 | orleans.codegen.cs
206 |
207 | # Since there are multiple workflows, uncomment next line to ignore bower_components
208 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
209 | #bower_components/
210 |
211 | # RIA/Silverlight projects
212 | Generated_Code/
213 |
214 | # Backup & report files from converting an old project file
215 | # to a newer Visual Studio version. Backup files are not needed,
216 | # because we have git ;-)
217 | _UpgradeReport_Files/
218 | Backup*/
219 | UpgradeLog*.XML
220 | UpgradeLog*.htm
221 |
222 | # SQL Server files
223 | *.mdf
224 | *.ldf
225 |
226 | # Business Intelligence projects
227 | *.rdl.data
228 | *.bim.layout
229 | *.bim_*.settings
230 |
231 | # Microsoft Fakes
232 | FakesAssemblies/
233 |
234 | # GhostDoc plugin setting file
235 | *.GhostDoc.xml
236 |
237 | # Node.js Tools for Visual Studio
238 | .ntvs_analysis.dat
239 |
240 | # Visual Studio 6 build log
241 | *.plg
242 |
243 | # Visual Studio 6 workspace options file
244 | *.opt
245 |
246 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
247 | *.vbw
248 |
249 | # Visual Studio LightSwitch build output
250 | **/*.HTMLClient/GeneratedArtifacts
251 | **/*.DesktopClient/GeneratedArtifacts
252 | **/*.DesktopClient/ModelManifest.xml
253 | **/*.Server/GeneratedArtifacts
254 | **/*.Server/ModelManifest.xml
255 | _Pvt_Extensions
256 |
257 | # Paket dependency manager
258 | .paket/paket.exe
259 | paket-files/
260 |
261 | # FAKE - F# Make
262 | .fake/
263 |
264 | # JetBrains Rider
265 | .idea/
266 | *.sln.iml
267 |
268 | # CodeRush
269 | .cr/
270 |
271 | # Python Tools for Visual Studio (PTVS)
272 | __pycache__/
273 | *.pyc
274 |
275 | # Cake - Uncomment if you are using it
276 | # tools/
277 |
--------------------------------------------------------------------------------
/RBush/RBush.cs:
--------------------------------------------------------------------------------
1 | using System.Diagnostics.CodeAnalysis;
2 |
3 | namespace RBush;
4 |
5 | ///
6 | /// An implementation of the R-tree data structure for 2-d spatial indexing.
7 | ///
8 | /// The type of elements in the index.
9 | public partial class RBush : ISpatialDatabase, ISpatialIndex where T : ISpatialData
10 | {
11 | private const int DefaultMaxEntries = 9;
12 | private const int MinimumMaxEntries = 4;
13 | private const int MinimumMinEntries = 2;
14 | private const double DefaultFillFactor = 0.4;
15 |
16 | private readonly IEqualityComparer _comparer;
17 | private readonly int _maxEntries;
18 | private readonly int _minEntries;
19 |
20 | ///
21 | /// The root of the R-tree.
22 | ///
23 | public Node Root { get; private set; }
24 |
25 | ///
26 | /// The bounding box of all elements currently in the data structure.
27 | ///
28 | public ref readonly Envelope Envelope => ref Root.Envelope;
29 |
30 | ///
31 | /// Initializes a new instance of the that is
32 | /// empty and has the default tree width and default .
33 | ///
34 | public RBush()
35 | : this(DefaultMaxEntries, EqualityComparer.Default) { }
36 |
37 | ///
38 | /// Initializes a new instance of the that is
39 | /// empty and has a custom max number of elements per tree node
40 | /// and default .
41 | ///
42 | ///
43 | public RBush(int maxEntries)
44 | : this(maxEntries, EqualityComparer.Default) { }
45 |
46 | ///
47 | /// Initializes a new instance of the that is
48 | /// empty and has a custom max number of elements per tree node
49 | /// and a custom .
50 | ///
51 | ///
52 | ///
53 | public RBush(int maxEntries, IEqualityComparer comparer)
54 | {
55 | _comparer = comparer;
56 | _maxEntries = Math.Max(MinimumMaxEntries, maxEntries);
57 | _minEntries = Math.Max(MinimumMinEntries, (int)Math.Ceiling(_maxEntries * DefaultFillFactor));
58 |
59 | Clear();
60 | }
61 |
62 | ///
63 | /// Gets the number of items currently stored in the
64 | ///
65 | public int Count { get; private set; }
66 |
67 | ///
68 | /// Removes all elements from the .
69 | ///
70 | [MemberNotNull(nameof(Root))]
71 | public void Clear()
72 | {
73 | Root = new Node([], 1);
74 | Count = 0;
75 | }
76 |
77 | ///
78 | /// Get all of the elements within the current .
79 | ///
80 | ///
81 | /// A list of every element contained in the .
82 | ///
83 | public IReadOnlyList Search() =>
84 | GetAllChildren([], Root);
85 |
86 | ///
87 | /// Get all of the elements from this
88 | /// within the bounding box.
89 | ///
90 | /// The area for which to find elements.
91 | ///
92 | /// A list of the points that are within the bounding box
93 | /// from this .
94 | ///
95 | public IReadOnlyList Search(in Envelope boundingBox) =>
96 | DoSearch(boundingBox);
97 |
98 | ///
99 | /// Adds an object to the
100 | ///
101 | ///
102 | /// The object to be added to .
103 | ///
104 | public void Insert(T item)
105 | {
106 | Insert(item, Root.Height);
107 | Count++;
108 | }
109 |
110 | ///
111 | /// Adds all of the elements from the collection to the .
112 | ///
113 | ///
114 | /// A collection of items to add to the .
115 | ///
116 | ///
117 | /// For multiple items, this method is more performant than
118 | /// adding items individually via .
119 | ///
120 | public void BulkLoad(IEnumerable items)
121 | {
122 | var data = items.ToArray();
123 | if (data.Length == 0) return;
124 |
125 | if (Root.IsLeaf &&
126 | Root.Items.Count + data.Length < _maxEntries)
127 | {
128 | foreach (var i in data)
129 | Insert(i);
130 | return;
131 | }
132 |
133 | if (data.Length < _minEntries)
134 | {
135 | foreach (var i in data)
136 | Insert(i);
137 | return;
138 | }
139 |
140 | var dataRoot = BuildTree(data);
141 | Count += data.Length;
142 |
143 | if (Root.Items.Count == 0)
144 | {
145 | Root = dataRoot;
146 | }
147 | else if (Root.Height == dataRoot.Height)
148 | {
149 | if (Root.Items.Count + dataRoot.Items.Count <= _maxEntries)
150 | {
151 | foreach (var isd in dataRoot.Items)
152 | Root.Add(isd);
153 | }
154 | else
155 | {
156 | SplitRoot(dataRoot);
157 | }
158 | }
159 | else
160 | {
161 | if (Root.Height < dataRoot.Height)
162 | {
163 | #pragma warning disable IDE0180 // netstandard 1.2 doesn't support tuple
164 | var tmp = Root;
165 | Root = dataRoot;
166 | dataRoot = tmp;
167 | #pragma warning restore IDE0180
168 | }
169 |
170 | Insert(dataRoot, Root.Height - dataRoot.Height);
171 | }
172 | }
173 |
174 | ///
175 | /// Removes an object from the .
176 | ///
177 | ///
178 | /// The object to be removed from the .
179 | ///
180 | /// indicating whether the item was deleted.
181 | public bool Delete(T item) =>
182 | DoDelete(Root, item);
183 |
184 | private bool DoDelete(Node node, T item)
185 | {
186 | if (!node.Envelope.Contains(item.Envelope))
187 | return false;
188 |
189 | if (node.IsLeaf)
190 | {
191 | var cnt = node.Items.RemoveAll(i => _comparer.Equals((T)i, item));
192 | if (cnt == 0)
193 | return false;
194 |
195 | Count -= cnt;
196 | node.ResetEnvelope();
197 | return true;
198 |
199 | }
200 |
201 | var flag = false;
202 | foreach (var n in node.Items)
203 | {
204 | flag |= DoDelete((Node)n, item);
205 | }
206 |
207 | if (flag)
208 | node.ResetEnvelope();
209 |
210 | return flag;
211 | }
212 | }
213 |
--------------------------------------------------------------------------------
/RBush/RBush.Utilities.cs:
--------------------------------------------------------------------------------
1 | namespace RBush;
2 |
3 | public partial class RBush
4 | {
5 | #region Sort Functions
6 | private static readonly IComparer s_compareMinX =
7 | Comparer.Create((x, y) => Comparer.Default.Compare(x.Envelope.MinX, y.Envelope.MinX));
8 | private static readonly IComparer s_compareMinY =
9 | Comparer.Create((x, y) => Comparer.Default.Compare(x.Envelope.MinY, y.Envelope.MinY));
10 | #endregion
11 |
12 | #region Search
13 | private List DoSearch(in Envelope boundingBox)
14 | {
15 | if (!Root.Envelope.Intersects(boundingBox))
16 | return [];
17 |
18 | var intersections = new List();
19 | var queue = new Queue();
20 | queue.Enqueue(Root);
21 |
22 | while (queue.Count != 0)
23 | {
24 | var item = queue.Dequeue();
25 |
26 | if (item.IsLeaf)
27 | {
28 | foreach (var i in item.Items)
29 | {
30 | if (i.Envelope.Intersects(boundingBox))
31 | intersections.Add((T)i);
32 | }
33 | }
34 | else
35 | {
36 | foreach (var i in item.Items)
37 | {
38 | if (i.Envelope.Intersects(boundingBox))
39 | queue.Enqueue((Node)i);
40 | }
41 | }
42 | }
43 |
44 | return intersections;
45 | }
46 | #endregion
47 |
48 | #region Insert
49 | private List FindCoveringArea(in Envelope area, int depth)
50 | {
51 | var path = new List();
52 | var node = Root;
53 |
54 | while (true)
55 | {
56 | path.Add(node);
57 | if (node.IsLeaf || path.Count == depth) return path;
58 |
59 | var next = node.Items[0];
60 | var nextArea = next.Envelope.Extend(area).Area;
61 |
62 | foreach (var i in node.Items)
63 | {
64 | var newArea = i.Envelope.Extend(area).Area;
65 | if (newArea > nextArea)
66 | continue;
67 |
68 | if (newArea == nextArea
69 | && i.Envelope.Area >= next.Envelope.Area)
70 | {
71 | continue;
72 | }
73 |
74 | next = i;
75 | nextArea = newArea;
76 | }
77 |
78 | node = (next as Node)!;
79 | }
80 | }
81 |
82 | private void Insert(ISpatialData data, int depth)
83 | {
84 | var path = FindCoveringArea(data.Envelope, depth);
85 |
86 | var insertNode = path[^1];
87 | insertNode.Add(data);
88 |
89 | while (--depth >= 0)
90 | {
91 | if (path[depth].Items.Count > _maxEntries)
92 | {
93 | var newNode = SplitNode(path[depth]);
94 | if (depth == 0)
95 | SplitRoot(newNode);
96 | else
97 | path[depth - 1].Add(newNode);
98 | }
99 | else
100 | {
101 | path[depth].ResetEnvelope();
102 | }
103 | }
104 | }
105 |
106 | #region SplitNode
107 | private void SplitRoot(Node newNode) =>
108 | Root = new Node([Root, newNode], Root.Height + 1);
109 |
110 | private Node SplitNode(Node node)
111 | {
112 | SortChildren(node);
113 |
114 | var splitPoint = GetBestSplitIndex(node.Items);
115 | var newChildren = node.Items.Skip(splitPoint).ToList();
116 | node.RemoveRange(splitPoint, node.Items.Count - splitPoint);
117 | return new Node(newChildren, node.Height);
118 | }
119 |
120 | #region SortChildren
121 | private void SortChildren(Node node)
122 | {
123 | node.Items.Sort(s_compareMinX);
124 | var splitsByX = GetPotentialSplitMargins(node.Items);
125 | node.Items.Sort(s_compareMinY);
126 | var splitsByY = GetPotentialSplitMargins(node.Items);
127 |
128 | if (splitsByX < splitsByY)
129 | node.Items.Sort(s_compareMinX);
130 | }
131 |
132 | private double GetPotentialSplitMargins(List children) =>
133 | GetPotentialEnclosingMargins(children) +
134 | GetPotentialEnclosingMargins(children.AsEnumerable().Reverse().ToList());
135 |
136 | private double GetPotentialEnclosingMargins(List children)
137 | {
138 | var envelope = Envelope.EmptyBounds;
139 | var i = 0;
140 | for (; i < _minEntries; i++)
141 | {
142 | envelope = envelope.Extend(children[i].Envelope);
143 | }
144 |
145 | var totalMargin = envelope.Margin;
146 | for (; i < children.Count - _minEntries; i++)
147 | {
148 | envelope = envelope.Extend(children[i].Envelope);
149 | totalMargin += envelope.Margin;
150 | }
151 |
152 | return totalMargin;
153 | }
154 | #endregion
155 |
156 | private int GetBestSplitIndex(List children)
157 | {
158 | return Enumerable.Range(_minEntries, children.Count - _minEntries)
159 | .Select(i =>
160 | {
161 | var leftEnvelope = GetEnclosingEnvelope(children.Take(i));
162 | var rightEnvelope = GetEnclosingEnvelope(children.Skip(i));
163 |
164 | var overlap = leftEnvelope.Intersection(rightEnvelope).Area;
165 | var totalArea = leftEnvelope.Area + rightEnvelope.Area;
166 | return new { i, overlap, totalArea };
167 | })
168 | .OrderBy(x => x.overlap)
169 | .ThenBy(x => x.totalArea)
170 | .Select(x => x.i)
171 | .First();
172 | }
173 | #endregion
174 | #endregion
175 |
176 | #region BuildTree
177 | private Node BuildTree(T[] data)
178 | {
179 | var treeHeight = GetDepth(data.Length);
180 | var rootMaxEntries = (int)Math.Ceiling(data.Length / Math.Pow(_maxEntries, treeHeight - 1));
181 | return BuildNodes(new ArraySegment(data), treeHeight, rootMaxEntries);
182 | }
183 |
184 | private int GetDepth(int numNodes) =>
185 | (int)Math.Ceiling(Math.Log(numNodes) / Math.Log(_maxEntries));
186 |
187 | private Node BuildNodes(ArraySegment data, int height, int maxEntries)
188 | {
189 | if (data.Count <= maxEntries)
190 | {
191 | return height == 1
192 | ? new Node(data.Cast().ToList(), height)
193 | : new Node(
194 | [
195 | BuildNodes(data, height - 1, _maxEntries),
196 | ],
197 | height);
198 | }
199 |
200 | // after much testing, this is faster than using Array.Sort() on the provided array
201 | // in spite of the additional memory cost and copying. go figure!
202 | var byX = new ArraySegment(data.OrderBy(i => i.Envelope.MinX).ToArray());
203 |
204 | var nodeSize = (data.Count + (maxEntries - 1)) / maxEntries;
205 | var subSortLength = nodeSize * (int)Math.Ceiling(Math.Sqrt(maxEntries));
206 |
207 | var children = new List(maxEntries);
208 | foreach (var subData in Chunk(byX, subSortLength))
209 | {
210 | var byY = new ArraySegment(subData.OrderBy(d => d.Envelope.MinY).ToArray());
211 |
212 | foreach (var nodeData in Chunk(byY, nodeSize))
213 | {
214 | children.Add(BuildNodes(nodeData, height - 1, _maxEntries));
215 | }
216 | }
217 |
218 | return new Node(children, height);
219 | }
220 |
221 | private static IEnumerable> Chunk(ArraySegment values, int chunkSize)
222 | {
223 | var start = 0;
224 | while (start < values.Count)
225 | {
226 | var len = Math.Min(values.Count - start, chunkSize);
227 | yield return new ArraySegment(values.Array!, values.Offset + start, len);
228 | start += chunkSize;
229 | }
230 | }
231 | #endregion
232 |
233 | private static Envelope GetEnclosingEnvelope(IEnumerable items)
234 | {
235 | var envelope = Envelope.EmptyBounds;
236 | foreach (var data in items)
237 | envelope = envelope.Extend(data.Envelope);
238 |
239 | return envelope;
240 | }
241 |
242 | private static List GetAllChildren(List list, Node n)
243 | {
244 | if (n.IsLeaf)
245 | {
246 | list.AddRange(n.Items.Cast());
247 | }
248 | else
249 | {
250 | foreach (var node in n.Items.Cast())
251 | _ = GetAllChildren(list, node);
252 | }
253 |
254 | return list;
255 | }
256 |
257 | }
258 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org/
2 |
3 | root = true
4 |
5 | [*]
6 | indent_style = tab
7 | insert_final_newline = true
8 | tab_width = 4
9 | indent_size = 4
10 | charset = utf-8
11 |
12 |
13 | ### Naming rules: ###
14 |
15 | # Constants are PascalCase
16 | dotnet_naming_rule.constants_should_be_pascal_case.severity = suggestion
17 | dotnet_naming_rule.constants_should_be_pascal_case.symbols = constants
18 | dotnet_naming_rule.constants_should_be_pascal_case.style = constant_style
19 |
20 | dotnet_naming_symbols.constants.applicable_kinds = field, local
21 | dotnet_naming_symbols.constants.required_modifiers = const
22 |
23 | dotnet_naming_style.constant_style.capitalization = pascal_case
24 |
25 | # Non-private readonly fields are PascalCase
26 | dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.severity = suggestion
27 | dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.symbols = non_private_readonly_fields
28 | dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.style = non_private_readonly_field_style
29 |
30 | dotnet_naming_symbols.non_private_readonly_fields.applicable_kinds = field
31 | dotnet_naming_symbols.non_private_readonly_fields.applicable_accessibilities = public, protected, internal, protected_internal, private_protected
32 | dotnet_naming_symbols.non_private_readonly_fields.required_modifiers = readonly
33 |
34 | dotnet_naming_style.non_private_readonly_field_style.capitalization = pascal_case
35 |
36 | # Non-private static fields are PascalCase
37 | dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.severity = suggestion
38 | dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.symbols = non_private_static_fields
39 | dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.style = non_private_static_field_style
40 |
41 | dotnet_naming_symbols.non_private_static_fields.applicable_kinds = field
42 | dotnet_naming_symbols.non_private_static_fields.applicable_accessibilities = public, protected, internal, protected_internal, private_protected
43 | dotnet_naming_symbols.non_private_static_fields.required_modifiers = static
44 |
45 | dotnet_naming_style.non_private_static_field_style.capitalization = pascal_case
46 |
47 | # Static fields are s_camelCase
48 | dotnet_naming_rule.static_fields_should_be_pascal_case.severity = suggestion
49 | dotnet_naming_rule.static_fields_should_be_pascal_case.symbols = static_fields
50 | dotnet_naming_rule.static_fields_should_be_pascal_case.style = static_field_style
51 |
52 | dotnet_naming_symbols.static_fields.applicable_kinds = field
53 | dotnet_naming_symbols.static_fields.required_modifiers = static
54 |
55 | dotnet_naming_style.static_field_style.capitalization = camel_case
56 | dotnet_naming_style.static_field_style.required_prefix = s_
57 |
58 | # Instance fields are camelCase and start with _
59 | dotnet_naming_rule.instance_fields_should_be_camel_case.severity = suggestion
60 | dotnet_naming_rule.instance_fields_should_be_camel_case.symbols = instance_fields
61 | dotnet_naming_rule.instance_fields_should_be_camel_case.style = instance_field_style
62 |
63 | dotnet_naming_symbols.instance_fields.applicable_kinds = field
64 |
65 | dotnet_naming_style.instance_field_style.capitalization = camel_case
66 | dotnet_naming_style.instance_field_style.required_prefix = _
67 |
68 | # Locals and parameters are camelCase
69 | dotnet_naming_rule.locals_should_be_camel_case.severity = suggestion
70 | dotnet_naming_rule.locals_should_be_camel_case.symbols = locals_and_parameters
71 | dotnet_naming_rule.locals_should_be_camel_case.style = camel_case_style
72 |
73 | dotnet_naming_symbols.locals_and_parameters.applicable_kinds = parameter, local
74 |
75 | dotnet_naming_style.camel_case_style.capitalization = camel_case
76 |
77 | # Local functions are PascalCase
78 | dotnet_naming_rule.local_functions_should_be_pascal_case.severity = suggestion
79 | dotnet_naming_rule.local_functions_should_be_pascal_case.symbols = local_functions
80 | dotnet_naming_rule.local_functions_should_be_pascal_case.style = local_function_style
81 |
82 | dotnet_naming_symbols.local_functions.applicable_kinds = local_function
83 |
84 | dotnet_naming_style.local_function_style.capitalization = pascal_case
85 |
86 | # By default, name items with PascalCase
87 | dotnet_naming_rule.members_should_be_pascal_case.severity = suggestion
88 | dotnet_naming_rule.members_should_be_pascal_case.symbols = all_members
89 | dotnet_naming_rule.members_should_be_pascal_case.style = pascal_case_style
90 |
91 | dotnet_naming_symbols.all_members.applicable_kinds = *
92 |
93 | dotnet_naming_style.pascal_case_style.capitalization = pascal_case
94 |
95 |
96 | ### Dotnet code style settings: ###
97 |
98 | # Sort using and Import directives with System.* appearing first
99 | dotnet_sort_system_directives_first = true
100 | dotnet_separate_import_directive_groups = false
101 |
102 | # require accessibility modifiers
103 | dotnet_style_require_accessibility_modifiers = for_non_interface_members:error
104 |
105 | # Avoid "this." and "Me." if not necessary
106 | dotnet_style_qualification_for_field = false:refactoring
107 | dotnet_style_qualification_for_property = false:refactoring
108 | dotnet_style_qualification_for_method = false:refactoring
109 | dotnet_style_qualification_for_event = false:refactoring
110 |
111 | # Use language keywords instead of framework type names for type references
112 | dotnet_style_predefined_type_for_locals_parameters_members = true:error
113 | dotnet_style_predefined_type_for_member_access = true:suggestion
114 |
115 | # Initializers
116 | dotnet_style_object_initializer = true:suggestion
117 | dotnet_style_collection_initializer = true:suggestion
118 | dotnet_style_prefer_collection_expression = when_types_loosely_match:warning
119 |
120 | # Null checks
121 | dotnet_style_coalesce_expression = true:suggestion
122 | dotnet_style_null_propagation = true:suggestion
123 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
124 |
125 | # Tuple Naming
126 | dotnet_style_explicit_tuple_names = true:suggestion
127 | dotnet_style_prefer_inferred_tuple_names = true:suggestion
128 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
129 |
130 | # Assignments
131 | dotnet_style_prefer_conditional_expression_over_assignment = true:error
132 | dotnet_style_prefer_conditional_expression_over_return = true:none
133 | dotnet_style_prefer_compound_assignment = true:warning
134 |
135 | # Parenthesis
136 | dotnet_code_quality_unused_parameters = all:suggestion
137 | dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:suggestion
138 | dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:suggestion
139 | dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:suggestion
140 | dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
141 |
142 | # Miscellaneous
143 | dotnet_style_prefer_auto_properties = true:warning
144 | dotnet_style_prefer_simplified_boolean_expressions = true:warning
145 | dotnet_style_prefer_simplified_interpolation = true:warning
146 | dotnet_style_namespace_match_folder = true:warning
147 | dotnet_style_operator_placement_when_wrapping = beginning_of_line
148 | dotnet_style_readonly_field = true:warning
149 |
150 | # New-line preferences
151 | dotnet_style_allow_multiple_blank_lines_experimental = false:warning
152 | dotnet_style_allow_statement_immediately_after_block_experimental = false:warning
153 | csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = false:warning
154 | csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = false:warning
155 |
156 | # Build scripts
157 | [*.{yml,yaml}]
158 | indent_style = spaces
159 | indent_size = 2
160 |
161 | # XML project files
162 | [*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj,props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
163 | indent_size = 2
164 |
165 | # Code files
166 | [*.cs]
167 |
168 | ## C# style settings:
169 |
170 | # Newline settings
171 | csharp_new_line_before_open_brace = all
172 | csharp_new_line_before_else = true
173 | csharp_new_line_before_catch = true
174 | csharp_new_line_before_finally = true
175 | csharp_new_line_before_members_in_object_initializers = true
176 | csharp_new_line_before_members_in_anonymous_types = true
177 | csharp_new_line_between_query_expression_clauses = true
178 |
179 | # Indentation preferences
180 | csharp_indent_block_contents = true
181 | csharp_indent_braces = false
182 | csharp_indent_case_contents = true
183 | csharp_indent_case_contents_when_block = false
184 | csharp_indent_switch_labels = true
185 | csharp_indent_labels = flush_left
186 |
187 | # Prefer "var" everywhere
188 | csharp_style_var_for_built_in_types = true:suggestion
189 | csharp_style_var_when_type_is_apparent = true:suggestion
190 | csharp_style_var_elsewhere = true:suggestion
191 |
192 | # Prefer method-like constructs to have a block body
193 | csharp_style_expression_bodied_methods = false:none
194 | csharp_style_expression_bodied_constructors = false:none
195 | csharp_style_expression_bodied_operators = false:none
196 |
197 | # Prefer local method constructs to have a block body
198 | csharp_style_expression_bodied_local_functions = true:suggestion
199 |
200 | # Prefer property-like constructs to have an expression-body
201 | csharp_style_expression_bodied_properties = true:suggestion
202 | csharp_style_expression_bodied_indexers = true:suggestion
203 | csharp_style_expression_bodied_accessors = true:suggestion
204 |
205 | # Space preferences
206 | csharp_space_after_cast = false
207 | csharp_space_after_colon_in_inheritance_clause = true
208 | csharp_space_after_comma = true
209 | csharp_space_after_dot = false
210 | csharp_space_after_keywords_in_control_flow_statements = true
211 | csharp_space_after_semicolon_in_for_statement = true
212 | csharp_space_around_binary_operators = before_and_after
213 | csharp_space_around_declaration_statements = do_not_ignore
214 | csharp_space_before_colon_in_inheritance_clause = true
215 | csharp_space_before_comma = false
216 | csharp_space_before_dot = false
217 | csharp_space_before_open_square_brackets = false
218 | csharp_space_before_semicolon_in_for_statement = false
219 | csharp_space_between_empty_square_brackets = false
220 | csharp_space_between_method_call_empty_parameter_list_parentheses = false
221 | csharp_space_between_method_call_name_and_opening_parenthesis = false
222 | csharp_space_between_method_call_parameter_list_parentheses = false
223 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
224 | csharp_space_between_method_declaration_name_and_open_parenthesis = false
225 | csharp_space_between_method_declaration_parameter_list_parentheses = false
226 | csharp_space_between_parentheses = false
227 | csharp_space_between_square_brackets = false
228 |
229 | # Blocks are allowed
230 | csharp_prefer_braces = when_multiline:silent
231 | csharp_preserve_single_line_blocks = true:silent
232 | csharp_preserve_single_line_statements = true:silent
233 |
234 | # Pattern Matching
235 | csharp_style_prefer_pattern_matching = true:warning
236 | csharp_style_prefer_not_pattern = true:warning
237 | csharp_style_prefer_extended_property_pattern = true:warning
238 | csharp_style_pattern_matching_over_as_with_null_check = true:warning
239 | csharp_style_pattern_matching_over_is_with_cast_check = true:warning
240 |
241 | # Namespace
242 | csharp_style_namespace_declarations = file_scoped:error
243 | csharp_using_directive_placement = outside_namespace:warning
244 |
245 | # Suggest more modern language features when available
246 | csharp_prefer_simple_default_expression = true:warning
247 | csharp_prefer_simple_using_statement = true:suggestion
248 | csharp_prefer_static_local_function = true:suggestion
249 | csharp_style_conditional_delegate_call = true:warning
250 | csharp_style_deconstructed_variable_declaration = true:warning
251 | csharp_style_expression_bodied_lambdas = true:suggestion
252 | csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
253 | csharp_style_inlined_variable_declaration = true:warning
254 | csharp_style_prefer_index_operator = true:suggestion
255 | csharp_style_prefer_local_over_anonymous_function = true:suggestion
256 | csharp_style_prefer_method_group_conversion = true:silent
257 | csharp_style_prefer_null_check_over_type_check = true:suggestion
258 | csharp_style_prefer_primary_constructors = true:warning
259 | csharp_style_prefer_range_operator = true:suggestion
260 | csharp_style_prefer_readonly_struct = true:suggestion
261 | csharp_style_prefer_readonly_struct_member = true:suggestion
262 | csharp_style_prefer_switch_expression = true:warning
263 | csharp_style_prefer_top_level_statements = true:silent
264 | csharp_style_prefer_tuple_swap = true:suggestion
265 | csharp_style_prefer_utf8_string_literals = true:suggestion
266 | csharp_style_throw_expression = true:warning
267 | csharp_style_unused_value_assignment_preference = discard_variable:warning
268 | csharp_style_unused_value_expression_statement_preference = discard_variable:warning
269 |
270 | # New Lines
271 | csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
272 | csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent
273 | csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent
274 | csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true:silent
275 | csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:silent
276 |
277 | # Style Analytics
278 | dotnet_analyzer_diagnostic.category-Style.severity = warning
279 |
280 | # XML Documentation
281 | dotnet_diagnostic.CS0105.severity = error # CS0105: Using directive is unnecessary.
282 | dotnet_diagnostic.CS1573.severity = error # CS1573: Missing XML comment for parameter
283 | dotnet_diagnostic.CS1591.severity = error # CS1591: Missing XML comment for publicly visible type or member
284 | dotnet_diagnostic.CS1712.severity = error # CS1712: Type parameter has no matching typeparam tag in the XML comment (but other type parameters do)
285 |
286 | # Async
287 | dotnet_diagnostic.CS1998.severity = error # CS1998: Async method lacks 'await' operators and will run synchronously
288 | dotnet_diagnostic.CS4014.severity = error # CS4014: Because this call is not awaited, execution of the current method continues before the call is completed
289 | dotnet_diagnostic.CA2007.severity = none # CA2007: Consider calling ConfigureAwait on the awaited task
290 |
291 | # Dispose things need disposing
292 | dotnet_diagnostic.CA2000.severity = error # CA2000: Dispose objects before losing scope
293 |
294 | dotnet_diagnostic.CA1034.severity = none # CA1034: Nested types should not be visible
295 | dotnet_diagnostic.CA1515.severity = none # CA1515: Consider making public types internal
296 | dotnet_diagnostic.CA1724.severity = none
297 |
298 | dotnet_diagnostic.MA0049.severity = none
299 | dotnet_diagnostic.IDE0305.severity = none
300 |
--------------------------------------------------------------------------------
/RBush.Test/RBushTests.cs:
--------------------------------------------------------------------------------
1 | using Xunit;
2 |
3 | namespace RBush.Test;
4 |
5 | public class RBushTests
6 | {
7 | private static readonly Point[] s_points = Point.CreatePoints(
8 | new double[,]
9 | {
10 | {0, 0, 0, 0}, {10, 10, 10, 10}, {20, 20, 20, 20}, {25, 0, 25, 0}, {35, 10, 35, 10}, {45, 20, 45, 20}, {0, 25, 0, 25}, {10, 35, 10, 35},
11 | {20, 45, 20, 45}, {25, 25, 25, 25}, {35, 35, 35, 35}, {45, 45, 45, 45}, {50, 0, 50, 0}, {60, 10, 60, 10}, {70, 20, 70, 20}, {75, 0, 75, 0},
12 | {85, 10, 85, 10}, {95, 20, 95, 20}, {50, 25, 50, 25}, {60, 35, 60, 35}, {70, 45, 70, 45}, {75, 25, 75, 25}, {85, 35, 85, 35}, {95, 45, 95, 45},
13 | {0, 50, 0, 50}, {10, 60, 10, 60}, {20, 70, 20, 70}, {25, 50, 25, 50}, {35, 60, 35, 60}, {45, 70, 45, 70}, {0, 75, 0, 75}, {10, 85, 10, 85},
14 | {20, 95, 20, 95}, {25, 75, 25, 75}, {35, 85, 35, 85}, {45, 95, 45, 95}, {50, 50, 50, 50}, {60, 60, 60, 60}, {70, 70, 70, 70}, {75, 50, 75, 50},
15 | {85, 60, 85, 60}, {95, 70, 95, 70}, {50, 75, 50, 75}, {60, 85, 60, 85}, {70, 95, 70, 95}, {75, 75, 75, 75}, {85, 85, 85, 85}, {95, 95, 95, 95},
16 | });
17 |
18 | private static List GetPoints(int cnt) =>
19 | Enumerable.Range(0, cnt)
20 | .Select(i => new Point(
21 | minX: i,
22 | minY: i,
23 | maxX: i,
24 | maxY: i))
25 | .ToList();
26 |
27 | [Test]
28 | public void RootLeafSplitWorks()
29 | {
30 | var data = GetPoints(12);
31 |
32 | var tree = new RBush();
33 | for (var i = 0; i < 9; i++)
34 | tree.Insert(data[i]);
35 |
36 | Assert.Equal(1, tree.Root.Height);
37 | Assert.Equal(9, tree.Root.Children.Count);
38 | Assert.True(tree.Root.IsLeaf);
39 |
40 | Assert.Equal(0, tree.Root.Envelope.MinX);
41 | Assert.Equal(0, tree.Root.Envelope.MinY);
42 | Assert.Equal(8, tree.Root.Envelope.MaxX);
43 | Assert.Equal(8, tree.Root.Envelope.MaxY);
44 |
45 | tree.Insert(data[9]);
46 |
47 | Assert.Equal(2, tree.Root.Height);
48 | Assert.Equal(2, tree.Root.Children.Count);
49 | Assert.False(tree.Root.IsLeaf);
50 |
51 | Assert.Equal(0, tree.Root.Envelope.MinX);
52 | Assert.Equal(0, tree.Root.Envelope.MinY);
53 | Assert.Equal(9, tree.Root.Envelope.MaxX);
54 | Assert.Equal(9, tree.Root.Envelope.MaxY);
55 | }
56 |
57 | [Test]
58 | public void InsertTestData()
59 | {
60 | var tree = new RBush();
61 | foreach (var p in s_points)
62 | tree.Insert(p);
63 |
64 | Assert.Equal(s_points.Length, tree.Count);
65 | Assert.True(new HashSet(s_points)
66 | .SetEquals(tree.Search()));
67 |
68 | Assert.Equal(
69 | s_points.Aggregate(Envelope.EmptyBounds, (e, p) => e.Extend(p.Envelope)),
70 | tree.Envelope);
71 | }
72 |
73 | [Test]
74 | public void BulkLoadTestData()
75 | {
76 | var tree = new RBush();
77 | tree.BulkLoad(s_points);
78 |
79 | Assert.Equal(s_points.Length, tree.Count);
80 | Assert.True(new HashSet(s_points)
81 | .SetEquals(tree.Search()));
82 | }
83 |
84 | [Test]
85 | public void BulkLoadSplitsTreeProperly()
86 | {
87 | var tree = new RBush(maxEntries: 4);
88 | tree.BulkLoad(s_points);
89 | tree.BulkLoad(s_points);
90 |
91 | Assert.Equal(s_points.Length * 2, tree.Count);
92 | Assert.Equal(4, tree.Root.Height);
93 | }
94 |
95 | [Test]
96 | public void BulkLoadMergesTreesProperly()
97 | {
98 | var smaller = GetPoints(10);
99 | var tree1 = new RBush(maxEntries: 4);
100 | tree1.BulkLoad(smaller);
101 | tree1.BulkLoad(s_points);
102 |
103 | var tree2 = new RBush(maxEntries: 4);
104 | tree2.BulkLoad(s_points);
105 | tree2.BulkLoad(smaller);
106 |
107 | Assert.True(tree1.Count == tree2.Count);
108 | Assert.True(tree1.Root.Height == tree2.Root.Height);
109 |
110 | var allPoints = new HashSet(s_points.Concat(smaller));
111 | Assert.True(allPoints.SetEquals(tree1.Search()));
112 | Assert.True(allPoints.SetEquals(tree2.Search()));
113 | }
114 |
115 | [Test]
116 | public void SearchReturnsEmptyResultIfNothingFound()
117 | {
118 | var tree = new RBush(maxEntries: 4);
119 | tree.BulkLoad(s_points);
120 |
121 | Assert.Equal([], tree.Search(new Envelope(200, 200, 210, 210)));
122 | }
123 |
124 | [Test]
125 | public void SearchReturnsMatchingResults()
126 | {
127 | var tree = new RBush(maxEntries: 4);
128 | tree.BulkLoad(s_points);
129 |
130 | var searchEnvelope = new Envelope(40, 20, 80, 70);
131 | var shouldFindPoints = new HashSet(s_points
132 | .Where(p => p.Envelope.Intersects(searchEnvelope)));
133 |
134 | Assert.True(shouldFindPoints.SetEquals(tree.Search(searchEnvelope)));
135 | }
136 |
137 | [Test]
138 | public void BasicRemoveTest()
139 | {
140 | var tree = new RBush(maxEntries: 4);
141 | tree.BulkLoad(s_points);
142 |
143 | var len = s_points.Length;
144 |
145 | _ = tree.Delete(s_points[0]);
146 | _ = tree.Delete(s_points[1]);
147 | _ = tree.Delete(s_points[2]);
148 |
149 | _ = tree.Delete(s_points[len - 1]);
150 | _ = tree.Delete(s_points[len - 2]);
151 | _ = tree.Delete(s_points[len - 3]);
152 |
153 | var shouldFindPoints = new HashSet(s_points
154 | .Skip(3).Take(len - 6));
155 |
156 | Assert.True(shouldFindPoints.SetEquals(tree.Search()));
157 | Assert.Equal(
158 | shouldFindPoints.Aggregate(Envelope.EmptyBounds, (e, p) => e.Extend(p.Envelope)),
159 | tree.Envelope);
160 | }
161 |
162 | [Test]
163 | public void NonExistentItemCanBeDeleted()
164 | {
165 | var tree = new RBush(maxEntries: 4);
166 | tree.BulkLoad(s_points);
167 |
168 | _ = tree.Delete(new Point(13, 13, 13, 13));
169 | Assert.Equal(s_points.Length, tree.Count);
170 | }
171 |
172 | [Test]
173 | public void DeleteTreeIsEmptyShouldNotThrow()
174 | {
175 | var tree = new RBush();
176 |
177 | _ = tree.Delete(new Point(1, 1, 1, 1));
178 |
179 | Assert.Equal(0, tree.Count);
180 | }
181 |
182 | [Test]
183 | public void DeleteDeletingLastPointShouldNotThrow()
184 | {
185 | var tree = new RBush();
186 | var p = new Point(1, 1, 1, 1);
187 | tree.Insert(p);
188 |
189 | _ = tree.Delete(p);
190 |
191 | Assert.Equal(0, tree.Count);
192 | }
193 |
194 | [Test]
195 | public void ClearWorks()
196 | {
197 | var tree = new RBush(maxEntries: 4);
198 | tree.BulkLoad(s_points);
199 | tree.Clear();
200 |
201 | Assert.Equal(0, tree.Count);
202 | Assert.Empty(tree.Root.Children);
203 | }
204 |
205 | [Test]
206 | public void TestSearchAfterInsert()
207 | {
208 | var maxEntries = 9;
209 | var tree = new RBush(maxEntries);
210 |
211 | var firstSet = s_points.Take(maxEntries).ToList();
212 | var firstSetEnvelope = firstSet
213 | .Aggregate(Envelope.EmptyBounds, (e, p) => e.Extend(p.Envelope));
214 |
215 | foreach (var p in firstSet)
216 | tree.Insert(p);
217 |
218 | Assert.True(new HashSet(firstSet)
219 | .SetEquals(tree.Search(firstSetEnvelope)));
220 | }
221 |
222 | [Test]
223 | public void TestSearchAfterInsertWithSplitRoot()
224 | {
225 | var maxEntries = 4;
226 | var tree = new RBush(maxEntries);
227 |
228 | var numFirstSet = (maxEntries * maxEntries) + 2; // Split-root will occur twice.
229 | var firstSet = s_points.Take(numFirstSet);
230 |
231 | foreach (var p in firstSet)
232 | tree.Insert(p);
233 |
234 | var numExtraPoints = 5;
235 | var extraPointsSet = s_points.Skip(s_points.Length - numExtraPoints).ToList();
236 | var extraPointsSetEnvelope = extraPointsSet
237 | .Aggregate(Envelope.EmptyBounds, (e, p) => e.Extend(p.Envelope));
238 |
239 | foreach (var p in extraPointsSet)
240 | tree.Insert(p);
241 |
242 | // first 10 entries and last 5 entries are completely mutually exclusive
243 | // so searching the bounds of the new set should only return the new set exactly
244 | Assert.True(new HashSet(extraPointsSet)
245 | .SetEquals(tree.Search(extraPointsSetEnvelope)));
246 | }
247 |
248 | [Test]
249 | public void TestSearchAfterBulkLoadWithSplitRoot()
250 | {
251 | var maxEntries = 4;
252 | var tree = new RBush(maxEntries);
253 |
254 | var numFirstSet = (maxEntries * maxEntries) + 2; // Split-root will occur twice.
255 | var firstSet = s_points.Take(numFirstSet);
256 |
257 | tree.BulkLoad(firstSet);
258 |
259 | var numExtraPoints = 5;
260 | var extraPointsSet = s_points.Skip(s_points.Length - numExtraPoints).ToList();
261 | var extraPointsSetEnvelope = extraPointsSet
262 | .Aggregate(Envelope.EmptyBounds, (e, p) => e.Extend(p.Envelope));
263 |
264 | tree.BulkLoad(extraPointsSet);
265 |
266 | // first 10 entries and last 5 entries are completely mutually exclusive
267 | // so searching the bounds of the new set should only return the new set exactly
268 | Assert.True(new HashSet(extraPointsSet)
269 | .SetEquals(tree.Search(extraPointsSetEnvelope)));
270 | }
271 |
272 | [Test]
273 | public void AdditionalRemoveTest()
274 | {
275 | var tree = new RBush();
276 | var numDelete = 18;
277 |
278 | foreach (var p in s_points)
279 | tree.Insert(p);
280 |
281 | foreach (var p in s_points.Take(numDelete))
282 | _ = tree.Delete(p);
283 |
284 | Assert.Equal(s_points.Length - numDelete, tree.Count);
285 | Assert.True(new HashSet(s_points.Skip(numDelete))
286 | .SetEquals(tree.Search()));
287 | }
288 |
289 | [Test]
290 | public void BulkLoadAfterDeleteTest1()
291 | {
292 | var pts = GetPoints(20);
293 | var ptsDelete = pts.Take(18);
294 | var tree = new RBush(maxEntries: 4);
295 |
296 | tree.BulkLoad(pts);
297 |
298 | foreach (var item in ptsDelete)
299 | _ = tree.Delete(item);
300 |
301 | tree.BulkLoad(ptsDelete);
302 |
303 | Assert.Equal(pts.Count, tree.Search().Count);
304 | Assert.True(new HashSet(pts).SetEquals(tree.Search()));
305 | }
306 |
307 | [Test]
308 | public void BulkLoadAfterDeleteTest2()
309 | {
310 | var pts = GetPoints(20);
311 | var ptsDelete = pts.Take(4);
312 | var tree = new RBush(maxEntries: 4);
313 |
314 | tree.BulkLoad(pts);
315 |
316 | foreach (var item in ptsDelete)
317 | _ = tree.Delete(item);
318 |
319 | tree.BulkLoad(ptsDelete);
320 |
321 | Assert.Equal(pts.Count, tree.Search().Count);
322 | Assert.True(new HashSet(pts)
323 | .SetEquals(tree.Search()));
324 | }
325 |
326 | [Test]
327 | public void InsertAfterDeleteTest1()
328 | {
329 | var pts = GetPoints(20);
330 | var ptsDelete = pts.Take(18).ToList();
331 | var tree = new RBush(maxEntries: 4);
332 |
333 | foreach (var item in pts)
334 | tree.Insert(item);
335 |
336 | foreach (var item in ptsDelete)
337 | _ = tree.Delete(item);
338 |
339 | foreach (var item in ptsDelete)
340 | tree.Insert(item);
341 |
342 | Assert.Equal(pts.Count, tree.Search().Count);
343 | Assert.True(new HashSet(pts)
344 | .SetEquals(tree.Search()));
345 | }
346 |
347 | [Test]
348 | public void InsertAfterDeleteTest2()
349 | {
350 | var pts = GetPoints(20);
351 | var ptsDelete = pts.Take(4).ToList();
352 | var tree = new RBush(maxEntries: 4);
353 |
354 | foreach (var item in pts)
355 | tree.Insert(item);
356 |
357 | foreach (var item in ptsDelete)
358 | _ = tree.Delete(item);
359 |
360 | foreach (var item in ptsDelete)
361 | tree.Insert(item);
362 |
363 | Assert.Equal(pts.Count, tree.Search().Count);
364 | Assert.True(new HashSet(pts)
365 | .SetEquals(tree.Search()));
366 | }
367 |
368 | private readonly List _missingEnvelopeTestData =
369 | [
370 | new Point(minX: 35.0457204123358, minY: 31.5946330633669, maxX: 35.1736414417038, maxY: 31.7658263429689),
371 | new Point(minX: 35.0011136524732, minY: 31.6701999643473, maxX: 35.0119650302309, maxY: 31.6763344627552),
372 | new Point(minX: 35.4519996266397, minY: 33.0521061332025, maxX: 35.6225745715679, maxY: 33.2873426178667),
373 | new Point(minX: 35.3963660077949, minY: 31.9833569998672, maxX: 35.609059834246, maxY: 32.6939307443726),
374 | new Point(minX: 34.8283506083251, minY: 32.2548085664601, maxX: 35.074434567496, maxY: 32.3931011267767),
375 | new Point(minX: 34.8331658736056, minY: 31.7799489556277, maxX: 35.0591449537042, maxY: 32.0096644072503),
376 | new Point(minX: 35.4232929081405, minY: 32.8928176841194, maxX: 35.6402606700131, maxY: 33.0831804221654),
377 | new Point(minX: 35.1547685550823, minY: 32.6409460084027, maxX: 35.3829851953318, maxY: 32.8357311630527),
378 | new Point(minX: 34.8921664127959, minY: 31.6053844677954, maxX: 35.0343017543245, maxY: 31.7780322047787),
379 | new Point(minX: 34.9263969975396, minY: 32.65352493197, maxX: 35.1011727083577, maxY: 32.8432505478028),
380 | new Point(minX: 35.2394825164923, minY: 31.8026823309661, maxX: 35.2967869133878, maxY: 31.8621074757081),
381 | new Point(minX: 35.3717873599347, minY: 32.9175807550062, maxX: 35.6478183130483, maxY: 33.1679615668549),
382 | new Point(minX: 35.196978533143, minY: 31.5634195882077, maxX: 35.6432919646234, maxY: 32.3029676465732),
383 | new Point(minX: 35.0004845245009, minY: 31.7630003816153, maxX: 35.1719739039302, maxY: 31.8400308568811),
384 | new Point(minX: 34.280847167853, minY: 31.0494793743498, maxX: 34.8906355571353, maxY: 31.5639320874003),
385 | new Point(minX: 34.6095401534748, minY: 31.7162092103111, maxX: 34.9053153865301, maxY: 31.9217629772612),
386 | new Point(minX: 34.836714064777, minY: 32.1214817541366, maxX: 34.9971028731628, maxY: 32.2220684750668),
387 | new Point(minX: 34.8718260788802, minY: 31.599197115334, maxX: 35.0378162094246, maxY: 31.8420049154987),
388 | new Point(minX: 35.1186400639205, minY: 31.8691974363715, maxX: 35.1868012907541, maxY: 31.8937001264381),
389 | new Point(minX: 34.7893345961988, minY: 32.1180328589326, maxX: 34.8912984464358, maxY: 32.2478719368633),
390 | new Point(minX: 35.0560255797127, minY: 32.6886762251772, maxX: 35.2038027609729, maxY: 32.8736600563471),
391 | new Point(minX: 34.5118108403562, minY: 31.482226934431, maxX: 35.034306207294, maxY: 31.7778320509551),
392 | new Point(minX: 34.7421834416939, minY: 32.0294016078206, maxX: 34.8522596224097, maxY: 32.1466473164001),
393 | new Point(minX: 35.2850369921205, minY: 31.736701022482, maxX: 35.3671187142204, maxY: 31.8582696930051),
394 | new Point(minX: 34.3920880329569, minY: 30.3321812616403, maxX: 35.2259162784014, maxY: 30.9730153115276),
395 | new Point(minX: 34.3430710039186, minY: 30.9079503533306, maxX: 35.3548545278468, maxY: 31.4431220155161),
396 | new Point(minX: 34.6081103075024, minY: 31.5923121167122, maxX: 35.1015149543026, maxY: 32.1534717814552),
397 | new Point(minX: 34.7988787882969, minY: 32.0164071891458, maxX: 34.9092712663857, maxY: 32.1086750906882),
398 | new Point(minX: 34.9359618915959, minY: 32.172467059147, maxX: 35.0574178548968, maxY: 32.283413702632),
399 | new Point(minX: 34.8656317039855, minY: 32.3738879752974, maxX: 35.0578412581373, maxY: 32.5522748453893),
400 | new Point(minX: 35.3422616609697, minY: 32.9728983905125, maxX: 35.8977567880068, maxY: 33.2954674685247),
401 | new Point(minX: 34.6588626003523, minY: 31.0609203542086, maxX: 34.9847215585919, maxY: 31.4067422387869),
402 | new Point(minX: 34.5795324959208, minY: 29.4630762907736, maxX: 35.2158119200309, maxY: 30.3333639093901),
403 | new Point(minX: 35.3205147130136, minY: 32.6967048307378, maxX: 35.646707739197, maxY: 32.9039553396515),
404 | new Point(minX: 35.1178267119206, minY: 30.3384402926262, maxX: 35.490247102555, maxY: 31.4709171748353),
405 | new Point(minX: 34.8226151100133, minY: 32.2534003894817, maxX: 36.0421996128722, maxY: 33.3817549409005),
406 | new Point(minX: 34.2676635415493, minY: 30.6224958789486, maxX: 34.6920096335759, maxY: 31.0234624221816),
407 | new Point(minX: 35.1817645082021, minY: 32.4848680398321, maxX: 35.5646264318558, maxY: 32.7613293133493),
408 | new Point(minX: 34.9166085325228, minY: 31.7671694138349, maxX: 35.1725615321371, maxY: 31.8549410969553),
409 | new Point(minX: 34.9337083845948, minY: 31.876626985909, maxX: 35.0264053464613, maxY: 32.0150065654742),
410 | new Point(minX: 34.835347572723, minY: 32.220362279336, maxX: 35.0109342273868, maxY: 32.3417009250043),
411 | new Point(minX: 35.2948832076848, minY: 30.9104180607267, maxX: 35.5915134856063, maxY: 31.5574185582711),
412 | new Point(minX: 34.7905250664059, minY: 32.1179577036489, maxX: 35.0581623045431, maxY: 32.3417009250043),
413 | new Point(minX: 34.956024319859, minY: 31.7012421676793, maxX: 35.0116424969789, maxY: 31.7745298632115),
414 | new Point(minX: 35.0202255429427, minY: 30.2435917751572, maxX: 35.4679175364723, maxY: 30.9740219831866),
415 | new Point(minX: 34.8966295938088, minY: 30.9115962957363, maxX: 35.4390784816745, maxY: 31.4930353034973),
416 | new Point(minX: 35.0662920752962, minY: 32.7971835290283, maxX: 35.4601689230693, maxY: 33.107112234543),
417 | new Point(minX: 34.7340665896918, minY: 31.9989943342548, maxX: 34.8204120930481, maxY: 32.0474591284795),
418 | new Point(minX: 34.6941456072459, minY: 31.8964808285729, maxX: 34.8640822052443, maxY: 32.0383069680269),
419 | new Point(minX: 34.8811115785741, minY: 31.6065949131837, maxX: 35.0267092460506, maxY: 31.7698965550383),
420 | new Point(minX: 34.8003277513142, minY: 32.052551536477, maxX: 34.8331150100439, maxY: 32.0806274386815),
421 | new Point(minX: 34.8449718901336, minY: 32.003746842197, maxX: 35.0354837295605, maxY: 32.1384163064534),
422 | new Point(minX: 34.6192253584107, minY: 31.7651896373863, maxX: 34.7668538532351, maxY: 31.8527172183214),
423 | new Point(minX: 34.9038898727945, minY: 32.4160192641257, maxX: 35.2017974842687, maxY: 32.7047870690854),
424 | new Point(minX: 34.5711602610893, minY: 30.484129839607, maxX: 34.9186022069881, maxY: 31.089343112111),
425 | new Point(minX: 34.1949967186793, minY: 30.3364832392137, maxX: 35.475053605904, maxY: 31.7707872010952),
426 | new Point(minX: 35.5779734622088, minY: 32.7292601351151, maxX: 35.8581674148078, maxY: 33.2881090529044),
427 | new Point(minX: 35.2749705330965, minY: 31.7397911324406, maxX: 35.3704140716109, maxY: 31.8401766909486),
428 | new Point(minX: 35.3673933021049, minY: 32.6843626605618, maxX: 35.9062174225714, maxY: 33.3147865589053),
429 | new Point(minX: 34.9008058486684, minY: 31.3746779045228, maxX: 35.5757203915414, maxY: 31.9598496573455),
430 | new Point(minX: 35.598379933386, minY: 32.6537719349482, maxX: 35.9300290636195, maxY: 33.0086059628293),
431 | new Point(minX: 34.6095401534748, minY: 31.7162092103111, maxX: 34.8405932932086, maxY: 31.9110715079679),
432 | new Point(minX: 34.8216158424473, minY: 32.0658349197875, maxX: 34.8603966863607, maxY: 32.1086750906882),
433 | new Point(minX: 34.7332714270174, minY: 31.9886947121707, maxX: 34.8215417657172, maxY: 32.046276160073),
434 | new Point(minX: 34.8149293733854, minY: 31.9417187138479, maxX: 34.9091599462782, maxY: 32.0574043905775),
435 | new Point(minX: 34.8449718901336, minY: 32.0178299374316, maxX: 35.0354837295605, maxY: 32.1394735385545),
436 | new Point(minX: 34.7988787882969, minY: 32.0178353029891, maxX: 34.8569010984257, maxY: 32.1055722745323),
437 | new Point(minX: 34.8280655613399, minY: 31.7799489556277, maxX: 35.0591449537042, maxY: 32.0377438786708),
438 | new Point(minX: 34.7408084826793, minY: 31.7662100576601, maxX: 34.9053153865301, maxY: 31.9217629772612),
439 | new Point(minX: 34.6847053378812, minY: 31.8964808285729, maxX: 34.8640822052443, maxY: 32.0105347066497),
440 | new Point(minX: 34.7701973085286, minY: 32.0901266819317, maxX: 34.8524324985417, maxY: 32.1473290327111),
441 | new Point(minX: 34.741988219981, minY: 32.0294093609064, maxX: 34.8140075719916, maxY: 32.1044330767071),
442 | new Point(minX: 34.835347572723, minY: 32.2091924053645, maxX: 35.0109342273868, maxY: 32.3417009250043),
443 | new Point(minX: 34.836714064777, minY: 32.1215021618748, maxX: 34.9971028731628, maxY: 32.2220684750668),
444 | new Point(minX: 34.7873539009184, minY: 32.1180328589326, maxX: 34.8883177683104, maxY: 32.2478719368633),
445 | new Point(minX: 34.92831830183, minY: 32.1710611024405, maxX: 35.0574178548968, maxY: 32.2903664069317),
446 | new Point(minX: 35.3205147130136, minY: 32.6967048307378, maxX: 35.6457702965872, maxY: 32.9039553396515),
447 | new Point(minX: 34.8952563054852, minY: 31.3899298627969, maxX: 35.5736607373982, maxY: 31.9625925083573),
448 | new Point(minX: 34.8449718901336, minY: 32.0178299374316, maxX: 35.0354837295605, maxY: 32.1384163064534),
449 | new Point(minX: 34.8890894442614, minY: 32.3883416928594, maxX: 35.2017974842687, maxY: 32.7047870690854),
450 | new Point(minX: 34.8656317039855, minY: 32.3738879752974, maxX: 35.0423849643375, maxY: 32.4979457789334),
451 | new Point(minX: 34.7701973085286, minY: 32.090031514185, maxX: 34.8524324985417, maxY: 32.1473290327111),
452 | new Point(minX: 34.993192032199, minY: 31.9080191148317, maxX: 35.5996892102095, maxY: 32.3212391412162),
453 | new Point(minX: 35.3963660077949, minY: 32.2180076624529, maxX: 35.609059834246, maxY: 32.6939307443726),
454 | new Point(minX: 34.9325067524752, minY: 32.0409113304089, maxX: 35.0356750377975, maxY: 32.1386430270217),
455 | new Point(minX: 35.043472159742, minY: 32.6886762251772, maxX: 35.2038027609729, maxY: 32.8736600563471),
456 | new Point(minX: 34.8449718901336, minY: 32.0178299374316, maxX: 34.9490546869212, maxY: 32.1297427854206),
457 | new Point(minX: 34.9263969975396, minY: 32.65352493197, maxX: 35.1011727083577, maxY: 32.8384413743296),
458 | new Point(minX: 35.1026228484405, minY: 32.6274637163331, maxX: 35.3827736665259, maxY: 32.8356933279248),
459 | new Point(minX: 34.8883898699046, minY: 32.3888924841112, maxX: 35.2011562856838, maxY: 32.6849848327049),
460 | new Point(minX: 34.4036190736879, minY: 31.4707834956166, maxX: 35.0902632353055, maxY: 32.3567064968128),
461 | new Point(minX: 34.6095401534748, minY: 31.7162092103111, maxX: 35.0591449537042, maxY: 32.0383069680269),
462 | new Point(minX: 34.7421834416939, minY: 32.003746842197, maxX: 35.0354837295605, maxY: 32.1466473164001),
463 | new Point(minX: 34.7905250664059, minY: 32.1179577036489, maxX: 35.0581623045431, maxY: 32.3417009250043),
464 | ];
465 |
466 | [Test]
467 | public void MissingEnvelopeTestInsertIndividually()
468 | {
469 | var tree = new RBush();
470 | foreach (var p in _missingEnvelopeTestData)
471 | tree.Insert(p);
472 |
473 | var envelope = new Envelope(
474 | MinX: 34.73274678,
475 | MinY: 31.87729923,
476 | MaxX: 34.73274678,
477 | MaxY: 31.87729923);
478 | Assert.Equal(
479 | expected: tree.Search().Count(p => p.Envelope.Intersects(envelope)),
480 | actual: tree.Search(envelope).Count);
481 | }
482 |
483 | [Test]
484 | public void TestBulk()
485 | {
486 | var tree = new RBush();
487 | tree.BulkLoad(_missingEnvelopeTestData);
488 |
489 | var envelope = new Envelope(
490 | MinX: 34.73274678,
491 | MinY: 31.87729923,
492 | MaxX: 34.73274678,
493 | MaxY: 31.87729923);
494 | Assert.Equal(
495 | expected: tree.Search().Count(p => p.Envelope.Intersects(envelope)),
496 | actual: tree.Search(envelope).Count);
497 | }
498 | }
499 |
--------------------------------------------------------------------------------