1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::envelope::Envelope;
use crate::object::PointDistance;
use crate::object::RTreeObject;
use crate::Point;

/// Advanced trait to iterate through an r-tree. Usually it should no be required to be implemented.
///
/// It is important to know some details about the inner structure of
/// r-trees to comprehend this trait. Any node in an r-tree is either a *leaf* (containing exactly one `T: RTreeObject`) or
/// a *parent* (containing multiple nodes).
/// The main benefit of r-trees lies in their ability to efficiently guide searches through
/// the tree. This is done by *pruning*: Knowing the envelopes of parent nodes
/// often allows to completely skip them and all contained children during a search instead having
/// to iterate through them, e.g. when searching for elements in a non-intersecting envelope.
/// This often reduces the expected time from `O(n)` to `O(log(n))`.
///
/// This trait can be used to define searches through the r-tree by defining if a node
/// should be further investigated ("unpacked") or pruned.
///
/// Usually, the various `locate_[...]` methods of [`RTree`](struct.RTree.html) should cover most
/// common searches. Otherwise, implementing `SelectionFunction` and using
/// [`locate_with_selection_function`](struct.RTree.html#method.locate_with_selection_function)
/// can be used to tailor a custom search.
pub trait SelectionFunction<T>
where
    T: RTreeObject,
{
    /// Return `true` if a parent node should be unpacked during a search.
    ///
    /// The parent node's envelope is given to guide the decision.
    fn should_unpack_parent(&self, envelope: &T::Envelope) -> bool;

    /// Returns `true` if a given child node should be returned during a search.
    /// The default implementation will always return `true`.
    fn should_unpack_leaf(&self, _leaf: &T) -> bool {
        true
    }
}

pub struct SelectInEnvelopeFunction<T>
where
    T: RTreeObject,
{
    envelope: T::Envelope,
}

impl<T> SelectInEnvelopeFunction<T>
where
    T: RTreeObject,
{
    pub fn new(envelope: T::Envelope) -> Self {
        SelectInEnvelopeFunction { envelope }
    }
}

impl<T> SelectionFunction<T> for SelectInEnvelopeFunction<T>
where
    T: RTreeObject,
{
    fn should_unpack_parent(&self, parent_envelope: &T::Envelope) -> bool {
        self.envelope.intersects(parent_envelope)
    }

    fn should_unpack_leaf(&self, leaf: &T) -> bool {
        self.envelope.contains_envelope(&leaf.envelope())
    }
}

pub struct SelectInEnvelopeFuncIntersecting<T>
where
    T: RTreeObject,
{
    envelope: T::Envelope,
}

impl<T> SelectInEnvelopeFuncIntersecting<T>
where
    T: RTreeObject,
{
    pub fn new(envelope: T::Envelope) -> Self {
        SelectInEnvelopeFuncIntersecting { envelope }
    }
}

impl<T> SelectionFunction<T> for SelectInEnvelopeFuncIntersecting<T>
where
    T: RTreeObject,
{
    fn should_unpack_parent(&self, envelope: &T::Envelope) -> bool {
        self.envelope.intersects(&envelope)
    }

    fn should_unpack_leaf(&self, leaf: &T) -> bool {
        leaf.envelope().intersects(&self.envelope)
    }
}

pub struct SelectAllFunc;

impl<T> SelectionFunction<T> for SelectAllFunc
where
    T: RTreeObject,
{
    fn should_unpack_parent(&self, _: &T::Envelope) -> bool {
        true
    }
}

/// A [trait.SelectionFunction] that only selects elements whose envelope
/// contains a specific point.
pub struct SelectAtPointFunction<T>
where
    T: RTreeObject,
{
    point: <T::Envelope as Envelope>::Point,
}

impl<T> SelectAtPointFunction<T>
where
    T: PointDistance,
{
    pub fn new(point: <T::Envelope as Envelope>::Point) -> Self {
        SelectAtPointFunction { point }
    }
}

impl<T> SelectionFunction<T> for SelectAtPointFunction<T>
where
    T: PointDistance,
{
    fn should_unpack_parent(&self, envelope: &T::Envelope) -> bool {
        envelope.contains_point(&self.point)
    }

    fn should_unpack_leaf(&self, leaf: &T) -> bool {
        leaf.contains_point(&self.point)
    }
}

/// A selection function that only chooses elements equal (`==`) to a
/// given element
pub struct SelectEqualsFunction<'a, T>
where
    T: RTreeObject + PartialEq + 'a,
{
    /// Only elements equal to this object will be removed.
    object_to_remove: &'a T,
}

impl<'a, T> SelectEqualsFunction<'a, T>
where
    T: RTreeObject + PartialEq,
{
    pub fn new(object_to_remove: &'a T) -> Self {
        SelectEqualsFunction { object_to_remove }
    }
}

impl<'a, T> SelectionFunction<T> for SelectEqualsFunction<'a, T>
where
    T: RTreeObject + PartialEq,
{
    fn should_unpack_parent(&self, parent_envelope: &T::Envelope) -> bool {
        parent_envelope.contains_envelope(&self.object_to_remove.envelope())
    }

    fn should_unpack_leaf(&self, leaf: &T) -> bool {
        leaf == self.object_to_remove
    }
}

pub struct SelectWithinDistanceFunction<T>
where
    T: RTreeObject + PointDistance,
{
    circle_origin: <T::Envelope as Envelope>::Point,
    squared_max_distance: <<T::Envelope as Envelope>::Point as Point>::Scalar,
}

impl<T> SelectWithinDistanceFunction<T>
where
    T: RTreeObject + PointDistance,
{
    pub fn new(
        circle_origin: <T::Envelope as Envelope>::Point,
        squared_max_distance: <<T::Envelope as Envelope>::Point as Point>::Scalar,
    ) -> Self {
        SelectWithinDistanceFunction {
            circle_origin,
            squared_max_distance,
        }
    }
}

impl<T> SelectionFunction<T> for SelectWithinDistanceFunction<T>
where
    T: RTreeObject + PointDistance,
{
    fn should_unpack_parent(&self, parent_envelope: &T::Envelope) -> bool {
        let envelope_distance = parent_envelope.distance_2(&self.circle_origin);
        envelope_distance <= self.squared_max_distance
    }

    fn should_unpack_leaf(&self, leaf: &T) -> bool {
        leaf.distance_2_if_less_or_equal(&self.circle_origin, self.squared_max_distance)
            .is_some()
    }
}

pub struct SelectByAddressFunction<T>
where
    T: RTreeObject,
{
    envelope: T::Envelope,
    element_address: *const T,
}

impl<T> SelectByAddressFunction<T>
where
    T: RTreeObject,
{
    pub fn new(envelope: T::Envelope, element_address: &T) -> Self {
        Self {
            envelope,
            element_address,
        }
    }
}

impl<T> SelectionFunction<T> for SelectByAddressFunction<T>
where
    T: RTreeObject,
{
    fn should_unpack_parent(&self, parent_envelope: &T::Envelope) -> bool {
        parent_envelope.contains_envelope(&self.envelope)
    }

    fn should_unpack_leaf(&self, leaf: &T) -> bool {
        std::ptr::eq(self.element_address, leaf)
    }
}