interface.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 2013-2022.
  4. // Modifications copyright (c) 2013-2022 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_RELATE_INTERFACE_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_INTERFACE_HPP
  11. #include <tuple>
  12. #include <boost/geometry/algorithms/detail/relate/de9im.hpp>
  13. #include <boost/geometry/algorithms/not_implemented.hpp>
  14. #include <boost/geometry/core/coordinate_dimension.hpp>
  15. #include <boost/geometry/core/tag.hpp>
  16. #include <boost/geometry/core/tags.hpp>
  17. #include <boost/geometry/core/topological_dimension.hpp>
  18. #include <boost/geometry/geometries/adapted/boost_variant.hpp>
  19. #include <boost/geometry/geometries/concepts/check.hpp>
  20. #include <boost/geometry/strategies/default_strategy.hpp>
  21. #include <boost/geometry/strategies/detail.hpp>
  22. #include <boost/geometry/strategies/relate/services.hpp>
  23. #include <boost/geometry/util/sequence.hpp>
  24. #include <boost/geometry/util/type_traits.hpp>
  25. namespace boost { namespace geometry {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail { namespace relate {
  28. // is_generic allows dispatch::relate to generate compile-time error
  29. template <typename Geometry1, typename Geometry2>
  30. struct is_generic
  31. {
  32. static const bool value = (util::is_polysegmental<Geometry1>::value
  33. && util::is_polysegmental<Geometry2>::value)
  34. ||
  35. (util::is_point<Geometry1>::value
  36. && util::is_polysegmental<Geometry2>::value)
  37. ||
  38. (util::is_polysegmental<Geometry1>::value
  39. && util::is_point<Geometry2>::value);
  40. };
  41. }} // namespace detail::relate
  42. #endif // DOXYGEN_NO_DETAIL
  43. #ifndef DOXYGEN_NO_DISPATCH
  44. namespace dispatch {
  45. template <typename Geometry1,
  46. typename Geometry2,
  47. typename Tag1 = typename geometry::tag<Geometry1>::type,
  48. typename Tag2 = typename geometry::tag<Geometry2>::type,
  49. int TopDim1 = geometry::topological_dimension<Geometry1>::value,
  50. int TopDim2 = geometry::topological_dimension<Geometry2>::value,
  51. bool IsGeneric = detail::relate::is_generic<Geometry1, Geometry2>::value
  52. >
  53. struct relate : not_implemented<Tag1, Tag2>
  54. {};
  55. } // namespace dispatch
  56. #endif // DOXYGEN_NO_DISPATCH
  57. #ifndef DOXYGEN_NO_DETAIL
  58. namespace detail { namespace relate {
  59. template <typename Geometry1, typename Geometry2>
  60. struct interruption_enabled
  61. {
  62. static const bool value =
  63. dispatch::relate<Geometry1, Geometry2>::interruption_enabled;
  64. };
  65. template <typename Geometry1, typename Geometry2, typename Result>
  66. struct result_handler_type
  67. : not_implemented<Result>
  68. {};
  69. template <typename Geometry1, typename Geometry2>
  70. struct result_handler_type<Geometry1, Geometry2, geometry::de9im::mask>
  71. {
  72. typedef mask_handler
  73. <
  74. geometry::de9im::mask,
  75. interruption_enabled
  76. <
  77. Geometry1,
  78. Geometry2
  79. >::value
  80. > type;
  81. };
  82. template <typename Geometry1, typename Geometry2, typename ...Masks>
  83. struct result_handler_type<Geometry1, Geometry2, std::tuple<Masks...>>
  84. {
  85. typedef mask_handler
  86. <
  87. std::tuple<Masks...>,
  88. interruption_enabled
  89. <
  90. Geometry1,
  91. Geometry2
  92. >::value
  93. > type;
  94. };
  95. template <typename Geometry1, typename Geometry2,
  96. char II, char IB, char IE,
  97. char BI, char BB, char BE,
  98. char EI, char EB, char EE>
  99. struct result_handler_type
  100. <
  101. Geometry1,
  102. Geometry2,
  103. geometry::de9im::static_mask<II, IB, IE, BI, BB, BE, EI, EB, EE>
  104. >
  105. {
  106. typedef static_mask_handler
  107. <
  108. geometry::de9im::static_mask<II, IB, IE, BI, BB, BE, EI, EB, EE>,
  109. interruption_enabled
  110. <
  111. Geometry1,
  112. Geometry2
  113. >::value
  114. > type;
  115. };
  116. template <typename Geometry1, typename Geometry2, typename ...StaticMasks>
  117. struct result_handler_type<Geometry1, Geometry2, util::type_sequence<StaticMasks...>>
  118. {
  119. typedef static_mask_handler
  120. <
  121. util::type_sequence<StaticMasks...>,
  122. interruption_enabled
  123. <
  124. Geometry1,
  125. Geometry2
  126. >::value
  127. > type;
  128. };
  129. }} // namespace detail::relate
  130. #endif // DOXYGEN_NO_DETAIL
  131. namespace resolve_strategy
  132. {
  133. template
  134. <
  135. typename Strategy,
  136. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategy>::value
  137. >
  138. struct relate
  139. {
  140. template <typename Geometry1, typename Geometry2, typename ResultHandler>
  141. static inline void apply(Geometry1 const& geometry1,
  142. Geometry2 const& geometry2,
  143. ResultHandler & handler,
  144. Strategy const& strategy)
  145. {
  146. dispatch::relate
  147. <
  148. Geometry1,
  149. Geometry2
  150. >::apply(geometry1, geometry2, handler, strategy);
  151. }
  152. };
  153. template <typename Strategy>
  154. struct relate<Strategy, false>
  155. {
  156. template <typename Geometry1, typename Geometry2, typename ResultHandler>
  157. static inline void apply(Geometry1 const& geometry1,
  158. Geometry2 const& geometry2,
  159. ResultHandler & handler,
  160. Strategy const& strategy)
  161. {
  162. using strategies::relate::services::strategy_converter;
  163. dispatch::relate
  164. <
  165. Geometry1,
  166. Geometry2
  167. >::apply(geometry1, geometry2, handler,
  168. strategy_converter<Strategy>::get(strategy));
  169. }
  170. };
  171. template <>
  172. struct relate<default_strategy, false>
  173. {
  174. template <typename Geometry1, typename Geometry2, typename ResultHandler>
  175. static inline void apply(Geometry1 const& geometry1,
  176. Geometry2 const& geometry2,
  177. ResultHandler & handler,
  178. default_strategy)
  179. {
  180. typedef typename strategies::relate::services::default_strategy
  181. <
  182. Geometry1,
  183. Geometry2
  184. >::type strategy_type;
  185. dispatch::relate
  186. <
  187. Geometry1,
  188. Geometry2
  189. >::apply(geometry1, geometry2, handler, strategy_type());
  190. }
  191. };
  192. } // resolve_strategy
  193. namespace resolve_dynamic
  194. {
  195. template
  196. <
  197. typename Geometry1, typename Geometry2,
  198. typename Tag1 = typename geometry::tag<Geometry1>::type,
  199. typename Tag2 = typename geometry::tag<Geometry2>::type
  200. >
  201. struct relate
  202. {
  203. template <typename Mask, typename Strategy>
  204. static inline bool apply(Geometry1 const& geometry1,
  205. Geometry2 const& geometry2,
  206. Mask const& mask,
  207. Strategy const& strategy)
  208. {
  209. concepts::check<Geometry1 const>();
  210. concepts::check<Geometry2 const>();
  211. assert_dimension_equal<Geometry1, Geometry2>();
  212. typename detail::relate::result_handler_type
  213. <
  214. Geometry1,
  215. Geometry2,
  216. Mask
  217. >::type handler(mask);
  218. resolve_strategy::relate<Strategy>::apply(geometry1, geometry2, handler, strategy);
  219. return handler.result();
  220. }
  221. };
  222. template <typename Geometry1, typename Geometry2, typename Tag2>
  223. struct relate<Geometry1, Geometry2, dynamic_geometry_tag, Tag2>
  224. {
  225. template <typename Mask, typename Strategy>
  226. static inline bool apply(Geometry1 const& geometry1,
  227. Geometry2 const& geometry2,
  228. Mask const& mask,
  229. Strategy const& strategy)
  230. {
  231. bool result = false;
  232. traits::visit<Geometry1>::apply([&](auto const& g1)
  233. {
  234. result = relate
  235. <
  236. util::remove_cref_t<decltype(g1)>,
  237. Geometry2
  238. >::apply(g1, geometry2, mask, strategy);
  239. }, geometry1);
  240. return result;
  241. }
  242. };
  243. template <typename Geometry1, typename Geometry2, typename Tag1>
  244. struct relate<Geometry1, Geometry2, Tag1, dynamic_geometry_tag>
  245. {
  246. template <typename Mask, typename Strategy>
  247. static inline bool apply(Geometry1 const& geometry1,
  248. Geometry2 const& geometry2,
  249. Mask const& mask,
  250. Strategy const& strategy)
  251. {
  252. bool result = false;
  253. traits::visit<Geometry2>::apply([&](auto const& g2)
  254. {
  255. result = relate
  256. <
  257. Geometry1,
  258. util::remove_cref_t<decltype(g2)>
  259. >::apply(geometry1, g2, mask, strategy);
  260. }, geometry2);
  261. return result;
  262. }
  263. };
  264. template <typename Geometry1, typename Geometry2>
  265. struct relate<Geometry1, Geometry2, dynamic_geometry_tag, dynamic_geometry_tag>
  266. {
  267. template <typename Mask, typename Strategy>
  268. static inline bool apply(Geometry1 const& geometry1,
  269. Geometry2 const& geometry2,
  270. Mask const& mask,
  271. Strategy const& strategy)
  272. {
  273. bool result = false;
  274. traits::visit<Geometry1, Geometry2>::apply([&](auto const& g1, auto const& g2)
  275. {
  276. result = relate
  277. <
  278. util::remove_cref_t<decltype(g1)>,
  279. util::remove_cref_t<decltype(g2)>
  280. >::apply(g1, g2, mask, strategy);
  281. }, geometry1, geometry2);
  282. return result;
  283. }
  284. };
  285. } // namespace resolve_dynamic
  286. /*!
  287. \brief Checks relation between a pair of geometries defined by a mask.
  288. \ingroup relate
  289. \tparam Geometry1 \tparam_geometry
  290. \tparam Geometry2 \tparam_geometry
  291. \tparam Mask An intersection model Mask type.
  292. \tparam Strategy \tparam_strategy{Relate}
  293. \param geometry1 \param_geometry
  294. \param geometry2 \param_geometry
  295. \param mask An intersection model mask object.
  296. \param strategy \param_strategy{relate}
  297. \return true if the relation is compatible with the mask, false otherwise.
  298. \qbk{distinguish,with strategy}
  299. \qbk{[include reference/algorithms/relate.qbk]}
  300. */
  301. template <typename Geometry1, typename Geometry2, typename Mask, typename Strategy>
  302. inline bool relate(Geometry1 const& geometry1,
  303. Geometry2 const& geometry2,
  304. Mask const& mask,
  305. Strategy const& strategy)
  306. {
  307. return resolve_dynamic::relate
  308. <
  309. Geometry1,
  310. Geometry2
  311. >::apply(geometry1, geometry2, mask, strategy);
  312. }
  313. /*!
  314. \brief Checks relation between a pair of geometries defined by a mask.
  315. \ingroup relate
  316. \tparam Geometry1 \tparam_geometry
  317. \tparam Geometry2 \tparam_geometry
  318. \tparam Mask An intersection model Mask type.
  319. \param geometry1 \param_geometry
  320. \param geometry2 \param_geometry
  321. \param mask An intersection model mask object.
  322. \return true if the relation is compatible with the mask, false otherwise.
  323. \qbk{[include reference/algorithms/relate.qbk]}
  324. */
  325. template <typename Geometry1, typename Geometry2, typename Mask>
  326. inline bool relate(Geometry1 const& geometry1,
  327. Geometry2 const& geometry2,
  328. Mask const& mask)
  329. {
  330. return resolve_dynamic::relate
  331. <
  332. Geometry1,
  333. Geometry2
  334. >::apply(geometry1, geometry2, mask, default_strategy());
  335. }
  336. }} // namespace boost::geometry
  337. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_INTERFACE_HPP