difference.hpp 13 KB

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