2 Copyright 2008 Intel Corporation
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).
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"
16 namespace boost { namespace polygon {
19 // utility function to round coordinate types down
20 // rounds down for both negative and positive numbers
21 // intended really for integer type T (does not make sense for float)
23 static inline T round_down(double val) {
24 T rounded_val = (T)(val);
25 if(val < (double)rounded_val)
30 static inline point_data<T> round_down(point_data<double> v) {
31 return point_data<T>(round_down<T>(v.x()),round_down<T>(v.y()));
37 template <typename ltype, typename rtype, int op_type> class polygon_set_view;
40 class polygon_set_data {
42 typedef T coordinate_type;
43 typedef point_data<T> point_type;
44 typedef std::pair<point_type, point_type> edge_type;
45 typedef std::pair<edge_type, int> element_type;
46 typedef std::vector<element_type> value_type;
47 typedef typename value_type::const_iterator iterator_type;
48 typedef polygon_set_data operator_arg_type;
50 // default constructor
51 inline polygon_set_data() : data_(), dirty_(false), unsorted_(false), is_45_(true) {}
53 // constructor from an iterator pair over edge data
54 template <typename iT>
55 inline polygon_set_data(iT input_begin, iT input_end) : data_(), dirty_(false), unsorted_(false), is_45_(true) {
56 for( ; input_begin != input_end; ++input_begin) { insert(*input_begin); }
60 inline polygon_set_data(const polygon_set_data& that) :
61 data_(that.data_), dirty_(that.dirty_), unsorted_(that.unsorted_), is_45_(that.is_45_) {}
64 template <typename ltype, typename rtype, int op_type>
65 inline polygon_set_data(const polygon_set_view<ltype, rtype, op_type>& that);
68 inline ~polygon_set_data() {}
70 // assignement operator
71 inline polygon_set_data& operator=(const polygon_set_data& that) {
72 if(this == &that) return *this;
75 unsorted_ = that.unsorted_;
80 template <typename ltype, typename rtype, int op_type>
81 inline polygon_set_data& operator=(const polygon_set_view<ltype, rtype, op_type>& geometry) {
82 (*this) = geometry.value();
88 template <typename geometry_object>
89 inline polygon_set_data& operator=(const geometry_object& geometry) {
96 // insert iterator range
97 inline void insert(iterator_type input_begin, iterator_type input_end, bool is_hole = false) {
98 if(input_begin == input_end || (!data_.empty() && &(*input_begin) == &(*(data_.begin())))) return;
101 while(input_begin != input_end) {
102 insert(*input_begin, is_hole);
107 // insert iterator range
108 template <typename iT>
109 inline void insert(iT input_begin, iT input_end, bool is_hole = false) {
110 if(input_begin == input_end) return;
111 for(; input_begin != input_end; ++input_begin) {
112 insert(*input_begin, is_hole);
116 template <typename geometry_type>
117 inline void insert(const geometry_type& geometry_object, bool is_hole = false) {
118 insert(geometry_object, is_hole, typename geometry_concept<geometry_type>::type());
121 template <typename polygon_type>
122 inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_concept ) {
123 insert_vertex_sequence(begin_points(polygon_object), end_points(polygon_object), winding(polygon_object), is_hole);
126 inline void insert(const polygon_set_data& ps, bool is_hole = false) {
127 insert(ps.data_.begin(), ps.data_.end(), is_hole);
130 template <typename polygon_45_set_type>
131 inline void insert(const polygon_45_set_type& ps, bool is_hole, polygon_45_set_concept) {
132 std::vector<polygon_45_with_holes_data<typename polygon_45_set_traits<polygon_45_set_type>::coordinate_type> > polys;
134 insert(polys.begin(), polys.end(), is_hole);
137 template <typename polygon_90_set_type>
138 inline void insert(const polygon_90_set_type& ps, bool is_hole, polygon_90_set_concept) {
139 std::vector<polygon_90_with_holes_data<typename polygon_90_set_traits<polygon_90_set_type>::coordinate_type> > polys;
141 insert(polys.begin(), polys.end(), is_hole);
144 template <typename polygon_type>
145 inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_45_concept ) {
146 insert(polygon_object, is_hole, polygon_concept()); }
148 template <typename polygon_type>
149 inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_90_concept ) {
150 insert(polygon_object, is_hole, polygon_concept()); }
152 template <typename polygon_with_holes_type>
153 inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole,
154 polygon_with_holes_concept ) {
155 insert(polygon_with_holes_object, is_hole, polygon_concept());
156 for(typename polygon_with_holes_traits<polygon_with_holes_type>::iterator_holes_type itr =
157 begin_holes(polygon_with_holes_object);
158 itr != end_holes(polygon_with_holes_object); ++itr) {
159 insert(*itr, !is_hole, polygon_concept());
163 template <typename polygon_with_holes_type>
164 inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole,
165 polygon_45_with_holes_concept ) {
166 insert(polygon_with_holes_object, is_hole, polygon_with_holes_concept()); }
168 template <typename polygon_with_holes_type>
169 inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole,
170 polygon_90_with_holes_concept ) {
171 insert(polygon_with_holes_object, is_hole, polygon_with_holes_concept()); }
173 template <typename rectangle_type>
174 inline void insert(const rectangle_type& rectangle_object, bool is_hole, rectangle_concept ) {
175 polygon_90_data<coordinate_type> poly;
176 assign(poly, rectangle_object);
177 insert(poly, is_hole, polygon_concept());
180 inline void insert_clean(const element_type& edge, bool is_hole = false) {
181 if( ! scanline_base<coordinate_type>::is_45_degree(edge.first) &&
182 ! scanline_base<coordinate_type>::is_horizontal(edge.first) &&
183 ! scanline_base<coordinate_type>::is_vertical(edge.first) ) is_45_ = false;
184 data_.push_back(edge);
185 if(data_.back().first.second < data_.back().first.first) {
186 std::swap(data_.back().first.second, data_.back().first.first);
187 data_.back().second *= -1;
190 data_.back().second *= -1;
193 inline void insert(const element_type& edge, bool is_hole = false) {
194 insert_clean(edge, is_hole);
200 inline void insert_vertex_sequence(iT begin_vertex, iT end_vertex, direction_1d winding, bool is_hole) {
201 bool first_iteration = true;
202 point_type first_point;
203 point_type previous_point;
204 point_type current_point;
205 direction_1d winding_dir = winding;
206 int multiplier = winding_dir == COUNTERCLOCKWISE ? 1 : -1;
207 if(is_hole) multiplier *= -1;
208 for( ; begin_vertex != end_vertex; ++begin_vertex) {
209 assign(current_point, *begin_vertex);
210 if(first_iteration) {
211 first_iteration = false;
212 first_point = previous_point = current_point;
214 if(previous_point != current_point) {
215 element_type elem(edge_type(previous_point, current_point),
216 ( previous_point.get(HORIZONTAL) == current_point.get(HORIZONTAL) ? -1 : 1) * multiplier);
220 previous_point = current_point;
222 current_point = first_point;
223 if(!first_iteration) {
224 if(previous_point != current_point) {
225 element_type elem(edge_type(previous_point, current_point),
226 ( previous_point.get(HORIZONTAL) == current_point.get(HORIZONTAL) ? -1 : 1) * multiplier);
234 template <typename output_container>
235 inline void get(output_container& output) const {
236 get_dispatch(output, typename geometry_concept<typename output_container::value_type>::type());
239 // append to the container cT with polygons of three or four verticies
240 // slicing orientation is vertical
242 void get_trapezoids(cT& container) const {
244 trapezoid_arbitrary_formation<coordinate_type> pf;
245 typedef typename polygon_arbitrary_formation<coordinate_type>::vertex_half_edge vertex_half_edge;
246 std::vector<vertex_half_edge> data;
247 for(iterator_type itr = data_.begin(); itr != data_.end(); ++itr){
248 data.push_back(vertex_half_edge((*itr).first.first, (*itr).first.second, (*itr).second));
249 data.push_back(vertex_half_edge((*itr).first.second, (*itr).first.first, -1 * (*itr).second));
251 gtlsort(data.begin(), data.end());
252 pf.scan(container, data.begin(), data.end());
253 //std::cout << "DONE FORMING POLYGONS\n";
256 // append to the container cT with polygons of three or four verticies
258 void get_trapezoids(cT& container, orientation_2d slicing_orientation) const {
259 if(slicing_orientation == VERTICAL) {
260 get_trapezoids(container);
262 polygon_set_data<T> ps(*this);
263 ps.transform(axis_transformation(axis_transformation::SWAP_XY));
265 ps.get_trapezoids(result);
266 for(typename cT::iterator itr = result.begin(); itr != result.end(); ++itr) {
267 ::boost::polygon::transform(*itr, axis_transformation(axis_transformation::SWAP_XY));
269 container.insert(container.end(), result.begin(), result.end());
273 // equivalence operator
274 inline bool operator==(const polygon_set_data& p) const {
277 return data_ == p.data_;
280 // inequivalence operator
281 inline bool operator!=(const polygon_set_data& p) const {
282 return !((*this) == p);
285 // get iterator to begin vertex data
286 inline iterator_type begin() const {
287 return data_.begin();
290 // get iterator to end vertex data
291 inline iterator_type end() const {
295 const value_type& value() const {
299 // clear the contents of the polygon_set_data
300 inline void clear() { data_.clear(); dirty_ = unsorted_ = false; }
302 // find out if Polygon set is empty
303 inline bool empty() const { return data_.empty(); }
305 // get the Polygon set size in vertices
306 inline std::size_t size() const { clean(); return data_.size(); }
308 // get the current Polygon set capacity in vertices
309 inline std::size_t capacity() const { return data_.capacity(); }
311 // reserve size of polygon set in vertices
312 inline void reserve(std::size_t size) { return data_.reserve(size); }
314 // find out if Polygon set is sorted
315 inline bool sorted() const { return !unsorted_; }
317 // find out if Polygon set is clean
318 inline bool dirty() const { return dirty_; }
324 gtlsort(data_.begin(), data_.end());
329 template <typename input_iterator_type>
330 void set(input_iterator_type input_begin, input_iterator_type input_end) {
332 insert(input_begin, input_end);
337 void set(const value_type& value) {
343 template <typename rectangle_type>
344 bool extents(rectangle_type& rect) {
346 if(empty()) return false;
347 bool first_iteration = true;
348 for(iterator_type itr = begin();
349 itr != end(); ++itr) {
350 rectangle_type edge_box;
351 set_points(edge_box, (*itr).first.first, (*itr).first.second);
355 encompass(rect, edge_box);
356 first_iteration = false;
361 inline polygon_set_data&
362 resize(coordinate_type resizing, bool corner_fill_arc = false, unsigned int num_circle_segments=0);
364 template <typename transform_type>
365 inline polygon_set_data&
366 transform(const transform_type& tr) {
367 std::vector<polygon_with_holes_data<T> > polys;
370 for(std::size_t i = 0 ; i < polys.size(); ++i) {
371 ::boost::polygon::transform(polys[i], tr);
379 inline polygon_set_data&
380 scale_up(typename coordinate_traits<coordinate_type>::unsigned_area_type factor) {
381 for(typename value_type::iterator itr = data_.begin(); itr != data_.end(); ++itr) {
382 ::boost::polygon::scale_up((*itr).first.first, factor);
383 ::boost::polygon::scale_up((*itr).first.second, factor);
388 inline polygon_set_data&
389 scale_down(typename coordinate_traits<coordinate_type>::unsigned_area_type factor) {
390 for(typename value_type::iterator itr = data_.begin(); itr != data_.end(); ++itr) {
391 ::boost::polygon::scale_down((*itr).first.first, factor);
392 ::boost::polygon::scale_down((*itr).first.second, factor);
399 template <typename scaling_type>
400 inline polygon_set_data& scale(polygon_set_data& polygon_set,
401 const scaling_type& scaling) {
402 for(typename value_type::iterator itr = begin(); itr != end(); ++itr) {
403 ::boost::polygon::scale((*itr).first.first, scaling);
404 ::boost::polygon::scale((*itr).first.second, scaling);
411 static inline void compute_offset_edge(point_data<long double>& pt1, point_data<long double>& pt2,
412 const point_data<long double>& prev_pt,
413 const point_data<long double>& current_pt,
414 long double distance, int multiplier) {
415 long double dx = current_pt.x() - prev_pt.x();
416 long double dy = current_pt.y() - prev_pt.y();
417 long double edge_length = std::sqrt(dx*dx + dy*dy);
418 long double dnx = dy;
419 long double dny = -dx;
420 dnx = dnx * (long double)distance / edge_length;
421 dny = dny * (long double)distance / edge_length;
422 pt1.x(prev_pt.x() + (long double)dnx * (long double)multiplier);
423 pt2.x(current_pt.x() + (long double)dnx * (long double)multiplier);
424 pt1.y(prev_pt.y() + (long double)dny * (long double)multiplier);
425 pt2.y(current_pt.y() + (long double)dny * (long double)multiplier);
428 static inline void modify_pt(point_data<coordinate_type>& pt, const point_data<coordinate_type>& prev_pt,
429 const point_data<coordinate_type>& current_pt, const point_data<coordinate_type>& next_pt,
430 coordinate_type distance, coordinate_type multiplier) {
431 std::pair<point_data<long double>, point_data<long double> > he1, he2;
432 he1.first.x((long double)(prev_pt.x()));
433 he1.first.y((long double)(prev_pt.y()));
434 he1.second.x((long double)(current_pt.x()));
435 he1.second.y((long double)(current_pt.y()));
436 he2.first.x((long double)(current_pt.x()));
437 he2.first.y((long double)(current_pt.y()));
438 he2.second.x((long double)(next_pt.x()));
439 he2.second.y((long double)(next_pt.y()));
440 compute_offset_edge(he1.first, he1.second, prev_pt, current_pt, distance, multiplier);
441 compute_offset_edge(he2.first, he2.second, current_pt, next_pt, distance, multiplier);
442 typename scanline_base<long double>::compute_intersection_pack pack;
443 point_data<long double> rpt;
444 point_data<long double> bisectorpt((he1.second.x()+he2.first.x())/2,
445 (he1.second.y()+he2.first.y())/2);
446 point_data<long double> orig_pt((long double)pt.x(), (long double)pt.y());
447 if(euclidean_distance(bisectorpt, orig_pt) < distance/2) {
448 if(!pack.compute_lazy_intersection(rpt, he1, he2, true, false)) {
449 rpt = he1.second; //colinear offset edges use shared point
452 if(!pack.compute_lazy_intersection(rpt, he1, std::pair<point_data<long double>, point_data<long double> >(orig_pt, bisectorpt), true, false)) {
453 rpt = he1.second; //colinear offset edges use shared point
456 pt.x((coordinate_type)(std::floor(rpt.x()+0.5)));
457 pt.y((coordinate_type)(std::floor(rpt.y()+0.5)));
460 static void resize_poly_up(std::vector<point_data<coordinate_type> >& poly, coordinate_type distance, coordinate_type multiplier) {
461 point_data<coordinate_type> first_pt = poly[0];
462 point_data<coordinate_type> second_pt = poly[1];
463 point_data<coordinate_type> prev_pt = poly[0];
464 point_data<coordinate_type> current_pt = poly[1];
465 for(std::size_t i = 2; i < poly.size()-1; ++i) {
466 point_data<coordinate_type> next_pt = poly[i];
467 modify_pt(poly[i-1], prev_pt, current_pt, next_pt, distance, multiplier);
468 prev_pt = current_pt;
469 current_pt = next_pt;
471 point_data<coordinate_type> next_pt = first_pt;
472 modify_pt(poly[poly.size()-2], prev_pt, current_pt, next_pt, distance, multiplier);
473 prev_pt = current_pt;
474 current_pt = next_pt;
476 modify_pt(poly[0], prev_pt, current_pt, next_pt, distance, multiplier);
477 poly.back() = poly.front();
479 static bool resize_poly_down(std::vector<point_data<coordinate_type> >& poly, coordinate_type distance, coordinate_type multiplier) {
480 std::vector<point_data<coordinate_type> > orig_poly(poly);
481 rectangle_data<coordinate_type> extents_rectangle;
482 set_points(extents_rectangle, poly[0], poly[0]);
483 point_data<coordinate_type> first_pt = poly[0];
484 point_data<coordinate_type> second_pt = poly[1];
485 point_data<coordinate_type> prev_pt = poly[0];
486 point_data<coordinate_type> current_pt = poly[1];
487 encompass(extents_rectangle, current_pt);
488 for(std::size_t i = 2; i < poly.size()-1; ++i) {
489 point_data<coordinate_type> next_pt = poly[i];
490 encompass(extents_rectangle, next_pt);
491 modify_pt(poly[i-1], prev_pt, current_pt, next_pt, distance, multiplier);
492 prev_pt = current_pt;
493 current_pt = next_pt;
495 if(delta(extents_rectangle, HORIZONTAL) <= std::abs(2*distance))
497 if(delta(extents_rectangle, VERTICAL) <= std::abs(2*distance))
499 point_data<coordinate_type> next_pt = first_pt;
500 modify_pt(poly[poly.size()-2], prev_pt, current_pt, next_pt, distance, multiplier);
501 prev_pt = current_pt;
502 current_pt = next_pt;
504 modify_pt(poly[0], prev_pt, current_pt, next_pt, distance, multiplier);
505 poly.back() = poly.front();
506 //if the line segments formed between orignial and new points cross for an edge that edge inverts
507 //if all edges invert the polygon should be discarded
508 //if even one edge does not invert return true because the polygon is valid
509 bool non_inverting_edge = false;
510 for(std::size_t i = 1; i < poly.size(); ++i) {
511 std::pair<point_data<coordinate_type>, point_data<coordinate_type> >
512 he1(poly[i], orig_poly[i]),
513 he2(poly[i-1], orig_poly[i-1]);
514 if(!scanline_base<coordinate_type>::intersects(he1, he2)) {
515 non_inverting_edge = true;
519 return non_inverting_edge;
523 bloat(typename coordinate_traits<coordinate_type>::unsigned_area_type distance) {
524 std::list<polygon_with_holes_data<coordinate_type> > polys;
527 for(typename std::list<polygon_with_holes_data<coordinate_type> >::iterator itr = polys.begin();
528 itr != polys.end(); ++itr) {
529 resize_poly_up((*itr).self_.coords_, (coordinate_type)distance, (coordinate_type)1);
530 insert_vertex_sequence((*itr).self_.begin(), (*itr).self_.end(), COUNTERCLOCKWISE, false); //inserts without holes
531 for(typename std::list<polygon_data<coordinate_type> >::iterator itrh = (*itr).holes_.begin();
532 itrh != (*itr).holes_.end(); ++itrh) {
533 if(resize_poly_down((*itrh).coords_, (coordinate_type)distance, (coordinate_type)1)) {
534 insert_vertex_sequence((*itrh).coords_.begin(), (*itrh).coords_.end(), CLOCKWISE, true);
542 shrink(typename coordinate_traits<coordinate_type>::unsigned_area_type distance) {
543 std::list<polygon_with_holes_data<coordinate_type> > polys;
546 for(typename std::list<polygon_with_holes_data<coordinate_type> >::iterator itr = polys.begin();
547 itr != polys.end(); ++itr) {
548 if(resize_poly_down((*itr).self_.coords_, (coordinate_type)distance, (coordinate_type)-1)) {
549 insert_vertex_sequence((*itr).self_.begin(), (*itr).self_.end(), COUNTERCLOCKWISE, false); //inserts without holes
550 for(typename std::list<polygon_data<coordinate_type> >::iterator itrh = (*itr).holes_.begin();
551 itrh != (*itr).holes_.end(); ++itrh) {
552 resize_poly_up((*itrh).coords_, (coordinate_type)distance, (coordinate_type)-1);
553 insert_vertex_sequence((*itrh).coords_.begin(), (*itrh).coords_.end(), CLOCKWISE, true);
560 // TODO:: should be private
561 template <typename geometry_type>
562 inline polygon_set_data&
563 insert_with_resize(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc=false, unsigned int num_circle_segments=0, bool hole = false) {
564 return insert_with_resize_dispatch(poly, resizing, corner_fill_arc, num_circle_segments, hole, typename geometry_concept<geometry_type>::type());
567 template <typename geometry_type>
568 inline polygon_set_data&
569 insert_with_resize_dispatch(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc, unsigned int num_circle_segments, bool hole,
570 polygon_with_holes_concept tag) {
571 insert_with_resize_dispatch(poly, resizing, corner_fill_arc, num_circle_segments, hole, polygon_concept());
572 for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
573 begin_holes(poly); itr != end_holes(poly);
575 insert_with_resize_dispatch(*itr, resizing, corner_fill_arc, num_circle_segments, !hole, polygon_concept());
580 template <typename geometry_type>
581 inline polygon_set_data&
582 insert_with_resize_dispatch(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc, unsigned int num_circle_segments, bool hole,
583 polygon_concept tag) {
589 // one dimensional used to store CCW/CW flag
590 //direction_1d wdir = winding(poly);
591 // LOW==CLOCKWISE just faster to type
593 //int multiplier = wdir == LOW ? -1 : 1;
594 //std::cout<<" multiplier : "<<multiplier<<std::endl;
595 //if(hole) resizing *= -1;
596 direction_1d resize_wdir = resizing>0?COUNTERCLOCKWISE:CLOCKWISE;
598 typedef typename polygon_data<T>::iterator_type piterator;
599 piterator first, second, third, end, real_end;
600 real_end = end_points(poly);
601 third = begin_points(poly);
603 if(first == real_end) return *this;
605 if(third == real_end) return *this;
606 second = end = third;
608 if(third == real_end) return *this;
611 std::vector<point_data<T> > first_pts;
612 std::vector<point_data<T> > all_pts;
613 direction_1d first_wdir = CLOCKWISE;
616 polygon_set_data<T> sizingSet;
617 bool sizing_sign = resizing<0;
618 bool prev_concave = true;
619 point_data<T> prev_point;
623 //insert minkofski shapes on edges and corners
624 do { // REAL WORK IS HERE
627 //first, second and third point to points in correct CCW order
628 // check if convex or concave case
629 point_data<coordinate_type> normal1( second->y()-first->y(), first->x()-second->x());
630 point_data<coordinate_type> normal2( third->y()-second->y(), second->x()-third->x());
631 double direction = normal1.x()*normal2.y()- normal2.x()*normal1.y();
632 bool convex = direction>0;
634 bool treat_as_concave = !convex;
636 treat_as_concave = convex;
637 point_data<double> v;
639 double s2 = (v.x()*v.x()+v.y()*v.y());
640 double s = sqrt(s2)/resizing;
641 v = point_data<double>(v.x()/s,v.y()/s);
642 point_data<T> curr_prev;
644 //TODO missing round_down()
645 curr_prev = point_data<T>(first->x()+v.x(),first->y()+v.y());
647 curr_prev = prev_point;
649 // around concave corners - insert rectangle
650 // if previous corner is concave it's point info may be ignored
651 if ( treat_as_concave) {
652 std::vector<point_data<T> > pts;
654 pts.push_back(point_data<T>(second->x()+v.x(),second->y()+v.y()));
655 pts.push_back(*second);
656 pts.push_back(*first);
657 pts.push_back(point_data<T>(curr_prev));
658 if (first_pts.size()){
659 sizingSet.insert_vertex_sequence(pts.begin(),pts.end(), resize_wdir,false);
662 first_wdir = resize_wdir;
666 // add either intersection_quad or pie_shape, based on corner_fill_arc option
667 // for convex corner (convexity depends on sign of resizing, whether we shrink or grow)
668 std::vector< std::vector<point_data<T> > > pts;
669 direction_1d winding;
670 winding = convex?COUNTERCLOCKWISE:CLOCKWISE;
671 if (make_resizing_vertex_list(pts, curr_prev, prev_concave, *first, *second, *third, resizing
672 , num_circle_segments, corner_fill_arc))
674 if (first_pts.size()) {
675 for (int i=0; i<pts.size(); i++) {
676 sizingSet.insert_vertex_sequence(pts[i].begin(),pts[i].end(),winding,false);
681 first_wdir = resize_wdir;
682 for (int i=1; i<pts.size(); i++) {
683 sizingSet.insert_vertex_sequence(pts[i].begin(),pts[i].end(),winding,false);
686 prev_point = curr_prev;
689 treat_as_concave = true;
693 prev_concave = treat_as_concave;
697 if(third == real_end) {
698 third = begin_points(poly);
699 if(*second == *third) {
700 ++third; //skip first point if it is duplicate of last point
703 } while(second != end);
705 // handle insertion of first point
707 first_pts[first_pts.size()-1]=prev_point;
709 sizingSet.insert_vertex_sequence(first_pts.begin(),first_pts.end(),first_wdir,false);
711 polygon_set_data<coordinate_type> tmp;
713 //insert original shape
714 tmp.insert(poly, false, polygon_concept());
715 if((resizing < 0) ^ hole) tmp -= sizingSet;
716 else tmp += sizingSet;
723 inline polygon_set_data&
724 interact(const polygon_set_data& that);
726 inline bool downcast(polygon_45_set_data<coordinate_type>& result) const {
727 if(!is_45_) return false;
728 for(iterator_type itr = begin(); itr != end(); ++itr) {
729 const element_type& elem = *itr;
730 int count = elem.second;
731 int rise = 1; //up sloping 45
732 if(scanline_base<coordinate_type>::is_horizontal(elem.first)) rise = 0;
733 else if(scanline_base<coordinate_type>::is_vertical(elem.first)) rise = 2;
735 if(!scanline_base<coordinate_type>::is_45_degree(elem.first)) {
737 return false; //consider throwing because is_45_ has be be wrong
739 if(elem.first.first.y() > elem.first.second.y()) rise = -1; //down sloping 45
741 typename polygon_45_set_data<coordinate_type>::Vertex45Compact vertex(elem.first.first, rise, count);
742 result.insert(vertex);
743 typename polygon_45_set_data<coordinate_type>::Vertex45Compact vertex2(elem.first.second, rise, -count);
744 result.insert(vertex2);
749 inline GEOMETRY_CONCEPT_ID concept_downcast() const {
750 typedef typename coordinate_traits<coordinate_type>::coordinate_difference delta_type;
752 for(iterator_type itr = begin(); itr != end(); ++itr) {
753 const element_type& elem = *itr;
754 delta_type h_delta = euclidean_distance(elem.first.first, elem.first.second, HORIZONTAL);
755 delta_type v_delta = euclidean_distance(elem.first.first, elem.first.second, VERTICAL);
756 if(h_delta != 0 || v_delta != 0) {
757 //neither delta is zero and the edge is not MANHATTAN
758 if(v_delta != h_delta && v_delta != -h_delta) return POLYGON_SET_CONCEPT;
762 if(is_45) return POLYGON_45_SET_CONCEPT;
763 return POLYGON_90_SET_CONCEPT;
767 mutable value_type data_;
769 mutable bool unsorted_;
775 template <typename output_container>
776 void get_dispatch(output_container& output, polygon_concept tag) const {
777 get_fracture(output, true, tag);
779 template <typename output_container>
780 void get_dispatch(output_container& output, polygon_with_holes_concept tag) const {
781 get_fracture(output, false, tag);
783 template <typename output_container, typename concept_type>
784 void get_fracture(output_container& container, bool fracture_holes, concept_type ) const {
786 polygon_arbitrary_formation<coordinate_type> pf(fracture_holes);
787 typedef typename polygon_arbitrary_formation<coordinate_type>::vertex_half_edge vertex_half_edge;
788 std::vector<vertex_half_edge> data;
789 for(iterator_type itr = data_.begin(); itr != data_.end(); ++itr){
790 data.push_back(vertex_half_edge((*itr).first.first, (*itr).first.second, (*itr).second));
791 data.push_back(vertex_half_edge((*itr).first.second, (*itr).first.first, -1 * (*itr).second));
793 gtlsort(data.begin(), data.end());
794 pf.scan(container, data.begin(), data.end());
798 struct polygon_set_concept;
799 template <typename T>
800 struct geometry_concept<polygon_set_data<T> > {
801 typedef polygon_set_concept type;
804 // template <typename T>
805 // inline double compute_area(point_data<T>& a, point_data<T>& b, point_data<T>& c) {
807 // return (double)(b.x()-a.x())*(double)(c.y()-a.y())- (double)(c.x()-a.x())*(double)(b.y()-a.y());
812 template <typename T>
813 inline int make_resizing_vertex_list(std::vector<std::vector<point_data< T> > >& return_points,
814 point_data<T>& curr_prev, bool ignore_prev_point,
815 point_data< T> start, point_data<T> middle, point_data< T> end,
816 double sizing_distance, unsigned int num_circle_segments, bool corner_fill_arc) {
818 // handle the case of adding an intersection point
819 point_data<double> dn1( middle.y()-start.y(), start.x()-middle.x());
820 double size = sizing_distance/sqrt( dn1.x()*dn1.x()+dn1.y()*dn1.y());
821 dn1 = point_data<double>( dn1.x()*size, dn1.y()* size);
822 point_data<double> dn2( end.y()-middle.y(), middle.x()-end.x());
823 size = sizing_distance/sqrt( dn2.x()*dn2.x()+dn2.y()*dn2.y());
824 dn2 = point_data<double>( dn2.x()*size, dn2.y()* size);
825 point_data<double> start_offset((start.x()+dn1.x()),(start.y()+dn1.y()));
826 point_data<double> mid1_offset((middle.x()+dn1.x()),(middle.y()+dn1.y()));
827 point_data<double> end_offset((end.x()+dn2.x()),(end.y()+dn2.y()));
828 point_data<double> mid2_offset((middle.x()+dn2.x()),(middle.y()+dn2.y()));
829 if (ignore_prev_point)
830 curr_prev = round_down<T>(start_offset);
833 if (corner_fill_arc) {
834 std::vector<point_data< T> > return_points1;
835 return_points.push_back(return_points1);
836 std::vector<point_data< T> >& return_points_back = return_points[return_points.size()-1];
837 return_points_back.push_back(round_down<T>(mid1_offset));
838 return_points_back.push_back(middle);
839 return_points_back.push_back(start);
840 return_points_back.push_back(curr_prev);
841 point_data<double> dmid(middle.x(),middle.y());
842 return_points.push_back(return_points1);
843 int num = make_arc(return_points[return_points.size()-1],mid1_offset,mid2_offset,dmid,sizing_distance,num_circle_segments);
844 curr_prev = round_down<T>(mid2_offset);
849 std::pair<point_data<double>,point_data<double> > he1(start_offset,mid1_offset);
850 std::pair<point_data<double>,point_data<double> > he2(mid2_offset ,end_offset);
851 //typedef typename high_precision_type<double>::type high_precision;
853 point_data<T> intersect;
854 typename scanline_base<T>::compute_intersection_pack pack;
855 bool res = pack.compute_intersection(intersect,he1,he2,true);
857 std::vector<point_data< T> > return_points1;
858 return_points.push_back(return_points1);
859 std::vector<point_data< T> >& return_points_back = return_points[return_points.size()-1];
860 return_points_back.push_back(intersect);
861 return_points_back.push_back(middle);
862 return_points_back.push_back(start);
863 return_points_back.push_back(curr_prev);
865 //double d1= compute_area(intersect,middle,start);
866 //double d2= compute_area(start,curr_prev,intersect);
868 curr_prev = intersect;
871 return return_points.size();
877 // this routine should take in start and end point s.t. end point is CCW from start
878 // it sould make a pie slice polygon that is an intersection of that arc
879 // with an ngon segments approximation of the circle centered at center with radius r
880 // point start is gauaranteed to be on the segmentation
881 // returnPoints will start with the first point after start
882 // returnPoints vector may be empty
883 template <typename T>
884 inline int make_arc(std::vector<point_data< T> >& return_points,
885 point_data< double> start, point_data< double> end,
886 point_data< double> center, double r, unsigned int num_circle_segments) {
887 const double our_pi=3.1415926535897932384626433832795028841971;
889 // derive start and end angles
890 double ps = atan2(start.y()-center.y(), start.x()-center.x());
891 double pe = atan2(end.y()-center.y(), end.x()-center.x());
896 if (ps >= 2.0 * our_pi)
900 double delta_angle = (2.0 * our_pi) / (double)num_circle_segments;
901 if ( start==end) // full circle?
903 ps = delta_angle*0.5;
904 pe = ps + our_pi * 2.0;
906 x = center.x() + r * cos(ps);
907 y = center.y() + r * sin(ps);
908 start = point_data<double>(x,y);
911 return_points.push_back(round_down<T>(center));
912 return_points.push_back(round_down<T>(start));
914 double curr_angle = ps+delta_angle;
915 while( curr_angle < pe - 0.01 && i < 2 * num_circle_segments) {
917 double x = center.x() + r * cos( curr_angle);
918 double y = center.y() + r * sin( curr_angle);
919 return_points.push_back( round_down<T>((point_data<double>(x,y))));
920 curr_angle+=delta_angle;
922 return_points.push_back(round_down<T>(end));
923 return return_points.size();
929 #include "detail/scan_arbitrary.hpp"
931 namespace boost { namespace polygon {
932 //ConnectivityExtraction computes the graph of connectivity between rectangle, polygon and
933 //polygon set graph nodes where an edge is created whenever the geometry in two nodes overlap
934 template <typename coordinate_type>
935 class connectivity_extraction{
937 typedef arbitrary_connectivity_extraction<coordinate_type, int> ce;
939 unsigned int nodeCount_;
941 inline connectivity_extraction() : ce_(), nodeCount_(0) {}
942 inline connectivity_extraction(const connectivity_extraction& that) : ce_(that.ce_),
943 nodeCount_(that.nodeCount_) {}
944 inline connectivity_extraction& operator=(const connectivity_extraction& that) {
946 nodeCount_ = that.nodeCount_; {}
950 //insert a polygon set graph node, the value returned is the id of the graph node
951 inline unsigned int insert(const polygon_set_data<coordinate_type>& ps) {
953 ce_.populateTouchSetData(ps.begin(), ps.end(), nodeCount_);
956 template <class GeoObjT>
957 inline unsigned int insert(const GeoObjT& geoObj) {
958 polygon_set_data<coordinate_type> ps;
963 //extract connectivity and store the edges in the graph
964 //graph must be indexable by graph node id and the indexed value must be a std::set of
966 template <class GraphT>
967 inline void extract(GraphT& graph) {
972 template <typename T>
974 polygon_set_data<T>::interact(const polygon_set_data<T>& that) {
975 connectivity_extraction<coordinate_type> ce;
976 std::vector<polygon_with_holes_data<T> > polys;
979 for(std::size_t i = 0; i < polys.size(); ++i) {
982 int id = ce.insert(that);
983 std::vector<std::set<int> > graph(id+1);
985 for(std::set<int>::iterator itr = graph[id].begin();
986 itr != graph[id].end(); ++itr) {
994 #include "polygon_set_traits.hpp"
995 #include "detail/polygon_set_view.hpp"
997 #include "polygon_set_concept.hpp"
998 #include "detail/minkowski.hpp"