difference.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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-2020.
  4. // Modifications copyright (c) 2017-2020, 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/intersection/multi.hpp>
  15. #include <boost/geometry/algorithms/detail/overlay/intersection_insert.hpp>
  16. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  17. #include <boost/geometry/strategies/default_strategy.hpp>
  18. #include <boost/geometry/strategies/detail.hpp>
  19. #include <boost/geometry/strategies/relate/services.hpp>
  20. #include <boost/geometry/util/range.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. #ifndef DOXYGEN_NO_DETAIL
  24. namespace detail { namespace difference
  25. {
  26. template
  27. <
  28. typename Geometry1,
  29. typename Geometry2,
  30. typename SingleOut,
  31. typename OutTag = typename detail::setop_insert_output_tag<SingleOut>::type,
  32. bool ReturnGeometry1 = (topological_dimension<Geometry1>::value
  33. > topological_dimension<Geometry2>::value)
  34. >
  35. struct call_intersection_insert
  36. {
  37. template
  38. <
  39. typename OutputIterator,
  40. typename RobustPolicy,
  41. typename Strategy
  42. >
  43. static inline OutputIterator apply(Geometry1 const& geometry1,
  44. Geometry2 const& geometry2,
  45. RobustPolicy const& robust_policy,
  46. OutputIterator out,
  47. Strategy const& strategy)
  48. {
  49. return geometry::dispatch::intersection_insert
  50. <
  51. Geometry1, Geometry2,
  52. SingleOut,
  53. overlay_difference,
  54. geometry::detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
  55. geometry::detail::overlay::do_reverse<geometry::point_order<Geometry2>::value, true>::value
  56. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  57. }
  58. };
  59. template
  60. <
  61. typename Geometry1,
  62. typename Geometry2,
  63. typename SingleOut
  64. >
  65. struct call_intersection_insert_tupled_base
  66. {
  67. typedef typename geometry::detail::single_tag_from_base_tag
  68. <
  69. typename geometry::tag_cast
  70. <
  71. typename geometry::tag<Geometry1>::type,
  72. pointlike_tag, linear_tag, areal_tag
  73. >::type
  74. >::type single_tag;
  75. typedef detail::expect_output
  76. <
  77. Geometry1, Geometry2, SingleOut, single_tag
  78. > expect_check;
  79. typedef typename geometry::detail::output_geometry_access
  80. <
  81. SingleOut, single_tag, single_tag
  82. > access;
  83. };
  84. template
  85. <
  86. typename Geometry1,
  87. typename Geometry2,
  88. typename SingleOut
  89. >
  90. struct call_intersection_insert
  91. <
  92. Geometry1, Geometry2, SingleOut,
  93. detail::tupled_output_tag,
  94. false
  95. >
  96. : call_intersection_insert_tupled_base<Geometry1, Geometry2, SingleOut>
  97. {
  98. typedef call_intersection_insert_tupled_base<Geometry1, Geometry2, SingleOut> base_t;
  99. template
  100. <
  101. typename OutputIterator,
  102. typename RobustPolicy,
  103. typename Strategy
  104. >
  105. static inline OutputIterator apply(Geometry1 const& geometry1,
  106. Geometry2 const& geometry2,
  107. RobustPolicy const& robust_policy,
  108. OutputIterator out,
  109. Strategy const& strategy)
  110. {
  111. base_t::access::get(out) = call_intersection_insert
  112. <
  113. Geometry1, Geometry2,
  114. typename base_t::access::type
  115. >::apply(geometry1, geometry2, robust_policy,
  116. base_t::access::get(out), strategy);
  117. return out;
  118. }
  119. };
  120. template
  121. <
  122. typename Geometry1,
  123. typename Geometry2,
  124. typename SingleOut
  125. >
  126. struct call_intersection_insert
  127. <
  128. Geometry1, Geometry2, SingleOut,
  129. detail::tupled_output_tag,
  130. true
  131. >
  132. : call_intersection_insert_tupled_base<Geometry1, Geometry2, SingleOut>
  133. {
  134. typedef call_intersection_insert_tupled_base<Geometry1, Geometry2, SingleOut> base_t;
  135. template
  136. <
  137. typename OutputIterator,
  138. typename RobustPolicy,
  139. typename Strategy
  140. >
  141. static inline OutputIterator apply(Geometry1 const& geometry1,
  142. Geometry2 const& geometry2,
  143. RobustPolicy const& robust_policy,
  144. OutputIterator out,
  145. Strategy const& strategy)
  146. {
  147. base_t::access::get(out) = geometry::detail::convert_to_output
  148. <
  149. Geometry1,
  150. typename base_t::access::type
  151. >::apply(geometry1, base_t::access::get(out));
  152. return out;
  153. }
  154. };
  155. /*!
  156. \brief_calc2{difference} \brief_strategy
  157. \ingroup difference
  158. \details \details_calc2{difference_insert, spatial set theoretic difference}
  159. \brief_strategy. \details_inserter{difference}
  160. \tparam GeometryOut output geometry type, must be specified
  161. \tparam Geometry1 \tparam_geometry
  162. \tparam Geometry2 \tparam_geometry
  163. \tparam OutputIterator output iterator
  164. \tparam Strategy \tparam_strategy_overlay
  165. \param geometry1 \param_geometry
  166. \param geometry2 \param_geometry
  167. \param out \param_out{difference}
  168. \param strategy \param_strategy{difference}
  169. \return \return_out
  170. \qbk{distinguish,with strategy}
  171. */
  172. template
  173. <
  174. typename GeometryOut,
  175. typename Geometry1,
  176. typename Geometry2,
  177. typename OutputIterator,
  178. typename Strategy
  179. >
  180. inline OutputIterator difference_insert(Geometry1 const& geometry1,
  181. Geometry2 const& geometry2,
  182. OutputIterator out,
  183. Strategy const& strategy)
  184. {
  185. concepts::check<Geometry1 const>();
  186. concepts::check<Geometry2 const>();
  187. //concepts::check<GeometryOut>();
  188. geometry::detail::output_geometry_concept_check<GeometryOut>::apply();
  189. typedef typename geometry::rescale_overlay_policy_type
  190. <
  191. Geometry1,
  192. Geometry2,
  193. typename Strategy::cs_tag
  194. >::type rescale_policy_type;
  195. rescale_policy_type robust_policy
  196. = geometry::get_rescale_policy<rescale_policy_type>(
  197. geometry1, geometry2, strategy);
  198. return geometry::detail::difference::call_intersection_insert
  199. <
  200. Geometry1, Geometry2, GeometryOut
  201. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  202. }
  203. /*!
  204. \brief_calc2{difference}
  205. \ingroup difference
  206. \details \details_calc2{difference_insert, spatial set theoretic difference}.
  207. \details_insert{difference}
  208. \tparam GeometryOut output geometry type, must be specified
  209. \tparam Geometry1 \tparam_geometry
  210. \tparam Geometry2 \tparam_geometry
  211. \tparam OutputIterator output iterator
  212. \param geometry1 \param_geometry
  213. \param geometry2 \param_geometry
  214. \param out \param_out{difference}
  215. \return \return_out
  216. \qbk{[include reference/algorithms/difference_insert.qbk]}
  217. */
  218. template
  219. <
  220. typename GeometryOut,
  221. typename Geometry1,
  222. typename Geometry2,
  223. typename OutputIterator
  224. >
  225. inline OutputIterator difference_insert(Geometry1 const& geometry1,
  226. Geometry2 const& geometry2,
  227. OutputIterator out)
  228. {
  229. typedef typename strategies::relate::services::default_strategy
  230. <
  231. Geometry1,
  232. Geometry2
  233. >::type strategy_type;
  234. return difference_insert<GeometryOut>(geometry1, geometry2, out,
  235. strategy_type());
  236. }
  237. }} // namespace detail::difference
  238. #endif // DOXYGEN_NO_DETAIL
  239. namespace resolve_strategy {
  240. template
  241. <
  242. typename Strategy,
  243. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategy>::value
  244. >
  245. struct difference
  246. {
  247. template <typename Geometry1, typename Geometry2, typename Collection>
  248. static inline void apply(Geometry1 const& geometry1,
  249. Geometry2 const& geometry2,
  250. Collection & output_collection,
  251. Strategy const& strategy)
  252. {
  253. typedef typename geometry::detail::output_geometry_value
  254. <
  255. Collection
  256. >::type single_out;
  257. detail::difference::difference_insert<single_out>(
  258. geometry1, geometry2,
  259. geometry::detail::output_geometry_back_inserter(output_collection),
  260. strategy);
  261. }
  262. };
  263. template <typename Strategy>
  264. struct difference<Strategy, false>
  265. {
  266. template <typename Geometry1, typename Geometry2, typename Collection>
  267. static inline void apply(Geometry1 const& geometry1,
  268. Geometry2 const& geometry2,
  269. Collection & output_collection,
  270. Strategy const& strategy)
  271. {
  272. using strategies::relate::services::strategy_converter;
  273. difference
  274. <
  275. decltype(strategy_converter<Strategy>::get(strategy))
  276. >::apply(geometry1, geometry2, output_collection,
  277. strategy_converter<Strategy>::get(strategy));
  278. }
  279. };
  280. template <>
  281. struct difference<default_strategy, false>
  282. {
  283. template <typename Geometry1, typename Geometry2, typename Collection>
  284. static inline void apply(Geometry1 const& geometry1,
  285. Geometry2 const& geometry2,
  286. Collection & output_collection,
  287. default_strategy)
  288. {
  289. typedef typename strategies::relate::services::default_strategy
  290. <
  291. Geometry1,
  292. Geometry2
  293. >::type strategy_type;
  294. difference
  295. <
  296. strategy_type
  297. >::apply(geometry1, geometry2, output_collection, strategy_type());
  298. }
  299. };
  300. } // resolve_strategy
  301. namespace resolve_variant
  302. {
  303. template <typename Geometry1, typename Geometry2>
  304. struct difference
  305. {
  306. template <typename Collection, typename Strategy>
  307. static inline void apply(Geometry1 const& geometry1,
  308. Geometry2 const& geometry2,
  309. Collection& output_collection,
  310. Strategy const& strategy)
  311. {
  312. resolve_strategy::difference
  313. <
  314. Strategy
  315. >::apply(geometry1, geometry2, output_collection, strategy);
  316. }
  317. };
  318. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  319. struct difference<variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  320. {
  321. template <typename Collection, typename Strategy>
  322. struct visitor: static_visitor<>
  323. {
  324. Geometry2 const& m_geometry2;
  325. Collection& m_output_collection;
  326. Strategy const& m_strategy;
  327. visitor(Geometry2 const& geometry2,
  328. Collection& output_collection,
  329. Strategy const& strategy)
  330. : m_geometry2(geometry2)
  331. , m_output_collection(output_collection)
  332. , m_strategy(strategy)
  333. {}
  334. template <typename Geometry1>
  335. void operator()(Geometry1 const& geometry1) const
  336. {
  337. difference
  338. <
  339. Geometry1,
  340. Geometry2
  341. >::apply(geometry1, m_geometry2, m_output_collection, m_strategy);
  342. }
  343. };
  344. template <typename Collection, typename Strategy>
  345. static inline void
  346. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  347. Geometry2 const& geometry2,
  348. Collection& output_collection,
  349. Strategy const& strategy)
  350. {
  351. boost::apply_visitor(visitor<Collection, Strategy>(geometry2,
  352. output_collection,
  353. strategy),
  354. geometry1);
  355. }
  356. };
  357. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  358. struct difference<Geometry1, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  359. {
  360. template <typename Collection, typename Strategy>
  361. struct visitor: static_visitor<>
  362. {
  363. Geometry1 const& m_geometry1;
  364. Collection& m_output_collection;
  365. Strategy const& m_strategy;
  366. visitor(Geometry1 const& geometry1,
  367. Collection& output_collection,
  368. Strategy const& strategy)
  369. : m_geometry1(geometry1)
  370. , m_output_collection(output_collection)
  371. , m_strategy(strategy)
  372. {}
  373. template <typename Geometry2>
  374. void operator()(Geometry2 const& geometry2) const
  375. {
  376. difference
  377. <
  378. Geometry1,
  379. Geometry2
  380. >::apply(m_geometry1, geometry2, m_output_collection, m_strategy);
  381. }
  382. };
  383. template <typename Collection, typename Strategy>
  384. static inline void
  385. apply(Geometry1 const& geometry1,
  386. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
  387. Collection& output_collection,
  388. Strategy const& strategy)
  389. {
  390. boost::apply_visitor(visitor<Collection, Strategy>(geometry1,
  391. output_collection,
  392. strategy),
  393. geometry2);
  394. }
  395. };
  396. template <BOOST_VARIANT_ENUM_PARAMS(typename T1), BOOST_VARIANT_ENUM_PARAMS(typename T2)>
  397. struct difference<variant<BOOST_VARIANT_ENUM_PARAMS(T1)>, variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
  398. {
  399. template <typename Collection, typename Strategy>
  400. struct visitor: static_visitor<>
  401. {
  402. Collection& m_output_collection;
  403. Strategy const& m_strategy;
  404. visitor(Collection& output_collection, Strategy const& strategy)
  405. : m_output_collection(output_collection)
  406. , m_strategy(strategy)
  407. {}
  408. template <typename Geometry1, typename Geometry2>
  409. void operator()(Geometry1 const& geometry1,
  410. Geometry2 const& geometry2) const
  411. {
  412. difference
  413. <
  414. Geometry1,
  415. Geometry2
  416. >::apply(geometry1, geometry2, m_output_collection, m_strategy);
  417. }
  418. };
  419. template <typename Collection, typename Strategy>
  420. static inline void
  421. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  422. variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
  423. Collection& output_collection,
  424. Strategy const& strategy)
  425. {
  426. boost::apply_visitor(visitor<Collection, Strategy>(output_collection,
  427. strategy),
  428. geometry1, geometry2);
  429. }
  430. };
  431. } // namespace resolve_variant
  432. /*!
  433. \brief_calc2{difference}
  434. \ingroup difference
  435. \details \details_calc2{difference, spatial set theoretic difference}.
  436. \tparam Geometry1 \tparam_geometry
  437. \tparam Geometry2 \tparam_geometry
  438. \tparam Collection \tparam_output_collection
  439. \tparam Strategy \tparam_strategy{Difference}
  440. \param geometry1 \param_geometry
  441. \param geometry2 \param_geometry
  442. \param output_collection the output collection
  443. \param strategy \param_strategy{difference}
  444. \qbk{distinguish,with strategy}
  445. \qbk{[include reference/algorithms/difference.qbk]}
  446. */
  447. template
  448. <
  449. typename Geometry1,
  450. typename Geometry2,
  451. typename Collection,
  452. typename Strategy
  453. >
  454. inline void difference(Geometry1 const& geometry1,
  455. Geometry2 const& geometry2,
  456. Collection& output_collection,
  457. Strategy const& strategy)
  458. {
  459. resolve_variant::difference
  460. <
  461. Geometry1,
  462. Geometry2
  463. >::apply(geometry1, geometry2, output_collection, strategy);
  464. }
  465. /*!
  466. \brief_calc2{difference}
  467. \ingroup difference
  468. \details \details_calc2{difference, spatial set theoretic difference}.
  469. \tparam Geometry1 \tparam_geometry
  470. \tparam Geometry2 \tparam_geometry
  471. \tparam Collection \tparam_output_collection
  472. \param geometry1 \param_geometry
  473. \param geometry2 \param_geometry
  474. \param output_collection the output collection
  475. \qbk{[include reference/algorithms/difference.qbk]}
  476. */
  477. template
  478. <
  479. typename Geometry1,
  480. typename Geometry2,
  481. typename Collection
  482. >
  483. inline void difference(Geometry1 const& geometry1,
  484. Geometry2 const& geometry2,
  485. Collection& output_collection)
  486. {
  487. resolve_variant::difference
  488. <
  489. Geometry1,
  490. Geometry2
  491. >::apply(geometry1, geometry2, output_collection, default_strategy());
  492. }
  493. }} // namespace boost::geometry
  494. #endif // BOOST_GEOMETRY_ALGORITHMS_DIFFERENCE_HPP