author | alfadur |
Fri, 02 Nov 2018 22:50:45 +0300 | |
changeset 14118 | 400864ecb954 |
parent 14116 | b2feb190e4bc |
child 14121 | 4d22be35cfa2 |
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 |
} |
|
14116 | 194 |
|
195 |
pub fn mirror(&mut self) { |
|
14118 | 196 |
let width = self.size.width as i32; |
14116 | 197 |
self.iter_mut() |
14118 | 198 |
.for_each(|p| p.x = width - 1 - p.x); |
14116 | 199 |
} |
200 |
||
201 |
pub fn flip(&mut self) { |
|
14118 | 202 |
let height = self.size.height as i32; |
203 |
self.iter_mut() |
|
204 |
.for_each(|p| p.y = height - 1 - p.y); |
|
14116 | 205 |
} |
14090 | 206 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
207 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
208 |
struct OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
209 |
outline: &'a OutlinePoints, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
210 |
island: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
211 |
index: usize, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
212 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
213 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
214 |
impl<'a> Iterator for OutlineSegmentsIterator<'a> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
215 |
type Item = Line; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
216 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
217 |
fn next(&mut self) -> Option<Self::Item> { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
218 |
if self.island < self.outline.islands.len() { |
14099 | 219 |
if self.index + 1 < self.outline.islands[self.island].len() { |
220 |
let result = Some(Line::new( |
|
221 |
self.outline.islands[self.island][self.index], |
|
222 |
self.outline.islands[self.island][self.index + 1], |
|
223 |
)); |
|
224 |
||
225 |
self.index += 1; |
|
226 |
||
227 |
result |
|
228 |
} else if self.index + 1 == self.outline.islands[self.island].len() { |
|
229 |
let result = Some(Line::new( |
|
230 |
self.outline.islands[self.island][self.index], |
|
231 |
self.outline.islands[self.island][0], |
|
232 |
)); |
|
233 |
||
234 |
self.island += 1; |
|
235 |
self.index = 0; |
|
236 |
||
237 |
result |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
238 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
239 |
self.island += 1; |
14099 | 240 |
self.index = 0; |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
241 |
self.next() |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
242 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
243 |
} else { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
244 |
None |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
245 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
246 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
247 |
} |
14099 | 248 |
|
249 |
#[test()] |
|
250 |
fn points_test() { |
|
251 |
let mut points = OutlinePoints { |
|
252 |
islands: vec![ |
|
253 |
vec![Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)], |
|
254 |
vec![Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)], |
|
255 |
], |
|
256 |
fill_points: vec![Point::new(1, 1)], |
|
257 |
play_box: Rect::from_box(0, 100, 0, 100).with_margin(10), |
|
258 |
size: Size::square(100), |
|
259 |
}; |
|
260 |
||
261 |
let segments: Vec<Line> = points.segments_iter().collect(); |
|
262 |
assert_eq!( |
|
263 |
segments.first(), |
|
264 |
Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) |
|
265 |
); |
|
266 |
assert_eq!( |
|
267 |
segments.last(), |
|
268 |
Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) |
|
269 |
); |
|
270 |
||
271 |
points.iter_mut().for_each(|p| p.x = 2); |
|
272 |
assert_eq!(points.fill_points[0].x, 2); |
|
273 |
assert_eq!(points.islands[0][0].x, 2); |
|
274 |
} |