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