Trait rstar::RTreeObject

source ·
pub trait RTreeObject {
    type Envelope: Envelope;

    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 that should be inserted into an r-tree. Some simple objects that already implement this trait can be found in the primitives module.

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

Type parameters

Envelope: The objects envelope type. At the moment, only AABB is feasible.

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])
    }
}

fn main()
{
    use rstar::{RTree, AABB};

    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

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

Required Methods

Returns the object’s envelope.

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

Implementors