cluster_exits.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2020 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2020-2023.
  4. // Modifications copyright (c) 2020-2023 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  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_OVERLAY_CLUSTER_EXITS_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CLUSTER_EXITS_HPP
  12. #include <cstddef>
  13. #include <set>
  14. #include <vector>
  15. #include <boost/range/value_type.hpp>
  16. #include <boost/geometry/core/assert.hpp>
  17. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  18. #include <boost/geometry/algorithms/detail/overlay/sort_by_side.hpp>
  19. #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
  20. #include <boost/geometry/util/condition.hpp>
  21. #if defined(BOOST_GEOMETRY_DEBUG_INTERSECTION) \
  22. || defined(BOOST_GEOMETRY_OVERLAY_REPORT_WKT) \
  23. || defined(BOOST_GEOMETRY_DEBUG_TRAVERSE)
  24. # include <string>
  25. # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
  26. # include <boost/geometry/io/wkt/wkt.hpp>
  27. #endif
  28. namespace boost { namespace geometry
  29. {
  30. #ifndef DOXYGEN_NO_DETAIL
  31. namespace detail { namespace overlay
  32. {
  33. // Structure to check relatively simple cluster cases
  34. template <overlay_type OverlayType,
  35. typename Turns,
  36. typename Sbs>
  37. struct cluster_exits
  38. {
  39. private :
  40. static const operation_type target_operation = operation_from_overlay<OverlayType>::value;
  41. typedef typename boost::range_value<Turns>::type turn_type;
  42. typedef typename turn_type::turn_operation_type turn_operation_type;
  43. struct linked_turn_op_info
  44. {
  45. explicit linked_turn_op_info(signed_size_type ti = -1, int oi = -1,
  46. signed_size_type nti = -1)
  47. : turn_index(ti)
  48. , op_index(oi)
  49. , next_turn_index(nti)
  50. , rank_index(-1)
  51. {}
  52. signed_size_type turn_index;
  53. int op_index;
  54. signed_size_type next_turn_index;
  55. signed_size_type rank_index;
  56. };
  57. inline signed_size_type get_rank(Sbs const& sbs,
  58. linked_turn_op_info const& info) const
  59. {
  60. for (auto const& rp : sbs.m_ranked_points)
  61. {
  62. if (rp.turn_index == info.turn_index
  63. && rp.operation_index == info.op_index
  64. && rp.direction == sort_by_side::dir_to)
  65. {
  66. return rp.rank;
  67. }
  68. }
  69. return -1;
  70. }
  71. std::set<signed_size_type> const& m_ids;
  72. std::vector<linked_turn_op_info> possibilities;
  73. std::vector<linked_turn_op_info> blocked;
  74. bool m_valid;
  75. bool collect(Turns const& turns)
  76. {
  77. for (auto cluster_turn_index : m_ids)
  78. {
  79. turn_type const& cluster_turn = turns[cluster_turn_index];
  80. if (cluster_turn.discarded)
  81. {
  82. continue;
  83. }
  84. if (cluster_turn.both(target_operation))
  85. {
  86. // Not (yet) supported, can be cluster of u/u turns
  87. return false;
  88. }
  89. for (int i = 0; i < 2; i++)
  90. {
  91. turn_operation_type const& op = cluster_turn.operations[i];
  92. turn_operation_type const& other_op = cluster_turn.operations[1 - i];
  93. signed_size_type const ni = op.enriched.get_next_turn_index();
  94. if (op.operation == target_operation
  95. || op.operation == operation_continue)
  96. {
  97. if (ni == cluster_turn_index)
  98. {
  99. // Not (yet) supported, traveling to itself, can be
  100. // hole
  101. return false;
  102. }
  103. possibilities.push_back(
  104. linked_turn_op_info(cluster_turn_index, i, ni));
  105. }
  106. else if (op.operation == operation_blocked
  107. && ! (ni == other_op.enriched.get_next_turn_index())
  108. && m_ids.count(ni) == 0)
  109. {
  110. // Points to turn, not part of this cluster,
  111. // and that way is blocked. But if the other operation
  112. // points at the same turn, it is still fine.
  113. blocked.push_back(
  114. linked_turn_op_info(cluster_turn_index, i, ni));
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120. bool check_blocked(Sbs const& sbs)
  121. {
  122. if (blocked.empty())
  123. {
  124. return true;
  125. }
  126. for (auto& info : possibilities)
  127. {
  128. info.rank_index = get_rank(sbs, info);
  129. }
  130. for (auto& info : blocked)
  131. {
  132. info.rank_index = get_rank(sbs, info);
  133. }
  134. for (auto const& lti : possibilities)
  135. {
  136. for (auto const& blti : blocked)
  137. {
  138. if (blti.next_turn_index == lti.next_turn_index
  139. && blti.rank_index == lti.rank_index)
  140. {
  141. return false;
  142. }
  143. }
  144. }
  145. return true;
  146. }
  147. public :
  148. cluster_exits(Turns const& turns,
  149. std::set<signed_size_type> const& ids,
  150. Sbs const& sbs)
  151. : m_ids(ids)
  152. , m_valid(collect(turns) && check_blocked(sbs))
  153. {
  154. }
  155. inline bool apply(signed_size_type& turn_index,
  156. int& op_index,
  157. bool first_run = true) const
  158. {
  159. if (! m_valid)
  160. {
  161. return false;
  162. }
  163. // Traversal can either enter the cluster in the first turn,
  164. // or it can start halfway.
  165. // If there is one (and only one) possibility pointing outside
  166. // the cluster, take that one.
  167. linked_turn_op_info target;
  168. for (auto const& lti : possibilities)
  169. {
  170. if (m_ids.count(lti.next_turn_index) == 0)
  171. {
  172. if (target.turn_index >= 0
  173. && target.next_turn_index != lti.next_turn_index)
  174. {
  175. // Points to different target
  176. return false;
  177. }
  178. if (first_run
  179. && BOOST_GEOMETRY_CONDITION(OverlayType == overlay_buffer)
  180. && target.turn_index >= 0)
  181. {
  182. // Target already assigned, so there are more targets
  183. // or more ways to the same target
  184. return false;
  185. }
  186. target = lti;
  187. }
  188. }
  189. if (target.turn_index < 0)
  190. {
  191. return false;
  192. }
  193. turn_index = target.turn_index;
  194. op_index = target.op_index;
  195. return true;
  196. }
  197. };
  198. }} // namespace detail::overlay
  199. #endif // DOXYGEN_NO_DETAIL
  200. }} // namespace boost::geometry
  201. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CLUSTER_EXITS_HPP