union.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014, 2017, 2018, 2019.
  4. // Modifications copyright (c) 2014-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_UNION_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_UNION_HPP
  12. #include <boost/range/metafunctions.hpp>
  13. #include <boost/geometry/core/is_areal.hpp>
  14. #include <boost/geometry/core/point_order.hpp>
  15. #include <boost/geometry/core/reverse_dispatch.hpp>
  16. #include <boost/geometry/geometries/concepts/check.hpp>
  17. #include <boost/geometry/algorithms/not_implemented.hpp>
  18. #include <boost/geometry/algorithms/detail/overlay/overlay.hpp>
  19. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  20. #include <boost/geometry/strategies/default_strategy.hpp>
  21. #include <boost/geometry/util/range.hpp>
  22. #include <boost/geometry/algorithms/detail/overlay/linear_linear.hpp>
  23. #include <boost/geometry/algorithms/detail/overlay/pointlike_pointlike.hpp>
  24. namespace boost { namespace geometry
  25. {
  26. #ifndef DOXYGEN_NO_DISPATCH
  27. namespace dispatch
  28. {
  29. template
  30. <
  31. typename Geometry1, typename Geometry2, typename GeometryOut,
  32. typename TagIn1 = typename tag<Geometry1>::type,
  33. typename TagIn2 = typename tag<Geometry2>::type,
  34. typename TagOut = typename tag<GeometryOut>::type,
  35. bool Areal1 = geometry::is_areal<Geometry1>::value,
  36. bool Areal2 = geometry::is_areal<Geometry2>::value,
  37. bool ArealOut = geometry::is_areal<GeometryOut>::value,
  38. bool Reverse1 = detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
  39. bool Reverse2 = detail::overlay::do_reverse<geometry::point_order<Geometry2>::value>::value,
  40. bool ReverseOut = detail::overlay::do_reverse<geometry::point_order<GeometryOut>::value>::value,
  41. bool Reverse = geometry::reverse_dispatch<Geometry1, Geometry2>::type::value
  42. >
  43. struct union_insert: not_implemented<TagIn1, TagIn2, TagOut>
  44. {};
  45. // If reversal is needed, perform it first
  46. template
  47. <
  48. typename Geometry1, typename Geometry2, typename GeometryOut,
  49. typename TagIn1, typename TagIn2, typename TagOut,
  50. bool Areal1, bool Areal2, bool ArealOut,
  51. bool Reverse1, bool Reverse2, bool ReverseOut
  52. >
  53. struct union_insert
  54. <
  55. Geometry1, Geometry2, GeometryOut,
  56. TagIn1, TagIn2, TagOut,
  57. Areal1, Areal2, ArealOut,
  58. Reverse1, Reverse2, ReverseOut,
  59. true
  60. >: union_insert<Geometry2, Geometry1, GeometryOut>
  61. {
  62. template <typename RobustPolicy, typename OutputIterator, typename Strategy>
  63. static inline OutputIterator apply(Geometry1 const& g1,
  64. Geometry2 const& g2,
  65. RobustPolicy const& robust_policy,
  66. OutputIterator out,
  67. Strategy const& strategy)
  68. {
  69. return union_insert
  70. <
  71. Geometry2, Geometry1, GeometryOut
  72. >::apply(g2, g1, robust_policy, out, strategy);
  73. }
  74. };
  75. template
  76. <
  77. typename Geometry1, typename Geometry2, typename GeometryOut,
  78. typename TagIn1, typename TagIn2, typename TagOut,
  79. bool Reverse1, bool Reverse2, bool ReverseOut
  80. >
  81. struct union_insert
  82. <
  83. Geometry1, Geometry2, GeometryOut,
  84. TagIn1, TagIn2, TagOut,
  85. true, true, true,
  86. Reverse1, Reverse2, ReverseOut,
  87. false
  88. > : detail::overlay::overlay
  89. <Geometry1, Geometry2, Reverse1, Reverse2, ReverseOut, GeometryOut, overlay_union>
  90. {};
  91. // dispatch for union of non-areal geometries
  92. template
  93. <
  94. typename Geometry1, typename Geometry2, typename GeometryOut,
  95. typename TagIn1, typename TagIn2, typename TagOut,
  96. bool Reverse1, bool Reverse2, bool ReverseOut
  97. >
  98. struct union_insert
  99. <
  100. Geometry1, Geometry2, GeometryOut,
  101. TagIn1, TagIn2, TagOut,
  102. false, false, false,
  103. Reverse1, Reverse2, ReverseOut,
  104. false
  105. > : union_insert
  106. <
  107. Geometry1, Geometry2, GeometryOut,
  108. typename tag_cast<TagIn1, pointlike_tag, linear_tag>::type,
  109. typename tag_cast<TagIn2, pointlike_tag, linear_tag>::type,
  110. TagOut,
  111. false, false, false,
  112. Reverse1, Reverse2, ReverseOut,
  113. false
  114. >
  115. {};
  116. // dispatch for union of linear geometries
  117. template
  118. <
  119. typename Linear1, typename Linear2, typename LineStringOut,
  120. bool Reverse1, bool Reverse2, bool ReverseOut
  121. >
  122. struct union_insert
  123. <
  124. Linear1, Linear2, LineStringOut,
  125. linear_tag, linear_tag, linestring_tag,
  126. false, false, false,
  127. Reverse1, Reverse2, ReverseOut,
  128. false
  129. > : detail::overlay::linear_linear_linestring
  130. <
  131. Linear1, Linear2, LineStringOut, overlay_union
  132. >
  133. {};
  134. // dispatch for point-like geometries
  135. template
  136. <
  137. typename PointLike1, typename PointLike2, typename PointOut,
  138. bool Reverse1, bool Reverse2, bool ReverseOut
  139. >
  140. struct union_insert
  141. <
  142. PointLike1, PointLike2, PointOut,
  143. pointlike_tag, pointlike_tag, point_tag,
  144. false, false, false,
  145. Reverse1, Reverse2, ReverseOut,
  146. false
  147. > : detail::overlay::union_pointlike_pointlike_point
  148. <
  149. PointLike1, PointLike2, PointOut
  150. >
  151. {};
  152. } // namespace dispatch
  153. #endif // DOXYGEN_NO_DISPATCH
  154. #ifndef DOXYGEN_NO_DETAIL
  155. namespace detail { namespace union_
  156. {
  157. /*!
  158. \brief_calc2{union}
  159. \ingroup union
  160. \details \details_calc2{union_insert, spatial set theoretic union}.
  161. \details_insert{union}
  162. \tparam GeometryOut output geometry type, must be specified
  163. \tparam Geometry1 \tparam_geometry
  164. \tparam Geometry2 \tparam_geometry
  165. \tparam OutputIterator output iterator
  166. \param geometry1 \param_geometry
  167. \param geometry2 \param_geometry
  168. \param out \param_out{union}
  169. \return \return_out
  170. */
  171. template
  172. <
  173. typename GeometryOut,
  174. typename Geometry1,
  175. typename Geometry2,
  176. typename OutputIterator
  177. >
  178. inline OutputIterator union_insert(Geometry1 const& geometry1,
  179. Geometry2 const& geometry2,
  180. OutputIterator out)
  181. {
  182. concepts::check<Geometry1 const>();
  183. concepts::check<Geometry2 const>();
  184. concepts::check<GeometryOut>();
  185. typename strategy::intersection::services::default_strategy
  186. <
  187. typename cs_tag<GeometryOut>::type
  188. >::type strategy;
  189. typedef typename geometry::rescale_overlay_policy_type
  190. <
  191. Geometry1,
  192. Geometry2
  193. >::type rescale_policy_type;
  194. rescale_policy_type robust_policy
  195. = geometry::get_rescale_policy<rescale_policy_type>(
  196. geometry1, geometry2, strategy);
  197. return dispatch::union_insert
  198. <
  199. Geometry1, Geometry2, GeometryOut
  200. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  201. }
  202. }} // namespace detail::union_
  203. #endif // DOXYGEN_NO_DETAIL
  204. namespace resolve_strategy {
  205. struct union_
  206. {
  207. template
  208. <
  209. typename Geometry1,
  210. typename Geometry2,
  211. typename Collection,
  212. typename Strategy
  213. >
  214. static inline void apply(Geometry1 const& geometry1,
  215. Geometry2 const& geometry2,
  216. Collection & output_collection,
  217. Strategy const& strategy)
  218. {
  219. typedef typename boost::range_value<Collection>::type geometry_out;
  220. typedef typename geometry::rescale_overlay_policy_type
  221. <
  222. Geometry1,
  223. Geometry2,
  224. typename Strategy::cs_tag
  225. >::type rescale_policy_type;
  226. rescale_policy_type robust_policy
  227. = geometry::get_rescale_policy<rescale_policy_type>(
  228. geometry1, geometry2, strategy);
  229. dispatch::union_insert
  230. <
  231. Geometry1, Geometry2, geometry_out
  232. >::apply(geometry1, geometry2, robust_policy,
  233. range::back_inserter(output_collection),
  234. strategy);
  235. }
  236. template
  237. <
  238. typename Geometry1,
  239. typename Geometry2,
  240. typename Collection
  241. >
  242. static inline void apply(Geometry1 const& geometry1,
  243. Geometry2 const& geometry2,
  244. Collection & output_collection,
  245. default_strategy)
  246. {
  247. typedef typename strategy::relate::services::default_strategy
  248. <
  249. Geometry1,
  250. Geometry2
  251. >::type strategy_type;
  252. apply(geometry1, geometry2, output_collection, strategy_type());
  253. }
  254. };
  255. } // resolve_strategy
  256. namespace resolve_variant
  257. {
  258. template <typename Geometry1, typename Geometry2>
  259. struct union_
  260. {
  261. template <typename Collection, typename Strategy>
  262. static inline void apply(Geometry1 const& geometry1,
  263. Geometry2 const& geometry2,
  264. Collection& output_collection,
  265. Strategy const& strategy)
  266. {
  267. concepts::check<Geometry1 const>();
  268. concepts::check<Geometry2 const>();
  269. concepts::check<typename boost::range_value<Collection>::type>();
  270. resolve_strategy::union_::apply(geometry1, geometry2,
  271. output_collection,
  272. strategy);
  273. }
  274. };
  275. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  276. struct union_<variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  277. {
  278. template <typename Collection, typename Strategy>
  279. struct visitor: static_visitor<>
  280. {
  281. Geometry2 const& m_geometry2;
  282. Collection& m_output_collection;
  283. Strategy const& m_strategy;
  284. visitor(Geometry2 const& geometry2,
  285. Collection& output_collection,
  286. Strategy const& strategy)
  287. : m_geometry2(geometry2)
  288. , m_output_collection(output_collection)
  289. , m_strategy(strategy)
  290. {}
  291. template <typename Geometry1>
  292. void operator()(Geometry1 const& geometry1) const
  293. {
  294. union_
  295. <
  296. Geometry1,
  297. Geometry2
  298. >::apply(geometry1, m_geometry2, m_output_collection, m_strategy);
  299. }
  300. };
  301. template <typename Collection, typename Strategy>
  302. static inline void
  303. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  304. Geometry2 const& geometry2,
  305. Collection& output_collection,
  306. Strategy const& strategy)
  307. {
  308. boost::apply_visitor(visitor<Collection, Strategy>(geometry2,
  309. output_collection,
  310. strategy),
  311. geometry1);
  312. }
  313. };
  314. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  315. struct union_<Geometry1, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  316. {
  317. template <typename Collection, typename Strategy>
  318. struct visitor: static_visitor<>
  319. {
  320. Geometry1 const& m_geometry1;
  321. Collection& m_output_collection;
  322. Strategy const& m_strategy;
  323. visitor(Geometry1 const& geometry1,
  324. Collection& output_collection,
  325. Strategy const& strategy)
  326. : m_geometry1(geometry1)
  327. , m_output_collection(output_collection)
  328. , m_strategy(strategy)
  329. {}
  330. template <typename Geometry2>
  331. void operator()(Geometry2 const& geometry2) const
  332. {
  333. union_
  334. <
  335. Geometry1,
  336. Geometry2
  337. >::apply(m_geometry1, geometry2, m_output_collection, m_strategy);
  338. }
  339. };
  340. template <typename Collection, typename Strategy>
  341. static inline void
  342. apply(Geometry1 const& geometry1,
  343. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
  344. Collection& output_collection,
  345. Strategy const& strategy)
  346. {
  347. boost::apply_visitor(visitor<Collection, Strategy>(geometry1,
  348. output_collection,
  349. strategy),
  350. geometry2);
  351. }
  352. };
  353. template <BOOST_VARIANT_ENUM_PARAMS(typename T1), BOOST_VARIANT_ENUM_PARAMS(typename T2)>
  354. struct union_<variant<BOOST_VARIANT_ENUM_PARAMS(T1)>, variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
  355. {
  356. template <typename Collection, typename Strategy>
  357. struct visitor: static_visitor<>
  358. {
  359. Collection& m_output_collection;
  360. Strategy const& m_strategy;
  361. visitor(Collection& output_collection, Strategy const& strategy)
  362. : m_output_collection(output_collection)
  363. , m_strategy(strategy)
  364. {}
  365. template <typename Geometry1, typename Geometry2>
  366. void operator()(Geometry1 const& geometry1,
  367. Geometry2 const& geometry2) const
  368. {
  369. union_
  370. <
  371. Geometry1,
  372. Geometry2
  373. >::apply(geometry1, geometry2, m_output_collection, m_strategy);
  374. }
  375. };
  376. template <typename Collection, typename Strategy>
  377. static inline void
  378. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  379. variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
  380. Collection& output_collection,
  381. Strategy const& strategy)
  382. {
  383. boost::apply_visitor(visitor<Collection, Strategy>(output_collection,
  384. strategy),
  385. geometry1, geometry2);
  386. }
  387. };
  388. } // namespace resolve_variant
  389. /*!
  390. \brief Combines two geometries which each other
  391. \ingroup union
  392. \details \details_calc2{union, spatial set theoretic union}.
  393. \tparam Geometry1 \tparam_geometry
  394. \tparam Geometry2 \tparam_geometry
  395. \tparam Collection output collection, either a multi-geometry,
  396. or a std::vector<Geometry> / std::deque<Geometry> etc
  397. \tparam Strategy \tparam_strategy{Union_}
  398. \param geometry1 \param_geometry
  399. \param geometry2 \param_geometry
  400. \param output_collection the output collection
  401. \param strategy \param_strategy{union_}
  402. \note Called union_ because union is a reserved word.
  403. \qbk{distinguish,with strategy}
  404. \qbk{[include reference/algorithms/union.qbk]}
  405. */
  406. template
  407. <
  408. typename Geometry1,
  409. typename Geometry2,
  410. typename Collection,
  411. typename Strategy
  412. >
  413. inline void union_(Geometry1 const& geometry1,
  414. Geometry2 const& geometry2,
  415. Collection& output_collection,
  416. Strategy const& strategy)
  417. {
  418. resolve_variant::union_
  419. <
  420. Geometry1,
  421. Geometry2
  422. >::apply(geometry1, geometry2, output_collection, strategy);
  423. }
  424. /*!
  425. \brief Combines two geometries which each other
  426. \ingroup union
  427. \details \details_calc2{union, spatial set theoretic union}.
  428. \tparam Geometry1 \tparam_geometry
  429. \tparam Geometry2 \tparam_geometry
  430. \tparam Collection output collection, either a multi-geometry,
  431. or a std::vector<Geometry> / std::deque<Geometry> etc
  432. \param geometry1 \param_geometry
  433. \param geometry2 \param_geometry
  434. \param output_collection the output collection
  435. \note Called union_ because union is a reserved word.
  436. \qbk{[include reference/algorithms/union.qbk]}
  437. */
  438. template
  439. <
  440. typename Geometry1,
  441. typename Geometry2,
  442. typename Collection
  443. >
  444. inline void union_(Geometry1 const& geometry1,
  445. Geometry2 const& geometry2,
  446. Collection& output_collection)
  447. {
  448. resolve_variant::union_
  449. <
  450. Geometry1,
  451. Geometry2
  452. >::apply(geometry1, geometry2, output_collection, default_strategy());
  453. }
  454. }} // namespace boost::geometry
  455. #endif // BOOST_GEOMETRY_ALGORITHMS_UNION_HPP