select_rings.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2017-2022.
  5. // Modifications copyright (c) 2017-2022 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP
  12. #include <map>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/range/size.hpp>
  16. #include <boost/geometry/core/tags.hpp>
  17. #include <boost/geometry/algorithms/detail/covered_by/implementation.hpp>
  18. #include <boost/geometry/algorithms/detail/overlay/range_in_geometry.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/ring_properties.hpp>
  20. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  21. #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. #ifndef DOXYGEN_NO_DETAIL
  25. namespace detail { namespace overlay
  26. {
  27. struct ring_turn_info
  28. {
  29. bool has_traversed_turn;
  30. bool has_blocked_turn;
  31. bool within_other;
  32. ring_turn_info()
  33. : has_traversed_turn(false)
  34. , has_blocked_turn(false)
  35. , within_other(false)
  36. {}
  37. };
  38. namespace dispatch
  39. {
  40. template <typename Tag, typename Geometry>
  41. struct select_rings
  42. {};
  43. template <typename Box>
  44. struct select_rings<box_tag, Box>
  45. {
  46. template <typename Geometry, typename RingPropertyMap, typename Strategy>
  47. static inline void apply(Box const& box, Geometry const& ,
  48. ring_identifier const& id, RingPropertyMap& ring_properties,
  49. Strategy const& strategy)
  50. {
  51. ring_properties[id] = typename RingPropertyMap::mapped_type(box, strategy);
  52. }
  53. template <typename RingPropertyMap, typename Strategy>
  54. static inline void apply(Box const& box,
  55. ring_identifier const& id, RingPropertyMap& ring_properties,
  56. Strategy const& strategy)
  57. {
  58. ring_properties[id] = typename RingPropertyMap::mapped_type(box, strategy);
  59. }
  60. };
  61. template <typename Ring>
  62. struct select_rings<ring_tag, Ring>
  63. {
  64. template <typename Geometry, typename RingPropertyMap, typename Strategy>
  65. static inline void apply(Ring const& ring, Geometry const& ,
  66. ring_identifier const& id, RingPropertyMap& ring_properties,
  67. Strategy const& strategy)
  68. {
  69. if (boost::size(ring) > 0)
  70. {
  71. ring_properties[id] = typename RingPropertyMap::mapped_type(ring, strategy);
  72. }
  73. }
  74. template <typename RingPropertyMap, typename Strategy>
  75. static inline void apply(Ring const& ring,
  76. ring_identifier const& id, RingPropertyMap& ring_properties,
  77. Strategy const& strategy)
  78. {
  79. if (boost::size(ring) > 0)
  80. {
  81. ring_properties[id] = typename RingPropertyMap::mapped_type(ring, strategy);
  82. }
  83. }
  84. };
  85. template <typename Polygon>
  86. struct select_rings<polygon_tag, Polygon>
  87. {
  88. template <typename Geometry, typename RingPropertyMap, typename Strategy>
  89. static inline void apply(Polygon const& polygon, Geometry const& geometry,
  90. ring_identifier id, RingPropertyMap& ring_properties,
  91. Strategy const& strategy)
  92. {
  93. typedef typename geometry::ring_type<Polygon>::type ring_type;
  94. typedef select_rings<ring_tag, ring_type> per_ring;
  95. per_ring::apply(exterior_ring(polygon), geometry, id, ring_properties, strategy);
  96. auto const& rings = interior_rings(polygon);
  97. for (auto it = boost::begin(rings); it != boost::end(rings); ++it)
  98. {
  99. id.ring_index++;
  100. per_ring::apply(*it, geometry, id, ring_properties, strategy);
  101. }
  102. }
  103. template <typename RingPropertyMap, typename Strategy>
  104. static inline void apply(Polygon const& polygon,
  105. ring_identifier id, RingPropertyMap& ring_properties,
  106. Strategy const& strategy)
  107. {
  108. typedef typename geometry::ring_type<Polygon>::type ring_type;
  109. typedef select_rings<ring_tag, ring_type> per_ring;
  110. per_ring::apply(exterior_ring(polygon), id, ring_properties, strategy);
  111. auto const& rings = interior_rings(polygon);
  112. for (auto it = boost::begin(rings); it != boost::end(rings); ++it)
  113. {
  114. id.ring_index++;
  115. per_ring::apply(*it, id, ring_properties, strategy);
  116. }
  117. }
  118. };
  119. template <typename Multi>
  120. struct select_rings<multi_polygon_tag, Multi>
  121. {
  122. template <typename Geometry, typename RingPropertyMap, typename Strategy>
  123. static inline void apply(Multi const& multi, Geometry const& geometry,
  124. ring_identifier id, RingPropertyMap& ring_properties,
  125. Strategy const& strategy)
  126. {
  127. typedef select_rings<polygon_tag, typename boost::range_value<Multi>::type> per_polygon;
  128. id.multi_index = 0;
  129. for (auto it = boost::begin(multi); it != boost::end(multi); ++it)
  130. {
  131. id.ring_index = -1;
  132. per_polygon::apply(*it, geometry, id, ring_properties, strategy);
  133. id.multi_index++;
  134. }
  135. }
  136. };
  137. } // namespace dispatch
  138. template<overlay_type OverlayType>
  139. struct decide
  140. {
  141. // Default implementation (union, inflate, deflate, dissolve)
  142. static bool include(ring_identifier const& , ring_turn_info const& info)
  143. {
  144. return ! info.within_other;
  145. }
  146. static bool reversed(ring_identifier const& , ring_turn_info const& )
  147. {
  148. return false;
  149. }
  150. };
  151. template<>
  152. struct decide<overlay_difference>
  153. {
  154. static bool include(ring_identifier const& id, ring_turn_info const& info)
  155. {
  156. // Difference: A - B
  157. // If this is A (source_index=0), then the ring is inside B
  158. // If this is B (source_index=1), then the ring is NOT inside A
  159. // If this is A and the ring is within the other geometry,
  160. // then we should NOT include it.
  161. // If this is B then we SHOULD include it.
  162. return id.source_index == 0
  163. ? ! info.within_other
  164. : info.within_other;
  165. }
  166. static bool reversed(ring_identifier const& id, ring_turn_info const& info)
  167. {
  168. // Difference: A - B
  169. // If this is B, and the ring is included, it should be reversed afterwards
  170. return id.source_index == 1 && include(id, info);
  171. }
  172. };
  173. template<>
  174. struct decide<overlay_intersection>
  175. {
  176. static bool include(ring_identifier const& , ring_turn_info const& info)
  177. {
  178. return info.within_other;
  179. }
  180. static bool reversed(ring_identifier const& , ring_turn_info const& )
  181. {
  182. return false;
  183. }
  184. };
  185. template
  186. <
  187. overlay_type OverlayType,
  188. typename Geometry1,
  189. typename Geometry2,
  190. typename TurnInfoMap,
  191. typename RingPropertyMap,
  192. typename Strategy
  193. >
  194. inline void update_ring_selection(Geometry1 const& geometry1,
  195. Geometry2 const& geometry2,
  196. TurnInfoMap const& turn_info_map,
  197. RingPropertyMap const& all_ring_properties,
  198. RingPropertyMap& selected_ring_properties,
  199. Strategy const& strategy)
  200. {
  201. selected_ring_properties.clear();
  202. for (auto const& pair : all_ring_properties)
  203. {
  204. ring_identifier const& id = pair.first;
  205. ring_turn_info info;
  206. auto tcit = turn_info_map.find(id);
  207. if (tcit != turn_info_map.end())
  208. {
  209. info = tcit->second; // Copy by value
  210. }
  211. if (info.has_traversed_turn || info.has_blocked_turn)
  212. {
  213. // This turn is traversed or blocked,
  214. // don't include the original ring
  215. continue;
  216. }
  217. // Check if the ring is within the other geometry, by taking
  218. // a point lying on the ring
  219. switch(id.source_index)
  220. {
  221. // within
  222. case 0 :
  223. info.within_other = range_in_geometry(pair.second.point,
  224. geometry1, geometry2,
  225. strategy) > 0;
  226. break;
  227. case 1 :
  228. info.within_other = range_in_geometry(pair.second.point,
  229. geometry2, geometry1,
  230. strategy) > 0;
  231. break;
  232. }
  233. if (decide<OverlayType>::include(id, info))
  234. {
  235. auto properties = pair.second; // Copy by value
  236. properties.reversed = decide<OverlayType>::reversed(id, info);
  237. selected_ring_properties[id] = properties;
  238. }
  239. }
  240. }
  241. /*!
  242. \brief The function select_rings select rings based on the overlay-type (union,intersection)
  243. */
  244. template
  245. <
  246. overlay_type OverlayType,
  247. typename Geometry1,
  248. typename Geometry2,
  249. typename RingTurnInfoMap,
  250. typename RingPropertyMap,
  251. typename Strategy
  252. >
  253. inline void select_rings(Geometry1 const& geometry1, Geometry2 const& geometry2,
  254. RingTurnInfoMap const& turn_info_per_ring,
  255. RingPropertyMap& selected_ring_properties,
  256. Strategy const& strategy)
  257. {
  258. typedef typename geometry::tag<Geometry1>::type tag1;
  259. typedef typename geometry::tag<Geometry2>::type tag2;
  260. RingPropertyMap all_ring_properties;
  261. dispatch::select_rings<tag1, Geometry1>::apply(geometry1, geometry2,
  262. ring_identifier(0, -1, -1), all_ring_properties,
  263. strategy);
  264. dispatch::select_rings<tag2, Geometry2>::apply(geometry2, geometry1,
  265. ring_identifier(1, -1, -1), all_ring_properties,
  266. strategy);
  267. update_ring_selection<OverlayType>(geometry1, geometry2, turn_info_per_ring,
  268. all_ring_properties, selected_ring_properties,
  269. strategy);
  270. }
  271. template
  272. <
  273. overlay_type OverlayType,
  274. typename Geometry,
  275. typename RingTurnInfoMap,
  276. typename RingPropertyMap,
  277. typename Strategy
  278. >
  279. inline void select_rings(Geometry const& geometry,
  280. RingTurnInfoMap const& turn_info_per_ring,
  281. RingPropertyMap& selected_ring_properties,
  282. Strategy const& strategy)
  283. {
  284. typedef typename geometry::tag<Geometry>::type tag;
  285. RingPropertyMap all_ring_properties;
  286. dispatch::select_rings<tag, Geometry>::apply(geometry,
  287. ring_identifier(0, -1, -1), all_ring_properties,
  288. strategy);
  289. update_ring_selection<OverlayType>(geometry, geometry, turn_info_per_ring,
  290. all_ring_properties, selected_ring_properties,
  291. strategy);
  292. }
  293. }} // namespace detail::overlay
  294. #endif // DOXYGEN_NO_DETAIL
  295. }} // namespace boost::geometry
  296. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP