rstar/
envelope.rs

1use crate::{Point, RTreeObject};
2
3/// An envelope type that encompasses some child nodes.
4///
5/// An envelope defines how different bounding boxes of inserted children in an r-tree can interact,
6/// e.g. how they can be merged or intersected.
7/// This trait is not meant to be implemented by the user. Currently, only one implementation
8/// exists ([crate::AABB]) and should be used.
9pub trait Envelope: Clone + PartialEq + ::core::fmt::Debug {
10    /// The envelope's point type.
11    type Point: Point;
12
13    /// Creates a new, empty envelope that does not encompass any child.
14    fn new_empty() -> Self;
15
16    /// Returns true if a point is contained within this envelope.
17    fn contains_point(&self, point: &Self::Point) -> bool;
18
19    /// Returns true if another envelope is _fully contained_ within `self`.
20    fn contains_envelope(&self, aabb: &Self) -> bool;
21
22    /// Extends `self` to contain another envelope.
23    fn merge(&mut self, other: &Self);
24    /// Returns the minimal envelope containing `self` and another envelope.
25    fn merged(&self, other: &Self) -> Self;
26
27    /// Returns true if `self` and `other` intersect. The intersection might be
28    /// of zero area (the two envelopes only touching each other).
29    fn intersects(&self, other: &Self) -> bool;
30    /// Returns the area of the intersection of `self` and another envelope.
31    fn intersection_area(&self, other: &Self) -> <Self::Point as Point>::Scalar;
32
33    /// Returns this envelope's area. Must be at least 0.
34    fn area(&self) -> <Self::Point as Point>::Scalar;
35
36    /// Returns the euclidean distance to the envelope's border.
37    fn distance_2(&self, point: &Self::Point) -> <Self::Point as Point>::Scalar;
38
39    /// Returns the squared min-max distance, a concept that helps to find nearest neighbors efficiently.
40    ///
41    /// Visually, if an AABB and a point are given, the min-max distance returns the distance at which we
42    /// can be assured that an element must be present. This serves as an upper bound during nearest neighbor search.
43    ///
44    /// # References
45    /// [Roussopoulos, Nick, Stephen Kelley, and Frédéric Vincent. "Nearest neighbor queries." ACM sigmod record. Vol. 24. No. 2. ACM, 1995.](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.133.2288)
46    fn min_max_dist_2(&self, point: &Self::Point) -> <Self::Point as Point>::Scalar;
47
48    /// Returns the envelope's center point.
49    fn center(&self) -> Self::Point;
50
51    /// Returns a value proportional to the envelope's perimeter.
52    fn perimeter_value(&self) -> <Self::Point as Point>::Scalar;
53
54    /// Sorts a given set of objects with envelopes along one of their axes.
55    fn sort_envelopes<T: RTreeObject<Envelope = Self>>(axis: usize, envelopes: &mut [T]);
56
57    /// Partitions objects with an envelope along a certain axis.
58    ///
59    /// After calling this, envelopes[0..selection_size] are all smaller
60    /// than envelopes[selection_size + 1..].
61    fn partition_envelopes<T: RTreeObject<Envelope = Self>>(
62        axis: usize,
63        envelopes: &mut [T],
64        selection_size: usize,
65    );
66}