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