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