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