overlay.hpp 15 KB

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