spatial/tree/mbr/
query.rs

1// Copyright 2016 spatial-rs Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::geometry::Rect;
9use std::fmt::Debug;
10use crate::tree::mbr::{MbrLeaf, MbrLeafGeometry, MbrNode};
11use crate::FP;
12
13/// Query trait for navigating the tree
14pub trait MbrQuery<P: FP, const DIM: usize, LG, T, NODE> {
15    /// Returns true if the leaf matches the query
16    fn accept_leaf(&self, leaf: &MbrLeaf<P, DIM, LG, T>) -> bool;
17    /// Returns true if the level matches the query
18    fn accept_level(&self, level: &NODE) -> bool;
19}
20
21/// Rect based query
22#[derive(Debug, Clone)]
23pub enum MbrRectQuery<P: FP, const DIM: usize> {
24    /// Matching leaves are ones that are completely contained by this rect
25    ContainedBy(Rect<P, DIM>),
26    /// Matching leaves are ones that overlap this rect
27    Overlaps(Rect<P, DIM>),
28}
29
30impl<P: FP, const DIM: usize, LG, T, NODE> MbrQuery<P, DIM, LG, T, NODE> for MbrRectQuery<P, DIM>
31where
32    LG: MbrLeafGeometry<P, DIM>,
33    NODE: MbrNode<P, DIM>,
34{
35    // Does this query accept the given leaf?
36    fn accept_leaf(&self, leaf: &MbrLeaf<P, DIM, LG, T>) -> bool {
37        match *self {
38            MbrRectQuery::ContainedBy(ref query) => leaf.geometry.contained_by_mbr(query),
39            MbrRectQuery::Overlaps(ref query) => leaf.geometry.overlapped_by_mbr(query),
40        }
41    }
42
43    // Does this query accept the given level?
44    fn accept_level(&self, level: &NODE) -> bool {
45        match *self {
46            MbrRectQuery::ContainedBy(ref query) => level.overlapped_by_mbr(query),
47            MbrRectQuery::Overlaps(ref query) => level.overlapped_by_mbr(query),
48        }
49    }
50}