difference.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 2017, 2019.
  4. // Modifications copyright (c) 2017, 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_DIFFERENCE_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DIFFERENCE_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_DETAIL
  21. namespace detail { namespace difference
  22. {
  23. /*!
  24. \brief_calc2{difference} \brief_strategy
  25. \ingroup difference
  26. \details \details_calc2{difference_insert, spatial set theoretic difference}
  27. \brief_strategy. \details_inserter{difference}
  28. \tparam GeometryOut output geometry type, must be specified
  29. \tparam Geometry1 \tparam_geometry
  30. \tparam Geometry2 \tparam_geometry
  31. \tparam OutputIterator output iterator
  32. \tparam Strategy \tparam_strategy_overlay
  33. \param geometry1 \param_geometry
  34. \param geometry2 \param_geometry
  35. \param out \param_out{difference}
  36. \param strategy \param_strategy{difference}
  37. \return \return_out
  38. \qbk{distinguish,with strategy}
  39. */
  40. template
  41. <
  42. typename GeometryOut,
  43. typename Geometry1,
  44. typename Geometry2,
  45. typename OutputIterator,
  46. typename Strategy
  47. >
  48. inline OutputIterator difference_insert(Geometry1 const& geometry1,
  49. Geometry2 const& geometry2,
  50. OutputIterator out,
  51. Strategy const& strategy)
  52. {
  53. concepts::check<Geometry1 const>();
  54. concepts::check<Geometry2 const>();
  55. concepts::check<GeometryOut>();
  56. typedef typename geometry::rescale_overlay_policy_type
  57. <
  58. Geometry1,
  59. Geometry2,
  60. typename Strategy::cs_tag
  61. >::type rescale_policy_type;
  62. rescale_policy_type robust_policy
  63. = geometry::get_rescale_policy<rescale_policy_type>(
  64. geometry1, geometry2, strategy);
  65. return geometry::dispatch::intersection_insert
  66. <
  67. Geometry1, Geometry2,
  68. GeometryOut,
  69. overlay_difference,
  70. geometry::detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
  71. geometry::detail::overlay::do_reverse<geometry::point_order<Geometry2>::value, true>::value
  72. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  73. }
  74. /*!
  75. \brief_calc2{difference}
  76. \ingroup difference
  77. \details \details_calc2{difference_insert, spatial set theoretic difference}.
  78. \details_insert{difference}
  79. \tparam GeometryOut output geometry type, must be specified
  80. \tparam Geometry1 \tparam_geometry
  81. \tparam Geometry2 \tparam_geometry
  82. \tparam OutputIterator output iterator
  83. \param geometry1 \param_geometry
  84. \param geometry2 \param_geometry
  85. \param out \param_out{difference}
  86. \return \return_out
  87. \qbk{[include reference/algorithms/difference_insert.qbk]}
  88. */
  89. template
  90. <
  91. typename GeometryOut,
  92. typename Geometry1,
  93. typename Geometry2,
  94. typename OutputIterator
  95. >
  96. inline OutputIterator difference_insert(Geometry1 const& geometry1,
  97. Geometry2 const& geometry2,
  98. OutputIterator out)
  99. {
  100. typedef typename strategy::relate::services::default_strategy
  101. <
  102. Geometry1,
  103. Geometry2
  104. >::type strategy_type;
  105. return difference_insert<GeometryOut>(geometry1, geometry2, out,
  106. strategy_type());
  107. }
  108. }} // namespace detail::difference
  109. #endif // DOXYGEN_NO_DETAIL
  110. namespace resolve_strategy {
  111. struct difference
  112. {
  113. template
  114. <
  115. typename Geometry1,
  116. typename Geometry2,
  117. typename Collection,
  118. typename Strategy
  119. >
  120. static inline void apply(Geometry1 const& geometry1,
  121. Geometry2 const& geometry2,
  122. Collection & output_collection,
  123. Strategy const& strategy)
  124. {
  125. typedef typename boost::range_value<Collection>::type geometry_out;
  126. detail::difference::difference_insert<geometry_out>(
  127. geometry1, geometry2,
  128. range::back_inserter(output_collection),
  129. strategy);
  130. }
  131. template
  132. <
  133. typename Geometry1,
  134. typename Geometry2,
  135. typename Collection
  136. >
  137. static inline void apply(Geometry1 const& geometry1,
  138. Geometry2 const& geometry2,
  139. Collection & output_collection,
  140. default_strategy)
  141. {
  142. typedef typename boost::range_value<Collection>::type geometry_out;
  143. detail::difference::difference_insert<geometry_out>(
  144. geometry1, geometry2,
  145. range::back_inserter(output_collection));
  146. }
  147. };
  148. } // resolve_strategy
  149. namespace resolve_variant
  150. {
  151. template <typename Geometry1, typename Geometry2>
  152. struct difference
  153. {
  154. template <typename Collection, typename Strategy>
  155. static inline void apply(Geometry1 const& geometry1,
  156. Geometry2 const& geometry2,
  157. Collection& output_collection,
  158. Strategy const& strategy)
  159. {
  160. resolve_strategy::difference::apply(geometry1, geometry2,
  161. output_collection,
  162. strategy);
  163. }
  164. };
  165. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  166. struct difference<variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  167. {
  168. template <typename Collection, typename Strategy>
  169. struct visitor: static_visitor<>
  170. {
  171. Geometry2 const& m_geometry2;
  172. Collection& m_output_collection;
  173. Strategy const& m_strategy;
  174. visitor(Geometry2 const& geometry2,
  175. Collection& output_collection,
  176. Strategy const& strategy)
  177. : m_geometry2(geometry2)
  178. , m_output_collection(output_collection)
  179. , m_strategy(strategy)
  180. {}
  181. template <typename Geometry1>
  182. void operator()(Geometry1 const& geometry1) const
  183. {
  184. difference
  185. <
  186. Geometry1,
  187. Geometry2
  188. >::apply(geometry1, m_geometry2, m_output_collection, m_strategy);
  189. }
  190. };
  191. template <typename Collection, typename Strategy>
  192. static inline void
  193. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  194. Geometry2 const& geometry2,
  195. Collection& output_collection,
  196. Strategy const& strategy)
  197. {
  198. boost::apply_visitor(visitor<Collection, Strategy>(geometry2,
  199. output_collection,
  200. strategy),
  201. geometry1);
  202. }
  203. };
  204. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  205. struct difference<Geometry1, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  206. {
  207. template <typename Collection, typename Strategy>
  208. struct visitor: static_visitor<>
  209. {
  210. Geometry1 const& m_geometry1;
  211. Collection& m_output_collection;
  212. Strategy const& m_strategy;
  213. visitor(Geometry1 const& geometry1,
  214. Collection& output_collection,
  215. Strategy const& strategy)
  216. : m_geometry1(geometry1)
  217. , m_output_collection(output_collection)
  218. , m_strategy(strategy)
  219. {}
  220. template <typename Geometry2>
  221. void operator()(Geometry2 const& geometry2) const
  222. {
  223. difference
  224. <
  225. Geometry1,
  226. Geometry2
  227. >::apply(m_geometry1, geometry2, m_output_collection, m_strategy);
  228. }
  229. };
  230. template <typename Collection, typename Strategy>
  231. static inline void
  232. apply(Geometry1 const& geometry1,
  233. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
  234. Collection& output_collection,
  235. Strategy const& strategy)
  236. {
  237. boost::apply_visitor(visitor<Collection, Strategy>(geometry1,
  238. output_collection,
  239. strategy),
  240. geometry2);
  241. }
  242. };
  243. template <BOOST_VARIANT_ENUM_PARAMS(typename T1), BOOST_VARIANT_ENUM_PARAMS(typename T2)>
  244. struct difference<variant<BOOST_VARIANT_ENUM_PARAMS(T1)>, variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
  245. {
  246. template <typename Collection, typename Strategy>
  247. struct visitor: static_visitor<>
  248. {
  249. Collection& m_output_collection;
  250. Strategy const& m_strategy;
  251. visitor(Collection& output_collection, Strategy const& strategy)
  252. : m_output_collection(output_collection)
  253. , m_strategy(strategy)
  254. {}
  255. template <typename Geometry1, typename Geometry2>
  256. void operator()(Geometry1 const& geometry1,
  257. Geometry2 const& geometry2) const
  258. {
  259. difference
  260. <
  261. Geometry1,
  262. Geometry2
  263. >::apply(geometry1, geometry2, m_output_collection, m_strategy);
  264. }
  265. };
  266. template <typename Collection, typename Strategy>
  267. static inline void
  268. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  269. variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
  270. Collection& output_collection,
  271. Strategy const& strategy)
  272. {
  273. boost::apply_visitor(visitor<Collection, Strategy>(output_collection,
  274. strategy),
  275. geometry1, geometry2);
  276. }
  277. };
  278. } // namespace resolve_variant
  279. /*!
  280. \brief_calc2{difference}
  281. \ingroup difference
  282. \details \details_calc2{difference, spatial set theoretic difference}.
  283. \tparam Geometry1 \tparam_geometry
  284. \tparam Geometry2 \tparam_geometry
  285. \tparam Collection \tparam_output_collection
  286. \tparam Strategy \tparam_strategy{Difference}
  287. \param geometry1 \param_geometry
  288. \param geometry2 \param_geometry
  289. \param output_collection the output collection
  290. \param strategy \param_strategy{difference}
  291. \qbk{distinguish,with strategy}
  292. \qbk{[include reference/algorithms/difference.qbk]}
  293. */
  294. template
  295. <
  296. typename Geometry1,
  297. typename Geometry2,
  298. typename Collection,
  299. typename Strategy
  300. >
  301. inline void difference(Geometry1 const& geometry1,
  302. Geometry2 const& geometry2,
  303. Collection& output_collection,
  304. Strategy const& strategy)
  305. {
  306. resolve_variant::difference
  307. <
  308. Geometry1,
  309. Geometry2
  310. >::apply(geometry1, geometry2, output_collection, strategy);
  311. }
  312. /*!
  313. \brief_calc2{difference}
  314. \ingroup difference
  315. \details \details_calc2{difference, spatial set theoretic difference}.
  316. \tparam Geometry1 \tparam_geometry
  317. \tparam Geometry2 \tparam_geometry
  318. \tparam Collection \tparam_output_collection
  319. \param geometry1 \param_geometry
  320. \param geometry2 \param_geometry
  321. \param output_collection the output collection
  322. \qbk{[include reference/algorithms/difference.qbk]}
  323. */
  324. template
  325. <
  326. typename Geometry1,
  327. typename Geometry2,
  328. typename Collection
  329. >
  330. inline void difference(Geometry1 const& geometry1,
  331. Geometry2 const& geometry2,
  332. Collection& output_collection)
  333. {
  334. resolve_variant::difference
  335. <
  336. Geometry1,
  337. Geometry2
  338. >::apply(geometry1, geometry2, output_collection, default_strategy());
  339. }
  340. }} // namespace boost::geometry
  341. #endif // BOOST_GEOMETRY_ALGORITHMS_DIFFERENCE_HPP