polygon_90_set_traits.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. Copyright 2008 Intel Corporation
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. */
  7. #ifndef BOOST_POLYGON_POLYGON_90_SET_TRAITS_HPP
  8. #define BOOST_POLYGON_POLYGON_90_SET_TRAITS_HPP
  9. namespace boost { namespace polygon{
  10. struct polygon_90_set_concept {};
  11. template <typename T, typename T2>
  12. struct traits_by_concept {};
  13. template <typename T>
  14. struct traits_by_concept<T, coordinate_concept> { typedef coordinate_traits<T> type; };
  15. template <typename T>
  16. struct traits_by_concept<T, interval_concept> { typedef interval_traits<T> type; };
  17. template <typename T>
  18. struct traits_by_concept<T, point_concept> { typedef point_traits<T> type; };
  19. template <typename T>
  20. struct traits_by_concept<T, rectangle_concept> { typedef rectangle_traits<T> type; };
  21. template <typename T>
  22. struct traits_by_concept<T, segment_concept> { typedef segment_traits<T> type; };
  23. template <typename T>
  24. struct traits_by_concept<T, polygon_90_concept> { typedef polygon_traits<T> type; };
  25. template <typename T>
  26. struct traits_by_concept<T, polygon_90_with_holes_concept> { typedef polygon_traits<T> type; };
  27. template <typename T>
  28. struct traits_by_concept<T, polygon_45_concept> { typedef polygon_traits<T> type; };
  29. template <typename T>
  30. struct traits_by_concept<T, polygon_45_with_holes_concept> { typedef polygon_traits<T> type; };
  31. template <typename T>
  32. struct traits_by_concept<T, polygon_concept> { typedef polygon_traits<T> type; };
  33. template <typename T>
  34. struct traits_by_concept<T, polygon_with_holes_concept> { typedef polygon_traits<T> type; };
  35. struct polygon_45_set_concept;
  36. struct polygon_set_concept;
  37. template <typename T>
  38. struct polygon_90_set_traits;
  39. template <typename T>
  40. struct polygon_45_set_traits;
  41. template <typename T>
  42. struct polygon_set_traits;
  43. template <typename T>
  44. struct traits_by_concept<T, polygon_90_set_concept> { typedef polygon_90_set_traits<T> type; };
  45. template <typename T>
  46. struct traits_by_concept<T, polygon_45_set_concept> { typedef polygon_45_set_traits<T> type; };
  47. template <typename T>
  48. struct traits_by_concept<T, polygon_set_concept> { typedef polygon_set_traits<T> type; };
  49. template <typename T, typename T2>
  50. struct get_coordinate_type {
  51. typedef typename traits_by_concept<T, T2>::type traits_type;
  52. typedef typename traits_type::coordinate_type type;
  53. };
  54. //want to prevent recursive template definition syntax errors, so duplicate get_coordinate_type
  55. template <typename T, typename T2>
  56. struct get_coordinate_type_2 {
  57. typedef typename traits_by_concept<T, T2>::type traits_type;
  58. typedef typename traits_type::coordinate_type type;
  59. };
  60. template <typename T>
  61. struct get_coordinate_type<T, undefined_concept> {
  62. typedef typename get_coordinate_type_2<typename std::iterator_traits
  63. <typename T::iterator>::value_type,
  64. typename geometry_concept<typename std::iterator_traits
  65. <typename T::iterator>::value_type>::type>::type type; };
  66. template <typename T, typename T2>
  67. struct get_iterator_type_2 {
  68. typedef const T* type;
  69. static type begin(const T& t) { return &t; }
  70. static type end(const T& t) { const T* tp = &t; ++tp; return tp; }
  71. };
  72. template <typename T>
  73. struct get_iterator_type {
  74. typedef get_iterator_type_2<T, typename geometry_concept<T>::type> indirect_type;
  75. typedef typename indirect_type::type type;
  76. static type begin(const T& t) { return indirect_type::begin(t); }
  77. static type end(const T& t) { return indirect_type::end(t); }
  78. };
  79. template <typename T>
  80. struct get_iterator_type_2<T, undefined_concept> {
  81. typedef typename T::const_iterator type;
  82. static type begin(const T& t) { return t.begin(); }
  83. static type end(const T& t) { return t.end(); }
  84. };
  85. // //helpers for allowing polygon 45 and containers of polygon 45 to behave interchangably in polygon_45_set_traits
  86. // template <typename T, typename T2>
  87. // struct get_coordinate_type_45 {};
  88. // template <typename T, typename T2>
  89. // struct get_coordinate_type_2_45 {};
  90. // template <typename T>
  91. // struct get_coordinate_type_45<T, void> {
  92. // typedef typename get_coordinate_type_2_45< typename T::value_type, typename geometry_concept<typename T::value_type>::type >::type type; };
  93. // template <typename T>
  94. // struct get_coordinate_type_45<T, polygon_45_concept> { typedef typename polygon_traits<T>::coordinate_type type; };
  95. // template <typename T>
  96. // struct get_coordinate_type_45<T, polygon_45_with_holes_concept> { typedef typename polygon_traits<T>::coordinate_type type; };
  97. // template <typename T>
  98. // struct get_coordinate_type_2_45<T, polygon_45_concept> { typedef typename polygon_traits<T>::coordinate_type type; };
  99. // template <typename T>
  100. // struct get_coordinate_type_2_45<T, polygon_45_with_holes_concept> { typedef typename polygon_traits<T>::coordinate_type type; };
  101. // template <typename T, typename T2>
  102. // struct get_iterator_type_45 {};
  103. // template <typename T>
  104. // struct get_iterator_type_45<T, void> {
  105. // typedef typename T::const_iterator type;
  106. // static type begin(const T& t) { return t.begin(); }
  107. // static type end(const T& t) { return t.end(); }
  108. // };
  109. // template <typename T>
  110. // struct get_iterator_type_45<T, polygon_45_concept> {
  111. // typedef const T* type;
  112. // static type begin(const T& t) { return &t; }
  113. // static type end(const T& t) { const T* tp = &t; ++tp; return tp; }
  114. // };
  115. // template <typename T>
  116. // struct get_iterator_type_45<T, polygon_45_with_holes_concept> {
  117. // typedef const T* type;
  118. // static type begin(const T& t) { return &t; }
  119. // static type end(const T& t) { const T* tp = &t; ++tp; return tp; }
  120. // };
  121. // template <typename T>
  122. // struct get_iterator_type_45<T, polygon_90_set_concept> {
  123. // typedef const T* type;
  124. // static type begin(const T& t) { return &t; }
  125. // static type end(const T& t) { const T* tp = &t; ++tp; return tp; }
  126. // };
  127. template <typename T>
  128. struct polygon_90_set_traits {
  129. typedef typename get_coordinate_type<T, typename geometry_concept<T>::type >::type coordinate_type;
  130. typedef get_iterator_type<T> indirection_type;
  131. typedef typename get_iterator_type<T>::type iterator_type;
  132. typedef T operator_arg_type;
  133. static inline iterator_type begin(const T& polygon_set) {
  134. return indirection_type::begin(polygon_set);
  135. }
  136. static inline iterator_type end(const T& polygon_set) {
  137. return indirection_type::end(polygon_set);
  138. }
  139. static inline orientation_2d orient(const T&) { return HORIZONTAL; }
  140. static inline bool clean(const T&) { return false; }
  141. static inline bool sorted(const T&) { return false; }
  142. };
  143. template <typename T>
  144. struct is_manhattan_polygonal_concept { typedef gtl_no type; };
  145. template <>
  146. struct is_manhattan_polygonal_concept<rectangle_concept> { typedef gtl_yes type; };
  147. template <>
  148. struct is_manhattan_polygonal_concept<polygon_90_concept> { typedef gtl_yes type; };
  149. template <>
  150. struct is_manhattan_polygonal_concept<polygon_90_with_holes_concept> { typedef gtl_yes type; };
  151. template <>
  152. struct is_manhattan_polygonal_concept<polygon_90_set_concept> { typedef gtl_yes type; };
  153. template <typename T>
  154. struct is_polygon_90_set_type {
  155. typedef typename is_manhattan_polygonal_concept<typename geometry_concept<T>::type>::type type;
  156. };
  157. template <typename T>
  158. struct is_polygon_90_set_type<std::list<T> > {
  159. typedef typename gtl_or<
  160. typename is_manhattan_polygonal_concept<typename geometry_concept<std::list<T> >::type>::type,
  161. typename is_manhattan_polygonal_concept<typename geometry_concept<typename std::list<T>::value_type>::type>::type>::type type;
  162. };
  163. template <typename T>
  164. struct is_polygon_90_set_type<std::vector<T> > {
  165. typedef typename gtl_or<
  166. typename is_manhattan_polygonal_concept<typename geometry_concept<std::vector<T> >::type>::type,
  167. typename is_manhattan_polygonal_concept<typename geometry_concept<typename std::vector<T>::value_type>::type>::type>::type type;
  168. };
  169. template <typename T>
  170. struct is_mutable_polygon_90_set_type {
  171. typedef typename gtl_same_type<polygon_90_set_concept, typename geometry_concept<T>::type>::type type;
  172. };
  173. template <typename T>
  174. struct is_mutable_polygon_90_set_type<std::list<T> > {
  175. typedef typename gtl_or<
  176. typename gtl_same_type<polygon_90_set_concept, typename geometry_concept<std::list<T> >::type>::type,
  177. typename is_manhattan_polygonal_concept<typename geometry_concept<typename std::list<T>::value_type>::type>::type>::type type;
  178. };
  179. template <typename T>
  180. struct is_mutable_polygon_90_set_type<std::vector<T> > {
  181. typedef typename gtl_or<
  182. typename gtl_same_type<polygon_90_set_concept, typename geometry_concept<std::vector<T> >::type>::type,
  183. typename is_manhattan_polygonal_concept<typename geometry_concept<typename std::vector<T>::value_type>::type>::type>::type type;
  184. };
  185. // //specialization for rectangle, polygon_90 and polygon_90_with_holes types
  186. // template <typename T>
  187. // struct polygon_90_set_traits
  188. // typedef typename geometry_concept<T>::type concept_type;
  189. // typedef typename get_coordinate_type<T, concept_type>::type coordinate_type;
  190. // typedef iterator_geometry_to_set<concept_type, T> iterator_type;
  191. // typedef T operator_arg_type;
  192. // static inline iterator_type begin(const T& polygon_set) {
  193. // return iterator_geometry_to_set<concept_type, T>(polygon_set, LOW, HORIZONTAL);
  194. // }
  195. // static inline iterator_type end(const T& polygon_set) {
  196. // return iterator_geometry_to_set<concept_type, T>(polygon_set, HIGH, HORIZONTAL);
  197. // }
  198. // static inline orientation_2d orient(const T& polygon_set) { return HORIZONTAL; }
  199. // static inline bool clean(const T& polygon_set) { return false; }
  200. // static inline bool sorted(const T& polygon_set) { return false; }
  201. // };
  202. // //specialization for containers of recangle, polygon_90, polygon_90_with_holes
  203. // template <typename T>
  204. // struct polygon_90_set_traits<T, typename is_manhattan_polygonal_concept<typename std::iterator_traits<typename T::iterator>::value_type>::type> {
  205. // typedef typename std::iterator_traits<typename T::iterator>::value_type geometry_type;
  206. // typedef typename geometry_concept<geometry_type>::type concept_type;
  207. // typedef typename get_coordinate_type<geometry_type, concept_type>::type coordinate_type;
  208. // typedef iterator_geometry_range_to_set<concept_type, typename T::const_iterator> iterator_type;
  209. // typedef T operator_arg_type;
  210. // static inline iterator_type begin(const T& polygon_set) {
  211. // return iterator_type(polygon_set.begin(), HORIZONTAL);
  212. // }
  213. // static inline iterator_type end(const T& polygon_set) {
  214. // return iterator_type(polygon_set.end(), HORIZONTAL);
  215. // }
  216. // static inline orientation_2d orient(const T& polygon_set) { return HORIZONTAL; }
  217. // static inline bool clean(const T& polygon_set) { return false; }
  218. // static inline bool sorted(const T& polygon_set) { return false; }
  219. // };
  220. //get dispatch functions
  221. template <typename output_container_type, typename pst>
  222. void get_90_dispatch(output_container_type& output, const pst& ps,
  223. orientation_2d orient, rectangle_concept ) {
  224. form_rectangles(output, ps.begin(), ps.end(), orient, rectangle_concept());
  225. }
  226. template <typename output_container_type, typename pst>
  227. void get_90_dispatch(output_container_type& output, const pst& ps,
  228. orientation_2d orient, polygon_90_concept tag) {
  229. get_polygons(output, ps.begin(), ps.end(), orient, true, tag);
  230. }
  231. template <typename output_container_type, typename pst>
  232. void get_90_dispatch(output_container_type& output, const pst& ps,
  233. orientation_2d orient, polygon_90_with_holes_concept tag) {
  234. get_polygons(output, ps.begin(), ps.end(), orient, false, tag);
  235. }
  236. //by default works with containers of rectangle, polygon or polygon with holes
  237. //must be specialized to work with anything else
  238. template <typename T>
  239. struct polygon_90_set_mutable_traits {};
  240. template <typename T>
  241. struct polygon_90_set_mutable_traits<std::list<T> > {
  242. typedef typename geometry_concept<T>::type concept_type;
  243. template <typename input_iterator_type>
  244. static inline void set(std::list<T>& polygon_set, input_iterator_type input_begin, input_iterator_type input_end, orientation_2d orient) {
  245. polygon_set.clear();
  246. polygon_90_set_data<typename polygon_90_set_traits<std::list<T> >::coordinate_type> ps(orient);
  247. ps.reserve(std::distance(input_begin, input_end));
  248. ps.insert(input_begin, input_end, orient);
  249. ps.clean();
  250. get_90_dispatch(polygon_set, ps, orient, concept_type());
  251. }
  252. };
  253. template <typename T>
  254. struct polygon_90_set_mutable_traits<std::vector<T> > {
  255. typedef typename geometry_concept<T>::type concept_type;
  256. template <typename input_iterator_type>
  257. static inline void set(std::vector<T>& polygon_set, input_iterator_type input_begin, input_iterator_type input_end, orientation_2d orient) {
  258. polygon_set.clear();
  259. size_t num_ele = std::distance(input_begin, input_end);
  260. polygon_set.reserve(num_ele);
  261. polygon_90_set_data<typename polygon_90_set_traits<std::list<T> >::coordinate_type> ps(orient);
  262. ps.reserve(num_ele);
  263. ps.insert(input_begin, input_end, orient);
  264. ps.clean();
  265. get_90_dispatch(polygon_set, ps, orient, concept_type());
  266. }
  267. };
  268. template <typename T>
  269. struct polygon_90_set_mutable_traits<polygon_90_set_data<T> > {
  270. template <typename input_iterator_type>
  271. static inline void set(polygon_90_set_data<T>& polygon_set,
  272. input_iterator_type input_begin, input_iterator_type input_end,
  273. orientation_2d orient) {
  274. polygon_set.clear();
  275. polygon_set.reserve(std::distance(input_begin, input_end));
  276. polygon_set.insert(input_begin, input_end, orient);
  277. }
  278. };
  279. template <typename T>
  280. struct polygon_90_set_traits<polygon_90_set_data<T> > {
  281. typedef typename polygon_90_set_data<T>::coordinate_type coordinate_type;
  282. typedef typename polygon_90_set_data<T>::iterator_type iterator_type;
  283. typedef typename polygon_90_set_data<T>::operator_arg_type operator_arg_type;
  284. static inline iterator_type begin(const polygon_90_set_data<T>& polygon_set) {
  285. return polygon_set.begin();
  286. }
  287. static inline iterator_type end(const polygon_90_set_data<T>& polygon_set) {
  288. return polygon_set.end();
  289. }
  290. static inline orientation_2d orient(const polygon_90_set_data<T>& polygon_set) { return polygon_set.orient(); }
  291. static inline bool clean(const polygon_90_set_data<T>& polygon_set) { polygon_set.clean(); return true; }
  292. static inline bool sorted(const polygon_90_set_data<T>& polygon_set) { polygon_set.sort(); return true; }
  293. };
  294. template <typename T>
  295. struct is_polygon_90_set_concept { };
  296. template <>
  297. struct is_polygon_90_set_concept<polygon_90_set_concept> { typedef gtl_yes type; };
  298. template <>
  299. struct is_polygon_90_set_concept<rectangle_concept> { typedef gtl_yes type; };
  300. template <>
  301. struct is_polygon_90_set_concept<polygon_90_concept> { typedef gtl_yes type; };
  302. template <>
  303. struct is_polygon_90_set_concept<polygon_90_with_holes_concept> { typedef gtl_yes type; };
  304. template <typename T>
  305. struct is_mutable_polygon_90_set_concept { typedef gtl_no type; };
  306. template <>
  307. struct is_mutable_polygon_90_set_concept<polygon_90_set_concept> { typedef gtl_yes type; };
  308. template <typename T>
  309. struct geometry_concept<polygon_90_set_data<T> > { typedef polygon_90_set_concept type; };
  310. //template <typename T>
  311. //typename enable_if<typename is_polygon_90_set_type<T>::type, void>::type
  312. //print_is_polygon_90_set_concept(const T& t) { std::cout << "is polygon 90 set concept\n"; }
  313. //template <typename T>
  314. //typename enable_if<typename is_mutable_polygon_90_set_type<T>::type, void>::type
  315. //print_is_mutable_polygon_90_set_concept(const T& t) { std::cout << "is mutable polygon 90 set concept\n"; }
  316. }
  317. }
  318. #endif