overlay.hpp 15 KB

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