polygon_set_view.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_SET_VIEW_HPP
  8. #define BOOST_POLYGON_POLYGON_SET_VIEW_HPP
  9. namespace boost { namespace polygon{
  10. template <typename coordinate_type>
  11. inline void polygon_set_data<coordinate_type>::clean() const {
  12. if(dirty_) {
  13. //polygon_45_set_data<coordinate_type> tmp;
  14. //very important:
  15. //the 45 degree algorithm does not satisfy
  16. //the precondition of arbitrary polygon formation
  17. //that vertices be "linearly consistent"
  18. //therefore it doesn't work to fall back on 45-degree
  19. //booleans for arbitrary angle polygons
  20. //if(0) { //downcast(tmp) ) {
  21. // tmp.clean();
  22. // data_.clear();
  23. // is_45_ = true;
  24. // polygon_set_data<coordinate_type> tmp2;
  25. // tmp2.insert(tmp);
  26. // data_.swap(tmp2.data_);
  27. // dirty_ = false;
  28. // sort();
  29. //} else {
  30. sort();
  31. arbitrary_boolean_op<coordinate_type> abo;
  32. polygon_set_data<coordinate_type> tmp2;
  33. abo.execute(tmp2, begin(), end(), end(), end(), 0);
  34. data_.swap(tmp2.data_);
  35. is_45_ = tmp2.is_45_;
  36. dirty_ = false;
  37. //}
  38. }
  39. }
  40. template <>
  41. inline void polygon_set_data<double>::clean() const {
  42. if(dirty_) {
  43. sort();
  44. arbitrary_boolean_op<double> abo;
  45. polygon_set_data<double> tmp2;
  46. abo.execute(tmp2, begin(), end(), end(), end(), 0);
  47. data_.swap(tmp2.data_);
  48. is_45_ = tmp2.is_45_;
  49. dirty_ = false;
  50. }
  51. }
  52. template <typename value_type, typename arg_type>
  53. inline void insert_into_view_arg(value_type& dest, const arg_type& arg);
  54. template <typename ltype, typename rtype, int op_type>
  55. class polygon_set_view;
  56. template <typename ltype, typename rtype, int op_type>
  57. struct polygon_set_traits<polygon_set_view<ltype, rtype, op_type> > {
  58. typedef typename polygon_set_view<ltype, rtype, op_type>::coordinate_type coordinate_type;
  59. typedef typename polygon_set_view<ltype, rtype, op_type>::iterator_type iterator_type;
  60. typedef typename polygon_set_view<ltype, rtype, op_type>::operator_arg_type operator_arg_type;
  61. static inline iterator_type begin(const polygon_set_view<ltype, rtype, op_type>& polygon_set);
  62. static inline iterator_type end(const polygon_set_view<ltype, rtype, op_type>& polygon_set);
  63. static inline bool clean(const polygon_set_view<ltype, rtype, op_type>& polygon_set);
  64. static inline bool sort(const polygon_set_view<ltype, rtype, op_type>& polygon_set);
  65. };
  66. //template <typename value_type, typename geometry_type_1, typename geometry_type_2, int op_type>
  67. //void execute_boolean_op(value_type& output_, const geometry_type_1& lvalue_, const geometry_type_2& rvalue_,
  68. // double coord) {
  69. // typedef geometry_type_1 ltype;
  70. // typedef geometry_type_2 rtype;
  71. // typedef typename polygon_set_traits<ltype>::coordinate_type coordinate_type;
  72. // value_type linput_;
  73. // value_type rinput_;
  74. // insert_into_view_arg(linput_, lvalue_);
  75. // insert_into_view_arg(rinput_, rvalue_);
  76. // arbitrary_boolean_op<coordinate_type> abo;
  77. // abo.execute(output_, linput_.begin(), linput_.end(),
  78. // rinput_.begin(), rinput_.end(), op_type);
  79. //}
  80. template <typename value_type, typename geometry_type_1, typename geometry_type_2, int op_type>
  81. void execute_boolean_op(value_type& output_, const geometry_type_1& lvalue_, const geometry_type_2& rvalue_) {
  82. typedef geometry_type_1 ltype;
  83. //typedef geometry_type_2 rtype;
  84. typedef typename polygon_set_traits<ltype>::coordinate_type coordinate_type;
  85. value_type linput_;
  86. value_type rinput_;
  87. insert_into_view_arg(linput_, lvalue_);
  88. insert_into_view_arg(rinput_, rvalue_);
  89. polygon_45_set_data<coordinate_type> l45, r45, o45;
  90. // if(linput_.downcast(l45) && rinput_.downcast(r45)) {
  91. // //the op codes are screwed up between 45 and arbitrary
  92. //#ifdef BOOST_POLYGON_MSVC
  93. //#pragma warning (push)
  94. //#pragma warning (disable: 4127)
  95. //#endif
  96. // if(op_type < 2)
  97. // l45.template applyAdaptiveBoolean_<op_type>(o45, r45);
  98. // else if(op_type == 2)
  99. // l45.template applyAdaptiveBoolean_<3>(o45, r45);
  100. // else
  101. // l45.template applyAdaptiveBoolean_<2>(o45, r45);
  102. //#ifdef BOOST_POLYGON_MSVC
  103. //#pragma warning (pop)
  104. //#endif
  105. // output_.insert(o45);
  106. // } else {
  107. arbitrary_boolean_op<coordinate_type> abo;
  108. abo.execute(output_, linput_.begin(), linput_.end(),
  109. rinput_.begin(), rinput_.end(), op_type);
  110. // }
  111. }
  112. template <typename ltype, typename rtype, int op_type>
  113. class polygon_set_view {
  114. public:
  115. typedef typename polygon_set_traits<ltype>::coordinate_type coordinate_type;
  116. typedef polygon_set_data<coordinate_type> value_type;
  117. typedef typename value_type::iterator_type iterator_type;
  118. typedef polygon_set_view operator_arg_type;
  119. private:
  120. const ltype& lvalue_;
  121. const rtype& rvalue_;
  122. mutable value_type output_;
  123. mutable bool evaluated_;
  124. polygon_set_view& operator=(const polygon_set_view&);
  125. public:
  126. polygon_set_view(const ltype& lvalue,
  127. const rtype& rvalue ) :
  128. lvalue_(lvalue), rvalue_(rvalue), output_(), evaluated_(false) {}
  129. // get iterator to begin vertex data
  130. public:
  131. const value_type& value() const {
  132. if(!evaluated_) {
  133. evaluated_ = true;
  134. execute_boolean_op<value_type, ltype, rtype, op_type>(output_, lvalue_, rvalue_);
  135. }
  136. return output_;
  137. }
  138. public:
  139. iterator_type begin() const { return value().begin(); }
  140. iterator_type end() const { return value().end(); }
  141. bool dirty() const { return false; } //result of a boolean is clean
  142. bool sorted() const { return true; } //result of a boolean is sorted
  143. void sort() const {} //is always sorted
  144. };
  145. template <typename ltype, typename rtype, int op_type>
  146. typename polygon_set_traits<polygon_set_view<ltype, rtype, op_type> >::iterator_type
  147. polygon_set_traits<polygon_set_view<ltype, rtype, op_type> >::
  148. begin(const polygon_set_view<ltype, rtype, op_type>& polygon_set) {
  149. return polygon_set.begin();
  150. }
  151. template <typename ltype, typename rtype, int op_type>
  152. typename polygon_set_traits<polygon_set_view<ltype, rtype, op_type> >::iterator_type
  153. polygon_set_traits<polygon_set_view<ltype, rtype, op_type> >::
  154. end(const polygon_set_view<ltype, rtype, op_type>& polygon_set) {
  155. return polygon_set.end();
  156. }
  157. template <typename ltype, typename rtype, int op_type>
  158. bool polygon_set_traits<polygon_set_view<ltype, rtype, op_type> >::
  159. clean(const polygon_set_view<ltype, rtype, op_type>& ) {
  160. return true; }
  161. template <typename ltype, typename rtype, int op_type>
  162. bool polygon_set_traits<polygon_set_view<ltype, rtype, op_type> >::
  163. sort(const polygon_set_view<ltype, rtype, op_type>& ) {
  164. return true; }
  165. template <typename value_type, typename arg_type>
  166. inline void insert_into_view_arg(value_type& dest, const arg_type& arg) {
  167. typedef typename polygon_set_traits<arg_type>::iterator_type literator;
  168. literator itr1, itr2;
  169. itr1 = polygon_set_traits<arg_type>::begin(arg);
  170. itr2 = polygon_set_traits<arg_type>::end(arg);
  171. dest.insert(itr1, itr2);
  172. }
  173. template <typename geometry_type_1, typename geometry_type_2, int op_type>
  174. geometry_type_1& self_assignment_boolean_op(geometry_type_1& lvalue_, const geometry_type_2& rvalue_) {
  175. typedef geometry_type_1 ltype;
  176. typedef typename polygon_set_traits<ltype>::coordinate_type coordinate_type;
  177. typedef polygon_set_data<coordinate_type> value_type;
  178. value_type output_;
  179. execute_boolean_op<value_type, geometry_type_1, geometry_type_2, op_type>(output_, lvalue_, rvalue_);
  180. polygon_set_mutable_traits<geometry_type_1>::set(lvalue_, output_.begin(), output_.end());
  181. return lvalue_;
  182. }
  183. // copy constructor
  184. template <typename coordinate_type>
  185. template <typename ltype, typename rtype, int op_type>
  186. polygon_set_data<coordinate_type>::polygon_set_data(const polygon_set_view<ltype, rtype, op_type>& that) :
  187. data_(that.value().data_), dirty_(that.value().dirty_), unsorted_(that.value().unsorted_), is_45_(that.value().is_45_) {}
  188. // equivalence operator
  189. template <typename coordinate_type>
  190. inline bool polygon_set_data<coordinate_type>::operator==(const polygon_set_data<coordinate_type>& p) const {
  191. typedef polygon_set_data<coordinate_type> value_type;
  192. value_type output_;
  193. execute_boolean_op<value_type, value_type, value_type, 2>(output_, (*this), p);
  194. return output_.data_.empty();
  195. }
  196. template <typename ltype, typename rtype, int op_type>
  197. struct geometry_concept<polygon_set_view<ltype, rtype, op_type> > { typedef polygon_set_concept type; };
  198. }
  199. }
  200. #endif