holes_proxy.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2010-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2020.
  4. // Modifications copyright (c) 2020, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_POLYGON_HOLES_PROXY_HPP
  10. #define BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_POLYGON_HOLES_PROXY_HPP
  11. // Adapts Geometries from Boost.Polygon for usage in Boost.Geometry
  12. // boost::polygon::polygon_with_holes_data -> boost::geometry::polygon
  13. // pair{begin_holes, begin_holes} -> interior_proxy
  14. #include <boost/polygon/polygon.hpp>
  15. #include <boost/geometry/geometries/adapted/boost_polygon/hole_iterator.hpp>
  16. #include <boost/geometry/geometries/adapted/boost_polygon/ring_proxy.hpp>
  17. namespace boost { namespace geometry
  18. {
  19. namespace adapt { namespace bp
  20. {
  21. // Polygon should implement the boost::polygon::polygon_with_holes_concept
  22. // Specify constness in the template parameter if necessary
  23. template<typename Polygon>
  24. struct holes_proxy
  25. {
  26. typedef ring_proxy
  27. <
  28. std::conditional_t
  29. <
  30. std::is_const<Polygon>::value,
  31. Polygon const,
  32. Polygon
  33. >
  34. > proxy_type;
  35. typedef hole_iterator<Polygon, proxy_type> iterator_type;
  36. // The next line does not work probably because coordinate_type is part of the
  37. // polygon_traits, but not of the polygon_with_holes_traits
  38. // typedef typename boost::polygon::polygon_traits<Polygon>::coordinate_type coordinate_type;
  39. // So we use:
  40. typedef typename Polygon::coordinate_type coordinate_type;
  41. inline holes_proxy(Polygon& p)
  42. : polygon(p)
  43. {}
  44. inline void clear()
  45. {
  46. Polygon empty;
  47. // Clear the holes
  48. polygon.set_holes
  49. (
  50. boost::polygon::begin_holes(empty),
  51. boost::polygon::end_holes(empty)
  52. );
  53. }
  54. inline void resize(std::size_t new_size)
  55. {
  56. std::vector<boost::polygon::polygon_data<coordinate_type> > temporary_copy
  57. (
  58. boost::polygon::begin_holes(polygon),
  59. boost::polygon::end_holes(polygon)
  60. );
  61. temporary_copy.resize(new_size);
  62. polygon.set_holes(temporary_copy.begin(), temporary_copy.end());
  63. }
  64. template <typename Ring>
  65. inline void push_back(Ring const& ring)
  66. {
  67. std::vector<boost::polygon::polygon_data<coordinate_type> > temporary_copy
  68. (
  69. boost::polygon::begin_holes(polygon),
  70. boost::polygon::end_holes(polygon)
  71. );
  72. boost::polygon::polygon_data<coordinate_type> added;
  73. boost::polygon::set_points(added, ring.begin(), ring.end());
  74. temporary_copy.push_back(added);
  75. polygon.set_holes(temporary_copy.begin(), temporary_copy.end());
  76. }
  77. Polygon& polygon;
  78. };
  79. // Support holes_proxy for Boost.Range ADP
  80. // Const versions
  81. template<typename Polygon>
  82. inline typename boost::geometry::adapt::bp::holes_proxy<Polygon const>::iterator_type
  83. range_begin(boost::geometry::adapt::bp::holes_proxy<Polygon const> const& proxy)
  84. {
  85. typename boost::geometry::adapt::bp::holes_proxy<Polygon const>::iterator_type
  86. begin(proxy.polygon, boost::polygon::begin_holes(proxy.polygon));
  87. return begin;
  88. }
  89. template<typename Polygon>
  90. inline typename boost::geometry::adapt::bp::holes_proxy<Polygon const>::iterator_type
  91. range_end(boost::geometry::adapt::bp::holes_proxy<Polygon const> const& proxy)
  92. {
  93. typename boost::geometry::adapt::bp::holes_proxy<Polygon const>::iterator_type
  94. end(proxy.polygon, boost::polygon::end_holes(proxy.polygon));
  95. return end;
  96. }
  97. // Mutable versions
  98. template<typename Polygon>
  99. inline typename boost::geometry::adapt::bp::holes_proxy<Polygon>::iterator_type
  100. range_begin(boost::geometry::adapt::bp::holes_proxy<Polygon>& proxy)
  101. {
  102. typename boost::geometry::adapt::bp::holes_proxy<Polygon>::iterator_type
  103. begin(proxy.polygon, boost::polygon::begin_holes(proxy.polygon));
  104. return begin;
  105. }
  106. template<typename Polygon>
  107. inline typename boost::geometry::adapt::bp::holes_proxy<Polygon>::iterator_type
  108. range_end(boost::geometry::adapt::bp::holes_proxy<Polygon>& proxy)
  109. {
  110. typename boost::geometry::adapt::bp::holes_proxy<Polygon>::iterator_type
  111. end(proxy.polygon, boost::polygon::end_holes(proxy.polygon));
  112. return end;
  113. }
  114. }}
  115. namespace traits
  116. {
  117. template <typename Polygon>
  118. struct rvalue_type<adapt::bp::holes_proxy<Polygon> >
  119. {
  120. typedef adapt::bp::holes_proxy<Polygon> type;
  121. };
  122. template <typename Polygon>
  123. struct clear<adapt::bp::holes_proxy<Polygon> >
  124. {
  125. static inline void apply(adapt::bp::holes_proxy<Polygon> proxy)
  126. {
  127. proxy.clear();
  128. }
  129. };
  130. template <typename Polygon>
  131. struct resize<adapt::bp::holes_proxy<Polygon> >
  132. {
  133. static inline void apply(adapt::bp::holes_proxy<Polygon> proxy, std::size_t new_size)
  134. {
  135. proxy.resize(new_size);
  136. }
  137. };
  138. template <typename Polygon>
  139. struct push_back<adapt::bp::holes_proxy<Polygon> >
  140. {
  141. template <typename Ring>
  142. static inline void apply(adapt::bp::holes_proxy<Polygon> proxy, Ring const& ring)
  143. {
  144. proxy.push_back(ring);
  145. }
  146. };
  147. } // namespace traits
  148. }}
  149. // Specialize holes_proxy for Boost.Range
  150. namespace boost
  151. {
  152. template<typename Polygon>
  153. struct range_mutable_iterator<geometry::adapt::bp::holes_proxy<Polygon> >
  154. {
  155. typedef typename geometry::adapt::bp::holes_proxy<Polygon>::iterator_type type;
  156. };
  157. template<typename Polygon>
  158. struct range_const_iterator<geometry::adapt::bp::holes_proxy<Polygon> >
  159. {
  160. typedef typename geometry::adapt::bp::holes_proxy<Polygon const>::iterator_type type;
  161. };
  162. } // namespace boost
  163. #endif // BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_POLYGON_HOLES_PROXY_HPP