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