overlay.hpp 14 KB

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