author | unC0Rr |
Fri, 02 Nov 2018 14:29:24 +0100 | |
changeset 14083 | bf40b5f938b0 |
parent 14082 | 5ade484f3351 |
child 14086 | 5d42204ac35e |
permissions | -rw-r--r-- |
14074 | 1 |
use itertools::Itertools; |
2 |
||
14083 | 3 |
use integral_geometry::{Line, Point, Rect, Size}; |
14074 | 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, |
|
14083 | 12 |
pub play_box: Rect, |
14074 | 13 |
} |
14 |
||
15 |
impl OutlinePoints { |
|
16 |
pub fn from_outline_template<I: Iterator<Item = u32>>( |
|
17 |
outline_template: &OutlineTemplate, |
|
14083 | 18 |
play_box: Rect, |
19 |
size: Size, |
|
14074 | 20 |
random_numbers: &mut I, |
21 |
) -> Self { |
|
22 |
Self { |
|
14083 | 23 |
play_box, |
24 |
size, |
|
14074 | 25 |
islands: outline_template |
26 |
.islands |
|
27 |
.iter() |
|
28 |
.map(|i| { |
|
29 |
i.iter() |
|
30 |
.zip(random_numbers.tuples()) |
|
31 |
.map(|(rect, (rnd_a, rnd_b))| { |
|
14083 | 32 |
rect.top_left() |
33 |
+ Point::new( |
|
34 |
(rnd_a % rect.width) as i32, |
|
35 |
(rnd_b % rect.height) as i32, |
|
36 |
) |
|
37 |
+ play_box.top_left() |
|
14074 | 38 |
}).collect() |
39 |
}).collect(), |
|
40 |
fill_points: outline_template.fill_points.clone(), |
|
41 |
} |
|
42 |
} |
|
43 |
||
44 |
pub fn total_len(&self) -> usize { |
|
45 |
self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len() |
|
46 |
} |
|
47 |
||
48 |
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
|
49 |
self.islands |
|
50 |
.iter_mut() |
|
51 |
.flat_map(|i| i.iter_mut()) |
|
52 |
.chain(self.fill_points.iter_mut()) |
|
53 |
} |
|
54 |
||
55 |
fn divide_edge<I: Iterator<Item = u32>>( |
|
56 |
&self, |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
57 |
segment: Line, |
14074 | 58 |
random_numbers: &mut I, |
59 |
) -> Option<Point> { |
|
60 |
None |
|
61 |
} |
|
62 |
||
63 |
fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
|
64 |
for is in 0..self.islands.len() { |
|
65 |
let mut i = 0; |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
66 |
let mut segment; |
14074 | 67 |
|
68 |
loop { |
|
69 |
{ |
|
70 |
let island = &self.islands[is]; |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
71 |
let mut end_point; |
14074 | 72 |
if i < island.len() { |
73 |
end_point = if i + 1 < island.len() { |
|
74 |
island[i + 1] |
|
75 |
} else { |
|
76 |
island[0] |
|
77 |
}; |
|
78 |
} else { |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
79 |
break; |
14074 | 80 |
} |
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
81 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
82 |
segment = Line::new(island[i], end_point); |
14074 | 83 |
} |
84 |
||
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
85 |
if let Some(new_point) = self.divide_edge(segment, random_numbers) { |
14074 | 86 |
self.islands[is].insert(i + 1, new_point); |
87 |
i += 2; |
|
88 |
} else { |
|
89 |
i += 1; |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
pub fn bezierize(&mut self) { |
|
96 |
unimplemented!() |
|
97 |
} |
|
98 |
||
99 |
pub fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
|
100 |
loop { |
|
101 |
let old_len = self.total_len(); |
|
102 |
self.divide_edges(random_numbers); |
|
103 |
||
104 |
if self.total_len() != old_len { |
|
105 |
break; |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
self.bezierize(); |
|
110 |
} |
|
111 |
||
112 |
pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) { |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
113 |
for segment in self.segments_iter() { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
114 |
land.draw_line(segment, value); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
115 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
116 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
117 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
118 |
fn segments_iter(&self) -> OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
119 |
OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
120 |
outline: self, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
121 |
island: 0, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
122 |
index: 0, |
14074 | 123 |
} |
124 |
} |
|
125 |
} |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
126 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
127 |
struct OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
128 |
outline: &'a OutlinePoints, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
129 |
island: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
130 |
index: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
131 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
132 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
133 |
impl<'a> Iterator for OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
134 |
type Item = Line; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
135 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
136 |
fn next(&mut self) -> Option<Self::Item> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
137 |
if self.island < self.outline.islands.len() { |
14083 | 138 |
if self.index + 1 < self.outline.islands[self.island].len() { |
139 |
let result = Some(Line::new( |
|
140 |
self.outline.islands[self.island][self.index], |
|
141 |
self.outline.islands[self.island][self.index + 1], |
|
142 |
)); |
|
143 |
||
144 |
self.index += 1; |
|
145 |
||
146 |
result |
|
147 |
} else if self.index + 1 == self.outline.islands[self.island].len() { |
|
148 |
let result = Some(Line::new( |
|
149 |
self.outline.islands[self.island][self.index], |
|
150 |
self.outline.islands[self.island][0], |
|
151 |
)); |
|
152 |
||
153 |
self.island += 1; |
|
154 |
self.index = 0; |
|
155 |
||
156 |
result |
|
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
157 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
158 |
self.island += 1; |
14083 | 159 |
self.index = 0; |
14081
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
160 |
self.next() |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
161 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
162 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
163 |
None |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
164 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
165 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14074
diff
changeset
|
166 |
} |
14083 | 167 |
|
168 |
#[test()] |
|
169 |
fn points_test() { |
|
170 |
let mut points = OutlinePoints { |
|
171 |
islands: vec![ |
|
172 |
vec![Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)], |
|
173 |
vec![Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)], |
|
174 |
], |
|
175 |
fill_points: vec![Point::new(1, 1)], |
|
176 |
play_box: Rect::from_box(0, 100, 0, 100).with_margin(10), |
|
177 |
size: Size::square(100), |
|
178 |
}; |
|
179 |
||
180 |
let segments: Vec<Line> = points.segments_iter().collect(); |
|
181 |
assert_eq!( |
|
182 |
segments.first(), |
|
183 |
Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) |
|
184 |
); |
|
185 |
assert_eq!( |
|
186 |
segments.last(), |
|
187 |
Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) |
|
188 |
); |
|
189 |
||
190 |
points.iter_mut().for_each(|p| p.x = 2); |
|
191 |
assert_eq!(points.fill_points[0].x, 2); |
|
192 |
assert_eq!(points.islands[0][0].x, 2); |
|
193 |
} |