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