Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/polygon/isotropy.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children |
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 | |
9 #ifndef BOOST_POLYGON_ISOTROPY_HPP | |
10 #define BOOST_POLYGON_ISOTROPY_HPP | |
11 | |
12 //external | |
13 #include <cmath> | |
14 #include <cstddef> | |
15 #include <cstdlib> | |
16 #include <vector> | |
17 #include <deque> | |
18 #include <map> | |
19 #include <set> | |
20 #include <list> | |
21 //#include <iostream> | |
22 #include <algorithm> | |
23 #include <limits> | |
24 #include <iterator> | |
25 #include <string> | |
26 | |
27 #ifndef BOOST_POLYGON_NO_DEPS | |
28 | |
29 #include <boost/config.hpp> | |
30 #ifdef BOOST_MSVC | |
31 #define BOOST_POLYGON_MSVC | |
32 #endif | |
33 #ifdef BOOST_INTEL | |
34 #define BOOST_POLYGON_ICC | |
35 #endif | |
36 #ifdef BOOST_HAS_LONG_LONG | |
37 #define BOOST_POLYGON_USE_LONG_LONG | |
38 typedef boost::long_long_type polygon_long_long_type; | |
39 typedef boost::ulong_long_type polygon_ulong_long_type; | |
40 //typedef long long polygon_long_long_type; | |
41 //typedef unsigned long long polygon_ulong_long_type; | |
42 #endif | |
43 #include <boost/mpl/size_t.hpp> | |
44 #include <boost/mpl/protect.hpp> | |
45 #include <boost/utility/enable_if.hpp> | |
46 #include <boost/mpl/bool.hpp> | |
47 #include <boost/mpl/and.hpp> | |
48 #include <boost/mpl/or.hpp> | |
49 #else | |
50 | |
51 #ifdef _WIN32 | |
52 #define BOOST_POLYGON_MSVC | |
53 #endif | |
54 #ifdef __ICC | |
55 #define BOOST_POLYGON_ICC | |
56 #endif | |
57 #define BOOST_POLYGON_USE_LONG_LONG | |
58 typedef long long polygon_long_long_type; | |
59 typedef unsigned long long polygon_ulong_long_type; | |
60 | |
61 namespace boost { | |
62 template <bool B, class T = void> | |
63 struct enable_if_c { | |
64 typedef T type; | |
65 }; | |
66 | |
67 template <class T> | |
68 struct enable_if_c<false, T> {}; | |
69 | |
70 template <class Cond, class T = void> | |
71 struct enable_if : public enable_if_c<Cond::value, T> {}; | |
72 | |
73 template <bool B, class T> | |
74 struct lazy_enable_if_c { | |
75 typedef typename T::type type; | |
76 }; | |
77 | |
78 template <class T> | |
79 struct lazy_enable_if_c<false, T> {}; | |
80 | |
81 template <class Cond, class T> | |
82 struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; | |
83 | |
84 | |
85 template <bool B, class T = void> | |
86 struct disable_if_c { | |
87 typedef T type; | |
88 }; | |
89 | |
90 template <class T> | |
91 struct disable_if_c<true, T> {}; | |
92 | |
93 template <class Cond, class T = void> | |
94 struct disable_if : public disable_if_c<Cond::value, T> {}; | |
95 | |
96 template <bool B, class T> | |
97 struct lazy_disable_if_c { | |
98 typedef typename T::type type; | |
99 }; | |
100 | |
101 template <class T> | |
102 struct lazy_disable_if_c<true, T> {}; | |
103 | |
104 template <class Cond, class T> | |
105 struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; | |
106 } | |
107 | |
108 #endif | |
109 | |
110 namespace boost { namespace polygon{ | |
111 | |
112 enum GEOMETRY_CONCEPT_ID { | |
113 COORDINATE_CONCEPT, | |
114 INTERVAL_CONCEPT, | |
115 POINT_CONCEPT, | |
116 POINT_3D_CONCEPT, | |
117 RECTANGLE_CONCEPT, | |
118 POLYGON_90_CONCEPT, | |
119 POLYGON_90_WITH_HOLES_CONCEPT, | |
120 POLYGON_45_CONCEPT, | |
121 POLYGON_45_WITH_HOLES_CONCEPT, | |
122 POLYGON_CONCEPT, | |
123 POLYGON_WITH_HOLES_CONCEPT, | |
124 POLYGON_90_SET_CONCEPT, | |
125 POLYGON_45_SET_CONCEPT, | |
126 POLYGON_SET_CONCEPT | |
127 }; | |
128 | |
129 struct undefined_concept {}; | |
130 | |
131 template <typename T> | |
132 struct geometry_concept { typedef undefined_concept type; }; | |
133 | |
134 template <typename GCT, typename T> | |
135 struct view_of {}; | |
136 | |
137 template <typename T1, typename T2> | |
138 view_of<T1, T2> view_as(const T2& obj) { return view_of<T1, T2>(obj); } | |
139 | |
140 template <typename T> | |
141 struct coordinate_traits {}; | |
142 | |
143 //used to override long double with an infinite precision datatype | |
144 template <typename T> | |
145 struct high_precision_type { | |
146 typedef long double type; | |
147 }; | |
148 | |
149 template <typename T> | |
150 T convert_high_precision_type(const typename high_precision_type<T>::type& v) { | |
151 return T(v); | |
152 } | |
153 | |
154 //used to override std::sort with an alternative (parallel) algorithm | |
155 template <typename iter_type> | |
156 void polygon_sort(iter_type _b_, iter_type _e_); | |
157 | |
158 template <typename iter_type, typename pred_type> | |
159 void polygon_sort(iter_type _b_, iter_type _e_, const pred_type& _pred_); | |
160 | |
161 | |
162 template <> | |
163 struct coordinate_traits<int> { | |
164 typedef int coordinate_type; | |
165 typedef long double area_type; | |
166 #ifdef BOOST_POLYGON_USE_LONG_LONG | |
167 typedef polygon_long_long_type manhattan_area_type; | |
168 typedef polygon_ulong_long_type unsigned_area_type; | |
169 typedef polygon_long_long_type coordinate_difference; | |
170 #else | |
171 typedef long manhattan_area_type; | |
172 typedef unsigned long unsigned_area_type; | |
173 typedef long coordinate_difference; | |
174 #endif | |
175 typedef long double coordinate_distance; | |
176 }; | |
177 | |
178 #ifdef BOOST_POLYGON_USE_LONG_LONG | |
179 template <> | |
180 struct coordinate_traits<polygon_long_long_type> { | |
181 typedef polygon_long_long_type coordinate_type; | |
182 typedef long double area_type; | |
183 typedef polygon_long_long_type manhattan_area_type; | |
184 typedef polygon_ulong_long_type unsigned_area_type; | |
185 typedef polygon_long_long_type coordinate_difference; | |
186 typedef long double coordinate_distance; | |
187 }; | |
188 #endif | |
189 | |
190 template <> | |
191 struct coordinate_traits<float> { | |
192 typedef float coordinate_type; | |
193 typedef float area_type; | |
194 typedef float manhattan_area_type; | |
195 typedef float unsigned_area_type; | |
196 typedef float coordinate_difference; | |
197 typedef float coordinate_distance; | |
198 }; | |
199 | |
200 template <> | |
201 struct coordinate_traits<double> { | |
202 typedef double coordinate_type; | |
203 typedef double area_type; | |
204 typedef double manhattan_area_type; | |
205 typedef double unsigned_area_type; | |
206 typedef double coordinate_difference; | |
207 typedef double coordinate_distance; | |
208 }; | |
209 | |
210 template <> | |
211 struct coordinate_traits<long double> { | |
212 typedef long double coordinate_type; | |
213 typedef long double area_type; | |
214 typedef long double manhattan_area_type; | |
215 typedef long double unsigned_area_type; | |
216 typedef long double coordinate_difference; | |
217 typedef long double coordinate_distance; | |
218 }; | |
219 | |
220 template <typename T> | |
221 struct scaling_policy { | |
222 template <typename T2> | |
223 static inline T round(T2 t2) { | |
224 return (T)std::floor(t2+0.5); | |
225 } | |
226 | |
227 static inline T round(T t2) { | |
228 return t2; | |
229 } | |
230 }; | |
231 | |
232 struct coordinate_concept {}; | |
233 | |
234 template <> | |
235 struct geometry_concept<int> { typedef coordinate_concept type; }; | |
236 #ifdef BOOST_POLYGON_USE_LONG_LONG | |
237 template <> | |
238 struct geometry_concept<polygon_long_long_type> { typedef coordinate_concept type; }; | |
239 #endif | |
240 template <> | |
241 struct geometry_concept<float> { typedef coordinate_concept type; }; | |
242 template <> | |
243 struct geometry_concept<double> { typedef coordinate_concept type; }; | |
244 template <> | |
245 struct geometry_concept<long double> { typedef coordinate_concept type; }; | |
246 | |
247 #ifndef BOOST_POLYGON_NO_DEPS | |
248 struct gtl_no : mpl::bool_<false> {}; | |
249 struct gtl_yes : mpl::bool_<true> {}; | |
250 template <typename T, typename T2> | |
251 struct gtl_and : mpl::and_<T, T2> {}; | |
252 template <typename T, typename T2, typename T3> | |
253 struct gtl_and_3 : mpl::and_<T, T2, T3> {}; | |
254 template <typename T, typename T2, typename T3, typename T4> | |
255 struct gtl_and_4 : mpl::and_<T, T2, T3, T4> {}; | |
256 // template <typename T, typename T2> | |
257 // struct gtl_or : mpl::or_<T, T2> {}; | |
258 // template <typename T, typename T2, typename T3> | |
259 // struct gtl_or_3 : mpl::or_<T, T2, T3> {}; | |
260 // template <typename T, typename T2, typename T3, typename T4> | |
261 // struct gtl_or_4 : mpl::or_<T, T2, T3, T4> {}; | |
262 #else | |
263 struct gtl_no { static const bool value = false; }; | |
264 struct gtl_yes { typedef gtl_yes type; | |
265 static const bool value = true; }; | |
266 | |
267 template <bool T, bool T2> | |
268 struct gtl_and_c { typedef gtl_no type; }; | |
269 template <> | |
270 struct gtl_and_c<true, true> { typedef gtl_yes type; }; | |
271 | |
272 template <typename T, typename T2> | |
273 struct gtl_and : gtl_and_c<T::value, T2::value> {}; | |
274 template <typename T, typename T2, typename T3> | |
275 struct gtl_and_3 { typedef typename gtl_and< | |
276 T, typename gtl_and<T2, T3>::type>::type type; }; | |
277 | |
278 template <typename T, typename T2, typename T3, typename T4> | |
279 struct gtl_and_4 { typedef typename gtl_and_3< | |
280 T, T2, typename gtl_and<T3, T4>::type>::type type; }; | |
281 #endif | |
282 template <typename T, typename T2> | |
283 struct gtl_or { typedef gtl_yes type; }; | |
284 template <typename T> | |
285 struct gtl_or<T, T> { typedef T type; }; | |
286 | |
287 template <typename T, typename T2, typename T3> | |
288 struct gtl_or_3 { typedef typename gtl_or< | |
289 T, typename gtl_or<T2, T3>::type>::type type; }; | |
290 | |
291 template <typename T, typename T2, typename T3, typename T4> | |
292 struct gtl_or_4 { typedef typename gtl_or< | |
293 T, typename gtl_or_3<T2, T3, T4>::type>::type type; }; | |
294 | |
295 template <typename T> | |
296 struct gtl_not { typedef gtl_no type; }; | |
297 template <> | |
298 struct gtl_not<gtl_no> { typedef gtl_yes type; }; | |
299 | |
300 template <typename T> | |
301 struct gtl_if { | |
302 #ifdef BOOST_POLYGON_MSVC | |
303 typedef gtl_no type; | |
304 #endif | |
305 }; | |
306 template <> | |
307 struct gtl_if<gtl_yes> { typedef gtl_yes type; }; | |
308 | |
309 template <typename T, typename T2> | |
310 struct gtl_same_type { typedef gtl_no type; }; | |
311 template <typename T> | |
312 struct gtl_same_type<T, T> { typedef gtl_yes type; }; | |
313 template <typename T, typename T2> | |
314 struct gtl_different_type { typedef typename gtl_not<typename gtl_same_type<T, T2>::type>::type type; }; | |
315 | |
316 struct manhattan_domain {}; | |
317 struct forty_five_domain {}; | |
318 struct general_domain {}; | |
319 | |
320 template <typename T> | |
321 struct geometry_domain { typedef general_domain type; }; | |
322 | |
323 template <typename domain_type, typename coordinate_type> | |
324 struct area_type_by_domain { typedef typename coordinate_traits<coordinate_type>::area_type type; }; | |
325 template <typename coordinate_type> | |
326 struct area_type_by_domain<manhattan_domain, coordinate_type> { | |
327 typedef typename coordinate_traits<coordinate_type>::manhattan_area_type type; }; | |
328 | |
329 struct y_c_edist : gtl_yes {}; | |
330 | |
331 template <typename coordinate_type_1, typename coordinate_type_2> | |
332 typename enable_if< | |
333 typename gtl_and_3<y_c_edist, typename gtl_same_type<typename geometry_concept<coordinate_type_1>::type, coordinate_concept>::type, | |
334 typename gtl_same_type<typename geometry_concept<coordinate_type_1>::type, coordinate_concept>::type>::type, | |
335 typename coordinate_traits<coordinate_type_1>::coordinate_difference>::type | |
336 euclidean_distance(const coordinate_type_1& lvalue, const coordinate_type_2& rvalue) { | |
337 typedef typename coordinate_traits<coordinate_type_1>::coordinate_difference Unit; | |
338 return (lvalue < rvalue) ? (Unit)rvalue - (Unit)lvalue : (Unit)lvalue - (Unit)rvalue; | |
339 } | |
340 | |
341 | |
342 | |
343 // predicated_swap swaps a and b if pred is true | |
344 | |
345 // predicated_swap is guarenteed to behave the same as | |
346 // if(pred){ | |
347 // T tmp = a; | |
348 // a = b; | |
349 // b = tmp; | |
350 // } | |
351 // but will not generate a branch instruction. | |
352 // predicated_swap always creates a temp copy of a, but does not | |
353 // create more than one temp copy of an input. | |
354 // predicated_swap can be used to optimize away branch instructions in C++ | |
355 template <class T> | |
356 inline bool predicated_swap(const bool& pred, | |
357 T& a, | |
358 T& b) { | |
359 const T tmp = a; | |
360 const T* input[2] = {&b, &tmp}; | |
361 a = *input[!pred]; | |
362 b = *input[pred]; | |
363 return pred; | |
364 } | |
365 | |
366 enum direction_1d_enum { LOW = 0, HIGH = 1, | |
367 LEFT = 0, RIGHT = 1, | |
368 CLOCKWISE = 0, COUNTERCLOCKWISE = 1, | |
369 REVERSE = 0, FORWARD = 1, | |
370 NEGATIVE = 0, POSITIVE = 1 }; | |
371 enum orientation_2d_enum { HORIZONTAL = 0, VERTICAL = 1 }; | |
372 enum direction_2d_enum { WEST = 0, EAST = 1, SOUTH = 2, NORTH = 3 }; | |
373 enum orientation_3d_enum { PROXIMAL = 2 }; | |
374 enum direction_3d_enum { DOWN = 4, UP = 5 }; | |
375 enum winding_direction { | |
376 clockwise_winding = 0, | |
377 counterclockwise_winding = 1, | |
378 unknown_winding = 2 | |
379 }; | |
380 | |
381 class direction_2d; | |
382 class direction_3d; | |
383 class orientation_2d; | |
384 | |
385 class direction_1d { | |
386 private: | |
387 unsigned int val_; | |
388 explicit direction_1d(int d); | |
389 public: | |
390 inline direction_1d() : val_(LOW) {} | |
391 inline direction_1d(const direction_1d& that) : val_(that.val_) {} | |
392 inline direction_1d(const direction_1d_enum val) : val_(val) {} | |
393 explicit inline direction_1d(const direction_2d& that); | |
394 explicit inline direction_1d(const direction_3d& that); | |
395 inline direction_1d& operator = (const direction_1d& d) { | |
396 val_ = d.val_; return * this; } | |
397 inline bool operator==(direction_1d d) const { return (val_ == d.val_); } | |
398 inline bool operator!=(direction_1d d) const { return !((*this) == d); } | |
399 inline unsigned int to_int(void) const { return val_; } | |
400 inline direction_1d& backward() { val_ ^= 1; return *this; } | |
401 inline int get_sign() const { return val_ * 2 - 1; } | |
402 }; | |
403 | |
404 class direction_2d; | |
405 | |
406 class orientation_2d { | |
407 private: | |
408 unsigned int val_; | |
409 explicit inline orientation_2d(int o); | |
410 public: | |
411 inline orientation_2d() : val_(HORIZONTAL) {} | |
412 inline orientation_2d(const orientation_2d& ori) : val_(ori.val_) {} | |
413 inline orientation_2d(const orientation_2d_enum val) : val_(val) {} | |
414 explicit inline orientation_2d(const direction_2d& that); | |
415 inline orientation_2d& operator=(const orientation_2d& ori) { | |
416 val_ = ori.val_; return * this; } | |
417 inline bool operator==(orientation_2d that) const { return (val_ == that.val_); } | |
418 inline bool operator!=(orientation_2d that) const { return (val_ != that.val_); } | |
419 inline unsigned int to_int() const { return (val_); } | |
420 inline void turn_90() { val_ = val_^ 1; } | |
421 inline orientation_2d get_perpendicular() const { | |
422 orientation_2d retval = *this; | |
423 retval.turn_90(); | |
424 return retval; | |
425 } | |
426 inline direction_2d get_direction(direction_1d dir) const; | |
427 }; | |
428 | |
429 class direction_2d { | |
430 private: | |
431 int val_; | |
432 | |
433 public: | |
434 | |
435 inline direction_2d() : val_(WEST) {} | |
436 | |
437 inline direction_2d(const direction_2d& that) : val_(that.val_) {} | |
438 | |
439 inline direction_2d(const direction_2d_enum val) : val_(val) {} | |
440 | |
441 inline direction_2d& operator=(const direction_2d& d) { | |
442 val_ = d.val_; | |
443 return * this; | |
444 } | |
445 | |
446 inline ~direction_2d() { } | |
447 | |
448 inline bool operator==(direction_2d d) const { return (val_ == d.val_); } | |
449 inline bool operator!=(direction_2d d) const { return !((*this) == d); } | |
450 inline bool operator< (direction_2d d) const { return (val_ < d.val_); } | |
451 inline bool operator<=(direction_2d d) const { return (val_ <= d.val_); } | |
452 inline bool operator> (direction_2d d) const { return (val_ > d.val_); } | |
453 inline bool operator>=(direction_2d d) const { return (val_ >= d.val_); } | |
454 | |
455 // Casting to int | |
456 inline unsigned int to_int(void) const { return val_; } | |
457 | |
458 inline direction_2d backward() const { | |
459 // flip the LSB, toggles 0 - 1 and 2 - 3 | |
460 return direction_2d(direction_2d_enum(val_ ^ 1)); | |
461 } | |
462 | |
463 // Returns a direction 90 degree left (LOW) or right(HIGH) to this one | |
464 inline direction_2d turn(direction_1d t) const { | |
465 return direction_2d(direction_2d_enum(val_ ^ 3 ^ (val_ >> 1) ^ t.to_int())); | |
466 } | |
467 | |
468 // Returns a direction 90 degree left to this one | |
469 inline direction_2d left() const {return turn(HIGH);} | |
470 | |
471 // Returns a direction 90 degree right to this one | |
472 inline direction_2d right() const {return turn(LOW);} | |
473 | |
474 // N, E are positive, S, W are negative | |
475 inline bool is_positive() const {return (val_ & 1);} | |
476 inline bool is_negative() const {return !is_positive();} | |
477 inline int get_sign() const {return ((is_positive()) << 1) -1;} | |
478 | |
479 }; | |
480 | |
481 direction_1d::direction_1d(const direction_2d& that) : val_(that.to_int() & 1) {} | |
482 | |
483 orientation_2d::orientation_2d(const direction_2d& that) : val_(that.to_int() >> 1) {} | |
484 | |
485 direction_2d orientation_2d::get_direction(direction_1d dir) const { | |
486 return direction_2d(direction_2d_enum((val_ << 1) + dir.to_int())); | |
487 } | |
488 | |
489 class orientation_3d { | |
490 private: | |
491 unsigned int val_; | |
492 explicit inline orientation_3d(int o); | |
493 public: | |
494 inline orientation_3d() : val_((int)HORIZONTAL) {} | |
495 inline orientation_3d(const orientation_3d& ori) : val_(ori.val_) {} | |
496 inline orientation_3d(orientation_2d ori) : val_(ori.to_int()) {} | |
497 inline orientation_3d(const orientation_3d_enum val) : val_(val) {} | |
498 explicit inline orientation_3d(const direction_2d& that); | |
499 explicit inline orientation_3d(const direction_3d& that); | |
500 inline ~orientation_3d() { } | |
501 inline orientation_3d& operator=(const orientation_3d& ori) { | |
502 val_ = ori.val_; return * this; } | |
503 inline bool operator==(orientation_3d that) const { return (val_ == that.val_); } | |
504 inline bool operator!=(orientation_3d that) const { return (val_ != that.val_); } | |
505 inline unsigned int to_int() const { return (val_); } | |
506 inline direction_3d get_direction(direction_1d dir) const; | |
507 }; | |
508 | |
509 class direction_3d { | |
510 private: | |
511 int val_; | |
512 | |
513 public: | |
514 | |
515 inline direction_3d() : val_(WEST) {} | |
516 | |
517 inline direction_3d(direction_2d that) : val_(that.to_int()) {} | |
518 inline direction_3d(const direction_3d& that) : val_(that.val_) {} | |
519 | |
520 inline direction_3d(const direction_2d_enum val) : val_(val) {} | |
521 inline direction_3d(const direction_3d_enum val) : val_(val) {} | |
522 | |
523 inline direction_3d& operator=(direction_3d d) { | |
524 val_ = d.val_; | |
525 return * this; | |
526 } | |
527 | |
528 inline ~direction_3d() { } | |
529 | |
530 inline bool operator==(direction_3d d) const { return (val_ == d.val_); } | |
531 inline bool operator!=(direction_3d d) const { return !((*this) == d); } | |
532 inline bool operator< (direction_3d d) const { return (val_ < d.val_); } | |
533 inline bool operator<=(direction_3d d) const { return (val_ <= d.val_); } | |
534 inline bool operator> (direction_3d d) const { return (val_ > d.val_); } | |
535 inline bool operator>=(direction_3d d) const { return (val_ >= d.val_); } | |
536 | |
537 // Casting to int | |
538 inline unsigned int to_int(void) const { return val_; } | |
539 | |
540 inline direction_3d backward() const { | |
541 // flip the LSB, toggles 0 - 1 and 2 - 3 and 4 - 5 | |
542 return direction_2d(direction_2d_enum(val_ ^ 1)); | |
543 } | |
544 | |
545 // N, E, U are positive, S, W, D are negative | |
546 inline bool is_positive() const {return (val_ & 1);} | |
547 inline bool is_negative() const {return !is_positive();} | |
548 inline int get_sign() const {return ((is_positive()) << 1) -1;} | |
549 | |
550 }; | |
551 | |
552 direction_1d::direction_1d(const direction_3d& that) : val_(that.to_int() & 1) {} | |
553 orientation_3d::orientation_3d(const direction_3d& that) : val_(that.to_int() >> 1) {} | |
554 orientation_3d::orientation_3d(const direction_2d& that) : val_(that.to_int() >> 1) {} | |
555 | |
556 direction_3d orientation_3d::get_direction(direction_1d dir) const { | |
557 return direction_3d(direction_3d_enum((val_ << 1) + dir.to_int())); | |
558 } | |
559 | |
560 } | |
561 } | |
562 #endif |