2
2
#![ doc = include_str ! ( "../README.md" ) ]
3
3
4
4
use bevy:: {
5
+ math:: bounding:: Aabb2d ,
5
6
prelude:: { Entity , Vec2 } ,
6
7
reflect:: Reflect ,
7
8
utils:: { HashMap , HashSet } ,
8
9
} ;
9
10
use smallvec:: SmallVec ;
10
11
11
- /// Axis aligned bounding box
12
- #[ derive( Reflect , Debug , Default , Clone , Copy , PartialEq ) ]
13
- pub struct Aabb {
14
- /// Lower left corner
15
- pub min : Vec2 ,
16
- /// Upper right corner
17
- pub max : Vec2 ,
18
- }
19
-
20
12
type Key = ( i32 , i32 ) ;
21
13
22
14
/// A spatial container that allows querying for entities that share one or more grid cell
@@ -27,7 +19,7 @@ pub struct SparseGrid2d<const TILE_SIZE: usize = 1> {
27
19
28
20
impl < const TILE_SIZE : usize > SparseGrid2d < TILE_SIZE > {
29
21
/// Insert an entity in the given Aabb coordinates
30
- pub fn insert_aabb ( & mut self , aabb : impl Into < Aabb > , entity : Entity ) {
22
+ pub fn insert_aabb ( & mut self , aabb : impl Into < Aabb2d > , entity : Entity ) {
31
23
for key in KeyIter :: new :: < TILE_SIZE > ( aabb) {
32
24
self . map . entry ( key) . or_default ( ) . push ( entity) ;
33
25
}
@@ -39,11 +31,11 @@ impl<const TILE_SIZE: usize> SparseGrid2d<TILE_SIZE> {
39
31
self . map . entry ( key) . or_default ( ) . push ( entity) ;
40
32
}
41
33
42
- /// Get an iterator with the entities in the grid cells covered by the given Aabb
34
+ /// Get an iterator with the entities in the grid cells covered by the given [`Aabb2d`]
43
35
///
44
36
/// may contain duplicates if some entities are in more than one grid cell
45
37
#[ inline]
46
- pub fn aabb_iter ( & ' _ self , aabb : impl Into < Aabb > ) -> impl Iterator < Item = Entity > + ' _ {
38
+ pub fn aabb_iter ( & ' _ self , aabb : impl Into < Aabb2d > ) -> impl Iterator < Item = Entity > + ' _ {
47
39
KeyIter :: new :: < TILE_SIZE > ( aabb)
48
40
. filter_map ( |key| self . map . get ( & key) )
49
41
. flatten ( )
@@ -61,9 +53,9 @@ impl<const TILE_SIZE: usize> SparseGrid2d<TILE_SIZE> {
61
53
. copied ( )
62
54
}
63
55
64
- /// Creates a hash set with all the entities in the grid cells covered by the given Aabb
56
+ /// Creates a hash set with all the entities in the grid cells covered by the given [`Aabb2d`]
65
57
#[ inline]
66
- pub fn query_aabb ( & self , aabb : impl Into < Aabb > ) -> HashSet < Entity > {
58
+ pub fn query_aabb ( & self , aabb : impl Into < Aabb2d > ) -> HashSet < Entity > {
67
59
self . aabb_iter ( aabb) . collect ( )
68
60
}
69
61
@@ -95,8 +87,8 @@ struct KeyIter {
95
87
}
96
88
97
89
impl KeyIter {
98
- fn new < const TILE_SIZE : usize > ( aabb : impl Into < Aabb > ) -> Self {
99
- let Aabb { min, max } = aabb. into ( ) ;
90
+ fn new < const TILE_SIZE : usize > ( aabb : impl Into < Aabb2d > ) -> Self {
91
+ let Aabb2d { min, max } = aabb. into ( ) ;
100
92
// convert to key space
101
93
let s = TILE_SIZE as f32 ;
102
94
let min = ( ( min. x / s) . floor ( ) as i32 , ( min. y / s) . floor ( ) as i32 ) ;
@@ -132,7 +124,7 @@ impl Iterator for KeyIter {
132
124
133
125
#[ cfg( test) ]
134
126
mod tests {
135
- use bevy:: math:: vec2;
127
+ use bevy:: math:: { bounding :: Aabb2d , vec2} ;
136
128
use bevy:: prelude:: default;
137
129
use bevy:: utils:: HashSet ;
138
130
@@ -142,7 +134,7 @@ mod tests {
142
134
143
135
#[ test]
144
136
fn keys_single ( ) {
145
- let keys: Vec < Key > = KeyIter :: new :: < TILE_SIZE > ( Aabb {
137
+ let keys: Vec < Key > = KeyIter :: new :: < TILE_SIZE > ( Aabb2d {
146
138
min : vec2 ( 0.001 , 0.001 ) ,
147
139
max : vec2 ( 0.001 , 0.001 ) ,
148
140
} )
@@ -153,7 +145,7 @@ mod tests {
153
145
154
146
#[ test]
155
147
fn keys_four_around_origin ( ) {
156
- let keys: Vec < Key > = KeyIter :: new :: < TILE_SIZE > ( Aabb {
148
+ let keys: Vec < Key > = KeyIter :: new :: < TILE_SIZE > ( Aabb2d {
157
149
min : vec2 ( -0.001 , -0.001 ) ,
158
150
max : vec2 ( 0.001 , 0.001 ) ,
159
151
} )
@@ -170,15 +162,15 @@ mod tests {
170
162
let entity = Entity :: from_raw ( 123 ) ;
171
163
let mut db = SparseGrid2d :: < TILE_SIZE > :: default ( ) ;
172
164
db. insert_aabb (
173
- Aabb {
165
+ Aabb2d {
174
166
min : vec2 ( -0.001 , -0.001 ) ,
175
167
max : vec2 ( 0.001 , 0.001 ) ,
176
168
} ,
177
169
entity,
178
170
) ;
179
171
180
172
let matches: Vec < Entity > = db
181
- . aabb_iter ( Aabb {
173
+ . aabb_iter ( Aabb2d {
182
174
min : vec2 ( 0.001 , 0.001 ) ,
183
175
max : vec2 ( 0.001 , 0.001 ) ,
184
176
} )
@@ -190,7 +182,7 @@ mod tests {
190
182
#[ test]
191
183
fn key_negative ( ) {
192
184
let h = TILE_SIZE as f32 / 2.0 ;
193
- let keys: Vec < Key > = KeyIter :: new :: < TILE_SIZE > ( Aabb {
185
+ let keys: Vec < Key > = KeyIter :: new :: < TILE_SIZE > ( Aabb2d {
194
186
min : vec2 ( -h, -h) ,
195
187
max : vec2 ( -h, -h) ,
196
188
} )
@@ -235,29 +227,29 @@ mod tests {
235
227
let e3 = Entity :: from_raw ( 3 ) ;
236
228
let mut db: SparseGrid2d = default ( ) ;
237
229
db. insert_aabb (
238
- Aabb {
230
+ Aabb2d {
239
231
min : vec2 ( -h, -h) ,
240
232
max : vec2 ( h, h) ,
241
233
} ,
242
234
e1,
243
235
) ;
244
236
db. insert_aabb (
245
- Aabb {
237
+ Aabb2d {
246
238
min : vec2 ( h, h) ,
247
239
max : vec2 ( h, h) ,
248
240
} ,
249
241
e2,
250
242
) ;
251
243
db. insert_aabb (
252
- Aabb {
244
+ Aabb2d {
253
245
min : vec2 ( -h, -h) ,
254
246
max : vec2 ( -h, -h) ,
255
247
} ,
256
248
e3,
257
249
) ;
258
250
259
251
let matches: Vec < Entity > = db
260
- . aabb_iter ( Aabb {
252
+ . aabb_iter ( Aabb2d {
261
253
min : vec2 ( -h, -h) ,
262
254
max : vec2 ( h, h) ,
263
255
} )
@@ -267,7 +259,7 @@ mod tests {
267
259
assert ! ( matches. contains( & e2) ) ;
268
260
assert ! ( matches. contains( & e3) ) ;
269
261
270
- let matches = db. query_aabb ( Aabb {
262
+ let matches = db. query_aabb ( Aabb2d {
271
263
min : vec2 ( -0.001 , -0.001 ) ,
272
264
max : vec2 ( -0.001 , -0.001 ) ,
273
265
} ) ;
@@ -276,7 +268,7 @@ mod tests {
276
268
assert ! ( matches. contains( & e3) ) ;
277
269
278
270
let matches: Vec < Entity > = db
279
- . aabb_iter ( Aabb {
271
+ . aabb_iter ( Aabb2d {
280
272
min : vec2 ( -0.001 , -0.001 ) ,
281
273
max : vec2 ( -0.001 , -0.001 ) ,
282
274
} )
0 commit comments