sym_difference.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2015, 2017, 2019.
  4. // Modifications copyright (c) 2015-2019 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_SYM_DIFFERENCE_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_SYM_DIFFERENCE_HPP
  12. #include <algorithm>
  13. #include <iterator>
  14. #include <vector>
  15. #include <boost/variant/apply_visitor.hpp>
  16. #include <boost/variant/static_visitor.hpp>
  17. #include <boost/variant/variant_fwd.hpp>
  18. #include <boost/geometry/algorithms/intersection.hpp>
  19. #include <boost/geometry/algorithms/union.hpp>
  20. #include <boost/geometry/geometries/multi_polygon.hpp>
  21. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  22. #include <boost/geometry/strategies/default_strategy.hpp>
  23. #include <boost/geometry/util/range.hpp>
  24. namespace boost { namespace geometry
  25. {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail { namespace sym_difference
  28. {
  29. template <typename GeometryOut>
  30. struct compute_difference
  31. {
  32. template
  33. <
  34. typename Geometry1,
  35. typename Geometry2,
  36. typename RobustPolicy,
  37. typename OutputIterator,
  38. typename Strategy
  39. >
  40. static inline OutputIterator apply(Geometry1 const& geometry1,
  41. Geometry2 const& geometry2,
  42. RobustPolicy const& robust_policy,
  43. OutputIterator out,
  44. Strategy const& strategy)
  45. {
  46. return geometry::dispatch::intersection_insert
  47. <
  48. Geometry1,
  49. Geometry2,
  50. GeometryOut,
  51. overlay_difference,
  52. geometry::detail::overlay::do_reverse
  53. <
  54. geometry::point_order<Geometry1>::value
  55. >::value,
  56. geometry::detail::overlay::do_reverse
  57. <
  58. geometry::point_order<Geometry2>::value, true
  59. >::value
  60. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  61. }
  62. };
  63. template <typename GeometryOut, typename Geometry1, typename Geometry2>
  64. struct sym_difference_generic
  65. {
  66. template
  67. <
  68. typename RobustPolicy,
  69. typename OutputIterator,
  70. typename Strategy
  71. >
  72. static inline OutputIterator apply(Geometry1 const& geometry1,
  73. Geometry2 const& geometry2,
  74. RobustPolicy const& robust_policy,
  75. OutputIterator out,
  76. Strategy const& strategy)
  77. {
  78. out = compute_difference
  79. <
  80. GeometryOut
  81. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  82. return compute_difference
  83. <
  84. GeometryOut
  85. >::apply(geometry2, geometry1, robust_policy, out, strategy);
  86. }
  87. };
  88. template <typename GeometryOut, typename Areal1, typename Areal2>
  89. struct sym_difference_areal_areal
  90. {
  91. template
  92. <
  93. typename RobustPolicy,
  94. typename OutputIterator,
  95. typename Strategy
  96. >
  97. static inline OutputIterator apply(Areal1 const& areal1,
  98. Areal2 const& areal2,
  99. RobustPolicy const& robust_policy,
  100. OutputIterator out,
  101. Strategy const& strategy)
  102. {
  103. typedef geometry::model::multi_polygon
  104. <
  105. GeometryOut
  106. > helper_geometry_type;
  107. helper_geometry_type diff12, diff21;
  108. std::back_insert_iterator<helper_geometry_type> oit12(diff12);
  109. std::back_insert_iterator<helper_geometry_type> oit21(diff21);
  110. compute_difference
  111. <
  112. GeometryOut
  113. >::apply(areal1, areal2, robust_policy, oit12, strategy);
  114. compute_difference
  115. <
  116. GeometryOut
  117. >::apply(areal2, areal1, robust_policy, oit21, strategy);
  118. return geometry::dispatch::union_insert
  119. <
  120. helper_geometry_type,
  121. helper_geometry_type,
  122. GeometryOut
  123. >::apply(diff12, diff21, robust_policy, out, strategy);
  124. }
  125. };
  126. }} // namespace detail::sym_difference
  127. #endif // DOXYGEN_NO_DETAIL
  128. #ifndef DOXYGEN_NO_DISPATCH
  129. namespace dispatch
  130. {
  131. template
  132. <
  133. typename Geometry1,
  134. typename Geometry2,
  135. typename GeometryOut,
  136. typename TagIn1 = typename geometry::tag_cast
  137. <
  138. typename tag<Geometry1>::type, areal_tag
  139. >::type,
  140. typename TagIn2 = typename geometry::tag_cast
  141. <
  142. typename tag<Geometry2>::type, areal_tag
  143. >::type,
  144. typename TagOut = typename geometry::tag<GeometryOut>::type
  145. >
  146. struct sym_difference_insert
  147. : detail::sym_difference::sym_difference_generic
  148. <
  149. GeometryOut, Geometry1, Geometry2
  150. >
  151. {};
  152. template
  153. <
  154. typename Areal1,
  155. typename Areal2,
  156. typename GeometryOut,
  157. typename TagOut
  158. >
  159. struct sym_difference_insert
  160. <
  161. Areal1, Areal2, GeometryOut,
  162. areal_tag, areal_tag, TagOut
  163. > : detail::sym_difference::sym_difference_areal_areal
  164. <
  165. GeometryOut, Areal1, Areal2
  166. >
  167. {};
  168. } // namespace dispatch
  169. #endif // DOXYGEN_NO_DISPATCH
  170. #ifndef DOXYGEN_NO_DETAIL
  171. namespace detail { namespace sym_difference
  172. {
  173. /*!
  174. \brief \brief_calc2{symmetric difference} \brief_strategy
  175. \ingroup sym_difference
  176. \details \details_calc2{symmetric difference, spatial set theoretic symmetric difference (XOR)}
  177. \brief_strategy. \details_insert{sym_difference}
  178. \tparam GeometryOut output geometry type, must be specified
  179. \tparam Geometry1 \tparam_geometry
  180. \tparam Geometry2 \tparam_geometry
  181. \tparam Strategy \tparam_strategy_overlay
  182. \param geometry1 \param_geometry
  183. \param geometry2 \param_geometry
  184. \param out \param_out{difference}
  185. \param strategy \param_strategy{difference}
  186. \return \return_out
  187. \qbk{distinguish,with strategy}
  188. */
  189. template
  190. <
  191. typename GeometryOut,
  192. typename Geometry1,
  193. typename Geometry2,
  194. typename OutputIterator,
  195. typename Strategy
  196. >
  197. inline OutputIterator sym_difference_insert(Geometry1 const& geometry1,
  198. Geometry2 const& geometry2,
  199. OutputIterator out,
  200. Strategy const& strategy)
  201. {
  202. concepts::check<Geometry1 const>();
  203. concepts::check<Geometry2 const>();
  204. concepts::check<GeometryOut>();
  205. typedef typename geometry::rescale_overlay_policy_type
  206. <
  207. Geometry1,
  208. Geometry2,
  209. typename Strategy::cs_tag
  210. >::type rescale_policy_type;
  211. rescale_policy_type robust_policy
  212. = geometry::get_rescale_policy<rescale_policy_type>(
  213. geometry1, geometry2, strategy);
  214. return dispatch::sym_difference_insert
  215. <
  216. Geometry1, Geometry2, GeometryOut
  217. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  218. }
  219. /*!
  220. \brief \brief_calc2{symmetric difference}
  221. \ingroup sym_difference
  222. \details \details_calc2{symmetric difference, spatial set theoretic symmetric difference (XOR)}
  223. \details_insert{sym_difference}
  224. \tparam GeometryOut output geometry type, must be specified
  225. \tparam Geometry1 \tparam_geometry
  226. \tparam Geometry2 \tparam_geometry
  227. \param geometry1 \param_geometry
  228. \param geometry2 \param_geometry
  229. \param out \param_out{difference}
  230. \return \return_out
  231. */
  232. template
  233. <
  234. typename GeometryOut,
  235. typename Geometry1,
  236. typename Geometry2,
  237. typename OutputIterator
  238. >
  239. inline OutputIterator sym_difference_insert(Geometry1 const& geometry1,
  240. Geometry2 const& geometry2, OutputIterator out)
  241. {
  242. concepts::check<Geometry1 const>();
  243. concepts::check<Geometry2 const>();
  244. concepts::check<GeometryOut>();
  245. typedef typename strategy::intersection::services::default_strategy
  246. <
  247. typename cs_tag<GeometryOut>::type
  248. >::type strategy_type;
  249. return sym_difference_insert<GeometryOut>(geometry1, geometry2, out, strategy_type());
  250. }
  251. }} // namespace detail::sym_difference
  252. #endif // DOXYGEN_NO_DETAIL
  253. namespace resolve_strategy {
  254. struct sym_difference
  255. {
  256. template
  257. <
  258. typename Geometry1,
  259. typename Geometry2,
  260. typename Collection,
  261. typename Strategy
  262. >
  263. static inline void apply(Geometry1 const& geometry1,
  264. Geometry2 const& geometry2,
  265. Collection & output_collection,
  266. Strategy const& strategy)
  267. {
  268. typedef typename boost::range_value<Collection>::type geometry_out;
  269. detail::sym_difference::sym_difference_insert<geometry_out>(
  270. geometry1, geometry2,
  271. range::back_inserter(output_collection),
  272. strategy);
  273. }
  274. template
  275. <
  276. typename Geometry1,
  277. typename Geometry2,
  278. typename Collection
  279. >
  280. static inline void apply(Geometry1 const& geometry1,
  281. Geometry2 const& geometry2,
  282. Collection & output_collection,
  283. default_strategy)
  284. {
  285. typedef typename boost::range_value<Collection>::type geometry_out;
  286. detail::sym_difference::sym_difference_insert<geometry_out>(
  287. geometry1, geometry2,
  288. range::back_inserter(output_collection));
  289. }
  290. };
  291. } // resolve_strategy
  292. namespace resolve_variant
  293. {
  294. template <typename Geometry1, typename Geometry2>
  295. struct sym_difference
  296. {
  297. template <typename Collection, typename Strategy>
  298. static inline void apply(Geometry1 const& geometry1,
  299. Geometry2 const& geometry2,
  300. Collection& output_collection,
  301. Strategy const& strategy)
  302. {
  303. resolve_strategy::sym_difference::apply(geometry1, geometry2,
  304. output_collection,
  305. strategy);
  306. }
  307. };
  308. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  309. struct sym_difference<variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  310. {
  311. template <typename Collection, typename Strategy>
  312. struct visitor: static_visitor<>
  313. {
  314. Geometry2 const& m_geometry2;
  315. Collection& m_output_collection;
  316. Strategy const& m_strategy;
  317. visitor(Geometry2 const& geometry2,
  318. Collection& output_collection,
  319. Strategy const& strategy)
  320. : m_geometry2(geometry2)
  321. , m_output_collection(output_collection)
  322. , m_strategy(strategy)
  323. {}
  324. template <typename Geometry1>
  325. void operator()(Geometry1 const& geometry1) const
  326. {
  327. sym_difference
  328. <
  329. Geometry1,
  330. Geometry2
  331. >::apply(geometry1, m_geometry2, m_output_collection, m_strategy);
  332. }
  333. };
  334. template <typename Collection, typename Strategy>
  335. static inline void
  336. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  337. Geometry2 const& geometry2,
  338. Collection& output_collection,
  339. Strategy const& strategy)
  340. {
  341. boost::apply_visitor(visitor<Collection, Strategy>(geometry2,
  342. output_collection,
  343. strategy),
  344. geometry1);
  345. }
  346. };
  347. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  348. struct sym_difference<Geometry1, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  349. {
  350. template <typename Collection, typename Strategy>
  351. struct visitor: static_visitor<>
  352. {
  353. Geometry1 const& m_geometry1;
  354. Collection& m_output_collection;
  355. Strategy const& m_strategy;
  356. visitor(Geometry1 const& geometry1,
  357. Collection& output_collection,
  358. Strategy const& strategy)
  359. : m_geometry1(geometry1)
  360. , m_output_collection(output_collection)
  361. , m_strategy(strategy)
  362. {}
  363. template <typename Geometry2>
  364. void operator()(Geometry2 const& geometry2) const
  365. {
  366. sym_difference
  367. <
  368. Geometry1,
  369. Geometry2
  370. >::apply(m_geometry1, geometry2, m_output_collection, m_strategy);
  371. }
  372. };
  373. template <typename Collection, typename Strategy>
  374. static inline void
  375. apply(Geometry1 const& geometry1,
  376. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
  377. Collection& output_collection,
  378. Strategy const& strategy)
  379. {
  380. boost::apply_visitor(visitor<Collection, Strategy>(geometry1,
  381. output_collection,
  382. strategy),
  383. geometry2);
  384. }
  385. };
  386. template <BOOST_VARIANT_ENUM_PARAMS(typename T1), BOOST_VARIANT_ENUM_PARAMS(typename T2)>
  387. struct sym_difference<variant<BOOST_VARIANT_ENUM_PARAMS(T1)>, variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
  388. {
  389. template <typename Collection, typename Strategy>
  390. struct visitor: static_visitor<>
  391. {
  392. Collection& m_output_collection;
  393. Strategy const& m_strategy;
  394. visitor(Collection& output_collection, Strategy const& strategy)
  395. : m_output_collection(output_collection)
  396. , m_strategy(strategy)
  397. {}
  398. template <typename Geometry1, typename Geometry2>
  399. void operator()(Geometry1 const& geometry1,
  400. Geometry2 const& geometry2) const
  401. {
  402. sym_difference
  403. <
  404. Geometry1,
  405. Geometry2
  406. >::apply(geometry1, geometry2, m_output_collection, m_strategy);
  407. }
  408. };
  409. template <typename Collection, typename Strategy>
  410. static inline void
  411. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  412. variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
  413. Collection& output_collection,
  414. Strategy const& strategy)
  415. {
  416. boost::apply_visitor(visitor<Collection, Strategy>(output_collection,
  417. strategy),
  418. geometry1, geometry2);
  419. }
  420. };
  421. } // namespace resolve_variant
  422. /*!
  423. \brief \brief_calc2{symmetric difference}
  424. \ingroup sym_difference
  425. \details \details_calc2{symmetric difference, spatial set theoretic symmetric difference (XOR)}.
  426. \tparam Geometry1 \tparam_geometry
  427. \tparam Geometry2 \tparam_geometry
  428. \tparam Collection output collection, either a multi-geometry,
  429. or a std::vector<Geometry> / std::deque<Geometry> etc
  430. \tparam Strategy \tparam_strategy{Sym_difference}
  431. \param geometry1 \param_geometry
  432. \param geometry2 \param_geometry
  433. \param output_collection the output collection
  434. \param strategy \param_strategy{sym_difference}
  435. \qbk{distinguish,with strategy}
  436. \qbk{[include reference/algorithms/sym_difference.qbk]}
  437. */
  438. template
  439. <
  440. typename Geometry1,
  441. typename Geometry2,
  442. typename Collection,
  443. typename Strategy
  444. >
  445. inline void sym_difference(Geometry1 const& geometry1,
  446. Geometry2 const& geometry2,
  447. Collection& output_collection,
  448. Strategy const& strategy)
  449. {
  450. resolve_variant::sym_difference
  451. <
  452. Geometry1,
  453. Geometry2
  454. >::apply(geometry1, geometry2, output_collection, strategy);
  455. }
  456. /*!
  457. \brief \brief_calc2{symmetric difference}
  458. \ingroup sym_difference
  459. \details \details_calc2{symmetric difference, spatial set theoretic symmetric difference (XOR)}.
  460. \tparam Geometry1 \tparam_geometry
  461. \tparam Geometry2 \tparam_geometry
  462. \tparam Collection output collection, either a multi-geometry,
  463. or a std::vector<Geometry> / std::deque<Geometry> etc
  464. \param geometry1 \param_geometry
  465. \param geometry2 \param_geometry
  466. \param output_collection the output collection
  467. \qbk{[include reference/algorithms/sym_difference.qbk]}
  468. */
  469. template
  470. <
  471. typename Geometry1,
  472. typename Geometry2,
  473. typename Collection
  474. >
  475. inline void sym_difference(Geometry1 const& geometry1,
  476. Geometry2 const& geometry2,
  477. Collection& output_collection)
  478. {
  479. resolve_variant::sym_difference
  480. <
  481. Geometry1,
  482. Geometry2
  483. >::apply(geometry1, geometry2, output_collection, default_strategy());
  484. }
  485. }} // namespace boost::geometry
  486. #endif // BOOST_GEOMETRY_ALGORITHMS_SYM_DIFFERENCE_HPP