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