author | alfadur |
Sat, 03 Nov 2018 16:02:38 +0300 | |
changeset 14136 | 1dcd7e5e0aee |
parent 14135 | 9eca6f2cac6d |
child 14137 | 83bd383e6d11 |
permissions | -rw-r--r-- |
14090 | 1 |
use itertools::Itertools; |
14102 | 2 |
use std::cmp::min; |
14090 | 3 |
|
14099 | 4 |
use integral_geometry::{Line, Point, Rect, Size}; |
14090 | 5 |
use land2d::Land2D; |
6 |
||
7 |
use outline_template::OutlineTemplate; |
|
8 |
||
9 |
pub struct OutlinePoints { |
|
10 |
pub islands: Vec<Vec<Point>>, |
|
11 |
pub fill_points: Vec<Point>, |
|
12 |
pub size: Size, |
|
14099 | 13 |
pub play_box: Rect, |
14090 | 14 |
} |
15 |
||
16 |
impl OutlinePoints { |
|
17 |
pub fn from_outline_template<I: Iterator<Item = u32>>( |
|
18 |
outline_template: &OutlineTemplate, |
|
14099 | 19 |
play_box: Rect, |
20 |
size: Size, |
|
14090 | 21 |
random_numbers: &mut I, |
22 |
) -> Self { |
|
23 |
Self { |
|
14099 | 24 |
play_box, |
25 |
size, |
|
14090 | 26 |
islands: outline_template |
27 |
.islands |
|
28 |
.iter() |
|
29 |
.map(|i| { |
|
30 |
i.iter() |
|
31 |
.zip(random_numbers.tuples()) |
|
32 |
.map(|(rect, (rnd_a, rnd_b))| { |
|
14099 | 33 |
rect.top_left() |
34 |
+ Point::new( |
|
35 |
(rnd_a % rect.width) as i32, |
|
36 |
(rnd_b % rect.height) as i32, |
|
37 |
) |
|
38 |
+ play_box.top_left() |
|
14090 | 39 |
}).collect() |
40 |
}).collect(), |
|
41 |
fill_points: outline_template.fill_points.clone(), |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
pub fn total_len(&self) -> usize { |
|
46 |
self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len() |
|
47 |
} |
|
48 |
||
14121 | 49 |
pub fn iter(&self) -> impl Iterator<Item = &Point> { |
50 |
self.islands |
|
51 |
.iter() |
|
52 |
.flat_map(|i| i.iter()) |
|
53 |
.chain(self.fill_points.iter()) |
|
54 |
} |
|
55 |
||
14090 | 56 |
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
57 |
self.islands |
|
58 |
.iter_mut() |
|
59 |
.flat_map(|i| i.iter_mut()) |
|
60 |
.chain(self.fill_points.iter_mut()) |
|
61 |
} |
|
62 |
||
63 |
fn divide_edge<I: Iterator<Item = u32>>( |
|
64 |
&self, |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
65 |
segment: Line, |
14121 | 66 |
distance_divisor: u32, |
14090 | 67 |
random_numbers: &mut I, |
68 |
) -> Option<Point> { |
|
14121 | 69 |
#[inline] |
70 |
fn intersect(p: &Point, m: &Point, p1: &Point, p2: &Point) -> bool { |
|
14130 | 71 |
let t1 = (*m - *p1).cross(*p); |
72 |
let t2 = (*m - *p2).cross(*p); |
|
14121 | 73 |
|
74 |
(t1 > 0) != (t2 > 0) |
|
75 |
} |
|
76 |
||
77 |
#[inline] |
|
78 |
fn solve_intersection(p: &Point, m: &Point, s: &Point, e: &Point) -> Option<(i32, u32)> { |
|
79 |
let f = *e - *s; |
|
14129 | 80 |
let aqpb = p.cross(f) as i64; |
14121 | 81 |
|
82 |
if aqpb != 0 { |
|
83 |
let iy = ((((s.x - m.x) as i64 * p.y as i64 + m.y as i64 * p.x as i64) |
|
84 |
* f.y as i64 |
|
85 |
- s.y as i64 * f.x as i64 * p.y as i64) |
|
86 |
/ aqpb) as i32; |
|
87 |
let ix = if p.y.abs() > f.y.abs() { |
|
88 |
(iy - m.y) * p.x / p.y + m.x |
|
89 |
} else { |
|
90 |
(iy - s.y) * f.x / f.y + s.x |
|
91 |
}; |
|
92 |
||
93 |
let intersection_point = Point::new(ix, iy); |
|
94 |
let diff_point = *m - intersection_point; |
|
95 |
let d = diff_point.integral_norm(); |
|
14129 | 96 |
let t = p.dot(diff_point); |
14121 | 97 |
|
98 |
Some((t, d)) |
|
99 |
} else { |
|
100 |
None |
|
101 |
} |
|
102 |
} |
|
103 |
||
14102 | 104 |
let min_distance = 40; |
105 |
// new point should fall inside this box |
|
106 |
let map_box = self.play_box.with_margin(min_distance); |
|
107 |
||
14128 | 108 |
let p = segment.scaled_normal(); |
14102 | 109 |
let mid_point = segment.center(); |
110 |
||
14126 | 111 |
if (p.integral_norm() < min_distance as u32 * 3) || !map_box.contains_inside(mid_point) { |
14102 | 112 |
return None; |
113 |
} |
|
114 |
||
115 |
let mut dist_left = (self.size.width + self.size.height) as u32; |
|
116 |
let mut dist_right = dist_left; |
|
117 |
||
118 |
// find distances to map borders |
|
119 |
if p.x != 0 { |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
120 |
// where the normal line intersects the left map border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
121 |
let left_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
122 |
map_box.left(), |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
123 |
(map_box.left() - mid_point.x) * p.y / p.x + mid_point.y); |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
124 |
dist_left = (mid_point - left_intersection).integral_norm(); |
14102 | 125 |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
126 |
// same for the right border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
127 |
let right_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
128 |
map_box.right(), |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
129 |
(map_box.right() - mid_point.x) * p.y / p.x + mid_point.y); |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
130 |
dist_right = (mid_point - right_intersection).integral_norm(); |
14102 | 131 |
|
14132
269f13ac670d
simplify normal direction check for bounds distances
alfadur
parents:
14131
diff
changeset
|
132 |
if p.x > 0 { |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
133 |
std::mem::swap(&mut dist_left, &mut dist_right); |
14102 | 134 |
} |
135 |
} |
|
136 |
||
137 |
if p.y != 0 { |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
138 |
// where the normal line intersects the top map border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
139 |
let top_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
140 |
(map_box.top() - mid_point.y) * p.x / p.y + mid_point.x, |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
141 |
map_box.top()); |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
142 |
let dl = (mid_point - top_intersection).integral_norm(); |
14102 | 143 |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
144 |
// same for the bottom border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
145 |
let bottom_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
146 |
(map_box.bottom() - mid_point.y) * p.x / p.y + mid_point.x, |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
147 |
map_box.bottom()); |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
148 |
let dr = (mid_point - bottom_intersection).integral_norm(); |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
149 |
|
14132
269f13ac670d
simplify normal direction check for bounds distances
alfadur
parents:
14131
diff
changeset
|
150 |
if p.y < 0 { |
14102 | 151 |
dist_left = min(dist_left, dl); |
152 |
dist_right = min(dist_right, dr); |
|
153 |
} else { |
|
154 |
dist_left = min(dist_left, dr); |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
155 |
dist_right = min(dist_right, dl); |
14102 | 156 |
} |
157 |
} |
|
158 |
||
159 |
// now go through all other segments |
|
14121 | 160 |
for s in self.segments_iter() { |
161 |
if s != segment { |
|
162 |
if intersect(&p, &mid_point, &s.start, &s.end) { |
|
163 |
if let Some((t, d)) = solve_intersection(&p, &mid_point, &s.start, &s.end) { |
|
164 |
if t > 0 { |
|
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
165 |
dist_right = min(dist_right, d); |
14121 | 166 |
} else { |
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
167 |
dist_left = min(dist_left, d); |
14121 | 168 |
} |
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
14102 | 173 |
|
14121 | 174 |
// go through all points, including fill points |
175 |
for pi in self.iter() { |
|
176 |
if *pi != segment.start && *pi != segment.end { |
|
177 |
if intersect(&p, &pi, &segment.start, &segment.end) { |
|
178 |
// ray from segment.start |
|
179 |
if let Some((t, d)) = solve_intersection(&p, &mid_point, &segment.start, &pi) { |
|
180 |
if t > 0 { |
|
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
181 |
dist_right = min(dist_right, d); |
14121 | 182 |
} else { |
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
183 |
dist_left = min(dist_left, d); |
14121 | 184 |
} |
185 |
} |
|
186 |
||
187 |
// ray from segment.end |
|
188 |
if let Some((t, d)) = solve_intersection(&p, &mid_point, &segment.end, &pi) { |
|
189 |
if t > 0 { |
|
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
190 |
dist_right = min(dist_right, d); |
14121 | 191 |
} else { |
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
192 |
dist_left = min(dist_left, d); |
14121 | 193 |
} |
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
let max_dist = p.integral_norm() * 100 / distance_divisor; |
|
200 |
dist_left = min(dist_left, max_dist); |
|
201 |
dist_right = min(dist_right, max_dist); |
|
202 |
||
203 |
if dist_right + dist_left < min_distance as u32 * 2 + 10 { |
|
204 |
// limits are too narrow, just divide |
|
205 |
Some(mid_point) |
|
206 |
} else { |
|
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
207 |
// select distance within [-dist_right; dist_left], keeping min_distance in mind |
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
208 |
let d = -(dist_right as i32) |
14121 | 209 |
+ min_distance |
210 |
+ random_numbers.next().unwrap() as i32 |
|
211 |
% (dist_right as i32 + dist_left as i32 - min_distance * 2); |
|
212 |
||
14136 | 213 |
let norm = p.integral_norm() as i32; |
14121 | 214 |
Some(Point::new( |
14136 | 215 |
mid_point.x + p.x * d / norm, |
216 |
mid_point.y + p.y * d / norm, |
|
14121 | 217 |
)) |
218 |
} |
|
14090 | 219 |
} |
220 |
||
14121 | 221 |
fn divide_edges<I: Iterator<Item = u32>>( |
222 |
&mut self, |
|
223 |
distance_divisor: u32, |
|
224 |
random_numbers: &mut I, |
|
225 |
) { |
|
14090 | 226 |
for is in 0..self.islands.len() { |
227 |
let mut i = 0; |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
228 |
let mut segment; |
14090 | 229 |
|
230 |
loop { |
|
231 |
{ |
|
232 |
let island = &self.islands[is]; |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
233 |
let mut end_point; |
14090 | 234 |
if i < island.len() { |
235 |
end_point = if i + 1 < island.len() { |
|
236 |
island[i + 1] |
|
237 |
} else { |
|
238 |
island[0] |
|
239 |
}; |
|
240 |
} else { |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
241 |
break; |
14090 | 242 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
243 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
244 |
segment = Line::new(island[i], end_point); |
14090 | 245 |
} |
246 |
||
14121 | 247 |
if let Some(new_point) = self.divide_edge(segment, distance_divisor, random_numbers) |
248 |
{ |
|
14090 | 249 |
self.islands[is].insert(i + 1, new_point); |
250 |
i += 2; |
|
251 |
} else { |
|
252 |
i += 1; |
|
253 |
} |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
257 |
||
14121 | 258 |
pub fn bezierize(&mut self) {} |
14090 | 259 |
|
14121 | 260 |
pub fn distort<I: Iterator<Item = u32>>( |
261 |
&mut self, |
|
262 |
distance_divisor: u32, |
|
263 |
random_numbers: &mut I, |
|
264 |
) { |
|
14090 | 265 |
loop { |
266 |
let old_len = self.total_len(); |
|
14121 | 267 |
self.divide_edges(distance_divisor, random_numbers); |
14090 | 268 |
|
14114 | 269 |
if self.total_len() == old_len { |
14090 | 270 |
break; |
271 |
} |
|
272 |
} |
|
273 |
||
274 |
self.bezierize(); |
|
275 |
} |
|
276 |
||
277 |
pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) { |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
278 |
for segment in self.segments_iter() { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
279 |
land.draw_line(segment, value); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
280 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
281 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
282 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
283 |
fn segments_iter(&self) -> OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
284 |
OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
285 |
outline: self, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
286 |
island: 0, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
287 |
index: 0, |
14090 | 288 |
} |
289 |
} |
|
14116 | 290 |
|
291 |
pub fn mirror(&mut self) { |
|
14121 | 292 |
let r = self.size.width as i32 - 1; |
293 |
||
294 |
self.iter_mut().for_each(|p| p.x = r - p.x); |
|
14116 | 295 |
} |
296 |
||
297 |
pub fn flip(&mut self) { |
|
14121 | 298 |
let t = self.size.height as i32 - 1; |
299 |
||
300 |
self.iter_mut().for_each(|p| p.y = t - p.y); |
|
14116 | 301 |
} |
14090 | 302 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
303 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
304 |
struct OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
305 |
outline: &'a OutlinePoints, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
306 |
island: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
307 |
index: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
308 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
309 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
310 |
impl<'a> Iterator for OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
311 |
type Item = Line; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
312 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
313 |
fn next(&mut self) -> Option<Self::Item> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
314 |
if self.island < self.outline.islands.len() { |
14099 | 315 |
if self.index + 1 < self.outline.islands[self.island].len() { |
316 |
let result = Some(Line::new( |
|
317 |
self.outline.islands[self.island][self.index], |
|
318 |
self.outline.islands[self.island][self.index + 1], |
|
319 |
)); |
|
320 |
||
321 |
self.index += 1; |
|
322 |
||
323 |
result |
|
324 |
} else if self.index + 1 == self.outline.islands[self.island].len() { |
|
325 |
let result = Some(Line::new( |
|
326 |
self.outline.islands[self.island][self.index], |
|
327 |
self.outline.islands[self.island][0], |
|
328 |
)); |
|
329 |
||
330 |
self.island += 1; |
|
331 |
self.index = 0; |
|
332 |
||
333 |
result |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
334 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
335 |
self.island += 1; |
14099 | 336 |
self.index = 0; |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
337 |
self.next() |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
338 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
339 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
340 |
None |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
341 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
342 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
343 |
} |
14099 | 344 |
|
345 |
#[test()] |
|
346 |
fn points_test() { |
|
347 |
let mut points = OutlinePoints { |
|
348 |
islands: vec![ |
|
349 |
vec![Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)], |
|
350 |
vec![Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)], |
|
351 |
], |
|
352 |
fill_points: vec![Point::new(1, 1)], |
|
353 |
play_box: Rect::from_box(0, 100, 0, 100).with_margin(10), |
|
354 |
size: Size::square(100), |
|
355 |
}; |
|
356 |
||
357 |
let segments: Vec<Line> = points.segments_iter().collect(); |
|
358 |
assert_eq!( |
|
359 |
segments.first(), |
|
360 |
Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) |
|
361 |
); |
|
362 |
assert_eq!( |
|
363 |
segments.last(), |
|
364 |
Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) |
|
365 |
); |
|
366 |
||
367 |
points.iter_mut().for_each(|p| p.x = 2); |
|
368 |
assert_eq!(points.fill_points[0].x, 2); |
|
369 |
assert_eq!(points.islands[0][0].x, 2); |
|
370 |
} |