convert.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2025 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014-2024 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2017-2023.
  7. // Modifications copyright (c) 2017-2023, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_ALGORITHMS_CONVERT_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_CONVERT_HPP
  17. #include <cstddef>
  18. #include <type_traits>
  19. #include <boost/range/begin.hpp>
  20. #include <boost/range/end.hpp>
  21. #include <boost/range/size.hpp>
  22. #include <boost/range/value_type.hpp>
  23. #include <boost/variant/static_visitor.hpp>
  24. #include <boost/variant/variant_fwd.hpp>
  25. #include <boost/geometry/algorithms/clear.hpp>
  26. #include <boost/geometry/algorithms/num_points.hpp>
  27. #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
  28. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  29. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  30. #include <boost/geometry/algorithms/detail/convert_indexed_to_indexed.hpp>
  31. #include <boost/geometry/algorithms/not_implemented.hpp>
  32. #include <boost/geometry/core/closure.hpp>
  33. #include <boost/geometry/core/point_order.hpp>
  34. #include <boost/geometry/core/tag_cast.hpp>
  35. #include <boost/geometry/core/tags.hpp>
  36. #include <boost/geometry/geometries/concepts/check.hpp>
  37. #include <boost/geometry/util/numeric_cast.hpp>
  38. #include <boost/geometry/util/range.hpp>
  39. #include <boost/geometry/views/detail/closed_clockwise_view.hpp>
  40. namespace boost { namespace geometry
  41. {
  42. // Silence warning C4127: conditional expression is constant
  43. // Silence warning C4512: assignment operator could not be generated
  44. #if defined(_MSC_VER)
  45. #pragma warning(push)
  46. #pragma warning(disable : 4127 4512)
  47. #endif
  48. #ifndef DOXYGEN_NO_DETAIL
  49. namespace detail { namespace conversion
  50. {
  51. template
  52. <
  53. typename Point,
  54. typename Box,
  55. std::size_t Index,
  56. std::size_t Dimension,
  57. std::size_t DimensionCount
  58. >
  59. struct point_to_box
  60. {
  61. static inline void apply(Point const& point, Box& box)
  62. {
  63. set<Index, Dimension>(box,
  64. util::numeric_cast<coordinate_type_t<Box>>(get<Dimension>(point)));
  65. point_to_box
  66. <
  67. Point, Box,
  68. Index, Dimension + 1, DimensionCount
  69. >::apply(point, box);
  70. }
  71. };
  72. template
  73. <
  74. typename Point,
  75. typename Box,
  76. std::size_t Index,
  77. std::size_t DimensionCount
  78. >
  79. struct point_to_box<Point, Box, Index, DimensionCount, DimensionCount>
  80. {
  81. static inline void apply(Point const& , Box& )
  82. {}
  83. };
  84. template <bool Close, bool Reverse>
  85. struct box_to_range
  86. {
  87. template <typename Box, typename Range>
  88. static inline void apply(Box const& box, Range& range)
  89. {
  90. traits::resize<Range>::apply(range, Close ? 5 : 4);
  91. assign_box_corners_oriented<Reverse>(box, range);
  92. if (Close)
  93. {
  94. range::at(range, 4) = range::at(range, 0);
  95. }
  96. }
  97. };
  98. template <typename Segment, typename Range>
  99. struct segment_to_range
  100. {
  101. static inline void apply(Segment const& segment, Range& range)
  102. {
  103. traits::resize<Range>::apply(range, 2);
  104. auto it = boost::begin(range);
  105. assign_point_from_index<0>(segment, *it);
  106. ++it;
  107. assign_point_from_index<1>(segment, *it);
  108. }
  109. };
  110. struct range_to_range
  111. {
  112. struct default_policy
  113. {
  114. template <typename Point1, typename Point2>
  115. static inline void apply(Point1 const& point1, Point2 & point2)
  116. {
  117. geometry::detail::conversion::convert_point_to_point(point1, point2);
  118. }
  119. };
  120. template <typename Range1, typename Range2>
  121. static inline void apply(Range1 const& source, Range2& destination)
  122. {
  123. apply(source, destination, default_policy());
  124. }
  125. template <typename ConvertPointPolicy, typename Range1, typename Range2>
  126. static inline ConvertPointPolicy apply(Range1 const& source, Range2& destination,
  127. ConvertPointPolicy convert_point)
  128. {
  129. geometry::clear(destination);
  130. constexpr bool reverse = geometry::point_order<Range1>::value != geometry::point_order<Range2>::value;
  131. using view_type = detail::closed_clockwise_view
  132. <
  133. Range1 const,
  134. geometry::closure<Range1>::value,
  135. reverse ? counterclockwise : clockwise
  136. >;
  137. // We consider input always as closed, and skip last
  138. // point for open output.
  139. view_type const view(source);
  140. auto n = boost::size(view);
  141. if (geometry::closure<Range2>::value == geometry::open)
  142. {
  143. n--;
  144. }
  145. // If size == 0 && geometry::open <=> n = numeric_limits<size_type>::max()
  146. // but ok, sice below it == end()
  147. decltype(n) i = 0;
  148. for (auto it = boost::begin(view);
  149. it != boost::end(view) && i < n;
  150. ++it, ++i)
  151. {
  152. typename boost::range_value<Range2>::type point;
  153. convert_point.apply(*it, point);
  154. range::push_back(destination, point);
  155. }
  156. return convert_point;
  157. }
  158. };
  159. // Converts a polygon to an output iterator of ranges.
  160. // The iterator is updated such that it can used iteratively.
  161. struct polygon_to_multi_range
  162. {
  163. template <typename Polygon, typename OutputIterator>
  164. static inline void apply(Polygon const& source, OutputIterator& it)
  165. {
  166. range_to_range::apply(geometry::exterior_ring(source), *it++);
  167. for (auto const& ring : geometry::interior_rings(source))
  168. {
  169. range_to_range::apply(ring, *it++);
  170. }
  171. }
  172. };
  173. template <typename Polygon1, typename Polygon2>
  174. struct polygon_to_polygon
  175. {
  176. static inline void apply(Polygon1 const& source, Polygon2& destination)
  177. {
  178. range_to_range::apply(geometry::exterior_ring(source),
  179. geometry::exterior_ring(destination));
  180. // Container should be resizeable
  181. traits::resize
  182. <
  183. std::remove_reference_t
  184. <
  185. typename traits::interior_mutable_type<Polygon2>::type
  186. >
  187. >::apply(interior_rings(destination), num_interior_rings(source));
  188. auto const& rings_source = interior_rings(source);
  189. auto&& rings_dest = interior_rings(destination);
  190. auto it_source = boost::begin(rings_source);
  191. auto it_dest = boost::begin(rings_dest);
  192. for ( ; it_source != boost::end(rings_source); ++it_source, ++it_dest)
  193. {
  194. range_to_range::apply(*it_source, *it_dest);
  195. }
  196. }
  197. };
  198. struct range_to_multi_point
  199. {
  200. template <typename Range, typename Iterator>
  201. static inline void append_from_source(Range const& source, Iterator& it)
  202. {
  203. for (auto const& point : source)
  204. {
  205. detail::conversion::convert_point_to_point(point, *it);
  206. ++it;
  207. }
  208. }
  209. template <typename Range, typename MultiPoint>
  210. static inline void apply(Range const& source, MultiPoint& destination)
  211. {
  212. traits::resize<MultiPoint>::apply(destination, boost::size(source));
  213. auto it = boost::begin(destination);
  214. append_from_source(source, it);
  215. }
  216. };
  217. struct polygon_to_range
  218. {
  219. template <typename Polygon, typename Iterator>
  220. static inline void append_from_polygon(Polygon const& source, Iterator& it)
  221. {
  222. range_to_multi_point::append_from_source(geometry::exterior_ring(source), it);
  223. for (auto const& ring : geometry::interior_rings(source))
  224. {
  225. range_to_multi_point::append_from_source(ring, it);
  226. }
  227. }
  228. template <typename Polygon, typename MultiPoint>
  229. static inline void apply(Polygon const& source, MultiPoint& destination)
  230. {
  231. traits::resize<MultiPoint>::apply(destination, geometry::num_points(source));
  232. auto it = boost::begin(destination);
  233. append_from_polygon(source, it);
  234. }
  235. };
  236. struct multi_polygon_to_range
  237. {
  238. template <typename MultiPolygon, typename MultiPoint>
  239. static inline void apply(MultiPolygon const& source, MultiPoint& destination)
  240. {
  241. traits::resize<MultiPoint>::apply(destination, geometry::num_points(source));
  242. auto it = boost::begin(destination);
  243. for (auto const& polygon : source)
  244. {
  245. polygon_to_range::append_from_polygon(polygon, it);
  246. }
  247. }
  248. };
  249. template <typename Single, typename Multi, typename ConversionAlgorithm>
  250. struct single_to_multi
  251. {
  252. static inline void apply(Single const& single, Multi& multi)
  253. {
  254. traits::resize<Multi>::apply(multi, 1);
  255. ConversionAlgorithm::apply(single, *boost::begin(multi));
  256. }
  257. };
  258. template <typename Multi1, typename Multi2, typename ConversionAlgorithm>
  259. struct multi_to_multi
  260. {
  261. static inline void apply(Multi1 const& multi1, Multi2& multi2)
  262. {
  263. traits::resize<Multi2>::apply(multi2, boost::size(multi1));
  264. auto it1 = boost::begin(multi1);
  265. auto it2 = boost::begin(multi2);
  266. for (; it1 != boost::end(multi1); ++it1, ++it2)
  267. {
  268. ConversionAlgorithm::apply(*it1, *it2);
  269. }
  270. }
  271. };
  272. }} // namespace detail::conversion
  273. #endif // DOXYGEN_NO_DETAIL
  274. #ifndef DOXYGEN_NO_DISPATCH
  275. namespace dispatch
  276. {
  277. // TODO: We could use std::is_assignable instead of std::is_same.
  278. // Then we should rather check ! std::is_array<Geometry2>::value
  279. // which is Destination.
  280. template
  281. <
  282. typename Geometry1, typename Geometry2,
  283. typename Tag1 = tag_t<Geometry1>,
  284. typename Tag2 = tag_t<Geometry2>,
  285. std::size_t DimensionCount = dimension<Geometry1>::value,
  286. bool UseAssignment = std::is_same<Geometry1, Geometry2>::value
  287. && !std::is_array<Geometry1>::value
  288. >
  289. struct convert
  290. : not_implemented
  291. <
  292. Tag1, Tag2,
  293. std::integral_constant<std::size_t, DimensionCount>
  294. >
  295. {};
  296. template
  297. <
  298. typename Geometry1, typename Geometry2,
  299. typename Tag,
  300. std::size_t DimensionCount
  301. >
  302. struct convert<Geometry1, Geometry2, Tag, Tag, DimensionCount, true>
  303. {
  304. // Same geometry type -> copy geometry
  305. static inline void apply(Geometry1 const& source, Geometry2& destination)
  306. {
  307. destination = source;
  308. }
  309. };
  310. template <typename Geometry1, typename Geometry2, std::size_t DimensionCount>
  311. struct convert<Geometry1, Geometry2, point_tag, point_tag, DimensionCount, false>
  312. : detail::conversion::point_to_point<Geometry1, Geometry2, 0, DimensionCount>
  313. {};
  314. template <typename Box1, typename Box2, std::size_t DimensionCount>
  315. struct convert<Box1, Box2, box_tag, box_tag, DimensionCount, false>
  316. : detail::conversion::indexed_to_indexed<Box1, Box2, 0, DimensionCount>
  317. {};
  318. template <typename Segment1, typename Segment2, std::size_t DimensionCount>
  319. struct convert<Segment1, Segment2, segment_tag, segment_tag, DimensionCount, false>
  320. : detail::conversion::indexed_to_indexed<Segment1, Segment2, 0, DimensionCount>
  321. {};
  322. template <typename Segment, typename LineString, std::size_t DimensionCount>
  323. struct convert<Segment, LineString, segment_tag, linestring_tag, DimensionCount, false>
  324. : detail::conversion::segment_to_range<Segment, LineString>
  325. {};
  326. template <typename Ring1, typename Ring2, std::size_t DimensionCount>
  327. struct convert<Ring1, Ring2, ring_tag, ring_tag, DimensionCount, false>
  328. : detail::conversion::range_to_range
  329. {};
  330. template <typename LineString1, typename LineString2, std::size_t DimensionCount>
  331. struct convert<LineString1, LineString2, linestring_tag, linestring_tag, DimensionCount, false>
  332. : detail::conversion::range_to_range
  333. {};
  334. template <typename Polygon1, typename Polygon2, std::size_t DimensionCount>
  335. struct convert<Polygon1, Polygon2, polygon_tag, polygon_tag, DimensionCount, false>
  336. : detail::conversion::polygon_to_polygon<Polygon1, Polygon2>
  337. {};
  338. template <typename Box, typename LineString>
  339. struct convert<Box, LineString, box_tag, linestring_tag, 2, false>
  340. : detail::conversion::box_to_range<true, false>
  341. {};
  342. template <typename Box, typename Ring>
  343. struct convert<Box, Ring, box_tag, ring_tag, 2, false>
  344. : detail::conversion::box_to_range
  345. <
  346. geometry::closure<Ring>::value == closed,
  347. geometry::point_order<Ring>::value == counterclockwise
  348. >
  349. {};
  350. template <typename Box, typename MultiPoint>
  351. struct convert<Box, MultiPoint, box_tag, multi_point_tag, 2, false>
  352. : detail::conversion::box_to_range<false, false>
  353. {};
  354. template <typename Box, typename Polygon>
  355. struct convert<Box, Polygon, box_tag, polygon_tag, 2, false>
  356. {
  357. static inline void apply(Box const& box, Polygon& polygon)
  358. {
  359. convert
  360. <
  361. Box, ring_type_t<Polygon>,
  362. box_tag, ring_tag,
  363. 2, false
  364. >::apply(box, exterior_ring(polygon));
  365. }
  366. };
  367. template <typename Point, typename Box, std::size_t DimensionCount>
  368. struct convert<Point, Box, point_tag, box_tag, DimensionCount, false>
  369. {
  370. static inline void apply(Point const& point, Box& box)
  371. {
  372. detail::conversion::point_to_box
  373. <
  374. Point, Box, min_corner, 0, DimensionCount
  375. >::apply(point, box);
  376. detail::conversion::point_to_box
  377. <
  378. Point, Box, max_corner, 0, DimensionCount
  379. >::apply(point, box);
  380. }
  381. };
  382. template <typename Ring, typename Polygon, std::size_t DimensionCount>
  383. struct convert<Ring, Polygon, ring_tag, polygon_tag, DimensionCount, false>
  384. {
  385. static inline void apply(Ring const& ring, Polygon& polygon)
  386. {
  387. convert
  388. <
  389. Ring, ring_type_t<Polygon>,
  390. ring_tag, ring_tag,
  391. DimensionCount, false
  392. >::apply(ring, exterior_ring(polygon));
  393. }
  394. };
  395. // Polygon to ring, this ignores interior rings
  396. template <typename Polygon, typename Ring, std::size_t DimensionCount>
  397. struct convert<Polygon, Ring, polygon_tag, ring_tag, DimensionCount, false>
  398. {
  399. static inline void apply(Polygon const& polygon, Ring& ring)
  400. {
  401. convert
  402. <
  403. ring_type_t<Polygon>, Ring,
  404. ring_tag, ring_tag,
  405. DimensionCount, false
  406. >::apply(exterior_ring(polygon), ring);
  407. }
  408. };
  409. template <typename LineString, typename MultiPoint, std::size_t DimensionCount>
  410. struct convert<LineString, MultiPoint, linestring_tag, multi_point_tag, DimensionCount, false>
  411. : detail::conversion::range_to_multi_point {};
  412. template <typename Ring, typename MultiPoint, std::size_t DimensionCount>
  413. struct convert<Ring, MultiPoint, ring_tag, multi_point_tag, DimensionCount, false>
  414. : detail::conversion::range_to_multi_point {};
  415. template <typename Polygon, typename MultiPoint, std::size_t DimensionCount>
  416. struct convert<Polygon, MultiPoint, polygon_tag, multi_point_tag, DimensionCount, false>
  417. : detail::conversion::polygon_to_range {};
  418. template <typename MultiPolygon, typename MultiPoint, std::size_t DimensionCount>
  419. struct convert<MultiPolygon, MultiPoint, multi_polygon_tag, multi_point_tag, DimensionCount, false>
  420. : detail::conversion::multi_polygon_to_range {};
  421. template <typename MultiLineString, typename MultiPoint, std::size_t DimensionCount>
  422. struct convert<MultiLineString, MultiPoint, multi_linestring_tag, multi_point_tag, DimensionCount, false>
  423. {
  424. static inline void apply(MultiLineString const& source, MultiPoint& destination)
  425. {
  426. traits::resize<MultiPoint>::apply(destination, geometry::num_points(source));
  427. auto it = boost::begin(destination);
  428. for (auto const& linestring : source)
  429. {
  430. detail::conversion::range_to_multi_point::append_from_source(linestring, it);
  431. }
  432. }
  433. };
  434. template <typename Ring, typename MultiLinestring, std::size_t DimensionCount>
  435. struct convert<Ring, MultiLinestring, ring_tag, multi_linestring_tag, DimensionCount, false>
  436. {
  437. static inline void apply(Ring const& source, MultiLinestring& destination)
  438. {
  439. traits::resize<MultiLinestring>::apply(destination, 1);
  440. detail::conversion::range_to_range::apply(source, *boost::begin(destination));
  441. }
  442. };
  443. template <typename Polygon, typename MultiLinestring, std::size_t DimensionCount>
  444. struct convert<Polygon, MultiLinestring, polygon_tag, multi_linestring_tag, DimensionCount, false>
  445. {
  446. static inline void apply(Polygon const& source, MultiLinestring& destination)
  447. {
  448. traits::resize<MultiLinestring>::apply(destination, 1 + geometry::num_interior_rings(source));
  449. auto it = boost::begin(destination);
  450. detail::conversion::polygon_to_multi_range::apply(source, it);
  451. }
  452. };
  453. template <typename MultiPolygon, typename MultiLinestring, std::size_t DimensionCount>
  454. struct convert<MultiPolygon, MultiLinestring, multi_polygon_tag, multi_linestring_tag, DimensionCount, false>
  455. {
  456. static inline void apply(MultiPolygon const& source, MultiLinestring& destination)
  457. {
  458. std::size_t ring_count = boost::size(source);
  459. for (auto const& polygon : source)
  460. {
  461. ring_count += geometry::num_interior_rings(polygon);
  462. }
  463. traits::resize<MultiLinestring>::apply(destination, ring_count);
  464. auto it = boost::begin(destination);
  465. for (auto const& polygon : source)
  466. {
  467. detail::conversion::polygon_to_multi_range::apply(polygon, it);
  468. }
  469. }
  470. };
  471. template <typename Single, typename Multi, std::size_t DimensionCount>
  472. struct single_to_multi_convert
  473. : detail::conversion::single_to_multi
  474. <
  475. Single,
  476. Multi,
  477. convert
  478. <
  479. Single,
  480. typename boost::range_value<Multi>::type,
  481. tag_t<Single>,
  482. single_tag_of_t<tag_t<Multi>>,
  483. DimensionCount,
  484. false
  485. >
  486. >
  487. {};
  488. template <typename Multi1, typename Multi2, std::size_t DimensionCount>
  489. struct multi_to_multi_convert
  490. : detail::conversion::multi_to_multi
  491. <
  492. Multi1,
  493. Multi2,
  494. convert
  495. <
  496. typename boost::range_value<Multi1>::type,
  497. typename boost::range_value<Multi2>::type,
  498. single_tag_of_t<tag_t<Multi1>>,
  499. single_tag_of_t<tag_t<Multi2>>,
  500. DimensionCount
  501. >
  502. >
  503. {};
  504. // Multi to multi of the same tag type
  505. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  506. struct convert<GeometryIn, GeometryOut, multi_point_tag, multi_point_tag, DimensionCount, false>
  507. : multi_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  508. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  509. struct convert<GeometryIn, GeometryOut, multi_linestring_tag, multi_linestring_tag, DimensionCount, false>
  510. : multi_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  511. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  512. struct convert<GeometryIn, GeometryOut, multi_polygon_tag, multi_polygon_tag, DimensionCount, false>
  513. : multi_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  514. // Single to multi of the same topological tag type
  515. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  516. struct convert<GeometryIn, GeometryOut, point_tag, multi_point_tag, DimensionCount, false>
  517. : single_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  518. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  519. struct convert<GeometryIn, GeometryOut, segment_tag, multi_linestring_tag, DimensionCount, false>
  520. : single_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  521. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  522. struct convert<GeometryIn, GeometryOut, linestring_tag, multi_linestring_tag, DimensionCount, false>
  523. : single_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  524. // To multi_polygon (box, ring, polygon)
  525. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  526. struct convert<GeometryIn, GeometryOut, box_tag, multi_polygon_tag, DimensionCount, false>
  527. : single_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  528. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  529. struct convert<GeometryIn, GeometryOut, ring_tag, multi_polygon_tag, DimensionCount, false>
  530. : single_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  531. template <typename GeometryIn, typename GeometryOut, std::size_t DimensionCount>
  532. struct convert<GeometryIn, GeometryOut, polygon_tag, multi_polygon_tag, DimensionCount, false>
  533. : single_to_multi_convert<GeometryIn, GeometryOut, DimensionCount> {};
  534. } // namespace dispatch
  535. #endif // DOXYGEN_NO_DISPATCH
  536. namespace resolve_variant {
  537. template <typename Geometry1, typename Geometry2>
  538. struct convert
  539. {
  540. static inline void apply(Geometry1 const& geometry1, Geometry2& geometry2)
  541. {
  542. concepts::check_concepts_and_equal_dimensions<Geometry1 const, Geometry2>();
  543. dispatch::convert<Geometry1, Geometry2>::apply(geometry1, geometry2);
  544. }
  545. };
  546. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  547. struct convert<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  548. {
  549. struct visitor: static_visitor<void>
  550. {
  551. Geometry2& m_geometry2;
  552. visitor(Geometry2& geometry2)
  553. : m_geometry2(geometry2)
  554. {}
  555. template <typename Geometry1>
  556. inline void operator()(Geometry1 const& geometry1) const
  557. {
  558. convert<Geometry1, Geometry2>::apply(geometry1, m_geometry2);
  559. }
  560. };
  561. static inline void apply(
  562. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  563. Geometry2& geometry2
  564. )
  565. {
  566. boost::apply_visitor(visitor(geometry2), geometry1);
  567. }
  568. };
  569. }
  570. /*!
  571. \brief Converts one geometry to another geometry
  572. \details The convert algorithm converts one geometry, e.g. a BOX, to another
  573. geometry, e.g. a RING. This only works if it is possible and applicable.
  574. If the point-order is different, or the closure is different between two
  575. geometry types, it will be converted correctly by explicitly reversing the
  576. points or closing or opening the polygon rings.
  577. \ingroup convert
  578. \tparam Geometry1 \tparam_geometry
  579. \tparam Geometry2 \tparam_geometry
  580. \param geometry1 \param_geometry (source)
  581. \param geometry2 \param_geometry (target)
  582. \qbk{[include reference/algorithms/convert.qbk]}
  583. */
  584. template <typename Geometry1, typename Geometry2>
  585. inline void convert(Geometry1 const& geometry1, Geometry2& geometry2)
  586. {
  587. resolve_variant::convert<Geometry1, Geometry2>::apply(geometry1, geometry2);
  588. }
  589. #if defined(_MSC_VER)
  590. #pragma warning(pop)
  591. #endif
  592. }} // namespace boost::geometry
  593. #endif // BOOST_GEOMETRY_ALGORITHMS_CONVERT_HPP