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
use std::collections::VecDeque;
use crate::{direction::Direction, grid::GridData};
use super::{
coordinates::{Cartesian2D, Cartesian3D, CartesianCoordinates, CartesianPosition},
grid::CartesianGrid,
};
impl<C: CartesianCoordinates, D> GridData<C, D, CartesianGrid<C>> {
/// Returns a reference to the element at this position.
///
/// NO CHECK is done to verify that the given position is a valid position for this grid.
#[inline]
pub fn get_from_pos(&self, pos: &CartesianPosition) -> &D {
&self.get(self.grid().index_from_pos(pos))
}
/// Returns a reference to the element at this position.
///
/// NO CHECK is done to verify that the given position is a valid position for this grid.
#[inline]
pub fn get_mut_from_pos(&mut self, pos: &CartesianPosition) -> &mut D {
self.get_mut(self.grid().index_from_pos(pos))
}
/// Returns the data at the next position in the grid when moving 1 unit in `direction` from `grid_position`.
///
/// Returns `None` if the destination is not in the grid.
///
/// NO CHECK is done to verify that the given `grid_position` is a valid position for this grid.
pub fn get_next_in_direction(
&self,
grid_position: &CartesianPosition,
direction: Direction,
) -> Option<&D> {
self.grid()
.get_next_index_in_direction(grid_position, direction)
.and_then(|index| Some(self.get(index)))
}
}
/// Uses Copy if possible.
impl<C: CartesianCoordinates, D: Clone> GridData<C, D, CartesianGrid<C>> {
/// Sets all nodes of the grix with x=`x` to `value`
pub fn set_all_x(&mut self, x: u32, value: D) {
let mut index = x;
for _z in 0..self.grid().size_z() {
for _y in 0..self.grid().size_y() {
self.set_raw(index as usize, value.clone());
index += self.grid().size_x();
}
}
}
/// Sets all nodes of the grix with y=`y` to `value`
pub fn set_all_y(&mut self, y: u32, value: D) {
let mut index = y * self.grid().size_x();
for _z in 0..self.grid().size_z() {
for _x in 0..self.grid().size_x() {
self.set_raw(index as usize, value.clone());
index += 1;
}
index += self.grid().size_xy() - self.grid().size_x();
}
}
/// Sets all nodes of the grix with z=`z` to `value`
pub fn set_all_z(&mut self, z: u32, value: D) {
let mut index = z * self.grid().size_xy();
for _y in 0..self.grid().size_y() {
for _x in 0..self.grid().size_x() {
self.set_raw(index as usize, value.clone());
index += 1;
}
}
}
/// Sets all nodes of the grix with x=`x`and y=`y` to `value`
pub fn set_all_xy(&mut self, x: u32, y: u32, value: D) {
let mut index = x + y * self.grid().size_x();
for _z in 0..self.grid().size_z() {
self.set_raw(index as usize, value.clone());
index += self.grid().size_xy();
}
}
/// Sets all nodes of the grix with x=`x`and z=`z` to `value`
pub fn set_all_xz(&mut self, x: u32, z: u32, value: D) {
let mut index = x + z * self.grid().size_xy();
for _y in 0..self.grid().size_y() {
self.set_raw(index as usize, value.clone());
index += self.grid().size_x();
}
}
/// Sets all nodes of the grix with y=`y` and z=`z` to `value`
pub fn set_all_yz(&mut self, y: u32, z: u32, value: D) {
let mut index = y * self.grid().size_x() + z * self.grid().size_xy();
for _x in 0..self.grid().size_x() {
self.set_raw(index as usize, value.clone());
index += 1;
}
}
}
impl<D> GridData<Cartesian2D, D, CartesianGrid<Cartesian2D>> {
/// Returns a reference to the element at this position.
///
/// NO CHECK is done to verify that the given position is a valid position for this grid.
#[inline]
pub fn get_2d(&self, x: u32, y: u32) -> &D {
&self.get(self.grid().get_index_2d(x, y))
}
/// Returns a mutable reference to the data at this position.
///
/// NO CHECK is done to verify that the given position is a valid position for this grid.
#[inline]
pub fn get_2d_mut(&mut self, x: u32, y: u32) -> &mut D {
self.get_mut(self.grid().get_index_2d(x, y))
}
fn explore_vertical<C: FnMut(&D) -> bool, A: FnMut(&mut D)>(
&mut self,
queue: &mut VecDeque<CartesianPosition>,
from: &CartesianPosition,
condition: &mut C,
action: &mut A,
) {
for vertical_dir in vec![Direction::YForward, Direction::YBackward].iter() {
if let Some(vertical_node_pos) =
self.grid().get_next_pos_in_direction(&from, *vertical_dir)
{
let node_data = self.get_mut_from_pos(&vertical_node_pos);
if condition(node_data) {
action(node_data);
queue.push_back(vertical_node_pos);
}
}
}
}
// TODO Extend to Cartesian 3D
// TODO See NodeRef for starting position
/// Flood fill starting at `from`, applying `action` to all nodes for which `conditon` returns true.
///
/// - `conditon`should be true for `from` else the function returns immediately.
/// - If present `pre_allocated_queue` will be cleared before running the algorithm (but existing allocation will be kept)
///
/// Based on <https://en.wikipedia.org/wiki/Flood_fill#Further_potential_optimizations> but working with looping grids. Some more optimizations may be taken from <https://en.wikipedia.org/wiki/Flood_fill#Span_filling> once adapted to looping grids.
///
/// /!\ This uses 'conditon'+'action' as a way to not backtrack. If the effect of 'action' does not disables 'condition', this will loop !
pub fn flood_fill<CO: FnMut(&D) -> bool, AC: FnMut(&mut D)>(
&mut self,
from: impl Into<CartesianPosition>,
mut condition: CO,
mut action: AC,
pre_allocated_queue: Option<&mut VecDeque<CartesianPosition>>,
) {
let mut queue = match pre_allocated_queue {
Some(q) => {
q.clear();
q
}
None => &mut VecDeque::with_capacity(10),
};
let initial_pos = from.into();
let initial_node = self.get_mut_from_pos(&initial_pos);
// We do not add to the queue if a node is already set. If not set, set and add to queue (to avoid queuing nodes multiple times)
if !condition(initial_node) {
return;
} else {
action(initial_node);
queue.push_back(initial_pos);
}
while let Some(pos) = queue.pop_front() {
self.explore_vertical(&mut queue, &pos, &mut condition, &mut action);
for &horizontal_dir in vec![Direction::XBackward, Direction::XForward].iter() {
let mut x_pos = pos;
// Use size_x as an upper limit of the iteration count
for _ in 0..self.grid().size_x() {
// TODO Delta accessor helper: .delta(Direction::YForward)
if let Some(next_node_pos) = self
.grid()
.get_next_pos_in_direction(&x_pos, horizontal_dir)
{
let node_data = self.get_mut_from_pos(&next_node_pos);
if condition(node_data) {
action(node_data);
self.explore_vertical(
&mut queue,
&next_node_pos,
&mut condition,
&mut action,
);
x_pos = next_node_pos;
} else {
break;
}
} else {
break;
}
}
}
}
}
}
impl<D> GridData<Cartesian3D, D, CartesianGrid<Cartesian3D>> {
/// Returns a reference to the data at this position.
///
/// NO CHECK is done to verify that the given position is a valid position for this grid.
#[inline]
pub fn get_3d(&self, x: u32, y: u32, z: u32) -> &D {
&self.get(self.grid().index_from_coords(x, y, z))
}
/// Returns a mutable reference to the data at this position.
///
/// NO CHECK is done to verify that the given position is a valid position for this grid.
#[inline]
pub fn get_3d_mut(&mut self, x: u32, y: u32, z: u32) -> &mut D {
self.get_mut(self.grid().index_from_coords(x, y, z))
}
}