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