gc.hpp 11 KB

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