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. rstar
also supports bulk loading,
which cuts down the constant factors when creating an r-tree significantly compared to
sequential insertions.
R-trees are also dynamic: points can be inserted and removed from an existing tree.
§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
InsertionStrategy.
§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
crate::primitives module. Several primitives in the geo-types
crate also
implement these traits.
§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.
§Associating Data with Geometries
Users wishing to store associated data with geometries can use crate::primitives::GeomWithData.
§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.
Most of the selection methods, meaning those with names beginning with locate_
,
return iterators which are driven externally and can therefore be combined into
more complex pipelines using the combinators defined on the Iterator
trait.
This flexiblity does come at the cost of temporary heap allocations required
to keep track of the iteration state. Alternative methods using internal iteration
are provided to avoid this overhead, their names ending in _int
or _int_mut
.
They use a callback-based interface to pass the selected objects on to the caller thereby being able to use the stack to keep track of the state required for traversing the tree.
§Bulk loading
In many scenarios, insertion is only carried out once for many points. In this case,
RTree::bulk_load will be considerably faster. Its total run time
is still O(nlog(n))
, i.e. O(log(n))
per element inserted, but the scaling
factor is, on average, significantly improved compared with performing single
insertion n times in a row. Note the performance caveat
related to the computation of the envelope.
§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 is as small as 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)
.
§Type Parameters
T
: The type of objects stored in the r-tree.Params
: Compile time parameters that change the r-tree’s 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.
§(De)Serialization
Enable the serde
feature for Serde support.
§Further reading
For more information refer to the wikipedia article.
Implementations§
Source§impl<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 (OMT) as described in this paper by Lee and Lee (2003).
§Runtime
Bulk loading runs in O(n * log(n))
, where n
is the number of loaded
elements.
§Note
The envelope of each element will be accessed many times during loading. If that computation
is expensive, consider memoizing it using CachedEnvelope
.
Source§impl<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 RTree::bulk_load and RTreeParams.
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) -> RTreeIterator<'_, T>
pub fn iter(&self) -> RTreeIterator<'_, T>
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) -> RTreeIteratorMut<'_, T>
pub fn iter_mut(&mut self) -> RTreeIteratorMut<'_, T>
Returns an iterator over all mutable elements contained in the tree.
The order in which the elements are returned is not specified.
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 RTree::remove and reinsert it.
Sourcepub fn locate_in_envelope(
&self,
envelope: &T::Envelope,
) -> LocateInEnvelope<'_, T>
pub fn locate_in_envelope( &self, envelope: &T::Envelope, ) -> LocateInEnvelope<'_, T>
Returns all elements contained in an Envelope.
Usually, an envelope is an axis aligned bounding box. This method can be used to retrieve 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,
) -> LocateInEnvelopeMut<'_, T>
pub fn locate_in_envelope_mut( &mut self, envelope: &T::Envelope, ) -> LocateInEnvelopeMut<'_, T>
Mutable variant of locate_in_envelope.
Sourcepub fn locate_in_envelope_int<'a, V, B>(
&'a self,
envelope: &T::Envelope,
visitor: V,
) -> ControlFlow<B>
pub fn locate_in_envelope_int<'a, V, B>( &'a self, envelope: &T::Envelope, visitor: V, ) -> ControlFlow<B>
Variant of locate_in_envelope
using internal iteration.
Sourcepub fn locate_in_envelope_int_mut<'a, V, B>(
&'a mut self,
envelope: &T::Envelope,
visitor: V,
) -> ControlFlow<B>
pub fn locate_in_envelope_int_mut<'a, V, B>( &'a mut self, envelope: &T::Envelope, visitor: V, ) -> ControlFlow<B>
Mutable variant of locate_in_envelope_mut
.
Sourcepub fn drain(&mut self) -> DrainIterator<'_, T, SelectAllFunc, Params> ⓘ
pub fn drain(&mut self) -> DrainIterator<'_, T, SelectAllFunc, Params> ⓘ
Returns a draining iterator over all elements contained in the tree.
The order in which the elements are returned is not specified.
See drain_with_selection_function for more information.
Sourcepub fn drain_in_envelope(
&mut self,
envelope: T::Envelope,
) -> DrainIterator<'_, T, SelectInEnvelopeFunction<T>, Params> ⓘ
pub fn drain_in_envelope( &mut self, envelope: T::Envelope, ) -> DrainIterator<'_, T, SelectInEnvelopeFunction<T>, Params> ⓘ
Draining variant of locate_in_envelope.
Sourcepub fn locate_in_envelope_intersecting(
&self,
envelope: &T::Envelope,
) -> LocateInEnvelopeIntersecting<'_, T>
pub fn locate_in_envelope_intersecting( &self, envelope: &T::Envelope, ) -> LocateInEnvelopeIntersecting<'_, T>
Returns all elements whose envelope intersects a given envelope.
Any element fully contained within an envelope is also returned by this method. Two envelopes that “touch” each other (e.g. by sharing only a common corner) are also considered to intersect. 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.
§Example
use rstar::{RTree, AABB};
use rstar::primitives::Rectangle;
let left_piece = AABB::from_corners([0.0, 0.0], [0.4, 1.0]);
let right_piece = AABB::from_corners([0.6, 0.0], [1.0, 1.0]);
let middle_piece = AABB::from_corners([0.25, 0.0], [0.75, 1.0]);
let mut tree = RTree::<Rectangle<_>>::bulk_load(vec![
left_piece.into(),
right_piece.into(),
middle_piece.into(),
]);
let elements_intersecting_left_piece = tree.locate_in_envelope_intersecting(&left_piece);
// The left piece should not intersect the right piece!
assert_eq!(elements_intersecting_left_piece.count(), 2);
let elements_intersecting_middle = tree.locate_in_envelope_intersecting(&middle_piece);
// Only the middle piece intersects all pieces within the tree
assert_eq!(elements_intersecting_middle.count(), 3);
let large_piece = AABB::from_corners([-100., -100.], [100., 100.]);
let elements_intersecting_large_piece = tree.locate_in_envelope_intersecting(&large_piece);
// Any element that is fully contained should also be returned:
assert_eq!(elements_intersecting_large_piece.count(), 3);
Sourcepub fn locate_in_envelope_intersecting_mut(
&mut self,
envelope: &T::Envelope,
) -> LocateInEnvelopeIntersectingMut<'_, T>
pub fn locate_in_envelope_intersecting_mut( &mut self, envelope: &T::Envelope, ) -> LocateInEnvelopeIntersectingMut<'_, T>
Mutable variant of locate_in_envelope_intersecting
Sourcepub fn locate_in_envelope_intersecting_int<'a, V, B>(
&'a self,
envelope: &T::Envelope,
visitor: V,
) -> ControlFlow<B>
pub fn locate_in_envelope_intersecting_int<'a, V, B>( &'a self, envelope: &T::Envelope, visitor: V, ) -> ControlFlow<B>
Variant of locate_in_envelope_intersecting
using internal iteration.
Sourcepub fn locate_in_envelope_intersecting_int_mut<'a, V, B>(
&'a mut self,
envelope: &T::Envelope,
visitor: V,
) -> ControlFlow<B>
pub fn locate_in_envelope_intersecting_int_mut<'a, V, B>( &'a mut self, envelope: &T::Envelope, visitor: V, ) -> ControlFlow<B>
Mutable variant of locate_in_envelope_intersecting_int
.
Sourcepub fn locate_with_selection_function<S: SelectionFunction<T>>(
&self,
selection_function: S,
) -> SelectionIterator<'_, T, S> ⓘ
pub fn locate_with_selection_function<S: SelectionFunction<T>>( &self, selection_function: S, ) -> SelectionIterator<'_, T, S> ⓘ
Locates elements in the r-tree defined by a selection function.
Refer to the documentation of SelectionFunction
for
more information.
Usually, other locate
methods should cover most common use cases. This method is only required
in more specific situations.
Sourcepub fn locate_with_selection_function_mut<S: SelectionFunction<T>>(
&mut self,
selection_function: S,
) -> SelectionIteratorMut<'_, T, S> ⓘ
pub fn locate_with_selection_function_mut<S: SelectionFunction<T>>( &mut self, selection_function: S, ) -> SelectionIteratorMut<'_, T, S> ⓘ
Mutable variant of locate_with_selection_function
.
Sourcepub fn intersection_candidates_with_other_tree<'a, U>(
&'a self,
other: &'a RTree<U>,
) -> IntersectionIterator<'a, T, U> ⓘwhere
U: RTreeObject<Envelope = T::Envelope>,
pub fn intersection_candidates_with_other_tree<'a, U>(
&'a self,
other: &'a RTree<U>,
) -> IntersectionIterator<'a, T, U> ⓘwhere
U: RTreeObject<Envelope = T::Envelope>,
Returns all possible intersecting objects of this and another tree.
This will return all objects whose envelopes intersect. No geometric intersection checking is performed.
Sourcepub fn root(&self) -> &ParentNode<T>
pub fn root(&self) -> &ParentNode<T>
Returns the tree’s root node.
Usually, you will not need to call this method. However, for debugging purposes or for advanced algorithms, knowledge about the tree’s internal structure may be required. For these cases, this method serves as an entry point.
Sourcepub fn remove_with_selection_function<F>(&mut self, function: F) -> Option<T>where
F: SelectionFunction<T>,
pub fn remove_with_selection_function<F>(&mut self, function: F) -> Option<T>where
F: SelectionFunction<T>,
Removes and returns a single element from the tree. The element to remove is specified
by a SelectionFunction
.
See also: RTree::remove
, RTree::remove_at_point
Sourcepub fn drain_with_selection_function<F>(
&mut self,
function: F,
) -> DrainIterator<'_, T, F, Params> ⓘwhere
F: SelectionFunction<T>,
pub fn drain_with_selection_function<F>(
&mut self,
function: F,
) -> DrainIterator<'_, T, F, Params> ⓘwhere
F: SelectionFunction<T>,
Drain elements selected by a SelectionFunction
. Returns an
iterator that successively removes selected elements and returns
them. This is the most generic drain API, see also:
RTree::drain_in_envelope_intersecting
,
RTree::drain_within_distance
.
§Remarks
This API is similar to Vec::drain_filter
, but stopping the
iteration would stop the removal. However, the returned iterator
must be properly dropped. Leaking this iterator leads to a leak
amplification, where all the elements in the tree are leaked.
Sourcepub fn drain_in_envelope_intersecting(
&mut self,
envelope: T::Envelope,
) -> DrainIterator<'_, T, SelectInEnvelopeFuncIntersecting<T>, Params> ⓘ
pub fn drain_in_envelope_intersecting( &mut self, envelope: T::Envelope, ) -> DrainIterator<'_, T, SelectInEnvelopeFuncIntersecting<T>, Params> ⓘ
Drains elements intersecting the envelope
. Similar to
locate_in_envelope_intersecting
, except the elements are removed
and returned via an iterator.
Source§impl<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 RTree::locate_at_point.
Sourcepub fn locate_at_point_int(
&self,
point: &<T::Envelope as Envelope>::Point,
) -> Option<&T>
pub fn locate_at_point_int( &self, point: &<T::Envelope as Envelope>::Point, ) -> Option<&T>
Variant of locate_at_point
using internal iteration.
Sourcepub fn locate_at_point_int_mut(
&mut self,
point: &<T::Envelope as Envelope>::Point,
) -> Option<&mut T>
pub fn locate_at_point_int_mut( &mut self, point: &<T::Envelope as Envelope>::Point, ) -> Option<&mut T>
Mutable variant of locate_at_point_int
.
Sourcepub fn locate_all_at_point(
&self,
point: &<T::Envelope as Envelope>::Point,
) -> LocateAllAtPoint<'_, T>
pub fn locate_all_at_point( &self, point: &<T::Envelope as Envelope>::Point, ) -> LocateAllAtPoint<'_, T>
Locate all elements containing a given point.
Method PointDistance::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,
) -> LocateAllAtPointMut<'_, T>
pub fn locate_all_at_point_mut( &mut self, point: &<T::Envelope as Envelope>::Point, ) -> LocateAllAtPointMut<'_, T>
Mutable variant of locate_all_at_point
.
Sourcepub fn locate_all_at_point_int<'a, V, B>(
&'a self,
point: &<T::Envelope as Envelope>::Point,
visitor: V,
) -> ControlFlow<B>
pub fn locate_all_at_point_int<'a, V, B>( &'a self, point: &<T::Envelope as Envelope>::Point, visitor: V, ) -> ControlFlow<B>
Variant of locate_all_at_point
using internal iteration.
Sourcepub fn locate_all_at_point_int_mut<'a, V, B>(
&'a mut self,
point: &<T::Envelope as Envelope>::Point,
visitor: V,
) -> ControlFlow<B>
pub fn locate_all_at_point_int_mut<'a, V, B>( &'a mut self, point: &<T::Envelope as Envelope>::Point, visitor: V, ) -> ControlFlow<B>
Mutable variant of locate_all_at_point_int
.
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());
Source§impl<T, Params> RTree<T, Params>
impl<T, Params> RTree<T, Params>
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.
This method will only work correctly if two equal elements also have the same envelope.
§Example
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.
This method will only work correctly if two equal elements also have the same envelope.
§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());
Source§impl<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_neighbors(
&self,
query_point: &<T::Envelope as Envelope>::Point,
) -> Vec<&T>
pub fn nearest_neighbors( &self, query_point: &<T::Envelope as Envelope>::Point, ) -> Vec<&T>
Returns the nearest neighbors for a given point.
The distance is calculated by calling PointDistance::distance_2
All returned values will have the exact same distance from the given query point.
Returns an empty Vec
if the tree is empty.
§Example
use rstar::RTree;
let tree = RTree::bulk_load(vec![
[0.0, 0.0],
[0.0, 1.0],
[1.0, 0.0],
]);
// A single nearest neighbor
assert_eq!(tree.nearest_neighbors(&[0.01, 0.01]), &[&[0.0, 0.0]]);
// Two nearest neighbors
let nearest_two = tree.nearest_neighbors(&[1.0, 1.0]);
assert_eq!(nearest_two.len(), 2);
assert!(nearest_two.contains(&&[0.0, 1.0]));
assert!(nearest_two.contains(&&[1.0, 0.0]));
Sourcepub fn locate_within_distance(
&self,
query_point: <T::Envelope as Envelope>::Point,
max_squared_radius: <<T::Envelope as Envelope>::Point as Point>::Scalar,
) -> LocateWithinDistanceIterator<'_, T>
pub fn locate_within_distance( &self, query_point: <T::Envelope as Envelope>::Point, max_squared_radius: <<T::Envelope as Envelope>::Point as Point>::Scalar, ) -> LocateWithinDistanceIterator<'_, T>
Returns all elements of the tree within a certain distance.
The elements may be returned in any order. Each returned element will have a squared distance less or equal to the given squared distance.
This method makes use of PointDistance::distance_2_if_less_or_equal. If performance is critical and the distance calculation to the object is fast, overwriting this function may be beneficial.
Sourcepub fn drain_within_distance(
&mut self,
query_point: <T::Envelope as Envelope>::Point,
max_squared_radius: <<T::Envelope as Envelope>::Point as Point>::Scalar,
) -> DrainIterator<'_, T, SelectWithinDistanceFunction<T>, Params> ⓘ
pub fn drain_within_distance( &mut self, query_point: <T::Envelope as Envelope>::Point, max_squared_radius: <<T::Envelope as Envelope>::Point as Point>::Scalar, ) -> DrainIterator<'_, T, SelectWithinDistanceFunction<T>, Params> ⓘ
Drain all elements of the tree within a certain distance.
Similar to RTree::locate_within_distance
, but removes and
returns the elements via an iterator.
Sourcepub fn nearest_neighbor_iter(
&self,
query_point: &<T::Envelope as Envelope>::Point,
) -> NearestNeighborIterator<'_, T>
pub fn nearest_neighbor_iter( &self, query_point: &<T::Envelope as Envelope>::Point, ) -> NearestNeighborIterator<'_, 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]]);
Sourcepub fn nearest_neighbor_iter_with_distance(
&self,
query_point: &<T::Envelope as Envelope>::Point,
) -> NearestNeighborDistance2Iterator<'_, T>
👎Deprecated: Please use nearest_neighbor_iter_with_distance_2 instead
pub fn nearest_neighbor_iter_with_distance( &self, query_point: &<T::Envelope as Envelope>::Point, ) -> NearestNeighborDistance2Iterator<'_, T>
Returns (element, distance^2)
tuples of the tree sorted by their distance to a given point.
The distance is calculated by calling PointDistance::distance_2.
Sourcepub fn nearest_neighbor_iter_with_distance_2(
&self,
query_point: &<T::Envelope as Envelope>::Point,
) -> NearestNeighborDistance2Iterator<'_, T>
pub fn nearest_neighbor_iter_with_distance_2( &self, query_point: &<T::Envelope as Envelope>::Point, ) -> NearestNeighborDistance2Iterator<'_, T>
Returns (element, distance^2)
tuples of the tree sorted by their distance to a given point.
The distance is calculated by calling PointDistance::distance_2.
Sourcepub fn pop_nearest_neighbor(
&mut self,
query_point: &<T::Envelope as Envelope>::Point,
) -> Option<T>
pub fn pop_nearest_neighbor( &mut self, query_point: &<T::Envelope as Envelope>::Point, ) -> Option<T>
Removes the nearest neighbor for a given point and returns it.
The distance is calculated by calling PointDistance::distance_2.
§Example
use rstar::RTree;
let mut tree = RTree::bulk_load(vec![
[0.0, 0.0],
[0.0, 1.0],
]);
assert_eq!(tree.pop_nearest_neighbor(&[0.0, 0.0]), Some([0.0, 0.0]));
assert_eq!(tree.pop_nearest_neighbor(&[0.0, 0.0]), Some([0.0, 1.0]));
assert_eq!(tree.pop_nearest_neighbor(&[0.0, 0.0]), None);
Source§impl<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 is already 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.