union.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.
  4. // Modifications copyright (c) 2014-2017 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::intersection::services::default_strategy
  243. <
  244. typename cs_tag<geometry_out>::type
  245. >::type strategy_type;
  246. dispatch::union_insert
  247. <
  248. Geometry1, Geometry2, geometry_out
  249. >::apply(geometry1, geometry2, robust_policy,
  250. range::back_inserter(output_collection),
  251. strategy_type());
  252. }
  253. };
  254. } // resolve_strategy
  255. namespace resolve_variant
  256. {
  257. template <typename Geometry1, typename Geometry2>
  258. struct union_
  259. {
  260. template <typename Collection, typename Strategy>
  261. static inline void apply(Geometry1 const& geometry1,
  262. Geometry2 const& geometry2,
  263. Collection& output_collection,
  264. Strategy const& strategy)
  265. {
  266. concepts::check<Geometry1 const>();
  267. concepts::check<Geometry2 const>();
  268. concepts::check<typename boost::range_value<Collection>::type>();
  269. typedef typename geometry::rescale_overlay_policy_type
  270. <
  271. Geometry1,
  272. Geometry2
  273. >::type rescale_policy_type;
  274. rescale_policy_type robust_policy
  275. = geometry::get_rescale_policy<rescale_policy_type>(geometry1,
  276. geometry2);
  277. resolve_strategy::union_::apply(geometry1, geometry2,
  278. robust_policy,
  279. output_collection,
  280. strategy);
  281. }
  282. };
  283. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  284. struct union_<variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  285. {
  286. template <typename Collection, typename Strategy>
  287. struct visitor: static_visitor<>
  288. {
  289. Geometry2 const& m_geometry2;
  290. Collection& m_output_collection;
  291. Strategy const& m_strategy;
  292. visitor(Geometry2 const& geometry2,
  293. Collection& output_collection,
  294. Strategy const& strategy)
  295. : m_geometry2(geometry2)
  296. , m_output_collection(output_collection)
  297. , m_strategy(strategy)
  298. {}
  299. template <typename Geometry1>
  300. void operator()(Geometry1 const& geometry1) const
  301. {
  302. union_
  303. <
  304. Geometry1,
  305. Geometry2
  306. >::apply(geometry1, m_geometry2, m_output_collection, m_strategy);
  307. }
  308. };
  309. template <typename Collection, typename Strategy>
  310. static inline void
  311. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  312. Geometry2 const& geometry2,
  313. Collection& output_collection,
  314. Strategy const& strategy)
  315. {
  316. boost::apply_visitor(visitor<Collection, Strategy>(geometry2,
  317. output_collection,
  318. strategy),
  319. geometry1);
  320. }
  321. };
  322. template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  323. struct union_<Geometry1, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  324. {
  325. template <typename Collection, typename Strategy>
  326. struct visitor: static_visitor<>
  327. {
  328. Geometry1 const& m_geometry1;
  329. Collection& m_output_collection;
  330. Strategy const& m_strategy;
  331. visitor(Geometry1 const& geometry1,
  332. Collection& output_collection,
  333. Strategy const& strategy)
  334. : m_geometry1(geometry1)
  335. , m_output_collection(output_collection)
  336. , m_strategy(strategy)
  337. {}
  338. template <typename Geometry2>
  339. void operator()(Geometry2 const& geometry2) const
  340. {
  341. union_
  342. <
  343. Geometry1,
  344. Geometry2
  345. >::apply(m_geometry1, geometry2, m_output_collection, m_strategy);
  346. }
  347. };
  348. template <typename Collection, typename Strategy>
  349. static inline void
  350. apply(Geometry1 const& geometry1,
  351. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
  352. Collection& output_collection,
  353. Strategy const& strategy)
  354. {
  355. boost::apply_visitor(visitor<Collection, Strategy>(geometry1,
  356. output_collection,
  357. strategy),
  358. geometry2);
  359. }
  360. };
  361. template <BOOST_VARIANT_ENUM_PARAMS(typename T1), BOOST_VARIANT_ENUM_PARAMS(typename T2)>
  362. struct union_<variant<BOOST_VARIANT_ENUM_PARAMS(T1)>, variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
  363. {
  364. template <typename Collection, typename Strategy>
  365. struct visitor: static_visitor<>
  366. {
  367. Collection& m_output_collection;
  368. Strategy const& m_strategy;
  369. visitor(Collection& output_collection, Strategy const& strategy)
  370. : m_output_collection(output_collection)
  371. , m_strategy(strategy)
  372. {}
  373. template <typename Geometry1, typename Geometry2>
  374. void operator()(Geometry1 const& geometry1,
  375. Geometry2 const& geometry2) const
  376. {
  377. union_
  378. <
  379. Geometry1,
  380. Geometry2
  381. >::apply(geometry1, geometry2, m_output_collection, m_strategy);
  382. }
  383. };
  384. template <typename Collection, typename Strategy>
  385. static inline void
  386. apply(variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
  387. variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
  388. Collection& output_collection,
  389. Strategy const& strategy)
  390. {
  391. boost::apply_visitor(visitor<Collection, Strategy>(output_collection,
  392. strategy),
  393. geometry1, geometry2);
  394. }
  395. };
  396. } // namespace resolve_variant
  397. /*!
  398. \brief Combines two geometries which each other
  399. \ingroup union
  400. \details \details_calc2{union, spatial set theoretic union}.
  401. \tparam Geometry1 \tparam_geometry
  402. \tparam Geometry2 \tparam_geometry
  403. \tparam Collection output collection, either a multi-geometry,
  404. or a std::vector<Geometry> / std::deque<Geometry> etc
  405. \tparam Strategy \tparam_strategy{Union_}
  406. \param geometry1 \param_geometry
  407. \param geometry2 \param_geometry
  408. \param output_collection the output collection
  409. \param strategy \param_strategy{union_}
  410. \note Called union_ because union is a reserved word.
  411. \qbk{distinguish,with strategy}
  412. \qbk{[include reference/algorithms/union.qbk]}
  413. */
  414. template
  415. <
  416. typename Geometry1,
  417. typename Geometry2,
  418. typename Collection,
  419. typename Strategy
  420. >
  421. inline void union_(Geometry1 const& geometry1,
  422. Geometry2 const& geometry2,
  423. Collection& output_collection,
  424. Strategy const& strategy)
  425. {
  426. resolve_variant::union_
  427. <
  428. Geometry1,
  429. Geometry2
  430. >::apply(geometry1, geometry2, output_collection, strategy);
  431. }
  432. /*!
  433. \brief Combines two geometries which each other
  434. \ingroup union
  435. \details \details_calc2{union, spatial set theoretic union}.
  436. \tparam Geometry1 \tparam_geometry
  437. \tparam Geometry2 \tparam_geometry
  438. \tparam Collection output collection, either a multi-geometry,
  439. or a std::vector<Geometry> / std::deque<Geometry> etc
  440. \param geometry1 \param_geometry
  441. \param geometry2 \param_geometry
  442. \param output_collection the output collection
  443. \note Called union_ because union is a reserved word.
  444. \qbk{[include reference/algorithms/union.qbk]}
  445. */
  446. template
  447. <
  448. typename Geometry1,
  449. typename Geometry2,
  450. typename Collection
  451. >
  452. inline void union_(Geometry1 const& geometry1,
  453. Geometry2 const& geometry2,
  454. Collection& output_collection)
  455. {
  456. resolve_variant::union_
  457. <
  458. Geometry1,
  459. Geometry2
  460. >::apply(geometry1, geometry2, output_collection, default_strategy());
  461. }
  462. }} // namespace boost::geometry
  463. #endif // BOOST_GEOMETRY_ALGORITHMS_UNION_HPP