Skip to main content

RTreeObject

Trait RTreeObject 

Source
pub trait RTreeObject {
    type Envelope: Envelope;

    // Required method
    fn envelope(&self) -> Self::Envelope;
}
Expand description

An object that can be inserted into an r-tree.

This trait must be implemented for any object to be inserted into an r-tree. Some simple objects that already implement this trait can be found in the crate::primitives module.

The only property required of such an object is its crate::Envelope. Most simply, this method should return the axis aligned bounding box of the object. Other envelope types may be supported in the future.

Note: It is a logic error if an object’s envelope changes after insertion into an r-tree.

§Type parameters

Envelope: The object’s envelope type. At the moment, only AABB is available.

§Example implementation

use rstar::{RTreeObject, AABB};

struct Player
{
    name: String,
    x_coordinate: f64,
    y_coordinate: f64
}

impl RTreeObject for Player
{
    type Envelope = AABB<[f64; 2]>;

    fn envelope(&self) -> Self::Envelope
    {
        AABB::from_point([self.x_coordinate, self.y_coordinate])
    }
}

use rstar::RTree;

let mut tree = RTree::new();

// Insert a few players...
tree.insert(Player {
    name: "Forlorn Freeman".into(),
    x_coordinate: 1.,
    y_coordinate: 0.
});
tree.insert(Player {
    name: "Sarah Croft".into(),
    x_coordinate: 0.5,
    y_coordinate: 0.5,
});
tree.insert(Player {
    name: "Geralt of Trivia".into(),
    x_coordinate: 0.,
    y_coordinate: 2.,
});

// Now we are ready to ask some questions!
let envelope = AABB::from_point([0.5, 0.5]);
let likely_sarah_croft = tree.locate_in_envelope(envelope).next();
println!("Found {:?} lurking around at (0.5, 0.5)!", likely_sarah_croft.unwrap().name);

let unit_square = AABB::from_corners([-1.0, -1.0], [1., 1.]);
for player in tree.locate_in_envelope(unit_square) {
   println!("And here is {:?} spelunking in the unit square.", player.name);
}

Required Associated Types§

Source

type Envelope: Envelope

The object’s envelope type. Usually, AABB will be the right choice. This type also defines the object’s dimensionality.

Required Methods§

Source

fn envelope(&self) -> Self::Envelope

Returns the object’s envelope.

Usually, this will return the object’s axis aligned bounding box.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> RTreeObject for Arc<T>
where T: RTreeObject + ?Sized,

Source§

impl<T> RTreeObject for Rc<T>
where T: RTreeObject + ?Sized,

Implementors§