flat_spatial/
lib.rs

1//!
2//! `flat_spatial` is a crate dedicated to spatial partitioning structures that are not based on trees
3//! (which are recursive) but on simple flat structures such as grids.
4//!
5//! `Grid` partitions the space using cells of user defined width.
6//! `AABBGrid` partitions the space using cells too, but stores Axis-Aligned Bounding Boxes.
7//!
8//! Check `Grid` and `AABBGrid` docs for more information.
9//!
10
11pub mod aabbgrid;
12pub mod cell;
13pub mod grid;
14pub mod storage;
15
16pub use aabbgrid::AABBGrid;
17pub use grid::Grid;
18
19pub trait Vec2: From<[f32; 2]> + Copy {
20    fn x(&self) -> f32;
21    fn y(&self) -> f32;
22}
23
24pub trait AABB: Copy {
25    type V2: Vec2;
26
27    fn ll(&self) -> Self::V2;
28    fn ur(&self) -> Self::V2;
29
30    #[inline]
31    fn intersects(&self, b: &Self) -> bool {
32        let ll = self.ll();
33        let ur = self.ur();
34
35        let bll = b.ll();
36        let bur = b.ur();
37
38        let x = f32::abs((ll.x() + ur.x()) - (bll.x() + bur.x()))
39            <= (ur.x() - ll.x() + bur.x() - bll.x());
40        let y = f32::abs((ll.y() + ur.y()) - (bll.y() + bur.y()))
41            <= (ur.y() - ll.y() + bur.y() - bll.y());
42
43        x & y
44    }
45}
46
47impl Vec2 for [f32; 2] {
48    #[inline]
49    fn x(&self) -> f32 {
50        unsafe { *self.get_unchecked(0) }
51    }
52
53    #[inline]
54    fn y(&self) -> f32 {
55        unsafe { *self.get_unchecked(1) }
56    }
57}
58
59#[cfg(feature = "euclid")]
60mod euclid_impl {
61    use super::Vec2;
62    use super::AABB;
63    use euclid::{Point2D, Vector2D};
64
65    impl<U> Vec2 for Point2D<f32, U> {
66        fn x(&self) -> f32 {
67            self.x
68        }
69        fn y(&self) -> f32 {
70            self.y
71        }
72    }
73
74    impl<U> Vec2 for Vector2D<f32, U> {
75        fn x(&self) -> f32 {
76            self.x
77        }
78        fn y(&self) -> f32 {
79            self.y
80        }
81    }
82
83    impl<U> AABB for euclid::Rect<f32, U> {
84        type V2 = Point2D<f32, U>;
85        fn ll(&self) -> Self::V2 {
86            self.origin
87        }
88        fn ur(&self) -> Self::V2 {
89            self.origin + self.size
90        }
91    }
92}
93
94#[cfg(feature = "parry2d")]
95mod parry2d_impl {
96    use super::Vec2;
97    use super::AABB as AABBTrait;
98    use parry2d::bounding_volume::Aabb;
99    use parry2d::math::{Point, Vector};
100
101    impl Vec2 for Point<f32> {
102        fn x(&self) -> f32 {
103            self.x
104        }
105        fn y(&self) -> f32 {
106            self.y
107        }
108    }
109
110    impl Vec2 for Vector<f32> {
111        fn x(&self) -> f32 {
112            self.x
113        }
114        fn y(&self) -> f32 {
115            self.y
116        }
117    }
118
119    impl AABBTrait for Aabb {
120        type V2 = Point<f32>;
121        fn ll(&self) -> Self::V2 {
122            self.mins
123        }
124        fn ur(&self) -> Self::V2 {
125            self.maxs
126        }
127    }
128}