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