interface.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014-2024.
  4. // Modifications copyright (c) 2014-2024, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  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_INTERSECTION_INTERFACE_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_INTERFACE_HPP
  12. #include <boost/geometry/algorithms/detail/overlay/intersection_insert.hpp>
  13. #include <boost/geometry/algorithms/detail/tupled_output.hpp>
  14. #include <boost/geometry/geometries/adapted/boost_variant.hpp>
  15. #include <boost/geometry/strategies/default_strategy.hpp>
  16. #include <boost/geometry/strategies/detail.hpp>
  17. #include <boost/geometry/strategies/relate/services.hpp>
  18. #include <boost/geometry/util/range.hpp>
  19. #include <boost/geometry/util/type_traits_std.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. #ifndef DOXYGEN_NO_DISPATCH
  23. namespace dispatch
  24. {
  25. // By default, all is forwarded to the intersection_insert-dispatcher
  26. template
  27. <
  28. typename Geometry1, typename Geometry2,
  29. typename Tag1 = geometry::tag_t<Geometry1>,
  30. typename Tag2 = geometry::tag_t<Geometry2>,
  31. bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
  32. >
  33. struct intersection
  34. {
  35. template <typename GeometryOut, typename Strategy>
  36. static inline bool apply(Geometry1 const& geometry1,
  37. Geometry2 const& geometry2,
  38. GeometryOut& geometry_out,
  39. Strategy const& strategy)
  40. {
  41. using single_out = typename geometry::detail::output_geometry_value
  42. <
  43. GeometryOut
  44. >::type;
  45. intersection_insert
  46. <
  47. Geometry1, Geometry2, single_out,
  48. overlay_intersection
  49. >::apply(geometry1, geometry2,
  50. geometry::detail::output_geometry_back_inserter(geometry_out),
  51. strategy);
  52. return true;
  53. }
  54. };
  55. // If reversal is needed, perform it
  56. template
  57. <
  58. typename Geometry1, typename Geometry2,
  59. typename Tag1, typename Tag2
  60. >
  61. struct intersection
  62. <
  63. Geometry1, Geometry2,
  64. Tag1, Tag2,
  65. true
  66. >
  67. : intersection<Geometry2, Geometry1, Tag2, Tag1, false>
  68. {
  69. template <typename GeometryOut, typename Strategy>
  70. static inline bool apply(
  71. Geometry1 const& g1,
  72. Geometry2 const& g2,
  73. GeometryOut& out,
  74. Strategy const& strategy)
  75. {
  76. return intersection
  77. <
  78. Geometry2, Geometry1,
  79. Tag2, Tag1,
  80. false
  81. >::apply(g2, g1, out, strategy);
  82. }
  83. };
  84. } // namespace dispatch
  85. #endif // DOXYGEN_NO_DISPATCH
  86. namespace resolve_collection
  87. {
  88. template
  89. <
  90. typename Geometry1, typename Geometry2, typename GeometryOut,
  91. typename Tag1 = geometry::tag_t<Geometry1>,
  92. typename Tag2 = geometry::tag_t<Geometry2>,
  93. typename TagOut = geometry::tag_t<GeometryOut>
  94. >
  95. struct intersection
  96. {
  97. template <typename Strategy>
  98. static bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  99. GeometryOut & geometry_out, Strategy const& strategy)
  100. {
  101. return dispatch::intersection
  102. <
  103. Geometry1,
  104. Geometry2
  105. >::apply(geometry1, geometry2, geometry_out,
  106. strategy);
  107. }
  108. };
  109. } // namespace resolve_collection
  110. namespace resolve_strategy {
  111. template
  112. <
  113. typename Strategy,
  114. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategy>::value
  115. >
  116. struct intersection
  117. {
  118. template
  119. <
  120. typename Geometry1,
  121. typename Geometry2,
  122. typename GeometryOut
  123. >
  124. static inline bool apply(Geometry1 const& geometry1,
  125. Geometry2 const& geometry2,
  126. GeometryOut & geometry_out,
  127. Strategy const& strategy)
  128. {
  129. return resolve_collection::intersection
  130. <
  131. Geometry1, Geometry2, GeometryOut
  132. >::apply(geometry1, geometry2, geometry_out, strategy);
  133. }
  134. };
  135. template <typename Strategy>
  136. struct intersection<Strategy, false>
  137. {
  138. template
  139. <
  140. typename Geometry1,
  141. typename Geometry2,
  142. typename GeometryOut
  143. >
  144. static inline bool apply(Geometry1 const& geometry1,
  145. Geometry2 const& geometry2,
  146. GeometryOut & geometry_out,
  147. Strategy const& strategy)
  148. {
  149. using strategies::relate::services::strategy_converter;
  150. return intersection
  151. <
  152. decltype(strategy_converter<Strategy>::get(strategy))
  153. >::apply(geometry1, geometry2, geometry_out,
  154. strategy_converter<Strategy>::get(strategy));
  155. }
  156. };
  157. template <>
  158. struct intersection<default_strategy, false>
  159. {
  160. template
  161. <
  162. typename Geometry1,
  163. typename Geometry2,
  164. typename GeometryOut
  165. >
  166. static inline bool apply(Geometry1 const& geometry1,
  167. Geometry2 const& geometry2,
  168. GeometryOut & geometry_out,
  169. default_strategy)
  170. {
  171. using strategy_type = typename strategies::relate::services::default_strategy
  172. <
  173. Geometry1, Geometry2
  174. >::type;
  175. return intersection
  176. <
  177. strategy_type
  178. >::apply(geometry1, geometry2, geometry_out, strategy_type());
  179. }
  180. };
  181. } // resolve_strategy
  182. namespace resolve_dynamic
  183. {
  184. template
  185. <
  186. typename Geometry1, typename Geometry2,
  187. typename Tag1 = geometry::tag_t<Geometry1>,
  188. typename Tag2 = geometry::tag_t<Geometry2>
  189. >
  190. struct intersection
  191. {
  192. template <typename GeometryOut, typename Strategy>
  193. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  194. GeometryOut& geometry_out, Strategy const& strategy)
  195. {
  196. concepts::check<Geometry1 const>();
  197. concepts::check<Geometry2 const>();
  198. return resolve_strategy::intersection
  199. <
  200. Strategy
  201. >::apply(geometry1, geometry2, geometry_out, strategy);
  202. }
  203. };
  204. template <typename DynamicGeometry1, typename Geometry2, typename Tag2>
  205. struct intersection<DynamicGeometry1, Geometry2, dynamic_geometry_tag, Tag2>
  206. {
  207. template <typename GeometryOut, typename Strategy>
  208. static inline bool apply(DynamicGeometry1 const& geometry1, Geometry2 const& geometry2,
  209. GeometryOut& geometry_out, Strategy const& strategy)
  210. {
  211. bool result = false;
  212. traits::visit<DynamicGeometry1>::apply([&](auto const& g1)
  213. {
  214. result = intersection
  215. <
  216. util::remove_cref_t<decltype(g1)>,
  217. Geometry2
  218. >::apply(g1, geometry2, geometry_out, strategy);
  219. }, geometry1);
  220. return result;
  221. }
  222. };
  223. template <typename Geometry1, typename DynamicGeometry2, typename Tag1>
  224. struct intersection<Geometry1, DynamicGeometry2, Tag1, dynamic_geometry_tag>
  225. {
  226. template <typename GeometryOut, typename Strategy>
  227. static inline bool apply(Geometry1 const& geometry1, DynamicGeometry2 const& geometry2,
  228. GeometryOut& geometry_out, Strategy const& strategy)
  229. {
  230. bool result = false;
  231. traits::visit<DynamicGeometry2>::apply([&](auto const& g2)
  232. {
  233. result = intersection
  234. <
  235. Geometry1,
  236. util::remove_cref_t<decltype(g2)>
  237. >::apply(geometry1, g2, geometry_out, strategy);
  238. }, geometry2);
  239. return result;
  240. }
  241. };
  242. template <typename DynamicGeometry1, typename DynamicGeometry2>
  243. struct intersection<DynamicGeometry1, DynamicGeometry2, dynamic_geometry_tag, dynamic_geometry_tag>
  244. {
  245. template <typename GeometryOut, typename Strategy>
  246. static inline bool apply(DynamicGeometry1 const& geometry1, DynamicGeometry2 const& geometry2,
  247. GeometryOut& geometry_out, Strategy const& strategy)
  248. {
  249. bool result = false;
  250. traits::visit<DynamicGeometry1, DynamicGeometry2>::apply([&](auto const& g1, auto const& g2)
  251. {
  252. result = intersection
  253. <
  254. util::remove_cref_t<decltype(g1)>,
  255. util::remove_cref_t<decltype(g2)>
  256. >::apply(g1, g2, geometry_out, strategy);
  257. }, geometry1, geometry2);
  258. return result;
  259. }
  260. };
  261. } // namespace resolve_dynamic
  262. /*!
  263. \brief \brief_calc2{intersection}
  264. \ingroup intersection
  265. \details \details_calc2{intersection, spatial set theoretic intersection}.
  266. \tparam Geometry1 \tparam_geometry
  267. \tparam Geometry2 \tparam_geometry
  268. \tparam GeometryOut Collection of geometries (e.g. std::vector, std::deque, boost::geometry::multi*) of which
  269. the value_type fulfills a \p_l_or_c concept, or it is the output geometry (e.g. for a box)
  270. \tparam Strategy \tparam_strategy{Intersection}
  271. \param geometry1 \param_geometry
  272. \param geometry2 \param_geometry
  273. \param geometry_out The output geometry, either a multi_point, multi_polygon,
  274. multi_linestring, or a box (for intersection of two boxes)
  275. \param strategy \param_strategy{intersection}
  276. \qbk{distinguish,with strategy}
  277. \qbk{[include reference/algorithms/intersection.qbk]}
  278. */
  279. template
  280. <
  281. typename Geometry1,
  282. typename Geometry2,
  283. typename GeometryOut,
  284. typename Strategy
  285. >
  286. inline bool intersection(Geometry1 const& geometry1,
  287. Geometry2 const& geometry2,
  288. GeometryOut& geometry_out,
  289. Strategy const& strategy)
  290. {
  291. return resolve_dynamic::intersection
  292. <
  293. Geometry1,
  294. Geometry2
  295. >::apply(geometry1, geometry2, geometry_out, strategy);
  296. }
  297. /*!
  298. \brief \brief_calc2{intersection}
  299. \ingroup intersection
  300. \details \details_calc2{intersection, spatial set theoretic intersection}.
  301. \tparam Geometry1 \tparam_geometry
  302. \tparam Geometry2 \tparam_geometry
  303. \tparam GeometryOut Collection of geometries (e.g. std::vector, std::deque, boost::geometry::multi*) of which
  304. the value_type fulfills a \p_l_or_c concept, or it is the output geometry (e.g. for a box)
  305. \param geometry1 \param_geometry
  306. \param geometry2 \param_geometry
  307. \param geometry_out The output geometry, either a multi_point, multi_polygon,
  308. multi_linestring, or a box (for intersection of two boxes)
  309. \qbk{[include reference/algorithms/intersection.qbk]}
  310. */
  311. template
  312. <
  313. typename Geometry1,
  314. typename Geometry2,
  315. typename GeometryOut
  316. >
  317. inline bool intersection(Geometry1 const& geometry1,
  318. Geometry2 const& geometry2,
  319. GeometryOut& geometry_out)
  320. {
  321. return resolve_dynamic::intersection
  322. <
  323. Geometry1,
  324. Geometry2
  325. >::apply(geometry1, geometry2, geometry_out, default_strategy());
  326. }
  327. }} // namespace boost::geometry
  328. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_INTERFACE_HPP