rust/landgen/src/outline.rs
author unC0Rr
Fri, 02 Nov 2018 13:30:04 +0100
changeset 14098 5ade484f3351
parent 14097 e5904ead4864
child 14099 bf40b5f938b0
permissions -rw-r--r--
Refactor tests, add fn center() for Line
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;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     2
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
     3
use integral_geometry::{Line, Point, Size};
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     4
use land2d::Land2D;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     5
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     6
use outline_template::OutlineTemplate;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     7
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     8
pub struct OutlinePoints {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     9
    pub islands: Vec<Vec<Point>>,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    10
    pub fill_points: Vec<Point>,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    11
    pub size: Size,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    12
}
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    13
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    14
impl OutlinePoints {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    15
    pub fn from_outline_template<I: Iterator<Item = u32>>(
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    16
        outline_template: &OutlineTemplate,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    17
        random_numbers: &mut I,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    18
    ) -> Self {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    19
        Self {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    20
            islands: outline_template
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    21
                .islands
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    22
                .iter()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    23
                .map(|i| {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    24
                    i.iter()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    25
                        .zip(random_numbers.tuples())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    26
                        .map(|(rect, (rnd_a, rnd_b))| {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    27
                            Point::new(
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    28
                                rect.x + (rnd_a % rect.width) as i32,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    29
                                rect.y + (rnd_b % rect.height) as i32,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    30
                            )
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    31
                        }).collect()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    32
                }).collect(),
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    33
            fill_points: outline_template.fill_points.clone(),
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    34
            size: outline_template.size,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    35
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    36
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    37
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    38
    pub fn total_len(&self) -> usize {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    39
        self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    40
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    41
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    42
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    43
        self.islands
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    44
            .iter_mut()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    45
            .flat_map(|i| i.iter_mut())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    46
            .chain(self.fill_points.iter_mut())
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
    fn divide_edge<I: Iterator<Item = u32>>(
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    50
        &self,
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
    51
        segment: Line,
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    52
        random_numbers: &mut I,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    53
    ) -> Option<Point> {
14098
5ade484f3351 Refactor tests, add fn center() for Line
unC0Rr
parents: 14097
diff changeset
    54
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    55
        None
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    56
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    57
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    58
    fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    59
        for is in 0..self.islands.len() {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    60
            let mut i = 0;
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
    61
            let mut segment;
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    62
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    63
            loop {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    64
                {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    65
                    let island = &self.islands[is];
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
    66
                    let mut end_point;
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    67
                    if i < island.len() {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    68
                        end_point = if i + 1 < island.len() {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    69
                            island[i + 1]
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    70
                        } else {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    71
                            island[0]
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    72
                        };
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    73
                    } else {
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
    74
                        break;
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    75
                    }
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
    76
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
    77
                    segment = Line::new(island[i], end_point);
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    78
                }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    79
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
    80
                if let Some(new_point) = self.divide_edge(segment, random_numbers) {
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    81
                    self.islands[is].insert(i + 1, new_point);
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    82
                    i += 2;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    83
                } else {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    84
                    i += 1;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    85
                }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    86
            }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    87
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    88
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    89
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    90
    pub fn bezierize(&mut self) {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    91
        unimplemented!()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    92
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    93
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    94
    pub fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    95
        loop {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    96
            let old_len = self.total_len();
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    97
            self.divide_edges(random_numbers);
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    98
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    99
            if self.total_len() != old_len {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   100
                break;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   101
            }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   102
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   103
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   104
        self.bezierize();
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   105
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   106
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   107
    pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   108
        for segment in self.segments_iter() {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   109
            land.draw_line(segment, value);
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   110
        }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   111
    }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   112
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   113
    fn segments_iter(&self) -> OutlineSegmentsIterator {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   114
        OutlineSegmentsIterator {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   115
            outline: self,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   116
            island: 0,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   117
            index: 0,
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   118
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   119
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   120
}
14097
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   121
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   122
struct OutlineSegmentsIterator<'a> {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   123
    outline: &'a OutlinePoints,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   124
    island: usize,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   125
    index: usize,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   126
}
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   127
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   128
impl<'a> Iterator for OutlineSegmentsIterator<'a> {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   129
    type Item = Line;
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   130
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   131
    fn next(&mut self) -> Option<Self::Item> {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   132
        if self.island < self.outline.islands.len() {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   133
            if self.index + 1 < self.outline.islands[self.index].len() {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   134
                Some(Line::new(
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   135
                    self.outline.islands[self.index][self.index],
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   136
                    self.outline.islands[self.index][self.index + 1],
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   137
                ))
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   138
            } else if self.index + 1 == self.outline.islands[self.index].len() {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   139
                Some(Line::new(
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   140
                    self.outline.islands[self.index][self.index],
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   141
                    self.outline.islands[self.index][0],
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   142
                ))
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   143
            } else {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   144
                self.island += 1;
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   145
                self.next()
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   146
            }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   147
        } else {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   148
            None
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   149
        }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   150
    }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14090
diff changeset
   151
}