author | unC0Rr |
Wed, 15 Feb 2023 12:42:43 +0100 | |
branch | transitional_engine |
changeset 15956 | c273908218f3 |
parent 15951 | 5f00829c55ec |
child 16057 | 509ecce37522 |
permissions | -rw-r--r-- |
14090 | 1 |
use itertools::Itertools; |
14102 | 2 |
use std::cmp::min; |
14090 | 3 |
|
15850 | 4 |
use integral_geometry::{Line, Point, Polygon, Ray, Rect, Size}; |
14090 | 5 |
use land2d::Land2D; |
6 |
||
15951 | 7 |
use super::outline_template::OutlineTemplate; |
14090 | 8 |
|
9 |
pub struct OutlinePoints { |
|
14145 | 10 |
pub islands: Vec<Polygon>, |
14090 | 11 |
pub fill_points: Vec<Point>, |
12 |
pub size: Size, |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
13 |
pub play_box: Rect, |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
14 |
intersections_box: Rect, |
14090 | 15 |
} |
16 |
||
17 |
impl OutlinePoints { |
|
18 |
pub fn from_outline_template<I: Iterator<Item = u32>>( |
|
19 |
outline_template: &OutlineTemplate, |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
20 |
play_box: Rect, |
14099 | 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))| { |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
34 |
play_box.top_left() + rect.quotient(rnd_a as usize, rnd_b as usize) |
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(), |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
41 |
intersections_box: Rect::at_origin(size) |
14146 | 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] |
|
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
76 |
fn solve_intersection( |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
77 |
intersections_box: &Rect, |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
78 |
ray: &Ray, |
15850 | 79 |
edge: &Line, |
80 |
) -> Option<(i32, u32)> { |
|
14152 | 81 |
let edge_dir = edge.scaled_direction(); |
82 |
let aqpb = ray.direction.cross(edge_dir) as i64; |
|
14121 | 83 |
|
84 |
if aqpb != 0 { |
|
15850 | 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) |
|
14152 | 87 |
* edge_dir.y as i64 |
88 |
- edge.start.y as i64 * edge_dir.x as i64 * ray.direction.y as i64) |
|
14121 | 89 |
/ aqpb) as i32; |
14146 | 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 |
||
14152 | 98 |
let ix = if ray.direction.y.abs() > edge_dir.y.abs() { |
14163 | 99 |
ray.start.x + ray.direction.cotangent_mul(iy - ray.start.y) |
14121 | 100 |
} else { |
14163 | 101 |
edge.start.x + edge_dir.cotangent_mul(iy - edge.start.y) |
14121 | 102 |
}; |
103 |
||
14153 | 104 |
let intersection_point = Point::new(ix, iy).clamp(intersections_box); |
14152 | 105 |
let diff_point = ray.start - intersection_point; |
106 |
let t = ray.direction.dot(diff_point); |
|
14153 | 107 |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
108 |
if diff_point.max_norm() >= std::i16::MAX as i32 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
109 |
Some((t, std::i32::MAX as u32)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
110 |
} else { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
111 |
let d = diff_point.integral_norm(); |
14121 | 112 |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
113 |
Some((t, d)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
114 |
} |
14121 | 115 |
} else { |
116 |
None |
|
117 |
} |
|
118 |
} |
|
119 |
||
14102 | 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 |
||
14152 | 124 |
let normal = segment.scaled_normal(); |
125 |
let normal_len = normal.integral_norm(); |
|
14102 | 126 |
let mid_point = segment.center(); |
127 |
||
14152 | 128 |
if (normal_len < min_distance as u32 * 3) || !map_box.contains_inside(mid_point) { |
14102 | 129 |
return None; |
130 |
} |
|
131 |
||
14152 | 132 |
let normal_ray = Ray::new(mid_point, normal); |
14102 | 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 |
|
14152 | 137 |
if normal.x != 0 { |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
138 |
// where the normal line intersects the left map border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
139 |
let left_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
140 |
map_box.left(), |
14163 | 141 |
mid_point.y + normal.tangent_mul(map_box.left() - mid_point.x), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
142 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
143 |
dist_left = (mid_point - left_intersection).integral_norm(); |
14102 | 144 |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
145 |
// same for the right border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
146 |
let right_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
147 |
map_box.right(), |
15850 | 148 |
mid_point.y + normal.tangent_mul(map_box.right() - mid_point.x), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
149 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
150 |
dist_right = (mid_point - right_intersection).integral_norm(); |
14102 | 151 |
|
14152 | 152 |
if normal.x > 0 { |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
153 |
std::mem::swap(&mut dist_left, &mut dist_right); |
14102 | 154 |
} |
155 |
} |
|
156 |
||
14152 | 157 |
if normal.y != 0 { |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
158 |
// where the normal line intersects the top map border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
159 |
let top_intersection = Point::new( |
14163 | 160 |
mid_point.x + normal.cotangent_mul(map_box.top() - mid_point.y), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
161 |
map_box.top(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
162 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
163 |
let dl = (mid_point - top_intersection).integral_norm(); |
14102 | 164 |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
165 |
// same for the bottom border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
166 |
let bottom_intersection = Point::new( |
14163 | 167 |
mid_point.x + normal.cotangent_mul(map_box.bottom() - mid_point.y), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
168 |
map_box.bottom(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
169 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
170 |
let dr = (mid_point - bottom_intersection).integral_norm(); |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
171 |
|
14152 | 172 |
if normal.y < 0 { |
14102 | 173 |
dist_left = min(dist_left, dl); |
174 |
dist_right = min(dist_right, dr); |
|
175 |
} else { |
|
176 |
dist_left = min(dist_left, dr); |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
177 |
dist_right = min(dist_right, dl); |
14102 | 178 |
} |
179 |
} |
|
180 |
||
181 |
// now go through all other segments |
|
14121 | 182 |
for s in self.segments_iter() { |
15956 | 183 |
if s != segment && intersects(&normal_ray, &s) { |
184 |
if let Some((t, d)) = solve_intersection(&self.intersections_box, &normal_ray, &s) { |
|
185 |
if t > 0 { |
|
186 |
dist_right = min(dist_right, d); |
|
187 |
} else { |
|
188 |
dist_left = min(dist_left, d); |
|
14121 | 189 |
} |
190 |
} |
|
191 |
} |
|
192 |
} |
|
14102 | 193 |
|
14121 | 194 |
// go through all points, including fill points |
14147 | 195 |
for pi in self.iter().cloned() { |
15956 | 196 |
if pi != segment.start |
197 |
&& pi != segment.end |
|
198 |
&& intersects(&pi.ray_with_dir(normal), &segment) |
|
199 |
{ |
|
200 |
// ray from segment.start |
|
201 |
if let Some((t, d)) = solve_intersection( |
|
202 |
&self.intersections_box, |
|
203 |
&normal_ray, |
|
204 |
&segment.start.line_to(pi), |
|
205 |
) { |
|
206 |
if t > 0 { |
|
207 |
dist_right = min(dist_right, d); |
|
208 |
} else { |
|
209 |
dist_left = min(dist_left, d); |
|
14121 | 210 |
} |
15956 | 211 |
} |
14121 | 212 |
|
15956 | 213 |
// ray from segment.end |
214 |
if let Some((t, d)) = solve_intersection( |
|
215 |
&self.intersections_box, |
|
216 |
&normal_ray, |
|
217 |
&segment.end.line_to(pi), |
|
218 |
) { |
|
219 |
if t > 0 { |
|
220 |
dist_right = min(dist_right, d); |
|
221 |
} else { |
|
222 |
dist_left = min(dist_left, d); |
|
14121 | 223 |
} |
224 |
} |
|
225 |
} |
|
226 |
} |
|
227 |
||
14152 | 228 |
let max_dist = normal_len * 100 / distance_divisor; |
14121 | 229 |
dist_left = min(dist_left, max_dist); |
230 |
dist_right = min(dist_right, max_dist); |
|
231 |
||
232 |
if dist_right + dist_left < min_distance as u32 * 2 + 10 { |
|
233 |
// limits are too narrow, just divide |
|
234 |
Some(mid_point) |
|
235 |
} else { |
|
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
236 |
// 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
|
237 |
let d = -(dist_right as i32) |
14121 | 238 |
+ min_distance |
239 |
+ random_numbers.next().unwrap() as i32 |
|
240 |
% (dist_right as i32 + dist_left as i32 - min_distance * 2); |
|
241 |
||
14152 | 242 |
Some(mid_point + normal * d / normal_len as i32) |
14121 | 243 |
} |
14090 | 244 |
} |
245 |
||
14121 | 246 |
fn divide_edges<I: Iterator<Item = u32>>( |
247 |
&mut self, |
|
248 |
distance_divisor: u32, |
|
249 |
random_numbers: &mut I, |
|
250 |
) { |
|
14090 | 251 |
for is in 0..self.islands.len() { |
252 |
let mut i = 0; |
|
14145 | 253 |
while i < self.islands[is].edges_count() { |
254 |
let segment = self.islands[is].get_edge(i); |
|
14146 | 255 |
if let Some(new_point) = self.divide_edge(segment, distance_divisor, random_numbers) |
256 |
{ |
|
14145 | 257 |
self.islands[is].split_edge(i, new_point); |
14090 | 258 |
i += 2; |
259 |
} else { |
|
260 |
i += 1; |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
} |
|
265 |
||
14161 | 266 |
pub fn bezierize(&mut self, segments_number: u32) { |
267 |
for island in &mut self.islands { |
|
268 |
island.bezierize(segments_number); |
|
269 |
} |
|
270 |
} |
|
14090 | 271 |
|
14121 | 272 |
pub fn distort<I: Iterator<Item = u32>>( |
273 |
&mut self, |
|
274 |
distance_divisor: u32, |
|
275 |
random_numbers: &mut I, |
|
276 |
) { |
|
14090 | 277 |
loop { |
278 |
let old_len = self.total_len(); |
|
14121 | 279 |
self.divide_edges(distance_divisor, random_numbers); |
14090 | 280 |
|
14114 | 281 |
if self.total_len() == old_len { |
14090 | 282 |
break; |
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
||
15942 | 287 |
pub fn draw<T: Copy + PartialEq + Default>(&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 |
|
14145 | 293 |
fn segments_iter<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
294 |
self.islands.iter().flat_map(|p| p.iter_edges()) |
|
14090 | 295 |
} |
14116 | 296 |
|
297 |
pub fn mirror(&mut self) { |
|
14121 | 298 |
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
|
299 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
300 |
self.iter_mut().for_each(|p| p.x = r - p.x); |
14116 | 301 |
} |
302 |
||
303 |
pub fn flip(&mut self) { |
|
14121 | 304 |
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
|
305 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
306 |
self.iter_mut().for_each(|p| p.y = t - p.y); |
14116 | 307 |
} |
14090 | 308 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
309 |
|
15777 | 310 |
#[test] |
14099 | 311 |
fn points_test() { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
312 |
let size = Size::square(100); |
14099 | 313 |
let mut points = OutlinePoints { |
314 |
islands: vec![ |
|
14145 | 315 |
Polygon::new(&[Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)]), |
316 |
Polygon::new(&[Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)]), |
|
14099 | 317 |
], |
318 |
fill_points: vec![Point::new(1, 1)], |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
319 |
play_box: Rect::at_origin(size).with_margin(10), |
14099 | 320 |
size: Size::square(100), |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
321 |
intersections_box: Rect::at_origin(size), |
14099 | 322 |
}; |
323 |
||
324 |
let segments: Vec<Line> = points.segments_iter().collect(); |
|
325 |
assert_eq!( |
|
326 |
segments.first(), |
|
327 |
Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) |
|
328 |
); |
|
329 |
assert_eq!( |
|
330 |
segments.last(), |
|
331 |
Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) |
|
332 |
); |
|
333 |
||
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
334 |
points.iter_mut().for_each(|p| p.x = 2); |
14146 | 335 |
|
14099 | 336 |
assert_eq!(points.fill_points[0].x, 2); |
14145 | 337 |
assert_eq!(points.islands[0].get_edge(0).start.x, 2); |
14099 | 338 |
} |