space_partitioning/quadtree/
quad_rect.rs

1use crate::quadtree::aabb::AABB;
2use crate::quadtree::centered_aabb::CenteredAABB;
3
4/// A rectangle describing the extents of the QuadTree.
5///
6/// # Remarks
7/// Only the tree node stores its extents. Bounding boxes for sub-nodes are computed on the fly.
8#[derive(Debug, Copy, Clone)]
9pub struct QuadRect {
10    l: i32,
11    t: i32,
12    hx: i32,
13    hy: i32,
14}
15
16impl QuadRect {
17    pub fn new(left: i32, top: i32, width: i32, height: i32) -> Self {
18        Self {
19            l: left,
20            t: top,
21            hx: width,
22            hy: height,
23        }
24    }
25
26    #[inline]
27    pub fn contains(&self, rect: &AABB) -> bool {
28        let mx = (rect.tl.x + rect.br.x) >> 1;
29        let my = (rect.tl.y + rect.br.y) >> 1;
30
31        let r = self.l + self.hx;
32        let b = self.t + self.hy;
33        (mx >= self.l) & (mx <= r) & (my >= self.t) & (my <= b)
34    }
35}
36
37impl Default for QuadRect {
38    fn default() -> Self {
39        QuadRect {
40            l: i32::MIN >> 1,
41            t: i32::MIN >> 1,
42            hx: i32::MAX,
43            hy: i32::MAX,
44        }
45    }
46}
47
48impl Into<AABB> for QuadRect {
49    #[inline]
50    fn into(self) -> AABB {
51        AABB::new(self.l, self.t, self.l + self.hx, self.t + self.hy)
52    }
53}
54
55impl Into<CenteredAABB> for QuadRect {
56    #[inline]
57    fn into(self) -> CenteredAABB {
58        CenteredAABB::from_ltwh(self.l, self.t, self.hx, self.hy)
59    }
60}
61
62impl Into<CenteredAABB> for &QuadRect {
63    #[inline]
64    fn into(self) -> CenteredAABB {
65        CenteredAABB::from_ltwh(self.l, self.t, self.hx, self.hy)
66    }
67}