author | alfadur |
Sat, 03 Nov 2018 02:16:13 +0300 | |
changeset 14126 | 7e25f4800af8 |
parent 14125 | e2b6ff0421ec |
child 14127 | 5c1ce63114a5 |
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 { |
|
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 |
||
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 |
||
108 |
let p = Point::new( |
|
109 |
segment.end.y - segment.start.y, |
|
14124 | 110 |
segment.start.x - segment.end.x, |
14102 | 111 |
); |
112 |
let mid_point = segment.center(); |
|
113 |
||
14126 | 114 |
if (p.integral_norm() < min_distance as u32 * 3) || !map_box.contains_inside(mid_point) { |
14102 | 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 |
|
14121 | 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 |
} |
|
14102 | 184 |
|
14121 | 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 |
} |
|
14090 | 229 |
} |
230 |
||
14121 | 231 |
fn divide_edges<I: Iterator<Item = u32>>( |
232 |
&mut self, |
|
233 |
distance_divisor: u32, |
|
234 |
random_numbers: &mut I, |
|
235 |
) { |
|
14090 | 236 |
for is in 0..self.islands.len() { |
237 |
let mut i = 0; |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
238 |
let mut segment; |
14090 | 239 |
|
240 |
loop { |
|
241 |
{ |
|
242 |
let island = &self.islands[is]; |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
243 |
let mut end_point; |
14090 | 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 { |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
251 |
break; |
14090 | 252 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
253 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
254 |
segment = Line::new(island[i], end_point); |
14090 | 255 |
} |
256 |
||
14121 | 257 |
if let Some(new_point) = self.divide_edge(segment, distance_divisor, random_numbers) |
258 |
{ |
|
14090 | 259 |
self.islands[is].insert(i + 1, new_point); |
260 |
i += 2; |
|
261 |
} else { |
|
262 |
i += 1; |
|
263 |
} |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
267 |
||
14121 | 268 |
pub fn bezierize(&mut self) {} |
14090 | 269 |
|
14121 | 270 |
pub fn distort<I: Iterator<Item = u32>>( |
271 |
&mut self, |
|
272 |
distance_divisor: u32, |
|
273 |
random_numbers: &mut I, |
|
274 |
) { |
|
14090 | 275 |
loop { |
276 |
let old_len = self.total_len(); |
|
14121 | 277 |
self.divide_edges(distance_divisor, random_numbers); |
14090 | 278 |
|
14114 | 279 |
if self.total_len() == old_len { |
14090 | 280 |
break; |
281 |
} |
|
282 |
} |
|
283 |
||
284 |
self.bezierize(); |
|
285 |
} |
|
286 |
||
287 |
pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) { |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
288 |
for segment in self.segments_iter() { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
289 |
land.draw_line(segment, value); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
290 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
291 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
292 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
293 |
fn segments_iter(&self) -> OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
294 |
OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
295 |
outline: self, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
296 |
island: 0, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
297 |
index: 0, |
14090 | 298 |
} |
299 |
} |
|
14116 | 300 |
|
301 |
pub fn mirror(&mut self) { |
|
14121 | 302 |
let r = self.size.width as i32 - 1; |
303 |
||
304 |
self.iter_mut().for_each(|p| p.x = r - p.x); |
|
14116 | 305 |
} |
306 |
||
307 |
pub fn flip(&mut self) { |
|
14121 | 308 |
let t = self.size.height as i32 - 1; |
309 |
||
310 |
self.iter_mut().for_each(|p| p.y = t - p.y); |
|
14116 | 311 |
} |
14090 | 312 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
313 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
314 |
struct OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
315 |
outline: &'a OutlinePoints, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
316 |
island: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
317 |
index: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
318 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
319 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
320 |
impl<'a> Iterator for OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
321 |
type Item = Line; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
322 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
323 |
fn next(&mut self) -> Option<Self::Item> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
324 |
if self.island < self.outline.islands.len() { |
14099 | 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 |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
344 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
345 |
self.island += 1; |
14099 | 346 |
self.index = 0; |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
347 |
self.next() |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
348 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
349 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
350 |
None |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
351 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
352 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
353 |
} |
14099 | 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 |
} |