Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/polygon/polygon_set_data.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children | c530137014c0 |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
1 /* | |
2 Copyright 2008 Intel Corporation | |
3 | |
4 Use, modification and distribution are subject to the Boost Software License, | |
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
6 http://www.boost.org/LICENSE_1_0.txt). | |
7 */ | |
8 #ifndef BOOST_POLYGON_POLYGON_SET_DATA_HPP | |
9 #define BOOST_POLYGON_POLYGON_SET_DATA_HPP | |
10 #include "polygon_45_set_data.hpp" | |
11 #include "polygon_45_set_concept.hpp" | |
12 #include "polygon_traits.hpp" | |
13 #include "detail/polygon_arbitrary_formation.hpp" | |
14 | |
15 namespace boost { namespace polygon { | |
16 | |
17 | |
18 // utility function to round coordinate types down | |
19 // rounds down for both negative and positive numbers | |
20 // intended really for integer type T (does not make sense for float) | |
21 template <typename T> | |
22 static inline T round_down(double val) { | |
23 T rounded_val = (T)(val); | |
24 if(val < (double)rounded_val) | |
25 --rounded_val; | |
26 return rounded_val; | |
27 } | |
28 template <typename T> | |
29 static inline point_data<T> round_down(point_data<double> v) { | |
30 return point_data<T>(round_down<T>(v.x()),round_down<T>(v.y())); | |
31 } | |
32 | |
33 | |
34 | |
35 //foward declare view | |
36 template <typename ltype, typename rtype, int op_type> class polygon_set_view; | |
37 | |
38 template <typename T> | |
39 class polygon_set_data { | |
40 public: | |
41 typedef T coordinate_type; | |
42 typedef point_data<T> point_type; | |
43 typedef std::pair<point_type, point_type> edge_type; | |
44 typedef std::pair<edge_type, int> element_type; | |
45 typedef std::vector<element_type> value_type; | |
46 typedef typename value_type::const_iterator iterator_type; | |
47 typedef polygon_set_data operator_arg_type; | |
48 | |
49 // default constructor | |
50 inline polygon_set_data() : data_(), dirty_(false), unsorted_(false), is_45_(true) {} | |
51 | |
52 // constructor from an iterator pair over edge data | |
53 template <typename iT> | |
54 inline polygon_set_data(iT input_begin, iT input_end) : data_(), dirty_(false), unsorted_(false), is_45_(true) { | |
55 for( ; input_begin != input_end; ++input_begin) { insert(*input_begin); } | |
56 } | |
57 | |
58 // copy constructor | |
59 inline polygon_set_data(const polygon_set_data& that) : | |
60 data_(that.data_), dirty_(that.dirty_), unsorted_(that.unsorted_), is_45_(that.is_45_) {} | |
61 | |
62 // copy constructor | |
63 template <typename ltype, typename rtype, int op_type> | |
64 inline polygon_set_data(const polygon_set_view<ltype, rtype, op_type>& that); | |
65 | |
66 // destructor | |
67 inline ~polygon_set_data() {} | |
68 | |
69 // assignement operator | |
70 inline polygon_set_data& operator=(const polygon_set_data& that) { | |
71 if(this == &that) return *this; | |
72 data_ = that.data_; | |
73 dirty_ = that.dirty_; | |
74 unsorted_ = that.unsorted_; | |
75 is_45_ = that.is_45_; | |
76 return *this; | |
77 } | |
78 | |
79 template <typename ltype, typename rtype, int op_type> | |
80 inline polygon_set_data& operator=(const polygon_set_view<ltype, rtype, op_type>& geometry) { | |
81 (*this) = geometry.value(); | |
82 dirty_ = false; | |
83 unsorted_ = false; | |
84 return *this; | |
85 } | |
86 | |
87 template <typename geometry_object> | |
88 inline polygon_set_data& operator=(const geometry_object& geometry) { | |
89 data_.clear(); | |
90 insert(geometry); | |
91 return *this; | |
92 } | |
93 | |
94 | |
95 // insert iterator range | |
96 inline void insert(iterator_type input_begin, iterator_type input_end, bool is_hole = false) { | |
97 if(input_begin == input_end || (!data_.empty() && &(*input_begin) == &(*(data_.begin())))) return; | |
98 dirty_ = true; | |
99 unsorted_ = true; | |
100 while(input_begin != input_end) { | |
101 insert(*input_begin, is_hole); | |
102 ++input_begin; | |
103 } | |
104 } | |
105 | |
106 // insert iterator range | |
107 template <typename iT> | |
108 inline void insert(iT input_begin, iT input_end, bool is_hole = false) { | |
109 if(input_begin == input_end) return; | |
110 for(; input_begin != input_end; ++input_begin) { | |
111 insert(*input_begin, is_hole); | |
112 } | |
113 } | |
114 | |
115 template <typename geometry_type> | |
116 inline void insert(const geometry_type& geometry_object, bool is_hole = false) { | |
117 insert(geometry_object, is_hole, typename geometry_concept<geometry_type>::type()); | |
118 } | |
119 | |
120 template <typename polygon_type> | |
121 inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_concept ) { | |
122 insert_vertex_sequence(begin_points(polygon_object), end_points(polygon_object), winding(polygon_object), is_hole); | |
123 } | |
124 | |
125 inline void insert(const polygon_set_data& ps, bool is_hole = false) { | |
126 insert(ps.data_.begin(), ps.data_.end(), is_hole); | |
127 } | |
128 | |
129 template <typename polygon_45_set_type> | |
130 inline void insert(const polygon_45_set_type& ps, bool is_hole, polygon_45_set_concept) { | |
131 std::vector<polygon_45_with_holes_data<typename polygon_45_set_traits<polygon_45_set_type>::coordinate_type> > polys; | |
132 assign(polys, ps); | |
133 insert(polys.begin(), polys.end(), is_hole); | |
134 } | |
135 | |
136 template <typename polygon_90_set_type> | |
137 inline void insert(const polygon_90_set_type& ps, bool is_hole, polygon_90_set_concept) { | |
138 std::vector<polygon_90_with_holes_data<typename polygon_90_set_traits<polygon_90_set_type>::coordinate_type> > polys; | |
139 assign(polys, ps); | |
140 insert(polys.begin(), polys.end(), is_hole); | |
141 } | |
142 | |
143 template <typename polygon_type> | |
144 inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_45_concept ) { | |
145 insert(polygon_object, is_hole, polygon_concept()); } | |
146 | |
147 template <typename polygon_type> | |
148 inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_90_concept ) { | |
149 insert(polygon_object, is_hole, polygon_concept()); } | |
150 | |
151 template <typename polygon_with_holes_type> | |
152 inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole, | |
153 polygon_with_holes_concept ) { | |
154 insert(polygon_with_holes_object, is_hole, polygon_concept()); | |
155 for(typename polygon_with_holes_traits<polygon_with_holes_type>::iterator_holes_type itr = | |
156 begin_holes(polygon_with_holes_object); | |
157 itr != end_holes(polygon_with_holes_object); ++itr) { | |
158 insert(*itr, !is_hole, polygon_concept()); | |
159 } | |
160 } | |
161 | |
162 template <typename polygon_with_holes_type> | |
163 inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole, | |
164 polygon_45_with_holes_concept ) { | |
165 insert(polygon_with_holes_object, is_hole, polygon_with_holes_concept()); } | |
166 | |
167 template <typename polygon_with_holes_type> | |
168 inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole, | |
169 polygon_90_with_holes_concept ) { | |
170 insert(polygon_with_holes_object, is_hole, polygon_with_holes_concept()); } | |
171 | |
172 template <typename rectangle_type> | |
173 inline void insert(const rectangle_type& rectangle_object, bool is_hole, rectangle_concept ) { | |
174 polygon_90_data<coordinate_type> poly; | |
175 assign(poly, rectangle_object); | |
176 insert(poly, is_hole, polygon_concept()); | |
177 } | |
178 | |
179 inline void insert_clean(const element_type& edge, bool is_hole = false) { | |
180 if( ! scanline_base<coordinate_type>::is_45_degree(edge.first) && | |
181 ! scanline_base<coordinate_type>::is_horizontal(edge.first) && | |
182 ! scanline_base<coordinate_type>::is_vertical(edge.first) ) is_45_ = false; | |
183 data_.push_back(edge); | |
184 if(data_.back().first.second < data_.back().first.first) { | |
185 std::swap(data_.back().first.second, data_.back().first.first); | |
186 data_.back().second *= -1; | |
187 } | |
188 if(is_hole) | |
189 data_.back().second *= -1; | |
190 } | |
191 | |
192 inline void insert(const element_type& edge, bool is_hole = false) { | |
193 insert_clean(edge, is_hole); | |
194 dirty_ = true; | |
195 unsorted_ = true; | |
196 } | |
197 | |
198 template <class iT> | |
199 inline void insert_vertex_sequence(iT begin_vertex, iT end_vertex, direction_1d winding, bool is_hole) { | |
200 bool first_iteration = true; | |
201 point_type first_point; | |
202 point_type previous_point; | |
203 point_type current_point; | |
204 direction_1d winding_dir = winding; | |
205 int multiplier = winding_dir == COUNTERCLOCKWISE ? 1 : -1; | |
206 if(is_hole) multiplier *= -1; | |
207 for( ; begin_vertex != end_vertex; ++begin_vertex) { | |
208 assign(current_point, *begin_vertex); | |
209 if(first_iteration) { | |
210 first_iteration = false; | |
211 first_point = previous_point = current_point; | |
212 } else { | |
213 if(previous_point != current_point) { | |
214 element_type elem(edge_type(previous_point, current_point), | |
215 ( previous_point.get(HORIZONTAL) == current_point.get(HORIZONTAL) ? -1 : 1) * multiplier); | |
216 insert_clean(elem); | |
217 } | |
218 } | |
219 previous_point = current_point; | |
220 } | |
221 current_point = first_point; | |
222 if(!first_iteration) { | |
223 if(previous_point != current_point) { | |
224 element_type elem(edge_type(previous_point, current_point), | |
225 ( previous_point.get(HORIZONTAL) == current_point.get(HORIZONTAL) ? -1 : 1) * multiplier); | |
226 insert_clean(elem); | |
227 } | |
228 dirty_ = true; | |
229 unsorted_ = true; | |
230 } | |
231 } | |
232 | |
233 template <typename output_container> | |
234 inline void get(output_container& output) const { | |
235 get_dispatch(output, typename geometry_concept<typename output_container::value_type>::type()); | |
236 } | |
237 | |
238 // append to the container cT with polygons of three or four verticies | |
239 // slicing orientation is vertical | |
240 template <class cT> | |
241 void get_trapezoids(cT& container) const { | |
242 clean(); | |
243 trapezoid_arbitrary_formation<coordinate_type> pf; | |
244 typedef typename polygon_arbitrary_formation<coordinate_type>::vertex_half_edge vertex_half_edge; | |
245 std::vector<vertex_half_edge> data; | |
246 for(iterator_type itr = data_.begin(); itr != data_.end(); ++itr){ | |
247 data.push_back(vertex_half_edge((*itr).first.first, (*itr).first.second, (*itr).second)); | |
248 data.push_back(vertex_half_edge((*itr).first.second, (*itr).first.first, -1 * (*itr).second)); | |
249 } | |
250 polygon_sort(data.begin(), data.end()); | |
251 pf.scan(container, data.begin(), data.end()); | |
252 //std::cout << "DONE FORMING POLYGONS\n"; | |
253 } | |
254 | |
255 // append to the container cT with polygons of three or four verticies | |
256 template <class cT> | |
257 void get_trapezoids(cT& container, orientation_2d slicing_orientation) const { | |
258 if(slicing_orientation == VERTICAL) { | |
259 get_trapezoids(container); | |
260 } else { | |
261 polygon_set_data<T> ps(*this); | |
262 ps.transform(axis_transformation(axis_transformation::SWAP_XY)); | |
263 cT result; | |
264 ps.get_trapezoids(result); | |
265 for(typename cT::iterator itr = result.begin(); itr != result.end(); ++itr) { | |
266 ::boost::polygon::transform(*itr, axis_transformation(axis_transformation::SWAP_XY)); | |
267 } | |
268 container.insert(container.end(), result.begin(), result.end()); | |
269 } | |
270 } | |
271 | |
272 // equivalence operator | |
273 inline bool operator==(const polygon_set_data& p) const; | |
274 | |
275 // inequivalence operator | |
276 inline bool operator!=(const polygon_set_data& p) const { | |
277 return !((*this) == p); | |
278 } | |
279 | |
280 // get iterator to begin vertex data | |
281 inline iterator_type begin() const { | |
282 return data_.begin(); | |
283 } | |
284 | |
285 // get iterator to end vertex data | |
286 inline iterator_type end() const { | |
287 return data_.end(); | |
288 } | |
289 | |
290 const value_type& value() const { | |
291 return data_; | |
292 } | |
293 | |
294 // clear the contents of the polygon_set_data | |
295 inline void clear() { data_.clear(); dirty_ = unsorted_ = false; } | |
296 | |
297 // find out if Polygon set is empty | |
298 inline bool empty() const { return data_.empty(); } | |
299 | |
300 // get the Polygon set size in vertices | |
301 inline std::size_t size() const { clean(); return data_.size(); } | |
302 | |
303 // get the current Polygon set capacity in vertices | |
304 inline std::size_t capacity() const { return data_.capacity(); } | |
305 | |
306 // reserve size of polygon set in vertices | |
307 inline void reserve(std::size_t size) { return data_.reserve(size); } | |
308 | |
309 // find out if Polygon set is sorted | |
310 inline bool sorted() const { return !unsorted_; } | |
311 | |
312 // find out if Polygon set is clean | |
313 inline bool dirty() const { return dirty_; } | |
314 | |
315 void clean() const; | |
316 | |
317 void sort() const{ | |
318 if(unsorted_) { | |
319 polygon_sort(data_.begin(), data_.end()); | |
320 unsorted_ = false; | |
321 } | |
322 } | |
323 | |
324 template <typename input_iterator_type> | |
325 void set(input_iterator_type input_begin, input_iterator_type input_end) { | |
326 clear(); | |
327 reserve(std::distance(input_begin,input_end)); | |
328 insert(input_begin, input_end); | |
329 dirty_ = true; | |
330 unsorted_ = true; | |
331 } | |
332 | |
333 void set(const value_type& value) { | |
334 data_ = value; | |
335 dirty_ = true; | |
336 unsorted_ = true; | |
337 } | |
338 | |
339 template <typename rectangle_type> | |
340 bool extents(rectangle_type& rect) { | |
341 clean(); | |
342 if(empty()) return false; | |
343 bool first_iteration = true; | |
344 for(iterator_type itr = begin(); | |
345 itr != end(); ++itr) { | |
346 rectangle_type edge_box; | |
347 set_points(edge_box, (*itr).first.first, (*itr).first.second); | |
348 if(first_iteration) | |
349 rect = edge_box; | |
350 else | |
351 encompass(rect, edge_box); | |
352 first_iteration = false; | |
353 } | |
354 return true; | |
355 } | |
356 | |
357 inline polygon_set_data& | |
358 resize(coordinate_type resizing, bool corner_fill_arc = false, unsigned int num_circle_segments=0); | |
359 | |
360 template <typename transform_type> | |
361 inline polygon_set_data& | |
362 transform(const transform_type& tr) { | |
363 std::vector<polygon_with_holes_data<T> > polys; | |
364 get(polys); | |
365 clear(); | |
366 for(std::size_t i = 0 ; i < polys.size(); ++i) { | |
367 ::boost::polygon::transform(polys[i], tr); | |
368 insert(polys[i]); | |
369 } | |
370 unsorted_ = true; | |
371 dirty_ = true; | |
372 return *this; | |
373 } | |
374 | |
375 inline polygon_set_data& | |
376 scale_up(typename coordinate_traits<coordinate_type>::unsigned_area_type factor) { | |
377 for(typename value_type::iterator itr = data_.begin(); itr != data_.end(); ++itr) { | |
378 ::boost::polygon::scale_up((*itr).first.first, factor); | |
379 ::boost::polygon::scale_up((*itr).first.second, factor); | |
380 } | |
381 return *this; | |
382 } | |
383 | |
384 inline polygon_set_data& | |
385 scale_down(typename coordinate_traits<coordinate_type>::unsigned_area_type factor) { | |
386 for(typename value_type::iterator itr = data_.begin(); itr != data_.end(); ++itr) { | |
387 bool vb = (*itr).first.first.x() == (*itr).first.second.x(); | |
388 ::boost::polygon::scale_down((*itr).first.first, factor); | |
389 ::boost::polygon::scale_down((*itr).first.second, factor); | |
390 bool va = (*itr).first.first.x() == (*itr).first.second.x(); | |
391 if(!vb && va) { | |
392 (*itr).second *= -1; | |
393 } | |
394 } | |
395 unsorted_ = true; | |
396 dirty_ = true; | |
397 return *this; | |
398 } | |
399 | |
400 template <typename scaling_type> | |
401 inline polygon_set_data& scale(polygon_set_data& polygon_set, | |
402 const scaling_type& scaling) { | |
403 for(typename value_type::iterator itr = begin(); itr != end(); ++itr) { | |
404 bool vb = (*itr).first.first.x() == (*itr).first.second.x(); | |
405 ::boost::polygon::scale((*itr).first.first, scaling); | |
406 ::boost::polygon::scale((*itr).first.second, scaling); | |
407 bool va = (*itr).first.first.x() == (*itr).first.second.x(); | |
408 if(!vb && va) { | |
409 (*itr).second *= -1; | |
410 } | |
411 } | |
412 unsorted_ = true; | |
413 dirty_ = true; | |
414 return *this; | |
415 } | |
416 | |
417 static inline void compute_offset_edge(point_data<long double>& pt1, point_data<long double>& pt2, | |
418 const point_data<long double>& prev_pt, | |
419 const point_data<long double>& current_pt, | |
420 long double distance, int multiplier) { | |
421 long double dx = current_pt.x() - prev_pt.x(); | |
422 long double dy = current_pt.y() - prev_pt.y(); | |
423 long double edge_length = std::sqrt(dx*dx + dy*dy); | |
424 long double dnx = dy; | |
425 long double dny = -dx; | |
426 dnx = dnx * (long double)distance / edge_length; | |
427 dny = dny * (long double)distance / edge_length; | |
428 pt1.x(prev_pt.x() + (long double)dnx * (long double)multiplier); | |
429 pt2.x(current_pt.x() + (long double)dnx * (long double)multiplier); | |
430 pt1.y(prev_pt.y() + (long double)dny * (long double)multiplier); | |
431 pt2.y(current_pt.y() + (long double)dny * (long double)multiplier); | |
432 } | |
433 | |
434 static inline void modify_pt(point_data<coordinate_type>& pt, const point_data<coordinate_type>& prev_pt, | |
435 const point_data<coordinate_type>& current_pt, const point_data<coordinate_type>& next_pt, | |
436 coordinate_type distance, coordinate_type multiplier) { | |
437 std::pair<point_data<long double>, point_data<long double> > he1, he2; | |
438 he1.first.x((long double)(prev_pt.x())); | |
439 he1.first.y((long double)(prev_pt.y())); | |
440 he1.second.x((long double)(current_pt.x())); | |
441 he1.second.y((long double)(current_pt.y())); | |
442 he2.first.x((long double)(current_pt.x())); | |
443 he2.first.y((long double)(current_pt.y())); | |
444 he2.second.x((long double)(next_pt.x())); | |
445 he2.second.y((long double)(next_pt.y())); | |
446 compute_offset_edge(he1.first, he1.second, prev_pt, current_pt, distance, multiplier); | |
447 compute_offset_edge(he2.first, he2.second, current_pt, next_pt, distance, multiplier); | |
448 typedef scanline_base<long double>::compute_intersection_pack pack; | |
449 point_data<long double> rpt; | |
450 point_data<long double> bisectorpt((he1.second.x()+he2.first.x())/2, | |
451 (he1.second.y()+he2.first.y())/2); | |
452 point_data<long double> orig_pt((long double)pt.x(), (long double)pt.y()); | |
453 if(euclidean_distance(bisectorpt, orig_pt) < distance/2) { | |
454 if(!pack::compute_lazy_intersection(rpt, he1, he2, true, false)) { | |
455 rpt = he1.second; //colinear offset edges use shared point | |
456 } | |
457 } else { | |
458 if(!pack::compute_lazy_intersection(rpt, he1, std::pair<point_data<long double>, point_data<long double> >(orig_pt, bisectorpt), true, false)) { | |
459 rpt = he1.second; //colinear offset edges use shared point | |
460 } | |
461 } | |
462 pt.x((coordinate_type)(std::floor(rpt.x()+0.5))); | |
463 pt.y((coordinate_type)(std::floor(rpt.y()+0.5))); | |
464 } | |
465 | |
466 static void resize_poly_up(std::vector<point_data<coordinate_type> >& poly, coordinate_type distance, coordinate_type multiplier) { | |
467 point_data<coordinate_type> first_pt = poly[0]; | |
468 point_data<coordinate_type> second_pt = poly[1]; | |
469 point_data<coordinate_type> prev_pt = poly[0]; | |
470 point_data<coordinate_type> current_pt = poly[1]; | |
471 for(std::size_t i = 2; i < poly.size()-1; ++i) { | |
472 point_data<coordinate_type> next_pt = poly[i]; | |
473 modify_pt(poly[i-1], prev_pt, current_pt, next_pt, distance, multiplier); | |
474 prev_pt = current_pt; | |
475 current_pt = next_pt; | |
476 } | |
477 point_data<coordinate_type> next_pt = first_pt; | |
478 modify_pt(poly[poly.size()-2], prev_pt, current_pt, next_pt, distance, multiplier); | |
479 prev_pt = current_pt; | |
480 current_pt = next_pt; | |
481 next_pt = second_pt; | |
482 modify_pt(poly[0], prev_pt, current_pt, next_pt, distance, multiplier); | |
483 poly.back() = poly.front(); | |
484 } | |
485 static bool resize_poly_down(std::vector<point_data<coordinate_type> >& poly, coordinate_type distance, coordinate_type multiplier) { | |
486 std::vector<point_data<coordinate_type> > orig_poly(poly); | |
487 rectangle_data<coordinate_type> extents_rectangle; | |
488 set_points(extents_rectangle, poly[0], poly[0]); | |
489 point_data<coordinate_type> first_pt = poly[0]; | |
490 point_data<coordinate_type> second_pt = poly[1]; | |
491 point_data<coordinate_type> prev_pt = poly[0]; | |
492 point_data<coordinate_type> current_pt = poly[1]; | |
493 encompass(extents_rectangle, current_pt); | |
494 for(std::size_t i = 2; i < poly.size()-1; ++i) { | |
495 point_data<coordinate_type> next_pt = poly[i]; | |
496 encompass(extents_rectangle, next_pt); | |
497 modify_pt(poly[i-1], prev_pt, current_pt, next_pt, distance, multiplier); | |
498 prev_pt = current_pt; | |
499 current_pt = next_pt; | |
500 } | |
501 if(delta(extents_rectangle, HORIZONTAL) <= std::abs(2*distance)) | |
502 return false; | |
503 if(delta(extents_rectangle, VERTICAL) <= std::abs(2*distance)) | |
504 return false; | |
505 point_data<coordinate_type> next_pt = first_pt; | |
506 modify_pt(poly[poly.size()-2], prev_pt, current_pt, next_pt, distance, multiplier); | |
507 prev_pt = current_pt; | |
508 current_pt = next_pt; | |
509 next_pt = second_pt; | |
510 modify_pt(poly[0], prev_pt, current_pt, next_pt, distance, multiplier); | |
511 poly.back() = poly.front(); | |
512 //if the line segments formed between orignial and new points cross for an edge that edge inverts | |
513 //if all edges invert the polygon should be discarded | |
514 //if even one edge does not invert return true because the polygon is valid | |
515 bool non_inverting_edge = false; | |
516 for(std::size_t i = 1; i < poly.size(); ++i) { | |
517 std::pair<point_data<coordinate_type>, point_data<coordinate_type> > | |
518 he1(poly[i], orig_poly[i]), | |
519 he2(poly[i-1], orig_poly[i-1]); | |
520 if(!scanline_base<coordinate_type>::intersects(he1, he2)) { | |
521 non_inverting_edge = true; | |
522 break; | |
523 } | |
524 } | |
525 return non_inverting_edge; | |
526 } | |
527 | |
528 polygon_set_data& | |
529 bloat(typename coordinate_traits<coordinate_type>::unsigned_area_type distance) { | |
530 std::list<polygon_with_holes_data<coordinate_type> > polys; | |
531 get(polys); | |
532 clear(); | |
533 for(typename std::list<polygon_with_holes_data<coordinate_type> >::iterator itr = polys.begin(); | |
534 itr != polys.end(); ++itr) { | |
535 resize_poly_up((*itr).self_.coords_, (coordinate_type)distance, (coordinate_type)1); | |
536 insert_vertex_sequence((*itr).self_.begin(), (*itr).self_.end(), COUNTERCLOCKWISE, false); //inserts without holes | |
537 for(typename std::list<polygon_data<coordinate_type> >::iterator itrh = (*itr).holes_.begin(); | |
538 itrh != (*itr).holes_.end(); ++itrh) { | |
539 if(resize_poly_down((*itrh).coords_, (coordinate_type)distance, (coordinate_type)1)) { | |
540 insert_vertex_sequence((*itrh).coords_.begin(), (*itrh).coords_.end(), CLOCKWISE, true); | |
541 } | |
542 } | |
543 } | |
544 return *this; | |
545 } | |
546 | |
547 polygon_set_data& | |
548 shrink(typename coordinate_traits<coordinate_type>::unsigned_area_type distance) { | |
549 std::list<polygon_with_holes_data<coordinate_type> > polys; | |
550 get(polys); | |
551 clear(); | |
552 for(typename std::list<polygon_with_holes_data<coordinate_type> >::iterator itr = polys.begin(); | |
553 itr != polys.end(); ++itr) { | |
554 if(resize_poly_down((*itr).self_.coords_, (coordinate_type)distance, (coordinate_type)-1)) { | |
555 insert_vertex_sequence((*itr).self_.begin(), (*itr).self_.end(), COUNTERCLOCKWISE, false); //inserts without holes | |
556 for(typename std::list<polygon_data<coordinate_type> >::iterator itrh = (*itr).holes_.begin(); | |
557 itrh != (*itr).holes_.end(); ++itrh) { | |
558 resize_poly_up((*itrh).coords_, (coordinate_type)distance, (coordinate_type)-1); | |
559 insert_vertex_sequence((*itrh).coords_.begin(), (*itrh).coords_.end(), CLOCKWISE, true); | |
560 } | |
561 } | |
562 } | |
563 return *this; | |
564 } | |
565 | |
566 // TODO:: should be private | |
567 template <typename geometry_type> | |
568 inline polygon_set_data& | |
569 insert_with_resize(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc=false, unsigned int num_circle_segments=0, bool hole = false) { | |
570 return insert_with_resize_dispatch(poly, resizing, corner_fill_arc, num_circle_segments, hole, typename geometry_concept<geometry_type>::type()); | |
571 } | |
572 | |
573 template <typename geometry_type> | |
574 inline polygon_set_data& | |
575 insert_with_resize_dispatch(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc, unsigned int num_circle_segments, bool hole, | |
576 polygon_with_holes_concept tag) { | |
577 insert_with_resize_dispatch(poly, resizing, corner_fill_arc, num_circle_segments, hole, polygon_concept()); | |
578 for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr = | |
579 begin_holes(poly); itr != end_holes(poly); | |
580 ++itr) { | |
581 insert_with_resize_dispatch(*itr, resizing, corner_fill_arc, num_circle_segments, !hole, polygon_concept()); | |
582 } | |
583 return *this; | |
584 } | |
585 | |
586 template <typename geometry_type> | |
587 inline polygon_set_data& | |
588 insert_with_resize_dispatch(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc, unsigned int num_circle_segments, bool hole, | |
589 polygon_concept tag) { | |
590 | |
591 if (resizing==0) | |
592 return *this; | |
593 | |
594 | |
595 // one dimensional used to store CCW/CW flag | |
596 //direction_1d wdir = winding(poly); | |
597 // LOW==CLOCKWISE just faster to type | |
598 // so > 0 is CCW | |
599 //int multiplier = wdir == LOW ? -1 : 1; | |
600 //std::cout<<" multiplier : "<<multiplier<<std::endl; | |
601 //if(hole) resizing *= -1; | |
602 direction_1d resize_wdir = resizing>0?COUNTERCLOCKWISE:CLOCKWISE; | |
603 | |
604 typedef typename polygon_data<T>::iterator_type piterator; | |
605 piterator first, second, third, end, real_end; | |
606 real_end = end_points(poly); | |
607 third = begin_points(poly); | |
608 first = third; | |
609 if(first == real_end) return *this; | |
610 ++third; | |
611 if(third == real_end) return *this; | |
612 second = end = third; | |
613 ++third; | |
614 if(third == real_end) return *this; | |
615 | |
616 // for 1st corner | |
617 std::vector<point_data<T> > first_pts; | |
618 std::vector<point_data<T> > all_pts; | |
619 direction_1d first_wdir = CLOCKWISE; | |
620 | |
621 // for all corners | |
622 polygon_set_data<T> sizingSet; | |
623 bool sizing_sign = resizing<0; | |
624 bool prev_concave = true; | |
625 point_data<T> prev_point; | |
626 //int iCtr=0; | |
627 | |
628 | |
629 //insert minkofski shapes on edges and corners | |
630 do { // REAL WORK IS HERE | |
631 | |
632 | |
633 //first, second and third point to points in correct CCW order | |
634 // check if convex or concave case | |
635 point_data<coordinate_type> normal1( second->y()-first->y(), first->x()-second->x()); | |
636 point_data<coordinate_type> normal2( third->y()-second->y(), second->x()-third->x()); | |
637 double direction = normal1.x()*normal2.y()- normal2.x()*normal1.y(); | |
638 bool convex = direction>0; | |
639 | |
640 bool treat_as_concave = !convex; | |
641 if(sizing_sign) | |
642 treat_as_concave = convex; | |
643 point_data<double> v; | |
644 assign(v, normal1); | |
645 double s2 = (v.x()*v.x()+v.y()*v.y()); | |
646 double s = std::sqrt(s2)/resizing; | |
647 v = point_data<double>(v.x()/s,v.y()/s); | |
648 point_data<T> curr_prev; | |
649 if (prev_concave) | |
650 //TODO missing round_down() | |
651 curr_prev = point_data<T>(first->x()+v.x(),first->y()+v.y()); | |
652 else | |
653 curr_prev = prev_point; | |
654 | |
655 // around concave corners - insert rectangle | |
656 // if previous corner is concave it's point info may be ignored | |
657 if ( treat_as_concave) { | |
658 std::vector<point_data<T> > pts; | |
659 | |
660 pts.push_back(point_data<T>(second->x()+v.x(),second->y()+v.y())); | |
661 pts.push_back(*second); | |
662 pts.push_back(*first); | |
663 pts.push_back(point_data<T>(curr_prev)); | |
664 if (first_pts.size()){ | |
665 sizingSet.insert_vertex_sequence(pts.begin(),pts.end(), resize_wdir,false); | |
666 }else { | |
667 first_pts=pts; | |
668 first_wdir = resize_wdir; | |
669 } | |
670 } else { | |
671 | |
672 // add either intersection_quad or pie_shape, based on corner_fill_arc option | |
673 // for convex corner (convexity depends on sign of resizing, whether we shrink or grow) | |
674 std::vector< std::vector<point_data<T> > > pts; | |
675 direction_1d winding; | |
676 winding = convex?COUNTERCLOCKWISE:CLOCKWISE; | |
677 if (make_resizing_vertex_list(pts, curr_prev, prev_concave, *first, *second, *third, resizing | |
678 , num_circle_segments, corner_fill_arc)) | |
679 { | |
680 if (first_pts.size()) { | |
681 for (int i=0; i<pts.size(); i++) { | |
682 sizingSet.insert_vertex_sequence(pts[i].begin(),pts[i].end(),winding,false); | |
683 } | |
684 | |
685 } else { | |
686 first_pts = pts[0]; | |
687 first_wdir = resize_wdir; | |
688 for (int i=1; i<pts.size(); i++) { | |
689 sizingSet.insert_vertex_sequence(pts[i].begin(),pts[i].end(),winding,false); | |
690 } | |
691 } | |
692 prev_point = curr_prev; | |
693 | |
694 } else { | |
695 treat_as_concave = true; | |
696 } | |
697 } | |
698 | |
699 prev_concave = treat_as_concave; | |
700 first = second; | |
701 second = third; | |
702 ++third; | |
703 if(third == real_end) { | |
704 third = begin_points(poly); | |
705 if(*second == *third) { | |
706 ++third; //skip first point if it is duplicate of last point | |
707 } | |
708 } | |
709 } while(second != end); | |
710 | |
711 // handle insertion of first point | |
712 if (!prev_concave) { | |
713 first_pts[first_pts.size()-1]=prev_point; | |
714 } | |
715 sizingSet.insert_vertex_sequence(first_pts.begin(),first_pts.end(),first_wdir,false); | |
716 | |
717 polygon_set_data<coordinate_type> tmp; | |
718 | |
719 //insert original shape | |
720 tmp.insert(poly, false, polygon_concept()); | |
721 if((resizing < 0) ^ hole) tmp -= sizingSet; | |
722 else tmp += sizingSet; | |
723 //tmp.clean(); | |
724 insert(tmp, hole); | |
725 return (*this); | |
726 } | |
727 | |
728 | |
729 inline polygon_set_data& | |
730 interact(const polygon_set_data& that); | |
731 | |
732 inline bool downcast(polygon_45_set_data<coordinate_type>& result) const { | |
733 if(!is_45_) return false; | |
734 for(iterator_type itr = begin(); itr != end(); ++itr) { | |
735 const element_type& elem = *itr; | |
736 int count = elem.second; | |
737 int rise = 1; //up sloping 45 | |
738 if(scanline_base<coordinate_type>::is_horizontal(elem.first)) rise = 0; | |
739 else if(scanline_base<coordinate_type>::is_vertical(elem.first)) rise = 2; | |
740 else { | |
741 if(!scanline_base<coordinate_type>::is_45_degree(elem.first)) { | |
742 is_45_ = false; | |
743 return false; //consider throwing because is_45_ has be be wrong | |
744 } | |
745 if(elem.first.first.y() > elem.first.second.y()) rise = -1; //down sloping 45 | |
746 } | |
747 typename polygon_45_set_data<coordinate_type>::Vertex45Compact vertex(elem.first.first, rise, count); | |
748 result.insert(vertex); | |
749 typename polygon_45_set_data<coordinate_type>::Vertex45Compact vertex2(elem.first.second, rise, -count); | |
750 result.insert(vertex2); | |
751 } | |
752 return true; | |
753 } | |
754 | |
755 inline GEOMETRY_CONCEPT_ID concept_downcast() const { | |
756 typedef typename coordinate_traits<coordinate_type>::coordinate_difference delta_type; | |
757 bool is_45 = false; | |
758 for(iterator_type itr = begin(); itr != end(); ++itr) { | |
759 const element_type& elem = *itr; | |
760 delta_type h_delta = euclidean_distance(elem.first.first, elem.first.second, HORIZONTAL); | |
761 delta_type v_delta = euclidean_distance(elem.first.first, elem.first.second, VERTICAL); | |
762 if(h_delta != 0 || v_delta != 0) { | |
763 //neither delta is zero and the edge is not MANHATTAN | |
764 if(v_delta != h_delta && v_delta != -h_delta) return POLYGON_SET_CONCEPT; | |
765 else is_45 = true; | |
766 } | |
767 } | |
768 if(is_45) return POLYGON_45_SET_CONCEPT; | |
769 return POLYGON_90_SET_CONCEPT; | |
770 } | |
771 | |
772 private: | |
773 mutable value_type data_; | |
774 mutable bool dirty_; | |
775 mutable bool unsorted_; | |
776 mutable bool is_45_; | |
777 | |
778 private: | |
779 //functions | |
780 | |
781 template <typename output_container> | |
782 void get_dispatch(output_container& output, polygon_concept tag) const { | |
783 get_fracture(output, true, tag); | |
784 } | |
785 template <typename output_container> | |
786 void get_dispatch(output_container& output, polygon_with_holes_concept tag) const { | |
787 get_fracture(output, false, tag); | |
788 } | |
789 template <typename output_container, typename concept_type> | |
790 void get_fracture(output_container& container, bool fracture_holes, concept_type ) const { | |
791 clean(); | |
792 polygon_arbitrary_formation<coordinate_type> pf(fracture_holes); | |
793 typedef typename polygon_arbitrary_formation<coordinate_type>::vertex_half_edge vertex_half_edge; | |
794 std::vector<vertex_half_edge> data; | |
795 for(iterator_type itr = data_.begin(); itr != data_.end(); ++itr){ | |
796 data.push_back(vertex_half_edge((*itr).first.first, (*itr).first.second, (*itr).second)); | |
797 data.push_back(vertex_half_edge((*itr).first.second, (*itr).first.first, -1 * (*itr).second)); | |
798 } | |
799 polygon_sort(data.begin(), data.end()); | |
800 pf.scan(container, data.begin(), data.end()); | |
801 } | |
802 }; | |
803 | |
804 struct polygon_set_concept; | |
805 template <typename T> | |
806 struct geometry_concept<polygon_set_data<T> > { | |
807 typedef polygon_set_concept type; | |
808 }; | |
809 | |
810 // template <typename T> | |
811 // inline double compute_area(point_data<T>& a, point_data<T>& b, point_data<T>& c) { | |
812 | |
813 // return (double)(b.x()-a.x())*(double)(c.y()-a.y())- (double)(c.x()-a.x())*(double)(b.y()-a.y()); | |
814 | |
815 | |
816 // } | |
817 | |
818 template <typename T> | |
819 inline int make_resizing_vertex_list(std::vector<std::vector<point_data< T> > >& return_points, | |
820 point_data<T>& curr_prev, bool ignore_prev_point, | |
821 point_data< T> start, point_data<T> middle, point_data< T> end, | |
822 double sizing_distance, unsigned int num_circle_segments, bool corner_fill_arc) { | |
823 | |
824 // handle the case of adding an intersection point | |
825 point_data<double> dn1( middle.y()-start.y(), start.x()-middle.x()); | |
826 double size = sizing_distance/std::sqrt( dn1.x()*dn1.x()+dn1.y()*dn1.y()); | |
827 dn1 = point_data<double>( dn1.x()*size, dn1.y()* size); | |
828 point_data<double> dn2( end.y()-middle.y(), middle.x()-end.x()); | |
829 size = sizing_distance/std::sqrt( dn2.x()*dn2.x()+dn2.y()*dn2.y()); | |
830 dn2 = point_data<double>( dn2.x()*size, dn2.y()* size); | |
831 point_data<double> start_offset((start.x()+dn1.x()),(start.y()+dn1.y())); | |
832 point_data<double> mid1_offset((middle.x()+dn1.x()),(middle.y()+dn1.y())); | |
833 point_data<double> end_offset((end.x()+dn2.x()),(end.y()+dn2.y())); | |
834 point_data<double> mid2_offset((middle.x()+dn2.x()),(middle.y()+dn2.y())); | |
835 if (ignore_prev_point) | |
836 curr_prev = round_down<T>(start_offset); | |
837 | |
838 | |
839 if (corner_fill_arc) { | |
840 std::vector<point_data< T> > return_points1; | |
841 return_points.push_back(return_points1); | |
842 std::vector<point_data< T> >& return_points_back = return_points[return_points.size()-1]; | |
843 return_points_back.push_back(round_down<T>(mid1_offset)); | |
844 return_points_back.push_back(middle); | |
845 return_points_back.push_back(start); | |
846 return_points_back.push_back(curr_prev); | |
847 point_data<double> dmid(middle.x(),middle.y()); | |
848 return_points.push_back(return_points1); | |
849 int num = make_arc(return_points[return_points.size()-1],mid1_offset,mid2_offset,dmid,sizing_distance,num_circle_segments); | |
850 curr_prev = round_down<T>(mid2_offset); | |
851 return num; | |
852 | |
853 } | |
854 | |
855 std::pair<point_data<double>,point_data<double> > he1(start_offset,mid1_offset); | |
856 std::pair<point_data<double>,point_data<double> > he2(mid2_offset ,end_offset); | |
857 //typedef typename high_precision_type<double>::type high_precision; | |
858 | |
859 point_data<T> intersect; | |
860 typename scanline_base<T>::compute_intersection_pack pack; | |
861 bool res = pack.compute_intersection(intersect,he1,he2,true); | |
862 if( res ) { | |
863 std::vector<point_data< T> > return_points1; | |
864 return_points.push_back(return_points1); | |
865 std::vector<point_data< T> >& return_points_back = return_points[return_points.size()-1]; | |
866 return_points_back.push_back(intersect); | |
867 return_points_back.push_back(middle); | |
868 return_points_back.push_back(start); | |
869 return_points_back.push_back(curr_prev); | |
870 | |
871 //double d1= compute_area(intersect,middle,start); | |
872 //double d2= compute_area(start,curr_prev,intersect); | |
873 | |
874 curr_prev = intersect; | |
875 | |
876 | |
877 return return_points.size(); | |
878 } | |
879 return 0; | |
880 | |
881 } | |
882 | |
883 // this routine should take in start and end point s.t. end point is CCW from start | |
884 // it sould make a pie slice polygon that is an intersection of that arc | |
885 // with an ngon segments approximation of the circle centered at center with radius r | |
886 // point start is gauaranteed to be on the segmentation | |
887 // returnPoints will start with the first point after start | |
888 // returnPoints vector may be empty | |
889 template <typename T> | |
890 inline int make_arc(std::vector<point_data< T> >& return_points, | |
891 point_data< double> start, point_data< double> end, | |
892 point_data< double> center, double r, unsigned int num_circle_segments) { | |
893 const double our_pi=3.1415926535897932384626433832795028841971; | |
894 | |
895 // derive start and end angles | |
896 double ps = atan2(start.y()-center.y(), start.x()-center.x()); | |
897 double pe = atan2(end.y()-center.y(), end.x()-center.x()); | |
898 if (ps < 0.0) | |
899 ps += 2.0 * our_pi; | |
900 if (pe <= 0.0) | |
901 pe += 2.0 * our_pi; | |
902 if (ps >= 2.0 * our_pi) | |
903 ps -= 2.0 * our_pi; | |
904 while (pe <= ps) | |
905 pe += 2.0 * our_pi; | |
906 double delta_angle = (2.0 * our_pi) / (double)num_circle_segments; | |
907 if ( start==end) // full circle? | |
908 { | |
909 ps = delta_angle*0.5; | |
910 pe = ps + our_pi * 2.0; | |
911 double x,y; | |
912 x = center.x() + r * cos(ps); | |
913 y = center.y() + r * sin(ps); | |
914 start = point_data<double>(x,y); | |
915 end = start; | |
916 } | |
917 return_points.push_back(round_down<T>(center)); | |
918 return_points.push_back(round_down<T>(start)); | |
919 unsigned int i=0; | |
920 double curr_angle = ps+delta_angle; | |
921 while( curr_angle < pe - 0.01 && i < 2 * num_circle_segments) { | |
922 i++; | |
923 double x = center.x() + r * cos( curr_angle); | |
924 double y = center.y() + r * sin( curr_angle); | |
925 return_points.push_back( round_down<T>((point_data<double>(x,y)))); | |
926 curr_angle+=delta_angle; | |
927 } | |
928 return_points.push_back(round_down<T>(end)); | |
929 return return_points.size(); | |
930 } | |
931 | |
932 }// close namespace | |
933 }// close name space | |
934 | |
935 #include "detail/scan_arbitrary.hpp" | |
936 | |
937 namespace boost { namespace polygon { | |
938 //ConnectivityExtraction computes the graph of connectivity between rectangle, polygon and | |
939 //polygon set graph nodes where an edge is created whenever the geometry in two nodes overlap | |
940 template <typename coordinate_type> | |
941 class connectivity_extraction{ | |
942 private: | |
943 typedef arbitrary_connectivity_extraction<coordinate_type, int> ce; | |
944 ce ce_; | |
945 unsigned int nodeCount_; | |
946 public: | |
947 inline connectivity_extraction() : ce_(), nodeCount_(0) {} | |
948 inline connectivity_extraction(const connectivity_extraction& that) : ce_(that.ce_), | |
949 nodeCount_(that.nodeCount_) {} | |
950 inline connectivity_extraction& operator=(const connectivity_extraction& that) { | |
951 ce_ = that.ce_; | |
952 nodeCount_ = that.nodeCount_; {} | |
953 return *this; | |
954 } | |
955 | |
956 //insert a polygon set graph node, the value returned is the id of the graph node | |
957 inline unsigned int insert(const polygon_set_data<coordinate_type>& ps) { | |
958 ps.clean(); | |
959 ce_.populateTouchSetData(ps.begin(), ps.end(), nodeCount_); | |
960 return nodeCount_++; | |
961 } | |
962 template <class GeoObjT> | |
963 inline unsigned int insert(const GeoObjT& geoObj) { | |
964 polygon_set_data<coordinate_type> ps; | |
965 ps.insert(geoObj); | |
966 return insert(ps); | |
967 } | |
968 | |
969 //extract connectivity and store the edges in the graph | |
970 //graph must be indexable by graph node id and the indexed value must be a std::set of | |
971 //graph node id | |
972 template <class GraphT> | |
973 inline void extract(GraphT& graph) { | |
974 ce_.execute(graph); | |
975 } | |
976 }; | |
977 | |
978 template <typename T> | |
979 polygon_set_data<T>& | |
980 polygon_set_data<T>::interact(const polygon_set_data<T>& that) { | |
981 connectivity_extraction<coordinate_type> ce; | |
982 std::vector<polygon_with_holes_data<T> > polys; | |
983 get(polys); | |
984 clear(); | |
985 for(std::size_t i = 0; i < polys.size(); ++i) { | |
986 ce.insert(polys[i]); | |
987 } | |
988 int id = ce.insert(that); | |
989 std::vector<std::set<int> > graph(id+1); | |
990 ce.extract(graph); | |
991 for(std::set<int>::iterator itr = graph[id].begin(); | |
992 itr != graph[id].end(); ++itr) { | |
993 insert(polys[*itr]); | |
994 } | |
995 return *this; | |
996 } | |
997 } | |
998 } | |
999 | |
1000 #include "polygon_set_traits.hpp" | |
1001 #include "detail/polygon_set_view.hpp" | |
1002 | |
1003 #include "polygon_set_concept.hpp" | |
1004 #include "detail/minkowski.hpp" | |
1005 #endif |