rust/landgen/src/outline.rs
author unc0rr
Sun, 04 Nov 2018 00:20:27 +0100
changeset 14125 376a0551b00a
parent 14124 6e0be42d0a8f
child 14126 32383b888309
permissions -rw-r--r--
- Add distance-divider option to land_dump - Fix multiplication overflow errors (iy went widly out of bounds)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     1
use itertools::Itertools;
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
     2
use std::cmp::min;
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     3
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
     4
use integral_geometry::{Line, Point, Polygon, Rect, Size};
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     5
use land2d::Land2D;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     6
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     7
use outline_template::OutlineTemplate;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     8
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     9
pub struct OutlinePoints {
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
    10
    pub islands: Vec<Polygon>,
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    11
    pub fill_points: Vec<Point>,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    12
    pub size: Size,
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    13
    pub play_box: Rect,
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    14
    intersections_box: Rect,
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    15
}
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    16
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    17
impl OutlinePoints {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    18
    pub fn from_outline_template<I: Iterator<Item = u32>>(
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    19
        outline_template: &OutlineTemplate,
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    20
        play_box: Rect,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    21
        size: Size,
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    22
        random_numbers: &mut I,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    23
    ) -> Self {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    24
        Self {
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    25
            play_box,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    26
            size,
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    27
            islands: outline_template
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    28
                .islands
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    29
                .iter()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    30
                .map(|i| {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    31
                    i.iter()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    32
                        .zip(random_numbers.tuples())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    33
                        .map(|(rect, (rnd_a, rnd_b))| {
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    34
                            rect.top_left()
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    35
                                + Point::new(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    36
                                    (rnd_a % rect.width) as i32,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    37
                                    (rnd_b % rect.height) as i32,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    38
                                )
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
    39
                                + play_box.top_left()
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
    40
                        })
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    41
                        .collect::<Vec<_>>()
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    42
                        .into()
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
    43
                })
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
    44
                .collect(),
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    45
            fill_points: outline_template.fill_points.clone(),
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    46
            intersections_box: Rect::at_origin(size)
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    47
                .with_margin(size.to_square().width as i32 * -2),
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    48
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    49
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    50
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    51
    pub fn total_len(&self) -> usize {
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
    52
        self.islands.iter().map(|i| i.edges_count()).sum::<usize>() + self.fill_points.len()
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    53
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    54
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    55
    pub fn iter(&self) -> impl Iterator<Item = &Point> {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    56
        self.islands
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    57
            .iter()
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
    58
            .flat_map(|p| p.iter())
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    59
            .chain(self.fill_points.iter())
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    60
    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    61
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    62
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    63
        self.islands
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    64
            .iter_mut()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    65
            .flat_map(|i| i.iter_mut())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    66
            .chain(self.fill_points.iter_mut())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    67
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    68
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    69
    fn divide_edge<I: Iterator<Item = u32>>(
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    70
        &self,
14076
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14069
diff changeset
    71
        segment: Line,
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    72
        distance_divisor: u32,
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    73
        random_numbers: &mut I,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    74
    ) -> Option<Point> {
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    75
        #[inline]
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    76
        fn intersect(p: &Point, m: &Point, p1: &Point, p2: &Point) -> bool {
14109
913be386b413 fix typos
alfadur
parents: 14108
diff changeset
    77
            let t1 = (*m - *p1).cross(*p);
913be386b413 fix typos
alfadur
parents: 14108
diff changeset
    78
            let t2 = (*m - *p2).cross(*p);
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    79
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    80
            (t1 > 0) != (t2 > 0)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    81
        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    82
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    83
        #[inline]
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    84
        fn solve_intersection(
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    85
            intersections_box: &Rect,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    86
            p: &Point,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    87
            m: &Point,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    88
            s: &Point,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    89
            e: &Point,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    90
        ) -> Option<(i32, u32)> {
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    91
            let f = *e - *s;
14108
36b792842d5b a bit more simplification
alfadur
parents: 14107
diff changeset
    92
            let aqpb = p.cross(f) as i64;
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    93
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    94
            if aqpb != 0 {
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    95
                let mut iy = ((((s.x - m.x) as i64 * p.y as i64 + m.y as i64 * p.x as i64)
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    96
                    * f.y as i64
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    97
                    - s.y as i64 * f.x as i64 * p.y as i64)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
    98
                    / aqpb) as i32;
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
    99
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   100
                // is there better way to do it?
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   101
                if iy < intersections_box.top() {
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   102
                    iy = intersections_box.top();
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   103
                } else if iy > intersections_box.bottom() {
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   104
                    iy = intersections_box.bottom();
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   105
                }
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   106
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   107
                let ix = if p.y.abs() > f.y.abs() {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   108
                    (iy - m.y) * p.x / p.y + m.x
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   109
                } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   110
                    (iy - s.y) * f.x / f.y + s.x
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   111
                };
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   112
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   113
                let intersection_point = Point::new(ix, iy);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   114
                let diff_point = *m - intersection_point;
14108
36b792842d5b a bit more simplification
alfadur
parents: 14107
diff changeset
   115
                let t = p.dot(diff_point);
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   116
                if diff_point.max_norm() >= std::i16::MAX as i32 {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   117
                    Some((t, std::i32::MAX as u32))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   118
                } else {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   119
                    let d = diff_point.integral_norm();
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   120
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   121
                    Some((t, d))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   122
                }
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   123
            } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   124
                None
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   125
            }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   126
        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   127
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   128
        let min_distance = 40;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   129
        // new point should fall inside this box
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   130
        let map_box = self.play_box.with_margin(min_distance);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   131
14107
6a3bcb7c2981 swap coordinate system
alfadur
parents: 14106
diff changeset
   132
        let p = segment.scaled_normal();
14116
83bd383e6d11 probably should reuse it everywhere then
alfadur
parents: 14115
diff changeset
   133
        let p_norm = p.integral_norm();
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   134
        let mid_point = segment.center();
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   135
14116
83bd383e6d11 probably should reuse it everywhere then
alfadur
parents: 14115
diff changeset
   136
        if (p_norm < min_distance as u32 * 3) || !map_box.contains_inside(mid_point) {
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   137
            return None;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   138
        }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   139
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   140
        let mut dist_left = (self.size.width + self.size.height) as u32;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   141
        let mut dist_right = dist_left;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   142
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   143
        // find distances to map borders
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   144
        if p.x != 0 {
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   145
            // where the normal line intersects the left map border
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   146
            let left_intersection = Point::new(
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   147
                map_box.left(),
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   148
                (map_box.left() - mid_point.x) * p.y / p.x + mid_point.y,
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_left = (mid_point - left_intersection).integral_norm();
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   151
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   152
            // same for the right border
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   153
            let right_intersection = Point::new(
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   154
                map_box.right(),
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   155
                (map_box.right() - mid_point.x) * p.y / p.x + mid_point.y,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   156
            );
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   157
            dist_right = (mid_point - right_intersection).integral_norm();
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   158
14111
269f13ac670d simplify normal direction check for bounds distances
alfadur
parents: 14110
diff changeset
   159
            if p.x > 0 {
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   160
                std::mem::swap(&mut dist_left, &mut dist_right);
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   161
            }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   162
        }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   163
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   164
        if p.y != 0 {
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   165
            // where the normal line intersects the top map border
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   166
            let top_intersection = Point::new(
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   167
                (map_box.top() - mid_point.y) * p.x / p.y + mid_point.x,
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   168
                map_box.top(),
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 dl = (mid_point - top_intersection).integral_norm();
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   171
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   172
            // same for the bottom border
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   173
            let bottom_intersection = Point::new(
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   174
                (map_box.bottom() - mid_point.y) * p.x / p.y + mid_point.x,
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   175
                map_box.bottom(),
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14121
diff changeset
   176
            );
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   177
            let dr = (mid_point - bottom_intersection).integral_norm();
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   178
14111
269f13ac670d simplify normal direction check for bounds distances
alfadur
parents: 14110
diff changeset
   179
            if p.y < 0 {
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   180
                dist_left = min(dist_left, dl);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   181
                dist_right = min(dist_right, dr);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   182
            } else {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   183
                dist_left = min(dist_left, dr);
14110
21642eb0ff29 import some clarity into border distance computation
alfadur
parents: 14109
diff changeset
   184
                dist_right = min(dist_right, dl);
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   185
            }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   186
        }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   187
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   188
        // now go through all other segments
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   189
        for s in self.segments_iter() {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   190
            if s != segment {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   191
                if intersect(&p, &mid_point, &s.start, &s.end) {
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   192
                    if let Some((t, d)) = solve_intersection(
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   193
                        &self.intersections_box,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   194
                        &p,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   195
                        &mid_point,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   196
                        &s.start,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   197
                        &s.end,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   198
                    ) {
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   199
                        if t > 0 {
14113
be4419243735 fix normal offset for split points and make directions more consistent
alfadur
parents: 14111
diff changeset
   200
                            dist_right = min(dist_right, d);
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   201
                        } else {
14113
be4419243735 fix normal offset for split points and make directions more consistent
alfadur
parents: 14111
diff changeset
   202
                            dist_left = min(dist_left, d);
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   203
                        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   204
                    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   205
                }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   206
            }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   207
        }
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14078
diff changeset
   208
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   209
        // go through all points, including fill points
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   210
        for pi in self.iter() {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   211
            if *pi != segment.start && *pi != segment.end {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   212
                if intersect(&p, &pi, &segment.start, &segment.end) {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   213
                    // ray from segment.start
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   214
                    if let Some((t, d)) = solve_intersection(
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   215
                        &self.intersections_box,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   216
                        &p,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   217
                        &mid_point,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   218
                        &segment.start,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   219
                        &pi,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   220
                    ) {
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   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
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   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
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   225
                        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   226
                    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   227
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   228
                    // ray from segment.end
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   229
                    if let Some((t, d)) = solve_intersection(
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   230
                        &self.intersections_box,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   231
                        &p,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   232
                        &mid_point,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   233
                        &segment.end,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   234
                        &pi,
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   235
                    ) {
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   236
                        if t > 0 {
14113
be4419243735 fix normal offset for split points and make directions more consistent
alfadur
parents: 14111
diff changeset
   237
                            dist_right = min(dist_right, d);
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   238
                        } else {
14113
be4419243735 fix normal offset for split points and make directions more consistent
alfadur
parents: 14111
diff changeset
   239
                            dist_left = min(dist_left, d);
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   240
                        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   241
                    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   242
                }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   243
            }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   244
        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   245
14116
83bd383e6d11 probably should reuse it everywhere then
alfadur
parents: 14115
diff changeset
   246
        let max_dist = p_norm * 100 / distance_divisor;
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   247
        dist_left = min(dist_left, max_dist);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   248
        dist_right = min(dist_right, max_dist);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   249
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   250
        if dist_right + dist_left < min_distance as u32 * 2 + 10 {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   251
            // limits are too narrow, just divide
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   252
            Some(mid_point)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   253
        } else {
14113
be4419243735 fix normal offset for split points and make directions more consistent
alfadur
parents: 14111
diff changeset
   254
            // 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
   255
            let d = -(dist_right as i32)
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   256
                + min_distance
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   257
                + random_numbers.next().unwrap() as i32
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   258
                    % (dist_right as i32 + dist_left as i32 - min_distance * 2);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   259
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   260
            Some(Point::new(
14116
83bd383e6d11 probably should reuse it everywhere then
alfadur
parents: 14115
diff changeset
   261
                mid_point.x + p.x * d / p_norm as i32,
83bd383e6d11 probably should reuse it everywhere then
alfadur
parents: 14115
diff changeset
   262
                mid_point.y + p.y * d / p_norm as i32,
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   263
            ))
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   264
        }
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   265
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   266
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   267
    fn divide_edges<I: Iterator<Item = u32>>(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   268
        &mut self,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   269
        distance_divisor: u32,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   270
        random_numbers: &mut I,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   271
    ) {
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   272
        for is in 0..self.islands.len() {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   273
            let mut i = 0;
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   274
            while i < self.islands[is].edges_count() {
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   275
                let segment = self.islands[is].get_edge(i);
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   276
                if let Some(new_point) = self.divide_edge(segment, distance_divisor, random_numbers)
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   277
                {
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   278
                    self.islands[is].split_edge(i, new_point);
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   279
                    i += 2;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   280
                } else {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   281
                    i += 1;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   282
                }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   283
            }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   284
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   285
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   286
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   287
    pub fn bezierize(&mut self) {}
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   288
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   289
    pub fn distort<I: Iterator<Item = u32>>(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   290
        &mut self,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   291
        distance_divisor: u32,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   292
        random_numbers: &mut I,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   293
    ) {
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   294
        loop {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   295
            let old_len = self.total_len();
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   296
            self.divide_edges(distance_divisor, random_numbers);
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   297
14093
dbaa125a0fe9 fix infinite loop in landgen
alfadur
parents: 14081
diff changeset
   298
            if self.total_len() == old_len {
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   299
                break;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   300
            }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   301
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   302
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   303
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   304
    pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
14076
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14069
diff changeset
   305
        for segment in self.segments_iter() {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14069
diff changeset
   306
            land.draw_line(segment, value);
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14069
diff changeset
   307
        }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14069
diff changeset
   308
    }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14069
diff changeset
   309
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   310
    fn segments_iter<'a>(&'a self) -> impl Iterator<Item = Line> + 'a {
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   311
        self.islands.iter().flat_map(|p| p.iter_edges())
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   312
    }
14095
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14093
diff changeset
   313
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14093
diff changeset
   314
    pub fn mirror(&mut self) {
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   315
        let r = self.size.width as i32 - 1;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   316
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   317
        self.iter_mut().for_each(|p| p.x = r - p.x);
14095
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14093
diff changeset
   318
    }
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14093
diff changeset
   319
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14093
diff changeset
   320
    pub fn flip(&mut self) {
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   321
        let t = self.size.height as i32 - 1;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   322
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14097
diff changeset
   323
        self.iter_mut().for_each(|p| p.y = t - p.y);
14095
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14093
diff changeset
   324
    }
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   325
}
14076
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14069
diff changeset
   326
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   327
#[test()]
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   328
fn points_test() {
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   329
    let mut points = OutlinePoints {
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   330
        islands: vec![
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   331
            Polygon::new(&[Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)]),
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   332
            Polygon::new(&[Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)]),
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   333
        ],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   334
        fill_points: vec![Point::new(1, 1)],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   335
        play_box: Rect::from_box(0, 100, 0, 100).with_margin(10),
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   336
        size: Size::square(100),
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   337
        intersections_box: Rect::from_box(0, 0, 100, 100),
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   338
    };
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   339
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   340
    let segments: Vec<Line> = points.segments_iter().collect();
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   341
    assert_eq!(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   342
        segments.first(),
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   343
        Some(&Line::new(Point::new(0, 0), Point::new(20, 0)))
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   344
    );
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   345
    assert_eq!(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   346
        segments.last(),
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   347
        Some(&Line::new(Point::new(20, 15), Point::new(10, 15)))
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   348
    );
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   349
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   350
    points.iter_mut().for_each(|p| p.x = 2);
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14124
diff changeset
   351
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   352
    assert_eq!(points.fill_points[0].x, 2);
14124
6e0be42d0a8f polygonize OutlinePoints
alfadur
parents: 14122
diff changeset
   353
    assert_eq!(points.islands[0].get_edge(0).start.x, 2);
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14077
diff changeset
   354
}