union.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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.
  4. // Modifications copyright (c) 2014-2018 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. typedef typename geometry::rescale_overlay_policy_type
  186. <
  187. Geometry1,
  188. Geometry2
  189. >::type rescale_policy_type;
  190. typename strategy::intersection::services::default_strategy
  191. <
  192. typename cs_tag<GeometryOut>::type
  193. >::type strategy;
  194. rescale_policy_type robust_policy
  195. = geometry::get_rescale_policy<rescale_policy_type>(geometry1, geometry2);
  196. return dispatch::union_insert
  197. <
  198. Geometry1, Geometry2, GeometryOut
  199. >::apply(geometry1, geometry2, robust_policy, out, strategy);
  200. }
  201. }} // namespace detail::union_
  202. #endif // DOXYGEN_NO_DETAIL
  203. namespace resolve_strategy {
  204. struct union_
  205. {
  206. template
  207. <
  208. typename Geometry1,
  209. typename Geometry2,
  210. typename RobustPolicy,
  211. typename Collection,
  212. typename Strategy
  213. >
  214. static inline void apply(Geometry1 const& geometry1,
  215. Geometry2 const& geometry2,
  216. RobustPolicy const& robust_policy,
  217. Collection & output_collection,
  218. Strategy const& strategy)
  219. {
  220. typedef typename boost::range_value<Collection>::type geometry_out;
  221. dispatch::union_insert
  222. <
  223. Geometry1, Geometry2, geometry_out
  224. >::apply(geometry1, geometry2, robust_policy,
  225. range::back_inserter(output_collection),
  226. strategy);
  227. }
  228. template
  229. <
  230. typename Geometry1,
  231. typename Geometry2,
  232. typename RobustPolicy,
  233. typename Collection
  234. >
  235. static inline void apply(Geometry1 const& geometry1,
  236. Geometry2 const& geometry2,
  237. RobustPolicy const& robust_policy,
  238. Collection & output_collection,
  239. default_strategy)
  240. {
  241. typedef typename boost::range_value<Collection>::type geometry_out;
  242. typedef typename strategy::relate::services::default_strategy
  243. <
  244. Geometry1,
  245. Geometry2
  246. >::type strategy_type;
  247. dispatch::union_insert
  248. <
  249. Geometry1, Geometry2, geometry_out
  250. >::apply(geometry1, geometry2, robust_policy,
  251. range::back_inserter(output_collection),
  252. 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. typedef typename geometry::rescale_overlay_policy_type
  271. <
  272. Geometry1,
  273. Geometry2
  274. >::type rescale_policy_type;
  275. rescale_policy_type robust_policy
  276. = geometry::get_rescale_policy<rescale_policy_type>(geometry1,
  277. geometry2);
  278. resolve_strategy::union_::apply(geometry1, geometry2,
  279. robust_policy,
  280. output_collection,
  281. strategy);
  282. }
  283. };
  284. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  285. struct union_<variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  286. {
  287. template <typename Collection, typename Strategy>
  288. struct visitor: static_visitor<>
  289. {
  290. Geometry2 const& m_geometry2;
  291. Collection& m_output_collection;
  292. Strategy const& m_strategy;
  293. visitor(Geometry2 const& geometry2,
  294. Collection& output_collection,
  295. Strategy const& strategy)
  296. : m_geometry2(geometry2)
  297. , m_output_collection(output_collection)
  298. , m_strategy(strategy)
  299. {}
  300. template <typename Geometry1>
  301. void operator()(Geometry1 const& geometry1) const
  302. {
  303. union_
  304. <
  305. Geometry1,
  306. Geometry2
  307. >::apply(geometry1, m_geometry2, m_output_collection, m_strategy);
  308. }
  309. };
  310. template <typename Collection, typename Strategy>
  311. static inline void
  312. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  313. Geometry2 const& geometry2,
  314. Collection& output_collection,
  315. Strategy const& strategy)
  316. {
  317. boost::apply_visitor(visitor<Collection, Strategy>(geometry2,
  318. output_collection,
  319. strategy),
  320. geometry1);
  321. }
  322. };
  323. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  324. struct union_<Geometry1, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  325. {
  326. template <typename Collection, typename Strategy>
  327. struct visitor: static_visitor<>
  328. {
  329. Geometry1 const& m_geometry1;
  330. Collection& m_output_collection;
  331. Strategy const& m_strategy;
  332. visitor(Geometry1 const& geometry1,
  333. Collection& output_collection,
  334. Strategy const& strategy)
  335. : m_geometry1(geometry1)
  336. , m_output_collection(output_collection)
  337. , m_strategy(strategy)
  338. {}
  339. template <typename Geometry2>
  340. void operator()(Geometry2 const& geometry2) const
  341. {
  342. union_
  343. <
  344. Geometry1,
  345. Geometry2
  346. >::apply(m_geometry1, geometry2, m_output_collection, m_strategy);
  347. }
  348. };
  349. template <typename Collection, typename Strategy>
  350. static inline void
  351. apply(Geometry1 const& geometry1,
  352. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
  353. Collection& output_collection,
  354. Strategy const& strategy)
  355. {
  356. boost::apply_visitor(visitor<Collection, Strategy>(geometry1,
  357. output_collection,
  358. strategy),
  359. geometry2);
  360. }
  361. };
  362. template <BOOST_VARIANT_ENUM_PARAMS(typename T1), BOOST_VARIANT_ENUM_PARAMS(typename T2)>
  363. struct union_<variant<BOOST_VARIANT_ENUM_PARAMS(T1)>, variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
  364. {
  365. template <typename Collection, typename Strategy>
  366. struct visitor: static_visitor<>
  367. {
  368. Collection& m_output_collection;
  369. Strategy const& m_strategy;
  370. visitor(Collection& output_collection, Strategy const& strategy)
  371. : m_output_collection(output_collection)
  372. , m_strategy(strategy)
  373. {}
  374. template <typename Geometry1, typename Geometry2>
  375. void operator()(Geometry1 const& geometry1,
  376. Geometry2 const& geometry2) const
  377. {
  378. union_
  379. <
  380. Geometry1,
  381. Geometry2
  382. >::apply(geometry1, geometry2, m_output_collection, m_strategy);
  383. }
  384. };
  385. template <typename Collection, typename Strategy>
  386. static inline void
  387. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  388. variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
  389. Collection& output_collection,
  390. Strategy const& strategy)
  391. {
  392. boost::apply_visitor(visitor<Collection, Strategy>(output_collection,
  393. strategy),
  394. geometry1, geometry2);
  395. }
  396. };
  397. } // namespace resolve_variant
  398. /*!
  399. \brief Combines two geometries which each other
  400. \ingroup union
  401. \details \details_calc2{union, spatial set theoretic union}.
  402. \tparam Geometry1 \tparam_geometry
  403. \tparam Geometry2 \tparam_geometry
  404. \tparam Collection output collection, either a multi-geometry,
  405. or a std::vector<Geometry> / std::deque<Geometry> etc
  406. \tparam Strategy \tparam_strategy{Union_}
  407. \param geometry1 \param_geometry
  408. \param geometry2 \param_geometry
  409. \param output_collection the output collection
  410. \param strategy \param_strategy{union_}
  411. \note Called union_ because union is a reserved word.
  412. \qbk{distinguish,with strategy}
  413. \qbk{[include reference/algorithms/union.qbk]}
  414. */
  415. template
  416. <
  417. typename Geometry1,
  418. typename Geometry2,
  419. typename Collection,
  420. typename Strategy
  421. >
  422. inline void union_(Geometry1 const& geometry1,
  423. Geometry2 const& geometry2,
  424. Collection& output_collection,
  425. Strategy const& strategy)
  426. {
  427. resolve_variant::union_
  428. <
  429. Geometry1,
  430. Geometry2
  431. >::apply(geometry1, geometry2, output_collection, strategy);
  432. }
  433. /*!
  434. \brief Combines two geometries which each other
  435. \ingroup union
  436. \details \details_calc2{union, spatial set theoretic union}.
  437. \tparam Geometry1 \tparam_geometry
  438. \tparam Geometry2 \tparam_geometry
  439. \tparam Collection output collection, either a multi-geometry,
  440. or a std::vector<Geometry> / std::deque<Geometry> etc
  441. \param geometry1 \param_geometry
  442. \param geometry2 \param_geometry
  443. \param output_collection the output collection
  444. \note Called union_ because union is a reserved word.
  445. \qbk{[include reference/algorithms/union.qbk]}
  446. */
  447. template
  448. <
  449. typename Geometry1,
  450. typename Geometry2,
  451. typename Collection
  452. >
  453. inline void union_(Geometry1 const& geometry1,
  454. Geometry2 const& geometry2,
  455. Collection& output_collection)
  456. {
  457. resolve_variant::union_
  458. <
  459. Geometry1,
  460. Geometry2
  461. >::apply(geometry1, geometry2, output_collection, default_strategy());
  462. }
  463. }} // namespace boost::geometry
  464. #endif // BOOST_GEOMETRY_ALGORITHMS_UNION_HPP