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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::{Direction, NeighborOrientation, PNode, PixelMap};
use bevy_math::{uvec2, URect};
use num_traits::{NumCast, Unsigned};
use std::fmt::Debug;
impl<T: Copy + PartialEq, U: Unsigned + NumCast + Copy + Debug> PixelMap<T, U> {
#[inline]
pub fn visit_all_neighbors<F, V>(
&self,
rect: &URect,
node_region: &URect,
mut predicate: F,
mut visitor: V,
) where
F: FnMut(&PNode<T, U>, &URect) -> bool,
V: FnMut(&PNode<T, U>, &URect),
{
Direction::iter()
.for_each(|d| self.visit_neighbors(rect, node_region, d, &mut predicate, &mut visitor));
}
#[inline]
pub fn visit_diagonal_neighbors<F, V>(
&self,
rect: &URect,
node_region: &URect,
mut predicate: F,
mut visitor: V,
) where
F: FnMut(&PNode<T, U>, &URect) -> bool,
V: FnMut(&PNode<T, U>, &URect),
{
Direction::iter_diagonal()
.for_each(|d| self.visit_neighbors(rect, node_region, d, &mut predicate, &mut visitor));
}
#[inline]
pub fn visit_cardinal_neighbors<F, V>(
&self,
rect: &URect,
node_region: &URect,
mut predicate: F,
mut visitor: V,
) where
F: FnMut(&PNode<T, U>, &URect) -> bool,
V: FnMut(&PNode<T, U>, &URect),
{
Direction::iter_cardinal()
.for_each(|d| self.visit_neighbors(rect, node_region, d, &mut predicate, &mut visitor));
}
/// Visit neighboring nodes to the given node, on the specified edge or corner.
///
/// # Parameters
///
/// - `rect`: The rectangle in which contained or overlapping nodes will be visited.
/// - `node_region`: The region represented by the node for which to visit neighbors.
/// - `direction`: The direction of the edge of the node for which to visit neighbors. When the
/// given direction is one of the diagonal variants, the single respective corner node
/// is visited.
/// - `predicate`: A closure that takes a reference to a leaf node, and a reference to a rectangle as parameters.
/// This rectangle represents the intersection of the node's region and the `rect` parameter supplied to this method.
/// It returns `true` if the node matches the predicate, or `false` otherwise.
/// - `visitor`: A closure that takes a reference to a leaf node, and a reference to a rectangle as parameters.
/// The node reference is a neighbor of the `node` passed to this method, on the edge of the given `direction`,
/// that has been accepted by the given `predicate` callback. This rectangle represents
/// the intersection of the node's region and the `rect` parameter supplied to this method.
pub fn visit_neighbors<F, V>(
&self,
rect: &URect,
node_region: &URect,
direction: Direction,
mut predicate: F,
mut visitor: V,
) where
F: FnMut(&PNode<T, U>, &URect) -> bool,
V: FnMut(&PNode<T, U>, &URect),
{
let rect = rect.intersect(*node_region);
let neighbor_rect = rect_outer_edge(&rect, direction);
if neighbor_rect.is_empty() {
return;
}
self.root.visit_leaves_in_rect(
&neighbor_rect,
&mut |node, sub_rect| {
if predicate(node, sub_rect) {
visitor(node, sub_rect);
}
},
&mut 0,
);
}
/// Visit all leaf nodes that intersect with the given `rect` that are neighbors.
/// The `visitor` closure is called once for each unique pair of neighbor nodes.
///
/// # Parameters
///
/// - `rect`: The rectangle in which contained or overlapping nodes will be visited.
/// - `visitor`: A closure that takes:
/// - A [NeighborOrientation] that indicates the orientation of the neighboring nodes.
/// - The left or bottom node, depending on the orientation.
/// - The rectangle that is the effective intersection of the left or bottom node's region
/// and the `rect` parameter supplied to this method.
/// - The right or top node, depending on the orientation.
/// - The rectangle that is the effective intersection of the right or top node's region
/// and the `rect` parameter supplied to this method.
pub fn visit_neighbor_pairs<F>(&self, rect: &URect, visitor: &mut F)
where
F: FnMut(NeighborOrientation, &PNode<T, U>, &URect, &PNode<T, U>, &URect),
{
let sub_rect = self.map_rect.intersect(*rect);
if !sub_rect.is_empty() {
self.root.visit_neighbor_pairs_face(&sub_rect, visitor);
}
}
}
/// Obtain a rect that encompasses the outer edge of the given `rect`, for the
/// desired `direction`. The returned edge rect is 1 pixel "thick".
/// The returned rect for diagonal edges (corners) is 1x1 pixel in size.
#[inline]
#[must_use]
pub fn rect_outer_edge(rect: &URect, direction: Direction) -> URect {
match direction {
Direction::North => URect::from_corners(
uvec2(rect.min.x, rect.max.y),
uvec2(rect.max.x, rect.max.y + 1),
),
Direction::NorthEast => URect::from_corners(rect.max, rect.max + 1),
Direction::East => URect::from_corners(
uvec2(rect.max.x, rect.min.y),
uvec2(rect.max.x + 1, rect.max.y),
),
Direction::SouthEast => URect::from_corners(
uvec2(rect.max.x, rect.min.y.saturating_sub(1)),
uvec2(rect.max.x + 1, rect.min.y),
),
Direction::South => URect::from_corners(
uvec2(rect.min.x, rect.min.y.saturating_sub(1)),
uvec2(rect.max.x, rect.min.y),
),
Direction::SouthWest => URect::from_corners(
uvec2(rect.min.x.saturating_sub(1), rect.min.y.saturating_sub(1)),
rect.min,
),
Direction::West => URect::from_corners(
uvec2(rect.min.x.saturating_sub(1), rect.min.y),
uvec2(rect.min.x, rect.max.y),
),
Direction::NorthWest => URect::from_corners(
uvec2(rect.min.x.saturating_sub(1), rect.max.y),
uvec2(rect.min.x, rect.max.y + 1),
),
}
}
/// Obtain the rectangle of the neighboring cell of the given `cell`
/// in the desired `direction`.
#[inline]
#[must_use]
pub fn cell_neighbor(cell: &URect, direction: Direction) -> URect {
let cell_size = cell.width();
assert_eq!(cell_size, cell.height());
URect::from_corners(
direction.move_upoint(cell.min, cell_size),
direction.move_upoint(cell.max, cell_size),
)
}
#[cfg(test)]
mod test {
use crate::{Direction, PixelMap};
use bevy_math::{uvec2, URect, UVec2};
#[test]
fn test_visit_neighbors_out_of_bounds() {
let pm = PixelMap::<bool, u32>::new(&UVec2::splat(2), false, 1);
pm.visit_all_neighbors(
&pm.region().as_urect(),
&pm.root.region().as_urect(),
|n, _| *n.value(),
|_n, _| {
assert!(false);
},
);
pm.visit_all_neighbors(
&pm.region().as_urect(),
&pm.root.region().as_urect(),
|n, _| !*n.value(),
|_n, _| {
assert!(false);
},
);
}
#[test]
fn test_visit_no_neighbors() {
let mut pm = PixelMap::<u32, u16>::new(&UVec2::splat(4), 0, 1);
pm.set_pixel(uvec2(1, 1), 10);
let n = pm.root.find_node(uvec2(1, 1));
pm.visit_all_neighbors(
&pm.region().as_urect(),
&n.region().as_urect(),
|n, _| *n.value() != 0,
|_n, _| {
assert!(false);
},
);
}
#[test]
fn test_visit_neighbors() {
let mut pm = PixelMap::<u32, u16>::new(&UVec2::splat(4), 0, 1);
pm.set_pixel(uvec2(1, 1), 10); // Center
pm.set_pixel(uvec2(1, 2), 20); // North
pm.set_pixel(uvec2(2, 2), 30); // NorthEast
pm.set_pixel(uvec2(2, 1), 40); // East
pm.set_pixel(uvec2(2, 0), 50); // SouthEast
pm.set_pixel(uvec2(1, 0), 60); // South
pm.set_pixel(uvec2(0, 0), 70); // SouthWest
pm.set_pixel(uvec2(0, 1), 80); // West
pm.set_pixel(uvec2(0, 2), 90); // NorthWest
let center = &pm.root.find_node(uvec2(1, 1)).region().as_urect();
// North
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::North,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(1, 2), uvec2(2, 3))
);
visited = *n.value()
},
);
assert_eq!(visited, 20);
// NorthEast
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::NorthEast,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(2, 2), uvec2(3, 3))
);
visited = *n.value()
},
);
assert_eq!(visited, 30);
// East
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::East,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(2, 1), uvec2(3, 2))
);
visited = *n.value()
},
);
assert_eq!(visited, 40);
// SouthEast
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::SouthEast,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(2, 0), uvec2(3, 1))
);
visited = *n.value()
},
);
assert_eq!(visited, 50);
// South
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::South,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(1, 0), uvec2(2, 1))
);
visited = *n.value()
},
);
assert_eq!(visited, 60);
// SouthWest
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::SouthWest,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(0, 0), uvec2(1, 1))
);
visited = *n.value()
},
);
assert_eq!(visited, 70);
// West
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::West,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(0, 1), uvec2(1, 2))
);
visited = *n.value()
},
);
assert_eq!(visited, 80);
// NorthWest
let mut visited = 0u32;
pm.visit_neighbors(
&pm.region().as_urect(),
center,
Direction::NorthWest,
|n, _| *n.value() != 0,
|n, _| {
assert_eq!(
n.region().as_urect(),
URect::from_corners(uvec2(0, 2), uvec2(1, 3))
);
visited = *n.value()
},
);
assert_eq!(visited, 90);
}
}