RTree

Struct RTree 

Source
pub struct RTree<T>
where T: Clone,
{ /* private fields */ }
Expand description

R-tree for spatial indexing and queries

§Examples

use scirs2_spatial::rtree::RTree;
use scirs2_core::ndarray::array;

// Create points
let points = array![
    [0.0, 0.0],
    [1.0, 1.0],
    [2.0, 2.0],
    [3.0, 3.0],
    [4.0, 4.0],
];

// Build R-tree with points
let mut rtree = RTree::new(2, 4, 8).unwrap();
for (i, point) in points.rows().into_iter().enumerate() {
    rtree.insert(point.to_owned(), i).unwrap();
}

// Search for points within a range
let query_min = array![0.5, 0.5];
let query_max = array![2.5, 2.5];

// The views need to be passed explicitly in the actual code
let query_min_view = query_min.view();
let query_max_view = query_max.view();
let results = rtree.search_range(&query_min_view, &query_max_view).unwrap();

println!("Found {} points in range", results.len());

// Find the nearest neighbors to a point
let query_point = array![2.5, 2.5];
let query_point_view = query_point.view();
let nearest = rtree.nearest(&query_point_view, 2).unwrap();

println!("Nearest points: {:?}", nearest);

Implementations§

Source§

impl<T: Clone> RTree<T>

Source

pub fn delete<F>( &mut self, point: &ArrayView1<'_, f64>, data_predicate: Option<F>, ) -> SpatialResult<bool>
where F: Fn(&T) -> bool + Copy,

Delete a data point from the R-tree

§Arguments
  • point - The point coordinates to delete
  • data_predicate - An optional function that takes a reference to the data and returns true if it should be deleted. This is useful when multiple data points share the same coordinates.
§Returns

A SpatialResult containing true if a point was deleted, false otherwise

Source§

impl<T: Clone> RTree<T>

Source

pub fn insert(&mut self, point: Array1<f64>, data: T) -> SpatialResult<()>

Insert a data point into the R-tree

§Arguments
  • point - The point coordinates
  • data - The data associated with the point
§Returns

A SpatialResult containing nothing if successful, or an error if the point has invalid dimensions

Source

pub fn insert_rectangle( &mut self, min: Array1<f64>, max: Array1<f64>, data: T, ) -> SpatialResult<()>

Insert a rectangle into the R-tree

§Arguments
  • min - The minimum coordinates of the rectangle
  • max - The maximum coordinates of the rectangle
  • data - The data associated with the rectangle
§Returns

A SpatialResult containing nothing if successful, or an error if the rectangle has invalid dimensions

Source§

impl<T: Clone> RTree<T>

Source

pub fn new( ndim: usize, min_entries: usize, maxentries: usize, ) -> SpatialResult<Self>

Create a new R-tree

§Arguments
  • ndim - Number of dimensions
  • min_entries - Minimum number of entries in each node (except the root)
  • maxentries - Maximum number of entries in each node
§Returns

A SpatialResult containing the new R-tree, or an error if the parameters are invalid

Source

pub fn ndim(&self) -> usize

Get the number of dimensions in the R-tree

Source

pub fn size(&self) -> usize

Get the number of data points in the R-tree

Source

pub fn height(&self) -> usize

Get the height of the R-tree

Source

pub fn is_empty(&self) -> bool

Check if the R-tree is empty

Source

pub fn clear(&mut self)

Clear the R-tree, removing all data points

Source§

impl<T: Clone> RTree<T>

Source

pub fn optimize(&mut self) -> SpatialResult<()>

Optimize the R-tree by rebuilding it with the current data

This can significantly improve query performance by reducing overlap and creating a more balanced tree.

§Returns

A SpatialResult containing nothing if successful

Source

pub fn bulk_load( ndim: usize, min_entries: usize, max_entries: usize, points: Vec<(Array1<f64>, T)>, ) -> SpatialResult<Self>

Perform bulk loading of the R-tree with sorted data points

This is more efficient than inserting points one by one.

§Arguments
  • points - A vector of (point, data) pairs to insert
§Returns

A SpatialResult containing a new R-tree built from the data points

Source

pub fn calculate_total_overlap(&self) -> SpatialResult<f64>

Calculate the total overlap in the R-tree

This is a quality metric for the tree. Lower overlap generally means better query performance.

§Returns

The total overlap area between all pairs of nodes at each level

Source§

impl<T: Clone> RTree<T>

Source

pub fn search_range( &self, min: &ArrayView1<'_, f64>, max: &ArrayView1<'_, f64>, ) -> SpatialResult<Vec<(usize, T)>>

Search for data points within a range

§Arguments
  • min - Minimum coordinates of the search range
  • max - Maximum coordinates of the search range
§Returns

A SpatialResult containing a vector of (index, data) pairs for data points within the range, or an error if the range has invalid dimensions

Source

pub fn nearest( &self, point: &ArrayView1<'_, f64>, k: usize, ) -> SpatialResult<Vec<(usize, T, f64)>>

Find the k nearest neighbors to a query point

§Arguments
  • point - The query point
  • k - The number of nearest neighbors to find
§Returns

A SpatialResult containing a vector of (index, data, distance) tuples for the k nearest data points, sorted by distance (closest first), or an error if the point has invalid dimensions

Source

pub fn spatial_join<U, P>( &self, other: &RTree<U>, predicate: P, ) -> SpatialResult<Vec<(T, U)>>

Perform a spatial join between this R-tree and another

§Arguments
  • other - The other R-tree to join with
  • predicate - A function that takes MBRs from both trees and returns true if they should be joined, e.g., for an intersection join: |mbr1, mbr2| mbr1.intersects(mbr2)
§Returns

A SpatialResult containing a vector of pairs of data from both trees that satisfy the predicate, or an error if the R-trees have different dimensions

Trait Implementations§

Source§

impl<T> Clone for RTree<T>
where T: Clone + Clone,

Source§

fn clone(&self) -> RTree<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for RTree<T>
where T: Clone + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RTree<T>

§

impl<T> RefUnwindSafe for RTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for RTree<T>
where T: Send,

§

impl<T> Sync for RTree<T>
where T: Sync,

§

impl<T> Unpin for RTree<T>
where T: Unpin,

§

impl<T> UnwindSafe for RTree<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V