buffer_policies.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2012-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2017-2020.
  4. // Modifications copyright (c) 2017-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_ALGORITHMS_DETAIL_BUFFER_BUFFER_POLICIES_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_BUFFER_POLICIES_HPP
  11. #include <cstddef>
  12. #include <boost/range/value_type.hpp>
  13. #include <boost/geometry/core/coordinate_type.hpp>
  14. #include <boost/geometry/core/point_type.hpp>
  15. #include <boost/geometry/algorithms/detail/overlay/backtrack_check_si.hpp>
  16. #include <boost/geometry/algorithms/detail/overlay/traversal_info.hpp>
  17. #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
  18. #include <boost/geometry/strategies/buffer.hpp>
  19. namespace boost { namespace geometry
  20. {
  21. #ifndef DOXYGEN_NO_DETAIL
  22. namespace detail { namespace buffer
  23. {
  24. class backtrack_for_buffer
  25. {
  26. public :
  27. typedef detail::overlay::backtrack_state state_type;
  28. template
  29. <
  30. typename Operation,
  31. typename Rings,
  32. typename Turns,
  33. typename Geometry,
  34. typename Strategy,
  35. typename RobustPolicy,
  36. typename Visitor
  37. >
  38. static inline void apply(std::size_t size_at_start,
  39. Rings& rings, typename boost::range_value<Rings>::type& ring,
  40. Turns& turns,
  41. typename boost::range_value<Turns>::type const& /*turn*/,
  42. Operation& operation,
  43. detail::overlay::traverse_error_type /*traverse_error*/,
  44. Geometry const& ,
  45. Geometry const& ,
  46. Strategy const& ,
  47. RobustPolicy const& ,
  48. state_type& state,
  49. Visitor& /*visitor*/
  50. )
  51. {
  52. #if defined(BOOST_GEOMETRY_COUNT_BACKTRACK_WARNINGS)
  53. extern int g_backtrack_warning_count;
  54. g_backtrack_warning_count++;
  55. #endif
  56. //std::cout << "!";
  57. //std::cout << "WARNING " << traverse_error_string(traverse_error) << std::endl;
  58. state.m_good = false;
  59. // Make bad output clean
  60. rings.resize(size_at_start);
  61. ring.clear();
  62. // Reject this as a starting point
  63. operation.visited.set_rejected();
  64. // And clear all visit info
  65. clear_visit_info(turns);
  66. }
  67. };
  68. struct buffer_overlay_visitor
  69. {
  70. public :
  71. void print(char const* /*header*/)
  72. {
  73. }
  74. template <typename Turns>
  75. void print(char const* /*header*/, Turns const& /*turns*/, int /*turn_index*/)
  76. {
  77. }
  78. template <typename Turns>
  79. void print(char const* /*header*/, Turns const& /*turns*/, int /*turn_index*/, int /*op_index*/)
  80. {
  81. }
  82. template <typename Turns>
  83. void visit_turns(int , Turns const& ) {}
  84. template <typename Clusters, typename Turns>
  85. void visit_clusters(Clusters const& , Turns const& ) {}
  86. template <typename Turns, typename Turn, typename Operation>
  87. void visit_traverse(Turns const& /*turns*/, Turn const& /*turn*/, Operation const& /*op*/, const char* /*header*/)
  88. {
  89. }
  90. template <typename Turns, typename Turn, typename Operation>
  91. void visit_traverse_reject(Turns const& , Turn const& , Operation const& ,
  92. detail::overlay::traverse_error_type )
  93. {}
  94. template <typename Rings>
  95. void visit_generated_rings(Rings const& )
  96. {}
  97. };
  98. // Should follow traversal-turn-concept (enrichment, visit structure)
  99. // and adds index in piece vector to find it back
  100. template <typename Point, typename SegmentRatio>
  101. struct buffer_turn_operation
  102. : public detail::overlay::traversal_turn_operation<Point, SegmentRatio>
  103. {
  104. signed_size_type piece_index;
  105. signed_size_type index_in_robust_ring;
  106. inline buffer_turn_operation()
  107. : piece_index(-1)
  108. , index_in_robust_ring(-1)
  109. {}
  110. };
  111. // Version of turn_info for buffer with its turn index and other helper variables
  112. template <typename Point, typename SegmentRatio>
  113. struct buffer_turn_info
  114. : public detail::overlay::turn_info
  115. <
  116. Point,
  117. SegmentRatio,
  118. buffer_turn_operation<Point, SegmentRatio>
  119. >
  120. {
  121. typedef Point point_type;
  122. std::size_t turn_index;
  123. // Information if turn can be used. It is not traversable if it is within
  124. // another piece, or within the original (depending on deflation),
  125. // or (for deflate) if there are not enough points to traverse it.
  126. bool is_turn_traversable;
  127. bool is_linear_end_point;
  128. bool within_original;
  129. signed_size_type count_in_original; // increased by +1 for in ext.ring, -1 for int.ring
  130. inline buffer_turn_info()
  131. : turn_index(0)
  132. , is_turn_traversable(true)
  133. , is_linear_end_point(false)
  134. , within_original(false)
  135. , count_in_original(0)
  136. {}
  137. };
  138. struct buffer_less
  139. {
  140. template <typename Indexed>
  141. inline bool operator()(Indexed const& left, Indexed const& right) const
  142. {
  143. if (! (left.subject->seg_id == right.subject->seg_id))
  144. {
  145. return left.subject->seg_id < right.subject->seg_id;
  146. }
  147. // Both left and right are located on the SAME segment.
  148. if (! (left.subject->fraction == right.subject->fraction))
  149. {
  150. return left.subject->fraction < right.subject->fraction;
  151. }
  152. return left.turn_index < right.turn_index;
  153. }
  154. };
  155. template <typename Strategy>
  156. struct piece_get_box
  157. {
  158. explicit piece_get_box(Strategy const& strategy)
  159. : m_strategy(strategy)
  160. {}
  161. template <typename Box, typename Piece>
  162. inline void apply(Box& total, Piece const& piece) const
  163. {
  164. assert_coordinate_type_equal(total, piece.m_piece_border.m_envelope);
  165. if (piece.m_piece_border.m_has_envelope)
  166. {
  167. geometry::expand(total, piece.m_piece_border.m_envelope,
  168. m_strategy);
  169. }
  170. }
  171. Strategy const& m_strategy;
  172. };
  173. template <typename Strategy>
  174. struct piece_overlaps_box
  175. {
  176. explicit piece_overlaps_box(Strategy const& strategy)
  177. : m_strategy(strategy)
  178. {}
  179. template <typename Box, typename Piece>
  180. inline bool apply(Box const& box, Piece const& piece) const
  181. {
  182. assert_coordinate_type_equal(box, piece.m_piece_border.m_envelope);
  183. if (piece.type == strategy::buffer::buffered_flat_end
  184. || piece.type == strategy::buffer::buffered_concave)
  185. {
  186. // Turns cannot be inside a flat end (though they can be on border)
  187. // Neither we need to check if they are inside concave helper pieces
  188. // Skip all pieces not used as soon as possible
  189. return false;
  190. }
  191. if (! piece.m_piece_border.m_has_envelope)
  192. {
  193. return false;
  194. }
  195. return ! geometry::detail::disjoint::disjoint_box_box(box, piece.m_piece_border.m_envelope,
  196. m_strategy);
  197. }
  198. Strategy const& m_strategy;
  199. };
  200. template <typename Strategy>
  201. struct turn_get_box
  202. {
  203. explicit turn_get_box(Strategy const& strategy)
  204. : m_strategy(strategy)
  205. {}
  206. template <typename Box, typename Turn>
  207. inline void apply(Box& total, Turn const& turn) const
  208. {
  209. assert_coordinate_type_equal(total, turn.point);
  210. geometry::expand(total, turn.point, m_strategy);
  211. }
  212. Strategy const& m_strategy;
  213. };
  214. template <typename Strategy>
  215. struct turn_overlaps_box
  216. {
  217. explicit turn_overlaps_box(Strategy const& strategy)
  218. : m_strategy(strategy)
  219. {}
  220. template <typename Box, typename Turn>
  221. inline bool apply(Box const& box, Turn const& turn) const
  222. {
  223. assert_coordinate_type_equal(turn.point, box);
  224. return ! geometry::detail::disjoint::disjoint_point_box(turn.point, box,
  225. m_strategy);
  226. }
  227. Strategy const& m_strategy;
  228. };
  229. }} // namespace detail::buffer
  230. #endif // DOXYGEN_NO_DETAIL
  231. }} // namespace boost::geometry
  232. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_BUFFER_POLICIES_HPP