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