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