get_piece_turns.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2012-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2017-2020.
  5. // Modifications copyright (c) 2017-2020 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_GET_PIECE_TURNS_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_GET_PIECE_TURNS_HPP
  12. #include <boost/core/ignore_unused.hpp>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/range/value_type.hpp>
  16. #include <boost/geometry/core/assert.hpp>
  17. #include <boost/geometry/algorithms/equals.hpp>
  18. #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/segment_identifier.hpp>
  20. #include <boost/geometry/algorithms/detail/overlay/get_turn_info.hpp>
  21. #include <boost/geometry/algorithms/detail/sections/section_functions.hpp>
  22. #include <boost/geometry/algorithms/detail/buffer/buffer_policies.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail { namespace buffer
  27. {
  28. // Implements a unique_sub_range for a buffered piece,
  29. // the range can return subsequent points
  30. // known as "i", "j" and "k" (and further), indexed as 0,1,2,3
  31. template <typename Ring>
  32. struct unique_sub_range_from_piece
  33. {
  34. typedef typename boost::range_iterator<Ring const>::type iterator_type;
  35. typedef typename geometry::point_type<Ring const>::type point_type;
  36. unique_sub_range_from_piece(Ring const& ring,
  37. iterator_type iterator_at_i, iterator_type iterator_at_j)
  38. : m_ring(ring)
  39. , m_iterator_at_i(iterator_at_i)
  40. , m_iterator_at_j(iterator_at_j)
  41. , m_point_retrieved(false)
  42. {}
  43. static inline bool is_first_segment() { return false; }
  44. static inline bool is_last_segment() { return false; }
  45. static inline std::size_t size() { return 3u; }
  46. inline point_type const& at(std::size_t index) const
  47. {
  48. BOOST_GEOMETRY_ASSERT(index < size());
  49. switch (index)
  50. {
  51. case 0 : return *m_iterator_at_i;
  52. case 1 : return *m_iterator_at_j;
  53. case 2 : return get_point_k();
  54. default : return *m_iterator_at_i;
  55. }
  56. }
  57. private :
  58. inline point_type const& get_point_k() const
  59. {
  60. if (! m_point_retrieved)
  61. {
  62. m_iterator_at_k = advance_one(m_iterator_at_j);
  63. m_point_retrieved = true;
  64. }
  65. return *m_iterator_at_k;
  66. }
  67. inline void circular_advance_one(iterator_type& next) const
  68. {
  69. ++next;
  70. if (next == boost::end(m_ring))
  71. {
  72. next = boost::begin(m_ring) + 1;
  73. }
  74. }
  75. inline iterator_type advance_one(iterator_type it) const
  76. {
  77. iterator_type result = it;
  78. circular_advance_one(result);
  79. // TODO: we could also use piece-boundaries
  80. // to check if the point equals the last one
  81. while (geometry::equals(*it, *result))
  82. {
  83. circular_advance_one(result);
  84. }
  85. return result;
  86. }
  87. Ring const& m_ring;
  88. iterator_type m_iterator_at_i;
  89. iterator_type m_iterator_at_j;
  90. mutable iterator_type m_iterator_at_k;
  91. mutable bool m_point_retrieved;
  92. };
  93. template
  94. <
  95. typename Pieces,
  96. typename Rings,
  97. typename Turns,
  98. typename Strategy,
  99. typename RobustPolicy
  100. >
  101. class piece_turn_visitor
  102. {
  103. Pieces const& m_pieces;
  104. Rings const& m_rings;
  105. Turns& m_turns;
  106. Strategy const& m_strategy;
  107. RobustPolicy const& m_robust_policy;
  108. template <typename Piece>
  109. inline bool is_adjacent(Piece const& piece1, Piece const& piece2) const
  110. {
  111. if (piece1.first_seg_id.multi_index != piece2.first_seg_id.multi_index)
  112. {
  113. return false;
  114. }
  115. return piece1.index == piece2.left_index
  116. || piece1.index == piece2.right_index;
  117. }
  118. template <typename Piece>
  119. inline bool is_on_same_convex_ring(Piece const& piece1, Piece const& piece2) const
  120. {
  121. if (piece1.first_seg_id.multi_index != piece2.first_seg_id.multi_index)
  122. {
  123. return false;
  124. }
  125. return ! m_rings[piece1.first_seg_id.multi_index].has_concave;
  126. }
  127. template <std::size_t Dimension, typename Iterator, typename Box>
  128. inline void move_begin_iterator(Iterator& it_begin, Iterator it_beyond,
  129. signed_size_type& index, int dir,
  130. Box const& this_bounding_box,
  131. Box const& other_bounding_box)
  132. {
  133. for(; it_begin != it_beyond
  134. && it_begin + 1 != it_beyond
  135. && detail::section::preceding<Dimension>(dir, *(it_begin + 1),
  136. this_bounding_box,
  137. other_bounding_box,
  138. m_robust_policy);
  139. ++it_begin, index++)
  140. {}
  141. }
  142. template <std::size_t Dimension, typename Iterator, typename Box>
  143. inline void move_end_iterator(Iterator it_begin, Iterator& it_beyond,
  144. int dir, Box const& this_bounding_box,
  145. Box const& other_bounding_box)
  146. {
  147. while (it_beyond != it_begin
  148. && it_beyond - 1 != it_begin
  149. && it_beyond - 2 != it_begin)
  150. {
  151. if (detail::section::exceeding<Dimension>(dir, *(it_beyond - 2),
  152. this_bounding_box, other_bounding_box, m_robust_policy))
  153. {
  154. --it_beyond;
  155. }
  156. else
  157. {
  158. return;
  159. }
  160. }
  161. }
  162. template <typename Piece, typename Section>
  163. inline void calculate_turns(Piece const& piece1, Piece const& piece2,
  164. Section const& section1, Section const& section2)
  165. {
  166. typedef typename boost::range_value<Rings const>::type ring_type;
  167. typedef typename boost::range_value<Turns const>::type turn_type;
  168. signed_size_type const piece1_first_index = piece1.first_seg_id.segment_index;
  169. signed_size_type const piece2_first_index = piece2.first_seg_id.segment_index;
  170. if (piece1_first_index < 0 || piece2_first_index < 0)
  171. {
  172. return;
  173. }
  174. // Get indices of part of offsetted_rings for this monotonic section:
  175. signed_size_type const sec1_first_index = piece1_first_index + section1.begin_index;
  176. signed_size_type const sec2_first_index = piece2_first_index + section2.begin_index;
  177. // index of last point in section, beyond-end is one further
  178. signed_size_type const sec1_last_index = piece1_first_index + section1.end_index;
  179. signed_size_type const sec2_last_index = piece2_first_index + section2.end_index;
  180. // get geometry and iterators over these sections
  181. ring_type const& ring1 = m_rings[piece1.first_seg_id.multi_index];
  182. auto it1_first = boost::begin(ring1) + sec1_first_index;
  183. auto it1_beyond = boost::begin(ring1) + sec1_last_index + 1;
  184. ring_type const& ring2 = m_rings[piece2.first_seg_id.multi_index];
  185. auto it2_first = boost::begin(ring2) + sec2_first_index;
  186. auto it2_beyond = boost::begin(ring2) + sec2_last_index + 1;
  187. // Set begin/end of monotonic ranges, in both x/y directions
  188. signed_size_type index1 = sec1_first_index;
  189. move_begin_iterator<0>(it1_first, it1_beyond, index1,
  190. section1.directions[0], section1.bounding_box, section2.bounding_box);
  191. move_end_iterator<0>(it1_first, it1_beyond,
  192. section1.directions[0], section1.bounding_box, section2.bounding_box);
  193. move_begin_iterator<1>(it1_first, it1_beyond, index1,
  194. section1.directions[1], section1.bounding_box, section2.bounding_box);
  195. move_end_iterator<1>(it1_first, it1_beyond,
  196. section1.directions[1], section1.bounding_box, section2.bounding_box);
  197. signed_size_type index2 = sec2_first_index;
  198. move_begin_iterator<0>(it2_first, it2_beyond, index2,
  199. section2.directions[0], section2.bounding_box, section1.bounding_box);
  200. move_end_iterator<0>(it2_first, it2_beyond,
  201. section2.directions[0], section2.bounding_box, section1.bounding_box);
  202. move_begin_iterator<1>(it2_first, it2_beyond, index2,
  203. section2.directions[1], section2.bounding_box, section1.bounding_box);
  204. move_end_iterator<1>(it2_first, it2_beyond,
  205. section2.directions[1], section2.bounding_box, section1.bounding_box);
  206. turn_type the_model;
  207. the_model.operations[0].piece_index = piece1.index;
  208. the_model.operations[0].seg_id = piece1.first_seg_id;
  209. the_model.operations[0].seg_id.segment_index = index1; // override
  210. auto it1 = it1_first;
  211. for (auto prev1 = it1++;
  212. it1 != it1_beyond;
  213. prev1 = it1++, the_model.operations[0].seg_id.segment_index++)
  214. {
  215. the_model.operations[1].piece_index = piece2.index;
  216. the_model.operations[1].seg_id = piece2.first_seg_id;
  217. the_model.operations[1].seg_id.segment_index = index2; // override
  218. unique_sub_range_from_piece<ring_type> unique_sub_range1(ring1, prev1, it1);
  219. auto it2 = it2_first;
  220. for (auto prev2 = it2++;
  221. it2 != it2_beyond;
  222. prev2 = it2++, the_model.operations[1].seg_id.segment_index++)
  223. {
  224. unique_sub_range_from_piece<ring_type> unique_sub_range2(ring2, prev2, it2);
  225. typedef detail::overlay::get_turn_info
  226. <
  227. detail::overlay::assign_policy_only_start_turns
  228. > turn_policy;
  229. turn_policy::apply(unique_sub_range1, unique_sub_range2,
  230. the_model,
  231. m_strategy,
  232. m_robust_policy,
  233. std::back_inserter(m_turns));
  234. }
  235. }
  236. }
  237. public:
  238. piece_turn_visitor(Pieces const& pieces,
  239. Rings const& ring_collection,
  240. Turns& turns,
  241. Strategy const& strategy,
  242. RobustPolicy const& robust_policy)
  243. : m_pieces(pieces)
  244. , m_rings(ring_collection)
  245. , m_turns(turns)
  246. , m_strategy(strategy)
  247. , m_robust_policy(robust_policy)
  248. {}
  249. template <typename Section>
  250. inline bool apply(Section const& section1, Section const& section2,
  251. bool first = true)
  252. {
  253. boost::ignore_unused(first);
  254. typedef typename boost::range_value<Pieces const>::type piece_type;
  255. piece_type const& piece1 = m_pieces[section1.ring_id.source_index];
  256. piece_type const& piece2 = m_pieces[section2.ring_id.source_index];
  257. if ( piece1.index == piece2.index
  258. || is_adjacent(piece1, piece2)
  259. || is_on_same_convex_ring(piece1, piece2)
  260. || detail::disjoint::disjoint_box_box(section1.bounding_box,
  261. section2.bounding_box,
  262. m_strategy) )
  263. {
  264. return true;
  265. }
  266. calculate_turns(piece1, piece2, section1, section2);
  267. return true;
  268. }
  269. };
  270. }} // namespace detail::buffer
  271. #endif // DOXYGEN_NO_DETAIL
  272. }} // namespace boost::geometry
  273. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_GET_PIECE_TURNS_HPP