author | alfadur |
Fri, 02 Nov 2018 20:46:17 +0300 | |
changeset 14114 | dbaa125a0fe9 |
parent 14102 | 5d42204ac35e |
child 14116 | b2feb190e4bc |
permissions | -rw-r--r-- |
14090 | 1 |
use itertools::Itertools; |
14102 | 2 |
use std::cmp::min; |
14090 | 3 |
|
14099 | 4 |
use integral_geometry::{Line, Point, Rect, Size}; |
14090 | 5 |
use land2d::Land2D; |
6 |
||
7 |
use outline_template::OutlineTemplate; |
|
8 |
||
9 |
pub struct OutlinePoints { |
|
10 |
pub islands: Vec<Vec<Point>>, |
|
11 |
pub fill_points: Vec<Point>, |
|
12 |
pub size: Size, |
|
14099 | 13 |
pub play_box: Rect, |
14090 | 14 |
} |
15 |
||
16 |
impl OutlinePoints { |
|
17 |
pub fn from_outline_template<I: Iterator<Item = u32>>( |
|
18 |
outline_template: &OutlineTemplate, |
|
14099 | 19 |
play_box: Rect, |
20 |
size: Size, |
|
14090 | 21 |
random_numbers: &mut I, |
22 |
) -> Self { |
|
23 |
Self { |
|
14099 | 24 |
play_box, |
25 |
size, |
|
14090 | 26 |
islands: outline_template |
27 |
.islands |
|
28 |
.iter() |
|
29 |
.map(|i| { |
|
30 |
i.iter() |
|
31 |
.zip(random_numbers.tuples()) |
|
32 |
.map(|(rect, (rnd_a, rnd_b))| { |
|
14099 | 33 |
rect.top_left() |
34 |
+ Point::new( |
|
35 |
(rnd_a % rect.width) as i32, |
|
36 |
(rnd_b % rect.height) as i32, |
|
37 |
) |
|
38 |
+ play_box.top_left() |
|
14090 | 39 |
}).collect() |
40 |
}).collect(), |
|
41 |
fill_points: outline_template.fill_points.clone(), |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
pub fn total_len(&self) -> usize { |
|
46 |
self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len() |
|
47 |
} |
|
48 |
||
49 |
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
|
50 |
self.islands |
|
51 |
.iter_mut() |
|
52 |
.flat_map(|i| i.iter_mut()) |
|
53 |
.chain(self.fill_points.iter_mut()) |
|
54 |
} |
|
55 |
||
56 |
fn divide_edge<I: Iterator<Item = u32>>( |
|
57 |
&self, |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
58 |
segment: Line, |
14090 | 59 |
random_numbers: &mut I, |
60 |
) -> Option<Point> { |
|
14102 | 61 |
let min_distance = 40; |
62 |
// new point should fall inside this box |
|
63 |
let map_box = self.play_box.with_margin(min_distance); |
|
64 |
||
65 |
let p = Point::new( |
|
66 |
segment.end.y - segment.start.y, |
|
67 |
segment.start.x - segment.start.y, |
|
68 |
); |
|
69 |
let mid_point = segment.center(); |
|
70 |
||
71 |
if (p.integral_norm() < min_distance as u32 * 3) || !map_box.contains_inside(p) { |
|
72 |
return None; |
|
73 |
} |
|
74 |
||
75 |
let full_box = Rect::from_size(Point::zero(), self.size).with_margin(min_distance); |
|
76 |
||
77 |
let mut dist_left = (self.size.width + self.size.height) as u32; |
|
78 |
let mut dist_right = dist_left; |
|
79 |
||
80 |
// find distances to map borders |
|
81 |
if p.x != 0 { |
|
82 |
// check against left border |
|
83 |
let iyl = (map_box.left() - mid_point.x) * p.y / p.x + mid_point.y; |
|
84 |
let dl = Point::new(mid_point.x - map_box.left(), mid_point.y - iyl).integral_norm(); |
|
85 |
let t = p.x * (mid_point.x - full_box.left()) + p.y * (mid_point.y - iyl); |
|
86 |
||
87 |
if t > 0 { |
|
88 |
dist_left = dl; |
|
89 |
} else { |
|
90 |
dist_right = dl; |
|
91 |
} |
|
92 |
||
93 |
// right border |
|
94 |
let iyr = (map_box.right() - mid_point.x) * p.y / p.x + mid_point.y; |
|
95 |
let dr = Point::new(mid_point.x - full_box.right(), mid_point.y - iyr).integral_norm(); |
|
96 |
||
97 |
if t > 0 { |
|
98 |
dist_right = dr; |
|
99 |
} else { |
|
100 |
dist_left = dr; |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
if p.y != 0 { |
|
105 |
// top border |
|
106 |
let ixl = (map_box.top() - mid_point.y) * p.x / p.y + mid_point.x; |
|
107 |
let dl = Point::new(mid_point.y - map_box.top(), mid_point.x - ixl).integral_norm(); |
|
108 |
let t = p.y * (mid_point.y - full_box.top()) + p.x * (mid_point.x - ixl); |
|
109 |
||
110 |
if t > 0 { |
|
111 |
dist_left = min(dist_left, dl); |
|
112 |
} else { |
|
113 |
dist_right = min(dist_right, dl); |
|
114 |
} |
|
115 |
||
116 |
// bottom border |
|
117 |
let ixr = (map_box.bottom() - mid_point.y) * p.x / p.y + mid_point.x; |
|
118 |
let dr = Point::new(mid_point.y - full_box.bottom(), mid_point.x - ixr).integral_norm(); |
|
119 |
||
120 |
if t > 0 { |
|
121 |
dist_right = min(dist_right, dr); |
|
122 |
} else { |
|
123 |
dist_left = min(dist_left, dr); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
// now go through all other segments |
|
128 |
||
14090 | 129 |
None |
130 |
} |
|
131 |
||
132 |
fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
|
133 |
for is in 0..self.islands.len() { |
|
134 |
let mut i = 0; |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
135 |
let mut segment; |
14090 | 136 |
|
137 |
loop { |
|
138 |
{ |
|
139 |
let island = &self.islands[is]; |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
140 |
let mut end_point; |
14090 | 141 |
if i < island.len() { |
142 |
end_point = if i + 1 < island.len() { |
|
143 |
island[i + 1] |
|
144 |
} else { |
|
145 |
island[0] |
|
146 |
}; |
|
147 |
} else { |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
148 |
break; |
14090 | 149 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
150 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
151 |
segment = Line::new(island[i], end_point); |
14090 | 152 |
} |
153 |
||
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
154 |
if let Some(new_point) = self.divide_edge(segment, random_numbers) { |
14090 | 155 |
self.islands[is].insert(i + 1, new_point); |
156 |
i += 2; |
|
157 |
} else { |
|
158 |
i += 1; |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
pub fn bezierize(&mut self) { |
|
165 |
unimplemented!() |
|
166 |
} |
|
167 |
||
168 |
pub fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
|
169 |
loop { |
|
170 |
let old_len = self.total_len(); |
|
171 |
self.divide_edges(random_numbers); |
|
172 |
||
14114 | 173 |
if self.total_len() == old_len { |
14090 | 174 |
break; |
175 |
} |
|
176 |
} |
|
177 |
||
178 |
self.bezierize(); |
|
179 |
} |
|
180 |
||
181 |
pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) { |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
182 |
for segment in self.segments_iter() { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
183 |
land.draw_line(segment, value); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
184 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
185 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
186 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
187 |
fn segments_iter(&self) -> OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
188 |
OutlineSegmentsIterator { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
189 |
outline: self, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
190 |
island: 0, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
191 |
index: 0, |
14090 | 192 |
} |
193 |
} |
|
194 |
} |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
195 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
196 |
struct OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
197 |
outline: &'a OutlinePoints, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
198 |
island: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
199 |
index: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
200 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
201 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
202 |
impl<'a> Iterator for OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
203 |
type Item = Line; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
204 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
205 |
fn next(&mut self) -> Option<Self::Item> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
206 |
if self.island < self.outline.islands.len() { |
14099 | 207 |
if self.index + 1 < self.outline.islands[self.island].len() { |
208 |
let result = Some(Line::new( |
|
209 |
self.outline.islands[self.island][self.index], |
|
210 |
self.outline.islands[self.island][self.index + 1], |
|
211 |
)); |
|
212 |
||
213 |
self.index += 1; |
|
214 |
||
215 |
result |
|
216 |
} else if self.index + 1 == self.outline.islands[self.island].len() { |
|
217 |
let result = Some(Line::new( |
|
218 |
self.outline.islands[self.island][self.index], |
|
219 |
self.outline.islands[self.island][0], |
|
220 |
)); |
|
221 |
||
222 |
self.island += 1; |
|
223 |
self.index = 0; |
|
224 |
||
225 |
result |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
226 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
227 |
self.island += 1; |
14099 | 228 |
self.index = 0; |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
229 |
self.next() |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
230 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
231 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
232 |
None |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
233 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
234 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
235 |
} |
14099 | 236 |
|
237 |
#[test()] |
|
238 |
fn points_test() { |
|
239 |
let mut points = OutlinePoints { |
|
240 |
islands: vec![ |
|
241 |
vec![Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)], |
|
242 |
vec![Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)], |
|
243 |
], |
|
244 |
fill_points: vec![Point::new(1, 1)], |
|
245 |
play_box: Rect::from_box(0, 100, 0, 100).with_margin(10), |
|
246 |
size: Size::square(100), |
|
247 |
}; |
|
248 |
||
249 |
let segments: Vec<Line> = points.segments_iter().collect(); |
|
250 |
assert_eq!( |
|
251 |
segments.first(), |
|
252 |
Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) |
|
253 |
); |
|
254 |
assert_eq!( |
|
255 |
segments.last(), |
|
256 |
Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) |
|
257 |
); |
|
258 |
||
259 |
points.iter_mut().for_each(|p| p.x = 2); |
|
260 |
assert_eq!(points.fill_points[0].x, 2); |
|
261 |
assert_eq!(points.islands[0][0].x, 2); |
|
262 |
} |