author | unC0Rr |
Fri, 02 Nov 2018 13:17:46 +0100 | |
changeset 14097 | e5904ead4864 |
parent 14073 | 9c817b2eedae |
child 14099 | bf40b5f938b0 |
permissions | -rw-r--r-- |
13957 | 1 |
extern crate integral_geometry; |
13938 | 2 |
extern crate vec2d; |
3 |
||
13967 | 4 |
use std::cmp; |
13938 | 5 |
|
14053 | 6 |
use integral_geometry::{ |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
7 |
ArcPoints, EquidistantPoints, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
8 |
Point, Size, SizeMask, Line |
14053 | 9 |
}; |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13957
diff
changeset
|
10 |
|
13938 | 11 |
pub struct Land2D<T> { |
12 |
pixels: vec2d::Vec2D<T>, |
|
14073 | 13 |
play_size: Size, |
14053 | 14 |
mask: SizeMask |
13938 | 15 |
} |
16 |
||
13952 | 17 |
impl<T: Copy + PartialEq> Land2D<T> { |
14073 | 18 |
pub fn new(play_size: Size, fill_value: T) -> Self { |
19 |
let real_size = play_size.next_power_of_two(); |
|
13938 | 20 |
Self { |
14073 | 21 |
play_size, |
22 |
pixels: vec2d::Vec2D::new(real_size, fill_value), |
|
23 |
mask: real_size.to_mask() |
|
13938 | 24 |
} |
25 |
} |
|
26 |
||
27 |
#[inline] |
|
13945 | 28 |
pub fn width(&self) -> usize { |
29 |
self.pixels.width() |
|
30 |
} |
|
31 |
||
32 |
#[inline] |
|
33 |
pub fn height(&self) -> usize { |
|
34 |
self.pixels.height() |
|
35 |
} |
|
36 |
||
37 |
#[inline] |
|
14073 | 38 |
pub fn size(&self) -> Size { |
39 |
self.pixels.size() |
|
40 |
} |
|
41 |
||
42 |
#[inline] |
|
14071
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
43 |
pub fn play_width(&self) -> usize { |
14073 | 44 |
self.play_size.width |
14071
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
45 |
} |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
46 |
|
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
47 |
#[inline] |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
48 |
pub fn play_height(&self) -> usize { |
14073 | 49 |
self.play_size.height |
50 |
} |
|
51 |
||
52 |
#[inline] |
|
53 |
pub fn play_size(&self) -> Size { |
|
54 |
self.play_size |
|
14071
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
55 |
} |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
56 |
|
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
57 |
#[inline] |
13952 | 58 |
pub fn is_valid_x(&self, x: i32) -> bool { |
14053 | 59 |
self.mask.contains_x(x as usize) |
13952 | 60 |
} |
61 |
||
62 |
#[inline] |
|
63 |
pub fn is_valid_y(&self, y: i32) -> bool { |
|
14053 | 64 |
self.mask.contains_y(y as usize) |
13952 | 65 |
} |
66 |
||
67 |
#[inline] |
|
13945 | 68 |
pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool { |
13952 | 69 |
self.is_valid_x(x) && self.is_valid_y(y) |
13938 | 70 |
} |
71 |
||
72 |
#[inline] |
|
14051 | 73 |
pub fn rows(&self) -> impl Iterator<Item = &[T]> { |
74 |
self.pixels.rows() |
|
75 |
} |
|
76 |
||
77 |
#[inline] |
|
13961
1c30793b1cea
put back land2d.map accidentally replaced by testing code
alfadur
parents:
13959
diff
changeset
|
78 |
pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U { |
13938 | 79 |
if self.is_valid_coordinate(x, y) { |
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
80 |
unsafe { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
81 |
// hey, I just checked that coordinates are valid! |
13961
1c30793b1cea
put back land2d.map accidentally replaced by testing code
alfadur
parents:
13959
diff
changeset
|
82 |
f(self.pixels.get_unchecked_mut(y as usize, x as usize)) |
13952 | 83 |
} |
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
84 |
} else { |
13961
1c30793b1cea
put back land2d.map accidentally replaced by testing code
alfadur
parents:
13959
diff
changeset
|
85 |
U::default() |
13938 | 86 |
} |
87 |
} |
|
88 |
||
13965
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13964
diff
changeset
|
89 |
#[inline] |
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13964
diff
changeset
|
90 |
pub fn map_point<U: Default, F: FnOnce(&mut T) -> U>(&mut self, point: Point, f: F) -> U { |
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13964
diff
changeset
|
91 |
self.map(point.y, point.x, f) |
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
92 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
93 |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13957
diff
changeset
|
94 |
pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize |
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
95 |
where |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
96 |
I: std::iter::Iterator<Item = Point>, |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13957
diff
changeset
|
97 |
{ |
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
98 |
i.map(|p| { |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
99 |
self.map(p.y, p.x, |v| { |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
100 |
*v = value; |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
101 |
1 |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
102 |
}) |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
103 |
}).count() |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13957
diff
changeset
|
104 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13957
diff
changeset
|
105 |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
106 |
pub fn draw_line(&mut self, line: Line, value: T) -> usize { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
107 |
self.fill_from_iter(line.into_iter(), value) |
13938 | 108 |
} |
13945 | 109 |
|
13969 | 110 |
pub fn fill(&mut self, start_point: Point, border_value: T, fill_value: T) { |
111 |
debug_assert!(self.is_valid_coordinate(start_point.x - 1, start_point.y)); |
|
112 |
debug_assert!(self.is_valid_coordinate(start_point.x, start_point.y)); |
|
13945 | 113 |
|
114 |
let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new(); |
|
13952 | 115 |
fn push<T: Copy + PartialEq>( |
13945 | 116 |
land: &Land2D<T>, |
117 |
stack: &mut Vec<(usize, usize, usize, isize)>, |
|
118 |
xl: usize, |
|
119 |
xr: usize, |
|
120 |
y: usize, |
|
121 |
dir: isize, |
|
122 |
) { |
|
123 |
let yd = y as isize + dir; |
|
124 |
||
125 |
if land.is_valid_coordinate(0, yd as i32) { |
|
126 |
stack.push((xl, xr, yd as usize, dir)); |
|
127 |
} |
|
128 |
}; |
|
129 |
||
13969 | 130 |
let start_x_l = (start_point.x - 1) as usize; |
131 |
let start_x_r = start_point.x as usize; |
|
132 |
push( |
|
133 |
self, |
|
134 |
&mut stack, |
|
135 |
start_x_l, |
|
136 |
start_x_r, |
|
137 |
start_point.y as usize, |
|
138 |
-1, |
|
139 |
); |
|
140 |
push( |
|
141 |
self, |
|
142 |
&mut stack, |
|
143 |
start_x_l, |
|
144 |
start_x_r, |
|
145 |
start_point.y as usize, |
|
146 |
1, |
|
147 |
); |
|
13945 | 148 |
|
13972 | 149 |
while let Some(a) = stack.pop() { |
150 |
let (mut xl, mut xr, y, mut dir) = a; |
|
13945 | 151 |
|
13972 | 152 |
while xl > 0 && self |
153 |
.pixels |
|
154 |
.get(y, xl) |
|
155 |
.map_or(false, |v| *v != border_value && *v != fill_value) |
|
156 |
{ |
|
157 |
xl -= 1; |
|
158 |
} |
|
13945 | 159 |
|
13972 | 160 |
while xr < self.width() - 1 && self |
161 |
.pixels |
|
162 |
.get(y, xr) |
|
163 |
.map_or(false, |v| *v != border_value && *v != fill_value) |
|
164 |
{ |
|
165 |
xr += 1; |
|
166 |
} |
|
13945 | 167 |
|
13972 | 168 |
while xl < xr { |
169 |
while xl <= xr |
|
170 |
&& (self.pixels[y][xl] == border_value || self.pixels[y][xl] == fill_value) |
|
171 |
{ |
|
172 |
xl += 1; |
|
173 |
} |
|
13945 | 174 |
|
13972 | 175 |
let mut x = xl; |
13945 | 176 |
|
13972 | 177 |
while xl <= xr |
178 |
&& (self.pixels[y][xl] != border_value && self.pixels[y][xl] != fill_value) |
|
179 |
{ |
|
180 |
self.pixels[y][xl] = fill_value; |
|
13945 | 181 |
|
13972 | 182 |
xl += 1; |
183 |
} |
|
13945 | 184 |
|
13972 | 185 |
if x < xl { |
186 |
push(self, &mut stack, x, xl - 1, y, dir); |
|
187 |
push(self, &mut stack, x, xl - 1, y, -dir); |
|
13945 | 188 |
} |
189 |
} |
|
190 |
} |
|
191 |
} |
|
13952 | 192 |
|
193 |
#[inline] |
|
194 |
fn fill_circle_line<F: Fn(&mut T) -> usize>( |
|
195 |
&mut self, |
|
196 |
y: i32, |
|
197 |
x_from: i32, |
|
198 |
x_to: i32, |
|
199 |
f: &F, |
|
200 |
) -> usize { |
|
201 |
let mut result = 0; |
|
202 |
||
203 |
if self.is_valid_y(y) { |
|
204 |
for i in cmp::min(x_from, 0) as usize..cmp::max(x_to as usize, self.width() - 1) { |
|
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
205 |
unsafe { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
206 |
// coordinates are valid at this point |
13952 | 207 |
result += f(self.pixels.get_unchecked_mut(y as usize, i)); |
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
result |
|
213 |
} |
|
214 |
||
215 |
#[inline] |
|
216 |
fn fill_circle_lines<F: Fn(&mut T) -> usize>( |
|
217 |
&mut self, |
|
218 |
x: i32, |
|
219 |
y: i32, |
|
220 |
dx: i32, |
|
221 |
dy: i32, |
|
222 |
f: &F, |
|
223 |
) -> usize { |
|
224 |
self.fill_circle_line(y + dy, x - dx, x + dx, f) |
|
225 |
+ self.fill_circle_line(y - dy, x - dx, x + dx, f) |
|
226 |
+ self.fill_circle_line(y + dx, x - dy, x + dy, f) |
|
227 |
+ self.fill_circle_line(y - dx, x - dy, x + dy, f) |
|
228 |
} |
|
229 |
||
230 |
pub fn change_round<F: Fn(&mut T) -> usize>( |
|
231 |
&mut self, |
|
232 |
x: i32, |
|
233 |
y: i32, |
|
234 |
radius: i32, |
|
235 |
f: F, |
|
236 |
) -> usize { |
|
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
237 |
ArcPoints::new(radius) |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
238 |
.map(&mut |p: Point| self.fill_circle_lines(x, y, p.x, p.y, &f)) |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
239 |
.sum() |
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
240 |
} |
13952 | 241 |
|
14052 | 242 |
fn fill_row(&mut self, center: Point, offset: Point, value: T) -> usize { |
243 |
let row_index = center.y + offset.y; |
|
244 |
if self.is_valid_y(row_index) { |
|
245 |
let from_x = cmp::max(0, center.x - offset.x) as usize; |
|
246 |
let to_x = cmp::min(self.width() - 1, (center.x + offset.x) as usize); |
|
247 |
self.pixels[row_index as usize][from_x..=to_x] |
|
248 |
.iter_mut().for_each(|v| *v = value); |
|
249 |
to_x - from_x + 1 |
|
250 |
} else { |
|
251 |
0 |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
pub fn fill_circle(&mut self, center: Point, radius: i32, value: T) -> usize { |
|
256 |
let transforms = |
|
257 |
[[0, 1, 1, 0], [0, 1, -1, 0], |
|
258 |
[1, 0, 0, 1], [1, 0, 0, -1]]; |
|
259 |
ArcPoints::new(radius).map(|vector| { |
|
260 |
transforms.iter().map(|m| |
|
261 |
self.fill_row(center, vector.transform(m), value) |
|
262 |
).sum::<usize>() |
|
263 |
}).sum() |
|
264 |
} |
|
265 |
||
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
266 |
pub fn draw_thick_line(&mut self, line: Line, radius: i32, value: T) -> usize { |
13965
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13964
diff
changeset
|
267 |
let mut result = 0; |
13952 | 268 |
|
13970
a1895019bb94
change draw_thick_line iteration order to benchmark winner
alfadur
parents:
13969
diff
changeset
|
269 |
for vector in ArcPoints::new(radius) { |
a1895019bb94
change draw_thick_line iteration order to benchmark winner
alfadur
parents:
13969
diff
changeset
|
270 |
for delta in EquidistantPoints::new(vector) { |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
271 |
for point in line.into_iter() { |
13967 | 272 |
self.map_point(point + delta, |p| { |
273 |
if *p != value { |
|
274 |
*p = value; |
|
275 |
result += 1; |
|
276 |
} |
|
277 |
}) |
|
13965
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13964
diff
changeset
|
278 |
} |
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
279 |
} |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
280 |
} |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13961
diff
changeset
|
281 |
|
13965
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13964
diff
changeset
|
282 |
result |
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
283 |
} |
13938 | 284 |
} |
285 |
||
286 |
#[cfg(test)] |
|
287 |
mod tests { |
|
288 |
use super::*; |
|
289 |
||
290 |
#[test] |
|
291 |
fn basics() { |
|
14073 | 292 |
let l: Land2D<u8> = Land2D::new(Size::new(30, 50), 0); |
14071
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
293 |
|
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
294 |
assert_eq!(l.play_width(), 30); |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
295 |
assert_eq!(l.play_height(), 50); |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
296 |
assert_eq!(l.width(), 32); |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13972
diff
changeset
|
297 |
assert_eq!(l.height(), 64); |
13938 | 298 |
|
299 |
assert!(l.is_valid_coordinate(0, 0)); |
|
300 |
assert!(!l.is_valid_coordinate(-1, -1)); |
|
301 |
||
302 |
assert!(l.is_valid_coordinate(31, 63)); |
|
303 |
assert!(!l.is_valid_coordinate(32, 63)); |
|
304 |
assert!(!l.is_valid_coordinate(31, 64)); |
|
305 |
} |
|
306 |
||
13945 | 307 |
#[test] |
308 |
fn fill() { |
|
14053 | 309 |
let mut l: Land2D<u8> = Land2D::new(Size::square(128), 0); |
13945 | 310 |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
311 |
l.draw_line(Line::new(Point::new(0, 0), Point::new(32, 96)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
312 |
l.draw_line(Line::new(Point::new(32, 96), Point::new(64, 32)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
313 |
l.draw_line(Line::new(Point::new(64, 32), Point::new(96, 80)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
314 |
l.draw_line(Line::new(Point::new(96, 80), Point::new(128, 0)), 1); |
13945 | 315 |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
316 |
l.draw_line(Line::new(Point::new(0, 128), Point::new(64, 96)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14073
diff
changeset
|
317 |
l.draw_line(Line::new(Point::new(128, 128), Point::new(64, 96)), 1); |
13945 | 318 |
|
13969 | 319 |
l.fill(Point::new(32, 32), 1, 2); |
320 |
l.fill(Point::new(16, 96), 1, 3); |
|
321 |
l.fill(Point::new(60, 100), 1, 4); |
|
13945 | 322 |
|
323 |
assert_eq!(l.pixels[0][0], 1); |
|
324 |
assert_eq!(l.pixels[96][64], 1); |
|
325 |
||
326 |
assert_eq!(l.pixels[40][32], 2); |
|
327 |
assert_eq!(l.pixels[40][96], 2); |
|
328 |
assert_eq!(l.pixels[5][0], 3); |
|
329 |
assert_eq!(l.pixels[120][0], 3); |
|
330 |
assert_eq!(l.pixels[5][127], 3); |
|
331 |
assert_eq!(l.pixels[120][127], 3); |
|
332 |
assert_eq!(l.pixels[35][64], 3); |
|
333 |
assert_eq!(l.pixels[120][20], 4); |
|
334 |
assert_eq!(l.pixels[120][100], 4); |
|
335 |
assert_eq!(l.pixels[100][64], 4); |
|
336 |
} |
|
13938 | 337 |
} |