cluster_exits.hpp 7.2 KB

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