interface.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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, 2017, 2019.
  4. // Modifications copyright (c) 2014-2019, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_INTERFACE_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_INTERFACE_HPP
  11. #include <boost/variant/apply_visitor.hpp>
  12. #include <boost/variant/static_visitor.hpp>
  13. #include <boost/variant/variant_fwd.hpp>
  14. #include <boost/geometry/algorithms/detail/overlay/intersection_insert.hpp>
  15. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  16. #include <boost/geometry/strategies/default_strategy.hpp>
  17. #include <boost/geometry/util/range.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. #ifndef DOXYGEN_NO_DISPATCH
  21. namespace dispatch
  22. {
  23. // By default, all is forwarded to the intersection_insert-dispatcher
  24. template
  25. <
  26. typename Geometry1, typename Geometry2,
  27. typename Tag1 = typename geometry::tag<Geometry1>::type,
  28. typename Tag2 = typename geometry::tag<Geometry2>::type,
  29. bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
  30. >
  31. struct intersection
  32. {
  33. template <typename RobustPolicy, typename GeometryOut, typename Strategy>
  34. static inline bool apply(Geometry1 const& geometry1,
  35. Geometry2 const& geometry2,
  36. RobustPolicy const& robust_policy,
  37. GeometryOut& geometry_out,
  38. Strategy const& strategy)
  39. {
  40. typedef typename boost::range_value<GeometryOut>::type OneOut;
  41. intersection_insert
  42. <
  43. Geometry1, Geometry2, OneOut,
  44. overlay_intersection
  45. >::apply(geometry1, geometry2, robust_policy,
  46. range::back_inserter(geometry_out), strategy);
  47. return true;
  48. }
  49. };
  50. // If reversal is needed, perform it
  51. template
  52. <
  53. typename Geometry1, typename Geometry2,
  54. typename Tag1, typename Tag2
  55. >
  56. struct intersection
  57. <
  58. Geometry1, Geometry2,
  59. Tag1, Tag2,
  60. true
  61. >
  62. : intersection<Geometry2, Geometry1, Tag2, Tag1, false>
  63. {
  64. template <typename RobustPolicy, typename GeometryOut, typename Strategy>
  65. static inline bool apply(
  66. Geometry1 const& g1,
  67. Geometry2 const& g2,
  68. RobustPolicy const& robust_policy,
  69. GeometryOut& out,
  70. Strategy const& strategy)
  71. {
  72. return intersection
  73. <
  74. Geometry2, Geometry1,
  75. Tag2, Tag1,
  76. false
  77. >::apply(g2, g1, robust_policy, out, strategy);
  78. }
  79. };
  80. } // namespace dispatch
  81. #endif // DOXYGEN_NO_DISPATCH
  82. namespace resolve_strategy {
  83. struct intersection
  84. {
  85. template
  86. <
  87. typename Geometry1,
  88. typename Geometry2,
  89. typename GeometryOut,
  90. typename Strategy
  91. >
  92. static inline bool apply(Geometry1 const& geometry1,
  93. Geometry2 const& geometry2,
  94. GeometryOut & geometry_out,
  95. Strategy const& strategy)
  96. {
  97. typedef typename geometry::rescale_overlay_policy_type
  98. <
  99. Geometry1,
  100. Geometry2,
  101. typename Strategy::cs_tag
  102. >::type rescale_policy_type;
  103. rescale_policy_type robust_policy
  104. = geometry::get_rescale_policy<rescale_policy_type>(
  105. geometry1, geometry2, strategy);
  106. return dispatch::intersection
  107. <
  108. Geometry1,
  109. Geometry2
  110. >::apply(geometry1, geometry2, robust_policy, geometry_out,
  111. strategy);
  112. }
  113. template
  114. <
  115. typename Geometry1,
  116. typename Geometry2,
  117. typename GeometryOut
  118. >
  119. static inline bool apply(Geometry1 const& geometry1,
  120. Geometry2 const& geometry2,
  121. GeometryOut & geometry_out,
  122. default_strategy)
  123. {
  124. typedef typename geometry::rescale_overlay_policy_type
  125. <
  126. Geometry1,
  127. Geometry2,
  128. typename geometry::cs_tag<Geometry1>::type
  129. >::type rescale_policy_type;
  130. typename strategy::relate::services::default_strategy
  131. <
  132. Geometry1, Geometry2
  133. >::type strategy;
  134. rescale_policy_type robust_policy
  135. = geometry::get_rescale_policy<rescale_policy_type>(
  136. geometry1, geometry2, strategy);
  137. return dispatch::intersection
  138. <
  139. Geometry1,
  140. Geometry2
  141. >::apply(geometry1, geometry2, robust_policy, geometry_out,
  142. strategy);
  143. }
  144. };
  145. } // resolve_strategy
  146. namespace resolve_variant
  147. {
  148. template <typename Geometry1, typename Geometry2>
  149. struct intersection
  150. {
  151. template <typename GeometryOut, typename Strategy>
  152. static inline bool apply(Geometry1 const& geometry1,
  153. Geometry2 const& geometry2,
  154. GeometryOut& geometry_out,
  155. Strategy const& strategy)
  156. {
  157. concepts::check<Geometry1 const>();
  158. concepts::check<Geometry2 const>();
  159. return resolve_strategy::intersection::apply(geometry1,
  160. geometry2,
  161. geometry_out,
  162. strategy);
  163. }
  164. };
  165. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  166. struct intersection<variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  167. {
  168. template <typename GeometryOut, typename Strategy>
  169. struct visitor: static_visitor<bool>
  170. {
  171. Geometry2 const& m_geometry2;
  172. GeometryOut& m_geometry_out;
  173. Strategy const& m_strategy;
  174. visitor(Geometry2 const& geometry2,
  175. GeometryOut& geometry_out,
  176. Strategy const& strategy)
  177. : m_geometry2(geometry2)
  178. , m_geometry_out(geometry_out)
  179. , m_strategy(strategy)
  180. {}
  181. template <typename Geometry1>
  182. bool operator()(Geometry1 const& geometry1) const
  183. {
  184. return intersection
  185. <
  186. Geometry1,
  187. Geometry2
  188. >::apply(geometry1, m_geometry2, m_geometry_out, m_strategy);
  189. }
  190. };
  191. template <typename GeometryOut, typename Strategy>
  192. static inline bool
  193. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  194. Geometry2 const& geometry2,
  195. GeometryOut& geometry_out,
  196. Strategy const& strategy)
  197. {
  198. return boost::apply_visitor(visitor<GeometryOut, Strategy>(geometry2,
  199. geometry_out,
  200. strategy),
  201. geometry1);
  202. }
  203. };
  204. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  205. struct intersection<Geometry1, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  206. {
  207. template <typename GeometryOut, typename Strategy>
  208. struct visitor: static_visitor<bool>
  209. {
  210. Geometry1 const& m_geometry1;
  211. GeometryOut& m_geometry_out;
  212. Strategy const& m_strategy;
  213. visitor(Geometry1 const& geometry1,
  214. GeometryOut& geometry_out,
  215. Strategy const& strategy)
  216. : m_geometry1(geometry1)
  217. , m_geometry_out(geometry_out)
  218. , m_strategy(strategy)
  219. {}
  220. template <typename Geometry2>
  221. bool operator()(Geometry2 const& geometry2) const
  222. {
  223. return intersection
  224. <
  225. Geometry1,
  226. Geometry2
  227. >::apply(m_geometry1, geometry2, m_geometry_out, m_strategy);
  228. }
  229. };
  230. template <typename GeometryOut, typename Strategy>
  231. static inline bool
  232. apply(Geometry1 const& geometry1,
  233. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
  234. GeometryOut& geometry_out,
  235. Strategy const& strategy)
  236. {
  237. return boost::apply_visitor(visitor<GeometryOut, Strategy>(geometry1,
  238. geometry_out,
  239. strategy),
  240. geometry2);
  241. }
  242. };
  243. template <BOOST_VARIANT_ENUM_PARAMS(typename T1), BOOST_VARIANT_ENUM_PARAMS(typename T2)>
  244. struct intersection<variant<BOOST_VARIANT_ENUM_PARAMS(T1)>, variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
  245. {
  246. template <typename GeometryOut, typename Strategy>
  247. struct visitor: static_visitor<bool>
  248. {
  249. GeometryOut& m_geometry_out;
  250. Strategy const& m_strategy;
  251. visitor(GeometryOut& geometry_out, Strategy const& strategy)
  252. : m_geometry_out(geometry_out)
  253. , m_strategy(strategy)
  254. {}
  255. template <typename Geometry1, typename Geometry2>
  256. bool operator()(Geometry1 const& geometry1,
  257. Geometry2 const& geometry2) const
  258. {
  259. return intersection
  260. <
  261. Geometry1,
  262. Geometry2
  263. >::apply(geometry1, geometry2, m_geometry_out, m_strategy);
  264. }
  265. };
  266. template <typename GeometryOut, typename Strategy>
  267. static inline bool
  268. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  269. variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
  270. GeometryOut& geometry_out,
  271. Strategy const& strategy)
  272. {
  273. return boost::apply_visitor(visitor<GeometryOut, Strategy>(geometry_out,
  274. strategy),
  275. geometry1, geometry2);
  276. }
  277. };
  278. } // namespace resolve_variant
  279. /*!
  280. \brief \brief_calc2{intersection}
  281. \ingroup intersection
  282. \details \details_calc2{intersection, spatial set theoretic intersection}.
  283. \tparam Geometry1 \tparam_geometry
  284. \tparam Geometry2 \tparam_geometry
  285. \tparam GeometryOut Collection of geometries (e.g. std::vector, std::deque, boost::geometry::multi*) of which
  286. the value_type fulfills a \p_l_or_c concept, or it is the output geometry (e.g. for a box)
  287. \tparam Strategy \tparam_strategy{Intersection}
  288. \param geometry1 \param_geometry
  289. \param geometry2 \param_geometry
  290. \param geometry_out The output geometry, either a multi_point, multi_polygon,
  291. multi_linestring, or a box (for intersection of two boxes)
  292. \param strategy \param_strategy{intersection}
  293. \qbk{distinguish,with strategy}
  294. \qbk{[include reference/algorithms/intersection.qbk]}
  295. */
  296. template
  297. <
  298. typename Geometry1,
  299. typename Geometry2,
  300. typename GeometryOut,
  301. typename Strategy
  302. >
  303. inline bool intersection(Geometry1 const& geometry1,
  304. Geometry2 const& geometry2,
  305. GeometryOut& geometry_out,
  306. Strategy const& strategy)
  307. {
  308. return resolve_variant::intersection
  309. <
  310. Geometry1,
  311. Geometry2
  312. >::apply(geometry1, geometry2, geometry_out, strategy);
  313. }
  314. /*!
  315. \brief \brief_calc2{intersection}
  316. \ingroup intersection
  317. \details \details_calc2{intersection, spatial set theoretic intersection}.
  318. \tparam Geometry1 \tparam_geometry
  319. \tparam Geometry2 \tparam_geometry
  320. \tparam GeometryOut Collection of geometries (e.g. std::vector, std::deque, boost::geometry::multi*) of which
  321. the value_type fulfills a \p_l_or_c concept, or it is the output geometry (e.g. for a box)
  322. \param geometry1 \param_geometry
  323. \param geometry2 \param_geometry
  324. \param geometry_out The output geometry, either a multi_point, multi_polygon,
  325. multi_linestring, or a box (for intersection of two boxes)
  326. \qbk{[include reference/algorithms/intersection.qbk]}
  327. */
  328. template
  329. <
  330. typename Geometry1,
  331. typename Geometry2,
  332. typename GeometryOut
  333. >
  334. inline bool intersection(Geometry1 const& geometry1,
  335. Geometry2 const& geometry2,
  336. GeometryOut& geometry_out)
  337. {
  338. return resolve_variant::intersection
  339. <
  340. Geometry1,
  341. Geometry2
  342. >::apply(geometry1, geometry2, geometry_out, default_strategy());
  343. }
  344. }} // namespace boost::geometry
  345. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_INTERSECTION_INTERFACE_HPP