pub struct RTree<T>where
T: SpatialObject,{ /* private fields */ }
Expand description
A rust implementation of n dimensional r*-trees
R-trees provide efficient nearest-neighbor searches for
many objects. R*-trees ("R-Star-Trees")
are a common variant of r-trees and use more advanced heuristics to improve query performance.
Instead of linear time complexity, r-trees yield logarithmic complexity for look-up operations
and nearest neighbor queries. Inserting into an r-tree runs in O(log(n)) time on average.
Also, a bulk loading algorithm is implemented for faster and higher quality tree creation.
Some simple geometric primitives that can be inserted into an r-tree can be found in
the primitives
module. If your object is not among those, consider implementing the
SpatialObject
trait.
use nalgebra::{Point4};
use spade::rtree::RTree;
let mut tree = RTree::new();
tree.insert(Point4::new(13i32, 10, 10, 37));
Basic Example
extern crate cgmath; // Alternatively: use nalgebra or [f32; 2]
extern crate spade;
use spade::rtree::RTree;
use cgmath::Point2;
fn main() {
let mut rtree = RTree::new();
// Insert two points
rtree.insert(Point2::new(0.5, 0.5f32));
rtree.insert(Point2::new(1.0, 1.0f32));
if rtree.lookup(&Point2::new(0.5, 0.5)).is_some() {
println!("Found point at [0.5, 0.5]/");
}
let nearest = rtree.nearest_neighbor(&Point2::new(1.5, 1.5)).unwrap();
println!("nearest neighbor at [1.5, 1.5]: {:?}", nearest);
// Iterate over all elements
for point in rtree.iter() {
println!("Found point: {:?}", point);
}
}
Implementations
sourceimpl<T> RTree<T>where
T: SpatialObject,
impl<T> RTree<T>where
T: SpatialObject,
sourcepub fn mbr(&self) -> Option<BoundingRect<T::Point>>
pub fn mbr(&self) -> Option<BoundingRect<T::Point>>
Returns the trees minimal bounding box.
sourcepub fn iter(&self) -> RTreeIterator<'_, T>ⓘNotable traits for RTreeIterator<'a, T>impl<'a, T> Iterator for RTreeIterator<'a, T>where
T: SpatialObject, type Item = &'a T;
pub fn iter(&self) -> RTreeIterator<'_, T>ⓘNotable traits for RTreeIterator<'a, T>impl<'a, T> Iterator for RTreeIterator<'a, T>where
T: SpatialObject, type Item = &'a T;
T: SpatialObject, type Item = &'a T;
Returns an iterator over all contained elements.
sourcepub fn nearest_neighbor(&self, query_point: &T::Point) -> Option<&T>
pub fn nearest_neighbor(&self, query_point: &T::Point) -> Option<&T>
Returns the nearest neighbor.
Returns None
if the tree is empty.
sourcepub fn close_neighbor(&self, point: &T::Point) -> Option<&T>
pub fn close_neighbor(&self, point: &T::Point) -> Option<&T>
Returns an object close to a given point. This operation is faster than
nearest_neighbor
but will not neccessarily yield the real nearest neighbor.
sourcepub fn nearest_neighbors(&self, query_point: &T::Point) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn nearest_neighbors(&self, query_point: &T::Point) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Returns the nearest neighbors of a given point.
All returned values will have the exact same distance from the given query point.
Returns an empty Vec
if the tree is empty.
sourcepub fn nearest_n_neighbors(&self, query_point: &T::Point, n: usize) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn nearest_n_neighbors(&self, query_point: &T::Point, n: usize) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Returns the nearest n neighbors.
sourcepub fn nearest_neighbor_iterator(
&self,
query_point: &T::Point
) -> NearestNeighborIterator<'_, T>ⓘNotable traits for NearestNeighborIterator<'a, T>impl<'a, T> Iterator for NearestNeighborIterator<'a, T>where
T: SpatialObject + 'a, type Item = &'a T;
pub fn nearest_neighbor_iterator(
&self,
query_point: &T::Point
) -> NearestNeighborIterator<'_, T>ⓘNotable traits for NearestNeighborIterator<'a, T>impl<'a, T> Iterator for NearestNeighborIterator<'a, T>where
T: SpatialObject + 'a, type Item = &'a T;
T: SpatialObject + 'a, type Item = &'a T;
Returns an iterator over the nearest neighbors of a point.
sourcepub fn lookup_in_rectangle(&self, query_rect: &BoundingRect<T::Point>) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn lookup_in_rectangle(&self, query_rect: &BoundingRect<T::Point>) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Returns all objects (partially) contained in a rectangle
sourcepub fn lookup_in_circle(
&self,
circle_origin: &T::Point,
radius2: &<T::Point as PointN>::Scalar
) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn lookup_in_circle(
&self,
circle_origin: &T::Point,
radius2: &<T::Point as PointN>::Scalar
) -> Vec<&T>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Returns all objects (partially) contained in a circle.
Note that radius2
is the circle’s squared radius, not the actual radius.
An object is contained if a part of it lies within the circle.
sourceimpl<T> RTree<T>where
T: SpatialObject + Clone,
T::Point: TwoDimensional,
impl<T> RTree<T>where
T: SpatialObject + Clone,
T::Point: TwoDimensional,
sourcepub fn bulk_load(elements: Vec<T>) -> RTree<T>
pub fn bulk_load(elements: Vec<T>) -> RTree<T>
Creates a new rtree with some initial elements.
This method should run faster than inserting all elements sequentially. Also, the resulting rtree should have better quality in terms of query performance. This is an implementation of the OMT algorithm.
The current implementation only works for two dimensional data.
sourceimpl<T> RTree<T>where
T: SpatialObject,
impl<T> RTree<T>where
T: SpatialObject,
sourcepub fn lookup(&self, query_point: &T::Point) -> Option<&T>
pub fn lookup(&self, query_point: &T::Point) -> Option<&T>
Searches for an element at a given position.
If query_point
is contained by one object in the tree, this object will be returned.
If multiple objects contain the point, only one of them will be returned.
sourcepub fn lookup_mut(&mut self, query_point: &T::Point) -> Option<&mut T>
pub fn lookup_mut(&mut self, query_point: &T::Point) -> Option<&mut T>
Searches for an element at a given position and returns a mutable
reference.
If query_point
is contained by multiple objects in the tree,
one of them will be returned.
Do not change the object’s minimal bounding box.
sourcepub fn insert(&mut self, t: T)
pub fn insert(&mut self, t: T)
Inserts a new element into the tree.
This will require O(log(n))
operations on average, where n is the number of
elements contained in the tree.
sourcepub fn lookup_and_remove(&mut self, query_point: &T::Point) -> Option<T>
pub fn lookup_and_remove(&mut self, query_point: &T::Point) -> Option<T>
Searches for an element and removes it.
If the given point is contained by one object in the tree, this object is being removed and returned. If the point is contained by multiple objects, only one of them is removed and returned.
sourceimpl<T> RTree<T>where
T: SpatialObject + PartialEq,
impl<T> RTree<T>where
T: SpatialObject + PartialEq,
Trait Implementations
sourceimpl<T: Clone> Clone for RTree<T>where
T: SpatialObject,
impl<T: Clone> Clone for RTree<T>where
T: SpatialObject,
sourceimpl<T> Debug for RTree<T>where
T: SpatialObject + Debug,
impl<T> Debug for RTree<T>where
T: SpatialObject + Debug,
sourceimpl<T> Default for RTree<T>where
T: SpatialObject,
impl<T> Default for RTree<T>where
T: SpatialObject,
sourceimpl<T: PointN> DelaunayLocateStructure<T> for RTree<VertexEntry<T>>
impl<T: PointN> DelaunayLocateStructure<T> for RTree<VertexEntry<T>>
sourcefn insert_vertex_entry(&mut self, entry: VertexEntry<T>)
fn insert_vertex_entry(&mut self, entry: VertexEntry<T>)
sourcefn update_vertex_entry(&mut self, new_entry: VertexEntry<T>)
fn update_vertex_entry(&mut self, new_entry: VertexEntry<T>)
sourcefn remove_vertex_entry(&mut self, to_remove: &VertexEntry<T>)
fn remove_vertex_entry(&mut self, to_remove: &VertexEntry<T>)
sourcefn find_close_handle(&self, point: &T) -> FixedVertexHandle
fn find_close_handle(&self, point: &T) -> FixedVertexHandle
sourcefn new_query_result(&self, _: FixedVertexHandle)
fn new_query_result(&self, _: FixedVertexHandle)
Auto Trait Implementations
impl<T> RefUnwindSafe for RTree<T>where
T: RefUnwindSafe,
<T as SpatialObject>::Point: RefUnwindSafe,
impl<T> Send for RTree<T>where
T: Send,
<T as SpatialObject>::Point: Send,
impl<T> Sync for RTree<T>where
T: Sync,
<T as SpatialObject>::Point: Sync,
impl<T> Unpin for RTree<T>where
T: Unpin,
<T as SpatialObject>::Point: Unpin,
impl<T> UnwindSafe for RTree<T>where
T: UnwindSafe,
<T as SpatialObject>::Point: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read morefn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.