interface.hpp 13 KB

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