gc.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Boost.Geometry
  2. // Copyright (c) 2022-2024, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_GC_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_GC_HPP
  9. #include <tuple>
  10. #include <boost/range/size.hpp>
  11. #include <boost/geometry/algorithms/detail/gc_make_rtree.hpp>
  12. #include <boost/geometry/algorithms/detail/intersection/interface.hpp>
  13. #include <boost/geometry/views/detail/geometry_collection_view.hpp>
  14. namespace boost { namespace geometry
  15. {
  16. #ifndef DOXYGEN_NO_DETAIL
  17. namespace detail { namespace intersection
  18. {
  19. template <typename GC, typename Multi>
  20. struct gc_can_move_element
  21. {
  22. template <typename G>
  23. using is_same_as_single = std::is_same<G, typename boost::range_value<Multi>::type>;
  24. using gc_types = typename traits::geometry_types<GC>::type;
  25. using found_type = typename util::sequence_find_if<gc_types, is_same_as_single>::type;
  26. static const bool value = ! std::is_void<found_type>::value;
  27. };
  28. template <typename GC, typename Multi>
  29. struct gc_can_convert_element
  30. {
  31. template <typename G>
  32. using has_same_tag_as_single = std::is_same
  33. <
  34. geometry::tag_t<G>,
  35. geometry::tag_t<typename boost::range_value<Multi>::type>
  36. >;
  37. using gc_types = typename traits::geometry_types<GC>::type;
  38. using found_type = typename util::sequence_find_if<gc_types, has_same_tag_as_single>::type;
  39. static const bool value = ! std::is_void<found_type>::value;
  40. };
  41. template
  42. <
  43. typename GC, typename Multi,
  44. std::enable_if_t<gc_can_move_element<GC, Multi>::value, int> = 0
  45. >
  46. inline void gc_move_one_elem_multi_back(GC& gc, Multi&& multi)
  47. {
  48. range::emplace_back(gc, std::move(*boost::begin(multi)));
  49. }
  50. template
  51. <
  52. typename GC, typename Multi,
  53. std::enable_if_t<! gc_can_move_element<GC, Multi>::value && gc_can_convert_element<GC, Multi>::value, int> = 0
  54. >
  55. inline void gc_move_one_elem_multi_back(GC& gc, Multi&& multi)
  56. {
  57. typename gc_can_convert_element<GC, Multi>::found_type single_out;
  58. geometry::convert(*boost::begin(multi), single_out);
  59. range::emplace_back(gc, std::move(single_out));
  60. }
  61. template
  62. <
  63. typename GC, typename Multi,
  64. std::enable_if_t<! gc_can_move_element<GC, Multi>::value && ! gc_can_convert_element<GC, Multi>::value, int> = 0
  65. >
  66. inline void gc_move_one_elem_multi_back(GC& gc, Multi&& multi)
  67. {
  68. range::emplace_back(gc, std::move(multi));
  69. }
  70. template <typename GC, typename Multi>
  71. inline void gc_move_multi_back(GC& gc, Multi&& multi)
  72. {
  73. if (! boost::empty(multi))
  74. {
  75. if (boost::size(multi) == 1)
  76. {
  77. gc_move_one_elem_multi_back(gc, std::move(multi));
  78. }
  79. else
  80. {
  81. range::emplace_back(gc, std::move(multi));
  82. }
  83. }
  84. }
  85. }} // namespace detail::intersection
  86. #endif // DOXYGEN_NO_DETAIL
  87. namespace resolve_collection
  88. {
  89. template
  90. <
  91. typename Geometry1, typename Geometry2, typename GeometryOut
  92. >
  93. struct intersection
  94. <
  95. Geometry1, Geometry2, GeometryOut,
  96. geometry_collection_tag, geometry_collection_tag, geometry_collection_tag
  97. >
  98. {
  99. // NOTE: for now require all of the possible output types
  100. // technically only a subset could be needed.
  101. using multi_point_t = typename util::sequence_find_if
  102. <
  103. typename traits::geometry_types<GeometryOut>::type,
  104. util::is_multi_point
  105. >::type;
  106. using multi_linestring_t = typename util::sequence_find_if
  107. <
  108. typename traits::geometry_types<GeometryOut>::type,
  109. util::is_multi_linestring
  110. >::type;
  111. using multi_polygon_t = typename util::sequence_find_if
  112. <
  113. typename traits::geometry_types<GeometryOut>::type,
  114. util::is_multi_polygon
  115. >::type;
  116. using tuple_out_t = boost::tuple<multi_point_t, multi_linestring_t, multi_polygon_t>;
  117. template <typename Strategy>
  118. static inline bool apply(Geometry1 const& geometry1,
  119. Geometry2 const& geometry2,
  120. GeometryOut& geometry_out,
  121. Strategy const& strategy)
  122. {
  123. bool result = false;
  124. tuple_out_t out;
  125. auto const rtree2 = detail::gc_make_rtree_iterators(geometry2, strategy);
  126. detail::visit_breadth_first([&](auto const& g1)
  127. {
  128. bool r = g1_prod_gc2(g1, rtree2, out, strategy);
  129. result = result || r;
  130. return true;
  131. }, geometry1);
  132. detail::intersection::gc_move_multi_back(geometry_out, boost::get<0>(out));
  133. detail::intersection::gc_move_multi_back(geometry_out, boost::get<1>(out));
  134. detail::intersection::gc_move_multi_back(geometry_out, boost::get<2>(out));
  135. return result;
  136. }
  137. private:
  138. // Implemented as separate function because msvc is unable to do nested lambda capture
  139. template <typename G1, typename Rtree2, typename TupleOut, typename Strategy>
  140. static bool g1_prod_gc2(G1 const& g1, Rtree2 const& rtree2, TupleOut& out, Strategy const& strategy)
  141. {
  142. bool result = false;
  143. using box1_t = detail::gc_make_rtree_box_t<G1>;
  144. box1_t b1 = geometry::return_envelope<box1_t>(g1, strategy);
  145. detail::expand_by_epsilon(b1);
  146. for (auto qit = rtree2.qbegin(index::intersects(b1)); qit != rtree2.qend(); ++qit)
  147. {
  148. traits::iter_visit<Geometry2>::apply([&](auto const& g2)
  149. {
  150. TupleOut inters_result;
  151. using g2_t = util::remove_cref_t<decltype(g2)>;
  152. intersection<G1, g2_t, TupleOut>::apply(g1, g2, inters_result, strategy);
  153. // TODO: If possible merge based on adjacency lists, i.e. merge
  154. // only the intersections of elements that intersect each other
  155. // as subgroups. So the result could contain merged intersections
  156. // of several groups, not only one.
  157. // TODO: It'd probably be better to gather all of the parts first
  158. // and then merge them with merge_elements.
  159. // NOTE: template explicitly called because gcc-6 doesn't compile it
  160. // otherwise.
  161. bool const r0 = intersection::template merge_result<0>(inters_result, out, strategy);
  162. bool const r1 = intersection::template merge_result<1>(inters_result, out, strategy);
  163. bool const r2 = intersection::template merge_result<2>(inters_result, out, strategy);
  164. result = result || r0 || r1 || r2;
  165. }, qit->second);
  166. }
  167. return result;
  168. }
  169. template <std::size_t Index, typename Out, typename Strategy>
  170. static bool merge_result(Out const& inters_result, Out& out, Strategy const& strategy)
  171. {
  172. auto const& multi_result = boost::get<Index>(inters_result);
  173. auto& multi_out = boost::get<Index>(out);
  174. if (! boost::empty(multi_result))
  175. {
  176. std::remove_reference_t<decltype(multi_out)> temp_result;
  177. merge_two(multi_out, multi_result, temp_result, strategy);
  178. multi_out = std::move(temp_result);
  179. return true;
  180. }
  181. return false;
  182. }
  183. template <typename Out, typename Strategy, std::enable_if_t<! util::is_pointlike<Out>::value, int> = 0>
  184. static void merge_two(Out const& g1, Out const& g2, Out& out, Strategy const& strategy)
  185. {
  186. geometry::dispatch::intersection_insert
  187. <
  188. Out, Out, typename boost::range_value<Out>::type,
  189. overlay_union
  190. >::apply(g1,
  191. g2,
  192. geometry::range::back_inserter(out),
  193. strategy);
  194. }
  195. template <typename Out, typename Strategy, std::enable_if_t<util::is_pointlike<Out>::value, int> = 0>
  196. static void merge_two(Out const& g1, Out const& g2, Out& out, Strategy const& strategy)
  197. {
  198. detail::overlay::union_pointlike_pointlike_point
  199. <
  200. Out, Out, typename boost::range_value<Out>::type
  201. >::apply(g1,
  202. g2,
  203. geometry::range::back_inserter(out),
  204. strategy);
  205. }
  206. };
  207. template
  208. <
  209. typename Geometry1, typename Geometry2, typename GeometryOut, typename Tag1
  210. >
  211. struct intersection
  212. <
  213. Geometry1, Geometry2, GeometryOut,
  214. Tag1, geometry_collection_tag, geometry_collection_tag
  215. >
  216. {
  217. template <typename Strategy>
  218. static inline bool apply(Geometry1 const& geometry1,
  219. Geometry2 const& geometry2,
  220. GeometryOut& geometry_out,
  221. Strategy const& strategy)
  222. {
  223. using gc_view_t = geometry::detail::geometry_collection_view<Geometry1>;
  224. return intersection
  225. <
  226. gc_view_t, Geometry2, GeometryOut
  227. >::apply(gc_view_t(geometry1), geometry2, geometry_out, strategy);
  228. }
  229. };
  230. template
  231. <
  232. typename Geometry1, typename Geometry2, typename GeometryOut, typename Tag2
  233. >
  234. struct intersection
  235. <
  236. Geometry1, Geometry2, GeometryOut,
  237. geometry_collection_tag, Tag2, geometry_collection_tag
  238. >
  239. {
  240. template <typename Strategy>
  241. static inline bool apply(Geometry1 const& geometry1,
  242. Geometry2 const& geometry2,
  243. GeometryOut& geometry_out,
  244. Strategy const& strategy)
  245. {
  246. using gc_view_t = geometry::detail::geometry_collection_view<Geometry2>;
  247. return intersection
  248. <
  249. Geometry1, gc_view_t, GeometryOut
  250. >::apply(geometry1, gc_view_t(geometry2), geometry_out, strategy);
  251. }
  252. };
  253. template
  254. <
  255. typename Geometry1, typename Geometry2, typename GeometryOut, typename Tag1, typename Tag2
  256. >
  257. struct intersection
  258. <
  259. Geometry1, Geometry2, GeometryOut,
  260. Tag1, Tag2, geometry_collection_tag
  261. >
  262. {
  263. template <typename Strategy>
  264. static inline bool apply(Geometry1 const& geometry1,
  265. Geometry2 const& geometry2,
  266. GeometryOut& geometry_out,
  267. Strategy const& strategy)
  268. {
  269. using gc1_view_t = geometry::detail::geometry_collection_view<Geometry1>;
  270. using gc2_view_t = geometry::detail::geometry_collection_view<Geometry2>;
  271. return intersection
  272. <
  273. gc1_view_t, gc2_view_t, GeometryOut
  274. >::apply(gc1_view_t(geometry1), gc2_view_t(geometry2), geometry_out, strategy);
  275. }
  276. };
  277. } // namespace resolve_collection
  278. }} // namespace boost::geometry
  279. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_GC_HPP