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 squared distance between the envelope's border and a point.
37    ///
38    /// # Notes
39    /// - While euclidean distance will be the correct choice for most use cases, any distance metric
40    ///   fulfilling the [usual axioms](https://en.wikipedia.org/wiki/Metric_space)
41    ///   can be used when implementing this method
42    /// - Implementers **must** ensure that the distance metric used matches that of [crate::PointDistance::distance_2]
43    fn distance_2(&self, point: &Self::Point) -> <Self::Point as Point>::Scalar;
44
45    /// Returns the squared min-max distance, a concept that helps to find nearest neighbors efficiently.
46    ///
47    /// Visually, if an AABB and a point are given, the min-max distance returns the distance at which we
48    /// can be assured that an element must be present. This serves as an upper bound during nearest neighbor search.
49    ///
50    /// # References
51    /// [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)
52    fn min_max_dist_2(&self, point: &Self::Point) -> <Self::Point as Point>::Scalar;
53
54    /// Returns the envelope's center point.
55    fn center(&self) -> Self::Point;
56
57    /// Returns a value proportional to the envelope's perimeter.
58    fn perimeter_value(&self) -> <Self::Point as Point>::Scalar;
59
60    /// Sorts a given set of objects with envelopes along one of their axes.
61    fn sort_envelopes<T: RTreeObject<Envelope = Self>>(axis: usize, envelopes: &mut [T]);
62
63    /// Partitions objects with an envelope along a certain axis.
64    ///
65    /// After calling this, envelopes[0..selection_size] are all smaller
66    /// than envelopes[selection_size + 1..].
67    fn partition_envelopes<T: RTreeObject<Envelope = Self>>(
68        axis: usize,
69        envelopes: &mut [T],
70        selection_size: usize,
71    );
72}