author | unc0rr |
Fri, 02 Nov 2018 22:59:22 +0100 | |
changeset 14105 | 4d22be35cfa2 |
parent 14102 | 400864ecb954 |
child 14108 | 8556937ae338 |
permissions | -rw-r--r-- |
14074 | 1 |
use itertools::Itertools; |
14086 | 2 |
use std::cmp::min; |
14074 | 3 |
|
14083 | 4 |
use integral_geometry::{Line, Point, Rect, Size}; |
14074 | 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, |
|
14083 | 13 |
pub play_box: Rect, |
14074 | 14 |
} |
15 |
||
16 |
impl OutlinePoints { |
|
17 |
pub fn from_outline_template<I: Iterator<Item = u32>>( |
|
18 |
outline_template: &OutlineTemplate, |
|
14083 | 19 |
play_box: Rect, |
20 |
size: Size, |
|
14074 | 21 |
random_numbers: &mut I, |
22 |
) -> Self { |
|
23 |
Self { |
|
14083 | 24 |
play_box, |
25 |
size, |
|
14074 | 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))| { |
|
14083 | 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() |
|
14074 | 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 |
||
14105 | 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 |
||
14074 | 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, |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
65 |
segment: Line, |
14105 | 66 |
distance_divisor: u32, |
14074 | 67 |
random_numbers: &mut I, |
68 |
) -> Option<Point> { |
|
14105 | 69 |
#[inline] |
70 |
fn intersect(p: &Point, m: &Point, p1: &Point, p2: &Point) -> bool { |
|
71 |
let t1 = (m.x - p1.x) * p.y - p.x * (m.y - p1.y); |
|
72 |
let t2 = (m.x - p2.x) * p.y - p.x * (m.y - p2.y); |
|
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; |
|
80 |
let aqpb = (p.x * f.y - f.x * p.y) as i64; |
|
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(); |
|
96 |
let t = p.y * diff_point.y + p.x * diff_point.x; |
|
97 |
||
98 |
Some((t, d)) |
|
99 |
} else { |
|
100 |
None |
|
101 |
} |
|
102 |
} |
|
103 |
||
14086 | 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 |
||
108 |
let p = Point::new( |
|
109 |
segment.end.y - segment.start.y, |
|
110 |
segment.start.x - segment.start.y, |
|
111 |
); |
|
112 |
let mid_point = segment.center(); |
|
113 |
||
114 |
if (p.integral_norm() < min_distance as u32 * 3) || !map_box.contains_inside(p) { |
|
115 |
return None; |
|
116 |
} |
|
117 |
||
118 |
let full_box = Rect::from_size(Point::zero(), self.size).with_margin(min_distance); |
|
119 |
||
120 |
let mut dist_left = (self.size.width + self.size.height) as u32; |
|
121 |
let mut dist_right = dist_left; |
|
122 |
||
123 |
// find distances to map borders |
|
124 |
if p.x != 0 { |
|
125 |
// check against left border |
|
126 |
let iyl = (map_box.left() - mid_point.x) * p.y / p.x + mid_point.y; |
|
127 |
let dl = Point::new(mid_point.x - map_box.left(), mid_point.y - iyl).integral_norm(); |
|
128 |
let t = p.x * (mid_point.x - full_box.left()) + p.y * (mid_point.y - iyl); |
|
129 |
||
130 |
if t > 0 { |
|
131 |
dist_left = dl; |
|
132 |
} else { |
|
133 |
dist_right = dl; |
|
134 |
} |
|
135 |
||
136 |
// right border |
|
137 |
let iyr = (map_box.right() - mid_point.x) * p.y / p.x + mid_point.y; |
|
138 |
let dr = Point::new(mid_point.x - full_box.right(), mid_point.y - iyr).integral_norm(); |
|
139 |
||
140 |
if t > 0 { |
|
141 |
dist_right = dr; |
|
142 |
} else { |
|
143 |
dist_left = dr; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
if p.y != 0 { |
|
148 |
// top border |
|
149 |
let ixl = (map_box.top() - mid_point.y) * p.x / p.y + mid_point.x; |
|
150 |
let dl = Point::new(mid_point.y - map_box.top(), mid_point.x - ixl).integral_norm(); |
|
151 |
let t = p.y * (mid_point.y - full_box.top()) + p.x * (mid_point.x - ixl); |
|
152 |
||
153 |
if t > 0 { |
|
154 |
dist_left = min(dist_left, dl); |
|
155 |
} else { |
|
156 |
dist_right = min(dist_right, dl); |
|
157 |
} |
|
158 |
||
159 |
// bottom border |
|
160 |
let ixr = (map_box.bottom() - mid_point.y) * p.x / p.y + mid_point.x; |
|
161 |
let dr = Point::new(mid_point.y - full_box.bottom(), mid_point.x - ixr).integral_norm(); |
|
162 |
||
163 |
if t > 0 { |
|
164 |
dist_right = min(dist_right, dr); |
|
165 |
} else { |
|
166 |
dist_left = min(dist_left, dr); |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
// now go through all other segments |
|
14105 | 171 |
for s in self.segments_iter() { |
172 |
if s != segment { |
|
173 |
if intersect(&p, &mid_point, &s.start, &s.end) { |
|
174 |
if let Some((t, d)) = solve_intersection(&p, &mid_point, &s.start, &s.end) { |
|
175 |
if t > 0 { |
|
176 |
dist_left = min(d, dist_left); |
|
177 |
} else { |
|
178 |
dist_right = min(d, dist_right); |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
14086 | 184 |
|
14105 | 185 |
// go through all points, including fill points |
186 |
for pi in self.iter() { |
|
187 |
if *pi != segment.start && *pi != segment.end { |
|
188 |
if intersect(&p, &pi, &segment.start, &segment.end) { |
|
189 |
// ray from segment.start |
|
190 |
if let Some((t, d)) = solve_intersection(&p, &mid_point, &segment.start, &pi) { |
|
191 |
if t > 0 { |
|
192 |
dist_left = min(d, dist_left); |
|
193 |
} else { |
|
194 |
dist_right = min(d, dist_right); |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
// ray from segment.end |
|
199 |
if let Some((t, d)) = solve_intersection(&p, &mid_point, &segment.end, &pi) { |
|
200 |
if t > 0 { |
|
201 |
dist_left = min(d, dist_left); |
|
202 |
} else { |
|
203 |
dist_right = min(d, dist_right); |
|
204 |
} |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
let max_dist = p.integral_norm() * 100 / distance_divisor; |
|
211 |
dist_left = min(dist_left, max_dist); |
|
212 |
dist_right = min(dist_right, max_dist); |
|
213 |
||
214 |
if dist_right + dist_left < min_distance as u32 * 2 + 10 { |
|
215 |
// limits are too narrow, just divide |
|
216 |
Some(mid_point) |
|
217 |
} else { |
|
218 |
// select distance within [-dist_left; dist_right], keeping min_distance in mind |
|
219 |
let d = -(dist_left as i32) |
|
220 |
+ min_distance |
|
221 |
+ random_numbers.next().unwrap() as i32 |
|
222 |
% (dist_right as i32 + dist_left as i32 - min_distance * 2); |
|
223 |
||
224 |
Some(Point::new( |
|
225 |
mid_point.x + p.x * d / distance_divisor as i32, |
|
226 |
mid_point.y + p.y * d / distance_divisor as i32, |
|
227 |
)) |
|
228 |
} |
|
14074 | 229 |
} |
230 |
||
14105 | 231 |
fn divide_edges<I: Iterator<Item = u32>>( |
232 |
&mut self, |
|
233 |
distance_divisor: u32, |
|
234 |
random_numbers: &mut I, |
|
235 |
) { |
|
14074 | 236 |
for is in 0..self.islands.len() { |
237 |
let mut i = 0; |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
238 |
let mut segment; |
14074 | 239 |
|
240 |
loop { |
|
241 |
{ |
|
242 |
let island = &self.islands[is]; |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
243 |
let mut end_point; |
14074 | 244 |
if i < island.len() { |
245 |
end_point = if i + 1 < island.len() { |
|
246 |
island[i + 1] |
|
247 |
} else { |
|
248 |
island[0] |
|
249 |
}; |
|
250 |
} else { |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
251 |
break; |
14074 | 252 |
} |
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
253 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
254 |
segment = Line::new(island[i], end_point); |
14074 | 255 |
} |
256 |
||
14105 | 257 |
if let Some(new_point) = self.divide_edge(segment, distance_divisor, random_numbers) |
258 |
{ |
|
14074 | 259 |
self.islands[is].insert(i + 1, new_point); |
260 |
i += 2; |
|
261 |
} else { |
|
262 |
i += 1; |
|
263 |
} |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
267 |
||
14105 | 268 |
pub fn bezierize(&mut self) {} |
14074 | 269 |
|
14105 | 270 |
pub fn distort<I: Iterator<Item = u32>>( |
271 |
&mut self, |
|
272 |
distance_divisor: u32, |
|
273 |
random_numbers: &mut I, |
|
274 |
) { |
|
14074 | 275 |
loop { |
276 |
let old_len = self.total_len(); |
|
14105 | 277 |
self.divide_edges(distance_divisor, random_numbers); |
14074 | 278 |
|
14098 | 279 |
if self.total_len() == old_len { |
14074 | 280 |
break; |
281 |
} |
|
282 |
} |
|
283 |
||
284 |
self.bezierize(); |
|
285 |
} |
|
286 |
||
287 |
pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) { |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
288 |
for segment in self.segments_iter() { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
289 |
land.draw_line(segment, value); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
290 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
291 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
292 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
293 |
fn segments_iter(&self) -> OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
294 |
OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
295 |
outline: self, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
296 |
island: 0, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
297 |
index: 0, |
14074 | 298 |
} |
299 |
} |
|
14100 | 300 |
|
301 |
pub fn mirror(&mut self) { |
|
14105 | 302 |
let r = self.size.width as i32 - 1; |
303 |
||
304 |
self.iter_mut().for_each(|p| p.x = r - p.x); |
|
14100 | 305 |
} |
306 |
||
307 |
pub fn flip(&mut self) { |
|
14105 | 308 |
let t = self.size.height as i32 - 1; |
309 |
||
310 |
self.iter_mut().for_each(|p| p.y = t - p.y); |
|
14100 | 311 |
} |
14074 | 312 |
} |
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
313 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
314 |
struct OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
315 |
outline: &'a OutlinePoints, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
316 |
island: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
317 |
index: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
318 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
319 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
320 |
impl<'a> Iterator for OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
321 |
type Item = Line; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
322 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
323 |
fn next(&mut self) -> Option<Self::Item> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
324 |
if self.island < self.outline.islands.len() { |
14083 | 325 |
if self.index + 1 < self.outline.islands[self.island].len() { |
326 |
let result = Some(Line::new( |
|
327 |
self.outline.islands[self.island][self.index], |
|
328 |
self.outline.islands[self.island][self.index + 1], |
|
329 |
)); |
|
330 |
||
331 |
self.index += 1; |
|
332 |
||
333 |
result |
|
334 |
} else if self.index + 1 == self.outline.islands[self.island].len() { |
|
335 |
let result = Some(Line::new( |
|
336 |
self.outline.islands[self.island][self.index], |
|
337 |
self.outline.islands[self.island][0], |
|
338 |
)); |
|
339 |
||
340 |
self.island += 1; |
|
341 |
self.index = 0; |
|
342 |
||
343 |
result |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
344 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
345 |
self.island += 1; |
14083 | 346 |
self.index = 0; |
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
347 |
self.next() |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
348 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
349 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
350 |
None |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
351 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
352 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
353 |
} |
14083 | 354 |
|
355 |
#[test()] |
|
356 |
fn points_test() { |
|
357 |
let mut points = OutlinePoints { |
|
358 |
islands: vec![ |
|
359 |
vec![Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)], |
|
360 |
vec![Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)], |
|
361 |
], |
|
362 |
fill_points: vec![Point::new(1, 1)], |
|
363 |
play_box: Rect::from_box(0, 100, 0, 100).with_margin(10), |
|
364 |
size: Size::square(100), |
|
365 |
}; |
|
366 |
||
367 |
let segments: Vec<Line> = points.segments_iter().collect(); |
|
368 |
assert_eq!( |
|
369 |
segments.first(), |
|
370 |
Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) |
|
371 |
); |
|
372 |
assert_eq!( |
|
373 |
segments.last(), |
|
374 |
Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) |
|
375 |
); |
|
376 |
||
377 |
points.iter_mut().for_each(|p| p.x = 2); |
|
378 |
assert_eq!(points.fill_points[0].x, 2); |
|
379 |
assert_eq!(points.islands[0][0].x, 2); |
|
380 |
} |