A collection of space partitioning trees for Rust
Spart (space partitioning trees) is a Rust library that provides implementations of several common space partitioning tree data structures that can be used for indexing 2D and 3D point data to perform fast spatial queries, like k-nearest neighbor (kNN) and range search.
The library also includes Python bindings (see pyspart), so it can easily be used in Python applications.
At the moment, the following tree data structures and features are supported:
| # | Tree Type | 2D | 3D | kNN Search | Radius Search |
|---|---|---|---|---|---|
| 1 | Quadtree | ✓ | ✓ | ✓ | |
| 2 | Octree | ✓ | ✓ | ✓ | |
| 3 | Kd-tree | ✓ | ✓ | ✓ | ✓ |
| 4 | R-tree | ✓ | ✓ | ✓ | ✓ |
| 5 | R*-tree | ✓ | ✓ | ✓ | ✓ |
[!IMPORTANT] Spart is in early development, so bugs and breaking API changes are expected. Please use the issues page to report bugs or request features.
Installation
Spart requires Rust 1.83.0 or later.
Python Bindings
You can install the Python bindings for Spart using pip:
pip install pyspart
Check out the pyspart directory for more information about using Spart from Python.
Documentation
For the Rust API documentation, see docs.rs/spart.
Basic Concepts
The basic building blocks of Spart are point and tree.
Point
A point is a tuple of coordinates plus an optional data payload of any type.
There are two types of points: Point2D and Point3D.
Example of 2D and 3D points:
use ;
Tree
A tree is a spatial data structure that indexes points and provides methods for querying them.
Currently, the following trees are implemented:
- Quadtree (2D)
- Octree (3D)
- Kd-tree (2D and 3D)
- R-tree (2D and 3D)
- R*-tree (2D and 3D)
A tree provides at least the following methods:
new: creates a new tree given the following parameters:- The bounding area of the tree (for Quadtree and Octree only)
- The number of dimensions (for Kd-tree only)
- The maximum capacity of points per node (for Quadtree, Octree, and R-tree)
insert: inserts a point into the tree.insert_bulk: inserts multiple points into the tree at once.- This is generally more efficient than inserting points one by one.
delete: removes a point from the tree.knn_search: finds the k nearest neighbors to a query point.- The inputs are the query point and the number of neighbors to find.
range_search: finds all points within a given range of a query point.- The inputs are the query point and the range within which to search.
[!NOTE] Currently, the following properties hold for all trees:
- Duplicates are allowed: inserting a duplicate point will add another copy to the tree.
- Searches return duplicates: both
knn_searchandrange_searchcan return duplicate points if they were previously inserted.- Deletion removes one instance: if there are duplicate points, the
deleteoperation removes only one instance of the point from the tree.- A
knn_searchwithk=0will return an empty list.- A
knn_searchwithkgreater than the number of points in the tree will return all points.- A
range_searchwith a radius of0will return only points with the exact same coordinates.The distance metric used for nearest neighbor and range searches is the Euclidean distance by default. However, you can use a custom distance metric by implementing the
DistanceMetrictrait.For example, here is how you can define and use the Manhattan distance:
use ; // 1. Define a struct for your distance metric. ; // 2. Implement the `DistanceMetric` trait for your point type. // 3. Use it in a search function. // tree.knn_search::<ManhattanDistance>(&query_point, 1);
Serialization
Spart trees can be serialized and deserialized using the serde feature.
To enable serialization in Rust, you need to enable the serde feature in your Cargo.toml file:
[]
= { = "0.3.0", = ["serde"] }
Then, you can use bincode (or any other serde-compatible library) to serialize and deserialize the tree.
For example, you can save and load a tree to and from a file:
use ;
use Quadtree;
use File;
use ;
Debugging Mode
You can enable debugging mode for Spart by setting the DEBUG_SPART environment variable to true or 1.
# Enable debugging mode on Linux and macOS
# Enable debugging mode on Windows (PowerShell)
$env:DEBUG_SPART = "true"
[!NOTE] When debugging mode is enabled, Spart will be very verbose. It is recommended to use this only for debugging purposes.
Examples
- For Rust examples, see the examples directory.
- For Python examples, see pyspart/examples.
Feature Roadmap
-
Core Data Structures
- Quadtree (2D)
- Octree (3D)
- Kd-tree (2D and 3D)
- R-tree (2D and 3D)
- R*-tree (2D and 3D)
-
Supported Geometries and Queries
- Point data (
Point2D,Point3D) - kNN search
- Circular or Spherical range search
- Rectangular/Cuboid range search (
range_search_bbox) -
updatemethod for moving points (currently needs delete + insert) - Support for storing non-point geometries (for example, lines, polygons)
- Advanced intersection queries (like finding all stored items that intersect a given polygon)
- Point data (
-
Performance and Optimization
- Bulk loading implementations for faster tree construction
- Thread-safety for concurrent reads (like
&Treeaccessible from multiple threads) - Arena allocation for tree nodes to improve cache locality
- SIMD-accelerated distance and intersection calculations if possible
-
API and Developer Experience
- Simple API for tree creation and manipulation
- Serialization and deserialization via
serde - Custom distance metric support
- Public iterators for tree traversal (something like
tree.iter()) - Tree diagnostic methods (
height(),node_count(), etc.) - Replace internal panics with
Result-based error handling (for example, for invalid dimensions)
-
Ecosystem and Bindings
- Python bindings (
pyspart) for all tree types - Full feature parity for Python bindings (like bulk loading for all trees)
- WebAssembly support for browser and serverless environments
- Python bindings (
-
Benchmarks
- Benchmarks for comparing the performance of tree implementations and operations
- Benchmarks for comparing the performance against other similar libraries
Contributing
See CONTRIBUTING.md for details on how to make a contribution.
License
Spart is available under the terms of either of the following licenses:
- MIT License (LICENSE-MIT)
- Apache License, Version 2.0 (LICENSE-APACHE)
Acknowledgements
- The logo is from SVG Repo.