pub struct RTree<T, Params = DefaultParams>where
Params: RTreeParams,
T: RTreeObject,{ /* private fields */ }
Expand description
An n-dimensional r-tree data structure.
R-Trees
R-Trees are data structures containing multi-dimensional objects like points, rectangles or polygons. They are optimized for retrieving the nearest neighbor at any point.
R-trees can efficiently find answers to queries like “Find the nearest point of a polygon”,
“Find all police stations within a rectangle” or “Find the 10 nearest restaurants, sorted
by their distances”. Compared to a naive implementation for these scenarios that runs
in O(n)
for n
inserted elements, r-trees reduce this time to O(log(n))
.
However, creating an r-tree is time consuming
and runs in O(n * log(n))
. Thus, r-trees are suited best if many queries and only few
insertions are made. Also, rstar supports bulk loading,
which cuts down the constant factors when creating an r-tree significantly compared to
sequential insertions.
R-trees are also dynamic, thus, points can be inserted and removed even if the tree has been created before.
Partitioning heuristics
The inserted objects are internally partitioned into several boxes which should have small
overlap and volume. This is done heuristically. While the originally proposed heuristic focused
on fast insertion operations, the resulting r-trees were often suboptimally structured. Another
heuristic, called R*-tree
(r-star-tree), was proposed to improve the tree structure at the cost of
longer insertion operations and is currently the crate’s only implemented
insertion strategy.
Further reading
For more information refer to the wikipedia article.
Usage
The items inserted into an r-tree must implement the RTreeObject trait. To support nearest neighbor queries, implement the PointDistance trait. Some useful geometric primitives that implement the above traits can be found in the primitives module.
Example
use rstar::RTree;
let mut tree = RTree::new();
tree.insert([0.1, 0.0f32]);
tree.insert([0.2, 0.1]);
tree.insert([0.3, 0.0]);
assert_eq!(tree.nearest_neighbor(&[0.4, -0.1]), Some(&[0.3, 0.0]));
tree.remove(&[0.3, 0.0]);
assert_eq!(tree.nearest_neighbor(&[0.4, 0.3]), Some(&[0.2, 0.1]));
assert_eq!(tree.size(), 2);
// &RTree implements IntoIterator!
for point in &tree {
println!("Tree contains a point {:?}", point);
}
Supported point types
All types implementing the Point trait can be used as underlying point type. By default, fixed size arrays can be used as points.
Type Parameters
T
: The type of objects stored in the r-tree.Params
: Compile time parameters that change the r-trees internal layout. Refer to the RTreeParams trait for more information.
Defining methods generic over r-trees
If a library defines a method that should be generic over the r-tree type signature, make sure to include both type parameters like this:
pub fn generic_rtree_function<T, Params>(tree: &mut RTree<T, Params>)
where
T: RTreeObject,
Params: RTreeParams
{
// ...
}
Otherwise, any user of generic_rtree_function
would be forced to use
a tree with default parameters.
Runtime and Performance
The runtime of query operations (e.g. nearest neighbor
or contains
) is usually
O(log(n))
, where n
refers to the number of elements contained in the r-tree.
A naive sequential algorithm would take O(n)
time. However, r-trees incur higher
build up times: inserting an element into an r-tree costs O(log(n))
time.
Bulk loading
In many scenarios, insertion is only done once for many points. In this case,
bulk_load will be considerably faster. Its total run time
is still O(log(n))
.
Element distribution
The tree’s performance heavily relies on the spatial distribution of its elements. Best performance is achieved if:
- No element is inserted more than once
- The overlapping area of elements should be as small a possible.
For the edge case that all elements are overlapping (e.g, one and the same element
is contained n
times), the performance of most operations usually degrades to O(n)
.
(De)Serialization
Enable the serde_serialize
for Serde support.
Implementations
sourceimpl<T> RTree<T>where
T: RTreeObject,
impl<T> RTree<T>where
T: RTreeObject,
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty r-tree.
The created r-tree is configured with default parameters.
sourcepub fn bulk_load(elements: Vec<T>) -> Self
pub fn bulk_load(elements: Vec<T>) -> Self
Creates a new r-tree with some elements already inserted.
This method should be the preferred way for creating r-trees. It both runs faster and yields an r-tree with better internal structure that improves query performance.
This method implements the overlap minimizing top down bulk loading algorithm as described in this paper.
Runtime
Bulk loading runs in O(n * log(n))
, where n
is the number of loaded
elements.
sourceimpl<T> RTree<T>where
T: RTreeObject + Send + Sync + 'static,
T::Envelope: Send + Sync,
impl<T> RTree<T>where
T: RTreeObject + Send + Sync + 'static,
T::Envelope: Send + Sync,
sourcepub fn bulk_load_parallel(elements: Vec<T>) -> Self
pub fn bulk_load_parallel(elements: Vec<T>) -> Self
Creates a new r-tree with some elements already inserted.
See bulk_load for general information. This method performs the loading on multiple threads in parallel. However, as there is some synchronization overhead, this method may perform slower for only a few objects. The break even point may be around 40000 elements but will vary depending on the number of available cores.
Runtime
Bulk loading runs in O(n * log(n))
, where n
is the number of loaded
elements.
sourceimpl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: RTreeObject + Send + Sync + 'static,
T::Envelope: Send + Sync,
impl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: RTreeObject + Send + Sync + 'static,
T::Envelope: Send + Sync,
sourcepub fn bulk_load_with_params_parallel(elements: Vec<T>) -> Self
pub fn bulk_load_with_params_parallel(elements: Vec<T>) -> Self
Creates a new r-tree with some elements already inserted and configurable parameters.
See bulk_load_with_params for general information. This method performs the loading on multiple threads in parallel. However, as there is some synchronization overhead, this method may perform slower for only a few objects. The break even point may be around 40000 elements but will vary depending on the number of available cores and the used rtree parameters.
Runtime
Bulk loading runs in O(n * log(n))
, where n
is the number of loaded
elements.
sourceimpl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: RTreeObject,
impl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: RTreeObject,
sourcepub fn new_with_params() -> Self
pub fn new_with_params() -> Self
Creates a new, empty r-tree.
The tree’s compile time parameters must be specified. Refer to the RTreeParams trait for more information and a usage example.
sourcepub fn bulk_load_with_params(elements: Vec<T>) -> Self
pub fn bulk_load_with_params(elements: Vec<T>) -> Self
Creates a new r-tree with some given elements and configurable parameters.
For more information refer to bulk_load and RTreeParameters. There is also a multi threaded variant version of this method.
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the number of objects in an r-tree.
Example
use rstar::RTree;
let mut tree = RTree::new();
assert_eq!(tree.size(), 0);
tree.insert([0.0, 1.0, 2.0]);
assert_eq!(tree.size(), 1);
tree.remove(&[0.0, 1.0, 2.0]);
assert_eq!(tree.size(), 0);
sourcepub fn iter(&self) -> SelectionIterator<'_, T, SelectAllFunc>
pub fn iter(&self) -> SelectionIterator<'_, T, SelectAllFunc>
Returns an iterator over all elements contained in the tree.
The order in which the elements are returned is not specified.
Example
use rstar::RTree;
let tree = RTree::bulk_load(vec![[0.0, 0.1], [0.3, 0.2], [0.4, 0.2]]);
for point in tree.iter() {
println!("This tree contains point {:?}", point);
}
sourcepub fn iter_mut(&mut self) -> SelectionIteratorMut<'_, T, SelectAllFunc>
pub fn iter_mut(&mut self) -> SelectionIteratorMut<'_, T, SelectAllFunc>
Returns an iterator over all mutable elements contained in the tree.nearest_neighbor
The order in which the elements are returned is not specified.nearest_neighbor
Note: It is a logic error to change an inserted item’s position or dimensions. This method is primarily meant for own implementations of RTreeObject which can contain arbitrary additional data. If the position or location of an inserted object need to change, you will need to [remove] and reinsert it.
sourcepub fn locate_in_envelope(
&self,
envelope: &T::Envelope
) -> SelectionIterator<'_, T, SelectInEnvelopeFunction<T>>
pub fn locate_in_envelope(
&self,
envelope: &T::Envelope
) -> SelectionIterator<'_, T, SelectInEnvelopeFunction<T>>
Returns all elements contained in an Envelope.
Usually, an envelope is an axis aligned bounding box. This method can be used to get all elements that are fully contained within an envelope.
Example
use rstar::{RTree, AABB};
let mut tree = RTree::bulk_load(vec![
[0.0, 0.0],
[0.0, 1.0],
[1.0, 1.0]
]);
let half_unit_square = AABB::from_corners([0.0, 0.0], [0.5, 1.0]);
let unit_square = AABB::from_corners([0.0, 0.0], [1.0, 1.0]);
let elements_in_half_unit_square = tree.locate_in_envelope(&half_unit_square);
let elements_in_unit_square = tree.locate_in_envelope(&unit_square);
assert_eq!(elements_in_half_unit_square.count(), 2);
assert_eq!(elements_in_unit_square.count(), 3);
sourcepub fn locate_in_envelope_mut(
&mut self,
envelope: &T::Envelope
) -> SelectionIteratorMut<'_, T, SelectInEnvelopeFunction<T>>
pub fn locate_in_envelope_mut(
&mut self,
envelope: &T::Envelope
) -> SelectionIteratorMut<'_, T, SelectInEnvelopeFunction<T>>
Mutable variant of locate_in_envelope.
sourcepub fn locate_in_envelope_intersecting(
&self,
envelope: &T::Envelope
) -> SelectionIterator<'_, T, SelectInEnvelopeFuncIntersecting<T>>
pub fn locate_in_envelope_intersecting(
&self,
envelope: &T::Envelope
) -> SelectionIterator<'_, T, SelectInEnvelopeFuncIntersecting<T>>
Returns all elements whose envelope intersects a given envelope.
Usually, an envelope is an axis aligned bounding box. This method will return all elements whose AABB has some common area with a given AABB.
sourcepub fn locate_in_envelope_intersecting_mut(
&mut self,
envelope: &T::Envelope
) -> SelectionIteratorMut<'_, T, SelectInEnvelopeFuncIntersecting<T>>
pub fn locate_in_envelope_intersecting_mut(
&mut self,
envelope: &T::Envelope
) -> SelectionIteratorMut<'_, T, SelectInEnvelopeFuncIntersecting<T>>
Mutable variant of locate_in_envelope_intersecting
sourceimpl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: PointDistance,
impl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: PointDistance,
sourcepub fn locate_at_point(
&self,
point: &<T::Envelope as Envelope>::Point
) -> Option<&T>
pub fn locate_at_point(
&self,
point: &<T::Envelope as Envelope>::Point
) -> Option<&T>
Returns a single object that covers a given point.
Method contains_point is used to determine if a tree element contains the given point.
If multiple elements contain the given point, any of them is returned.
sourcepub fn locate_at_point_mut(
&mut self,
point: &<T::Envelope as Envelope>::Point
) -> Option<&mut T>
pub fn locate_at_point_mut(
&mut self,
point: &<T::Envelope as Envelope>::Point
) -> Option<&mut T>
Mutable variant of locate_at_point.
sourcepub fn locate_all_at_point(
&self,
point: &<T::Envelope as Envelope>::Point
) -> SelectionIterator<'_, T, SelectAtPointFunction<T>>
pub fn locate_all_at_point(
&self,
point: &<T::Envelope as Envelope>::Point
) -> SelectionIterator<'_, T, SelectAtPointFunction<T>>
Locate all elements containing a given point.
Method contains_point is used to determine if a tree element contains the given point.
Example
use rstar::RTree;
use rstar::primitives::Rectangle;
let tree = RTree::bulk_load(vec![
Rectangle::from_corners([0.0, 0.0], [2.0, 2.0]),
Rectangle::from_corners([1.0, 1.0], [3.0, 3.0])
]);
assert_eq!(tree.locate_all_at_point(&[1.5, 1.5]).count(), 2);
assert_eq!(tree.locate_all_at_point(&[0.0, 0.0]).count(), 1);
assert_eq!(tree.locate_all_at_point(&[-1., 0.0]).count(), 0);
sourcepub fn locate_all_at_point_mut(
&mut self,
point: &<T::Envelope as Envelope>::Point
) -> SelectionIteratorMut<'_, T, SelectAtPointFunction<T>>
pub fn locate_all_at_point_mut(
&mut self,
point: &<T::Envelope as Envelope>::Point
) -> SelectionIteratorMut<'_, T, SelectAtPointFunction<T>>
Mutable variant of locate_at_point_mut.
sourcepub fn remove_at_point(
&mut self,
point: &<T::Envelope as Envelope>::Point
) -> Option<T>
pub fn remove_at_point(
&mut self,
point: &<T::Envelope as Envelope>::Point
) -> Option<T>
Removes an element containing a given point.
The removed element, if any, is returned. If multiple elements cover the given point, only one of them is removed and returned.
Example
use rstar::RTree;
use rstar::primitives::Rectangle;
let mut tree = RTree::bulk_load(vec![
Rectangle::from_corners([0.0, 0.0], [2.0, 2.0]),
Rectangle::from_corners([1.0, 1.0], [3.0, 3.0])
]);
assert!(tree.remove_at_point(&[1.5, 1.5]).is_some());
assert!(tree.remove_at_point(&[1.5, 1.5]).is_some());
assert!(tree.remove_at_point(&[1.5, 1.5]).is_none());
sourceimpl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: RTreeObject + PartialEq,
impl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: RTreeObject + PartialEq,
sourcepub fn contains(&self, t: &T) -> bool
pub fn contains(&self, t: &T) -> bool
Returns true
if a given element is equal (==
) to an element in the
r-tree.
use rstar::RTree;
let mut tree = RTree::new();
assert!(!tree.contains(&[0.0, 2.0]));
tree.insert([0.0, 2.0]);
assert!(tree.contains(&[0.0, 2.0]));
sourcepub fn remove(&mut self, t: &T) -> Option<T>
pub fn remove(&mut self, t: &T) -> Option<T>
Removes and returns an element of the r-tree equal (==
) to a given element.
If multiple elements equal to the given elements are contained in the tree, only one of them is removed and returned.
Example
use rstar::RTree;
let mut tree = RTree::new();
tree.insert([0.0, 2.0]);
// The element can be inserted twice just fine
tree.insert([0.0, 2.0]);
assert!(tree.remove(&[0.0, 2.0]).is_some());
assert!(tree.remove(&[0.0, 2.0]).is_some());
assert!(tree.remove(&[0.0, 2.0]).is_none());
sourceimpl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: PointDistance,
impl<T, Params> RTree<T, Params>where
Params: RTreeParams,
T: PointDistance,
sourcepub fn nearest_neighbor(
&self,
query_point: &<T::Envelope as Envelope>::Point
) -> Option<&T>
pub fn nearest_neighbor(
&self,
query_point: &<T::Envelope as Envelope>::Point
) -> Option<&T>
Returns the nearest neighbor for a given point.
The distance is calculated by calling PointDistance::distance_2
Example
use rstar::RTree;
let tree = RTree::bulk_load(vec![
[0.0, 0.0],
[0.0, 1.0],
]);
assert_eq!(tree.nearest_neighbor(&[-1., 0.0]), Some(&[0.0, 0.0]));
assert_eq!(tree.nearest_neighbor(&[0.0, 2.0]), Some(&[0.0, 1.0]));
sourcepub fn nearest_neighbor_iter(
&self,
query_point: &<T::Envelope as Envelope>::Point
) -> impl Iterator<Item = &T>
pub fn nearest_neighbor_iter(
&self,
query_point: &<T::Envelope as Envelope>::Point
) -> impl Iterator<Item = &T>
Returns all elements of the tree sorted by their distance to a given point.
Runtime
Every next()
call runs in O(log(n))
. Creating the iterator runs in
O(log(n))
.
The r-tree documentation contains more information about
r-tree performance.
Example
use rstar::RTree;
let tree = RTree::bulk_load(vec![
[0.0, 0.0],
[0.0, 1.0],
]);
let nearest_neighbors = tree.nearest_neighbor_iter(&[0.5, 0.0]).collect::<Vec<_>>();
assert_eq!(nearest_neighbors, vec![&[0.0, 0.0], &[0.0, 1.0]]);
sourceimpl<T, Params> RTree<T, Params>where
T: RTreeObject,
Params: RTreeParams,
impl<T, Params> RTree<T, Params>where
T: RTreeObject,
Params: RTreeParams,
sourcepub fn insert(&mut self, t: T)
pub fn insert(&mut self, t: T)
Inserts a new element into the r-tree.
If the element has already been present in the tree, it will now be present twice.
Runtime
This method runs in O(log(n))
.
The r-tree documentation contains more information about
r-tree performance.