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