overlay.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013-2017 Adam Wulkiewicz, Lodz, Poland
  4. // This file was modified by Oracle on 2015-2024.
  5. // Modifications copyright (c) 2015-2024, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_OVERLAY_HPP
  13. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_OVERLAY_HPP
  14. #include <deque>
  15. #include <map>
  16. #include <boost/range/begin.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/value_type.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/graph/assign_side_counts.hpp>
  20. #include <boost/geometry/algorithms/detail/overlay/cluster_info.hpp>
  21. #include <boost/geometry/algorithms/detail/overlay/enrich_intersection_points.hpp>
  22. #include <boost/geometry/algorithms/detail/overlay/enrichment_info.hpp>
  23. #include <boost/geometry/algorithms/detail/overlay/get_properties_ahead.hpp>
  24. #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
  25. #include <boost/geometry/algorithms/detail/overlay/handle_colocations.hpp>
  26. #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
  27. #include <boost/geometry/algorithms/detail/overlay/needs_self_turns.hpp>
  28. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  29. #include <boost/geometry/algorithms/detail/overlay/traverse.hpp>
  30. #include <boost/geometry/algorithms/detail/overlay/traversal_info.hpp>
  31. #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
  32. #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
  33. #include <boost/geometry/algorithms/is_empty.hpp>
  34. #include <boost/geometry/algorithms/reverse.hpp>
  35. #include <boost/geometry/algorithms/detail/overlay/add_rings.hpp>
  36. #include <boost/geometry/algorithms/detail/overlay/assign_parents.hpp>
  37. #include <boost/geometry/algorithms/detail/overlay/ring_properties.hpp>
  38. #include <boost/geometry/algorithms/detail/overlay/select_rings.hpp>
  39. #include <boost/geometry/algorithms/detail/overlay/do_reverse.hpp>
  40. #include <boost/geometry/util/condition.hpp>
  41. namespace boost { namespace geometry
  42. {
  43. #ifndef DOXYGEN_NO_DETAIL
  44. namespace detail { namespace overlay
  45. {
  46. //! Default visitor for overlay, doing nothing
  47. struct overlay_null_visitor
  48. {
  49. template <typename Turns>
  50. void visit_turns(int , Turns const& ) {}
  51. template <typename Clusters, typename Turns>
  52. void visit_clusters(Clusters const& , Turns const& ) {}
  53. template <typename Turns, typename Cluster, typename Connections>
  54. inline void visit_cluster_connections(signed_size_type cluster_id,
  55. Turns const& turns, Cluster const& cluster, Connections const& connections) {}
  56. template <typename Turns, typename Turn, typename Operation>
  57. void visit_traverse(Turns const& , Turn const& , Operation const& , char const*)
  58. {}
  59. template <typename Rings>
  60. void visit_generated_rings(Rings const& )
  61. {}
  62. };
  63. template
  64. <
  65. overlay_type OverlayType,
  66. typename TurnInfoMap,
  67. typename Turns,
  68. typename Clusters
  69. >
  70. inline void get_ring_turn_info(TurnInfoMap& turn_info_map, Turns const& turns, Clusters const& clusters)
  71. {
  72. static const operation_type target_operation
  73. = operation_from_overlay<OverlayType>::value;
  74. static const operation_type opposite_operation
  75. = target_operation == operation_union
  76. ? operation_intersection
  77. : operation_union;
  78. static const bool is_union = target_operation == operation_union;
  79. for (auto const& turn : turns)
  80. {
  81. if (turn.discarded && (turn.method == method_start || is_self_turn<OverlayType>(turn)))
  82. {
  83. // Discarded self-turns or start turns don't need to block the ring
  84. continue;
  85. }
  86. for (int i = 0; i < 2; i++)
  87. {
  88. auto const& op = turn.operations[i];
  89. auto const& other_op = turn.operations[1 - i];
  90. ring_identifier const ring_id = ring_id_by_seg_id(op.seg_id);
  91. // The next condition is necessary for just two test cases.
  92. // TODO: fix it in get_turn_info
  93. // If the turn (one of its operations) is used during traversal,
  94. // and it is an intersection or difference, it cannot be set to blocked.
  95. // This is a rare case, related to floating point precision,
  96. // and can happen if there is, for example, only one start turn which is
  97. // used to traverse through one of the rings (the other should be marked
  98. // as not traversed, but neither blocked).
  99. bool const can_block = is_union || ! (op.enriched.is_traversed || other_op.enriched.is_traversed);
  100. if (! is_self_turn<OverlayType>(turn) && can_block)
  101. {
  102. turn_info_map[ring_id].has_blocked_turn = true;
  103. continue;
  104. }
  105. if (is_union && turn.any_blocked())
  106. {
  107. turn_info_map[ring_id].has_blocked_turn = true;
  108. }
  109. if (turn_info_map[ring_id].has_traversed_turn
  110. || turn_info_map[ring_id].has_blocked_turn)
  111. {
  112. continue;
  113. }
  114. // Block rings where any other turn is blocked,
  115. // and (with exceptions): i for union and u for intersection
  116. // Exceptions: don't block self-uu for intersection
  117. // don't block self-ii for union
  118. // don't block (for union) i/u if there is an self-ii too
  119. if (op.operation == opposite_operation
  120. && can_block
  121. && ! (turn.both(opposite_operation)
  122. && is_self_turn<OverlayType>(turn)))
  123. {
  124. turn_info_map[ring_id].has_blocked_turn = true;
  125. }
  126. }
  127. }
  128. }
  129. template
  130. <
  131. typename GeometryOut, overlay_type OverlayType, bool ReverseOut,
  132. typename Geometry1, typename Geometry2,
  133. typename OutputIterator, typename Strategy
  134. >
  135. inline OutputIterator return_if_one_input_is_empty(Geometry1 const& geometry1,
  136. Geometry2 const& geometry2,
  137. OutputIterator out, Strategy const& strategy)
  138. {
  139. using ring_type = geometry::ring_type_t<GeometryOut>;
  140. using ring_container_type = std::deque<ring_type>;
  141. using properties = ring_properties
  142. <
  143. geometry::point_type_t<ring_type>,
  144. typename geometry::area_result<ring_type, Strategy>::type
  145. >;
  146. // Silence warning C4127: conditional expression is constant
  147. #if defined(_MSC_VER)
  148. #pragma warning(push)
  149. #pragma warning(disable : 4127)
  150. #endif
  151. // Union: return either of them
  152. // Intersection: return nothing
  153. // Difference: return first of them
  154. if (OverlayType == overlay_intersection
  155. || (OverlayType == overlay_difference && geometry::is_empty(geometry1)))
  156. {
  157. return out;
  158. }
  159. #if defined(_MSC_VER)
  160. #pragma warning(pop)
  161. #endif
  162. std::map<ring_identifier, ring_turn_info> empty;
  163. std::map<ring_identifier, properties> all_of_one_of_them;
  164. select_rings<OverlayType>(geometry1, geometry2, empty, all_of_one_of_them, strategy);
  165. ring_container_type rings;
  166. assign_parents<OverlayType>(geometry1, geometry2, rings, all_of_one_of_them, strategy);
  167. return add_rings<GeometryOut>(all_of_one_of_them, geometry1, geometry2, rings, out, strategy);
  168. }
  169. template
  170. <
  171. typename Geometry1, typename Geometry2,
  172. bool Reverse1, bool Reverse2, bool ReverseOut,
  173. typename GeometryOut,
  174. overlay_type OverlayType
  175. >
  176. struct overlay
  177. {
  178. template <typename OutputIterator, typename Strategy, typename Visitor>
  179. static inline OutputIterator apply(
  180. Geometry1 const& geometry1, Geometry2 const& geometry2,
  181. OutputIterator out,
  182. Strategy const& strategy,
  183. Visitor& visitor)
  184. {
  185. bool const is_empty1 = geometry::is_empty(geometry1);
  186. bool const is_empty2 = geometry::is_empty(geometry2);
  187. if (is_empty1 && is_empty2)
  188. {
  189. return out;
  190. }
  191. if (is_empty1 || is_empty2)
  192. {
  193. return return_if_one_input_is_empty
  194. <
  195. GeometryOut, OverlayType, ReverseOut
  196. >(geometry1, geometry2, out, strategy);
  197. }
  198. using point_type = geometry::point_type_t<GeometryOut>;
  199. using turn_info = detail::overlay::traversal_turn_info
  200. <
  201. point_type,
  202. typename segment_ratio_type<point_type>::type
  203. >;
  204. using turn_container_type = std::deque<turn_info>;
  205. using ring_type = geometry::ring_type_t<GeometryOut>;
  206. using ring_container_type = std::deque<ring_type>;
  207. // Define the clusters, mapping cluster_id -> turns
  208. using cluster_type = std::map
  209. <
  210. signed_size_type,
  211. cluster_info
  212. >;
  213. turn_container_type turns;
  214. detail::get_turns::no_interrupt_policy policy;
  215. geometry::get_turns
  216. <
  217. Reverse1, Reverse2,
  218. assign_policy_only_start_turns
  219. >(geometry1, geometry2, strategy, turns, policy);
  220. visitor.visit_turns(1, turns);
  221. #if ! defined(BOOST_GEOMETRY_NO_SELF_TURNS)
  222. if (! turns.empty() || OverlayType == overlay_dissolve)
  223. {
  224. // Calculate self turns if the output contains turns already,
  225. // and if necessary (e.g.: multi-geometry, polygon with interior rings)
  226. if (needs_self_turns<Geometry1>::apply(geometry1))
  227. {
  228. self_get_turn_points::self_turns<Reverse1, assign_policy_only_start_turns>(geometry1,
  229. strategy, turns, policy, 0);
  230. }
  231. if (needs_self_turns<Geometry2>::apply(geometry2))
  232. {
  233. self_get_turn_points::self_turns<Reverse2, assign_policy_only_start_turns>(geometry2,
  234. strategy, turns, policy, 1);
  235. }
  236. }
  237. #endif
  238. cluster_type clusters;
  239. // Handle colocations, gathering clusters and (below) their properties.
  240. detail::overlay::handle_colocations(turns, clusters);
  241. detail::overlay::enrich_discard_turns<OverlayType>(
  242. turns, clusters, geometry1, geometry2, strategy);
  243. detail::overlay::enrich_turns<Reverse1, Reverse2, OverlayType>(
  244. turns, geometry1, geometry2, strategy);
  245. visitor.visit_turns(2, turns);
  246. detail::overlay::colocate_clusters(clusters, turns);
  247. // AssignCounts should be called:
  248. // * after "colocate_clusters"
  249. // * and "colocate_clusters" after "enrich_discard_turns"
  250. // because assigning side counts needs cluster centroids.
  251. //
  252. // For BUFFER - it is called before, to be able to block closed clusters
  253. // before enrichment.
  254. assign_side_counts
  255. <
  256. Reverse1, Reverse2, OverlayType
  257. >(geometry1, geometry2, turns, clusters, strategy, visitor);
  258. get_properties_ahead<Reverse1, Reverse2>(turns, clusters, geometry1, geometry2, strategy);
  259. // Traverse through intersection/turn points and create rings of them.
  260. // These rings are always in clockwise order.
  261. // In CCW polygons they are marked as "to be reversed" below.
  262. std::map<ring_identifier, ring_turn_info> turn_info_per_ring;
  263. ring_container_type rings;
  264. traverse<Reverse1, Reverse2, Geometry1, Geometry2, OverlayType>::apply
  265. (
  266. geometry1, geometry2,
  267. strategy,
  268. turns, rings,
  269. turn_info_per_ring,
  270. clusters,
  271. visitor
  272. );
  273. visitor.visit_clusters(clusters, turns);
  274. visitor.visit_turns(3, turns);
  275. get_ring_turn_info<OverlayType>(turn_info_per_ring, turns, clusters);
  276. using properties = ring_properties
  277. <
  278. point_type,
  279. typename geometry::area_result<ring_type, Strategy>::type
  280. >;
  281. // Select all rings which are NOT touched by any intersection point
  282. std::map<ring_identifier, properties> selected_ring_properties;
  283. select_rings<OverlayType>(geometry1, geometry2, turn_info_per_ring,
  284. selected_ring_properties, strategy);
  285. // Add rings created during traversal
  286. {
  287. ring_identifier id(2, 0, -1);
  288. for (auto const& ring : rings)
  289. {
  290. selected_ring_properties[id] = properties(ring, strategy);
  291. selected_ring_properties[id].reversed = ReverseOut;
  292. id.multi_index++;
  293. }
  294. }
  295. assign_parents<OverlayType>(geometry1, geometry2,
  296. rings, selected_ring_properties, strategy);
  297. // NOTE: There is no need to check result area for union because
  298. // as long as the polygons in the input are valid the resulting
  299. // polygons should be valid as well.
  300. // By default the area is checked (this is old behavior) however this
  301. // can be changed with #define. This may be important in non-cartesian CSes.
  302. // The result may be too big, so the area is negative. In this case either
  303. // it can be returned or an exception can be thrown.
  304. return add_rings<GeometryOut>(selected_ring_properties, geometry1, geometry2, rings, out,
  305. strategy,
  306. #if defined(BOOST_GEOMETRY_UNION_THROW_INVALID_OUTPUT_EXCEPTION)
  307. OverlayType == overlay_union ?
  308. add_rings_throw_if_reversed
  309. : add_rings_ignore_unordered
  310. #elif defined(BOOST_GEOMETRY_UNION_RETURN_INVALID)
  311. OverlayType == overlay_union ?
  312. add_rings_add_unordered
  313. : add_rings_ignore_unordered
  314. #else
  315. add_rings_ignore_unordered
  316. #endif
  317. );
  318. }
  319. template <typename OutputIterator, typename Strategy>
  320. static inline OutputIterator apply(
  321. Geometry1 const& geometry1, Geometry2 const& geometry2,
  322. OutputIterator out,
  323. Strategy const& strategy)
  324. {
  325. overlay_null_visitor visitor;
  326. return apply(geometry1, geometry2, out, strategy, visitor);
  327. }
  328. };
  329. }} // namespace detail::overlay
  330. #endif // DOXYGEN_NO_DETAIL
  331. }} // namespace boost::geometry
  332. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_OVERLAY_HPP