Struct rstar::RTree

source ·
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 tree data structures for multi dimensional data which support efficient insertion operations and nearest neighbor queries. Also, other types of queries, like retrieving all objects within a rectangle or a circle, can be implemented efficiently.

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);
for point in tree.iter() {
    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.

Runtime and Performance

The runtime of query operations (nearest neighbor queries, 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. It’s 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.

Implementations

Creates a new, empty r-tree.

The created r-tree is configured with default parameters.

Creates a new, empty r-tree.

The tree’s compile time parameters must be specified. Refer to the RTreeParams trait for more information.

Returns the number of objects in an r-tree.

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(&mut[[0.0, 0.1], [0.3, 0.2], [0.4, 0.2]]);
for point in tree.iter() {
    println!("This tree contains point {:?}", point);
}

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.

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 aabb.

Mutable variant of [locate_in_envelope_mut].

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.

Mutable variant of [#method.locate_in_envelope_intersecting]

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.

Mutable variant of locate_at_point.

Locate all elements containing a given point.

Method contains_point is used to determine if a tree element contains the given point.

Mutable variant of locate_at_point_mut.

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.

Returns true if a given element is equal (==) to an element in the r-tree.

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.

Returns the nearest neighbor for a given point.

The distance is calculated by calling PointDistance::distance_2

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.

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.

Creates a new r-tree with some given elements and configurable parameters.

For more information refer to bulk_load_with_params and RTreeParameters.

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.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.