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