convert.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 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 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/detail/assign_box_corners.hpp>
  27. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  28. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  29. #include <boost/geometry/algorithms/detail/convert_indexed_to_indexed.hpp>
  30. #include <boost/geometry/algorithms/not_implemented.hpp>
  31. #include <boost/geometry/core/closure.hpp>
  32. #include <boost/geometry/core/point_order.hpp>
  33. #include <boost/geometry/core/tags.hpp>
  34. #include <boost/geometry/geometries/concepts/check.hpp>
  35. #include <boost/geometry/util/numeric_cast.hpp>
  36. #include <boost/geometry/util/range.hpp>
  37. #include <boost/geometry/views/detail/closed_clockwise_view.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. // Silence warning C4127: conditional expression is constant
  41. // Silence warning C4512: assignment operator could not be generated
  42. #if defined(_MSC_VER)
  43. #pragma warning(push)
  44. #pragma warning(disable : 4127 4512)
  45. #endif
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace conversion
  48. {
  49. template
  50. <
  51. typename Point,
  52. typename Box,
  53. std::size_t Index,
  54. std::size_t Dimension,
  55. std::size_t DimensionCount
  56. >
  57. struct point_to_box
  58. {
  59. static inline void apply(Point const& point, Box& box)
  60. {
  61. typedef typename coordinate_type<Box>::type coordinate_type;
  62. set<Index, Dimension>(box,
  63. util::numeric_cast<coordinate_type>(get<Dimension>(point)));
  64. point_to_box
  65. <
  66. Point, Box,
  67. Index, Dimension + 1, DimensionCount
  68. >::apply(point, box);
  69. }
  70. };
  71. template
  72. <
  73. typename Point,
  74. typename Box,
  75. std::size_t Index,
  76. std::size_t DimensionCount
  77. >
  78. struct point_to_box<Point, Box, Index, DimensionCount, DimensionCount>
  79. {
  80. static inline void apply(Point const& , Box& )
  81. {}
  82. };
  83. template <typename Box, typename Range, bool Close, bool Reverse>
  84. struct box_to_range
  85. {
  86. static inline void apply(Box const& box, Range& range)
  87. {
  88. traits::resize<Range>::apply(range, Close ? 5 : 4);
  89. assign_box_corners_oriented<Reverse>(box, range);
  90. if (Close)
  91. {
  92. range::at(range, 4) = range::at(range, 0);
  93. }
  94. }
  95. };
  96. template <typename Segment, typename Range>
  97. struct segment_to_range
  98. {
  99. static inline void apply(Segment const& segment, Range& range)
  100. {
  101. traits::resize<Range>::apply(range, 2);
  102. auto it = boost::begin(range);
  103. assign_point_from_index<0>(segment, *it);
  104. ++it;
  105. assign_point_from_index<1>(segment, *it);
  106. }
  107. };
  108. template
  109. <
  110. typename Range1,
  111. typename Range2,
  112. bool Reverse = false
  113. >
  114. struct range_to_range
  115. {
  116. struct default_policy
  117. {
  118. template <typename Point1, typename Point2>
  119. static inline void apply(Point1 const& point1, Point2 & point2)
  120. {
  121. geometry::detail::conversion::convert_point_to_point(point1, point2);
  122. }
  123. };
  124. static inline void apply(Range1 const& source, Range2& destination)
  125. {
  126. apply(source, destination, default_policy());
  127. }
  128. template <typename ConvertPointPolicy>
  129. static inline ConvertPointPolicy apply(Range1 const& source, Range2& destination,
  130. ConvertPointPolicy convert_point)
  131. {
  132. geometry::clear(destination);
  133. using view_type = detail::closed_clockwise_view
  134. <
  135. Range1 const,
  136. geometry::closure<Range1>::value,
  137. Reverse ? counterclockwise : clockwise
  138. >;
  139. // We consider input always as closed, and skip last
  140. // point for open output.
  141. view_type const view(source);
  142. typedef typename boost::range_size<Range1>::type size_type;
  143. size_type n = boost::size(view);
  144. if (geometry::closure<Range2>::value == geometry::open)
  145. {
  146. n--;
  147. }
  148. // If size == 0 && geometry::open <=> n = numeric_limits<size_type>::max()
  149. // but ok, sice below it == end()
  150. size_type i = 0;
  151. for (auto it = boost::begin(view);
  152. it != boost::end(view) && i < n;
  153. ++it, ++i)
  154. {
  155. typename boost::range_value<Range2>::type point;
  156. convert_point.apply(*it, point);
  157. range::push_back(destination, point);
  158. }
  159. return convert_point;
  160. }
  161. };
  162. template <typename Polygon1, typename Polygon2>
  163. struct polygon_to_polygon
  164. {
  165. typedef range_to_range
  166. <
  167. typename geometry::ring_type<Polygon1>::type,
  168. typename geometry::ring_type<Polygon2>::type,
  169. geometry::point_order<Polygon1>::value
  170. != geometry::point_order<Polygon2>::value
  171. > per_ring;
  172. static inline void apply(Polygon1 const& source, Polygon2& destination)
  173. {
  174. // Clearing managed per ring, and in the resizing of interior rings
  175. per_ring::apply(geometry::exterior_ring(source),
  176. geometry::exterior_ring(destination));
  177. // Container should be resizeable
  178. traits::resize
  179. <
  180. typename std::remove_reference
  181. <
  182. typename traits::interior_mutable_type<Polygon2>::type
  183. >::type
  184. >::apply(interior_rings(destination), num_interior_rings(source));
  185. auto const& rings_source = interior_rings(source);
  186. auto&& rings_dest = interior_rings(destination);
  187. auto it_source = boost::begin(rings_source);
  188. auto it_dest = boost::begin(rings_dest);
  189. for ( ; it_source != boost::end(rings_source); ++it_source, ++it_dest)
  190. {
  191. per_ring::apply(*it_source, *it_dest);
  192. }
  193. }
  194. };
  195. template <typename Single, typename Multi, typename Policy>
  196. struct single_to_multi: private Policy
  197. {
  198. static inline void apply(Single const& single, Multi& multi)
  199. {
  200. traits::resize<Multi>::apply(multi, 1);
  201. Policy::apply(single, *boost::begin(multi));
  202. }
  203. };
  204. template <typename Multi1, typename Multi2, typename Policy>
  205. struct multi_to_multi: private Policy
  206. {
  207. static inline void apply(Multi1 const& multi1, Multi2& multi2)
  208. {
  209. traits::resize<Multi2>::apply(multi2, boost::size(multi1));
  210. auto it1 = boost::begin(multi1);
  211. auto it2 = boost::begin(multi2);
  212. for (; it1 != boost::end(multi1); ++it1, ++it2)
  213. {
  214. Policy::apply(*it1, *it2);
  215. }
  216. }
  217. };
  218. }} // namespace detail::conversion
  219. #endif // DOXYGEN_NO_DETAIL
  220. #ifndef DOXYGEN_NO_DISPATCH
  221. namespace dispatch
  222. {
  223. // TODO: We could use std::is_assignable instead of std::is_same.
  224. // Then we should rather check ! std::is_array<Geometry2>::value
  225. // which is Destination.
  226. template
  227. <
  228. typename Geometry1, typename Geometry2,
  229. typename Tag1 = typename tag_cast<typename tag<Geometry1>::type, multi_tag>::type,
  230. typename Tag2 = typename tag_cast<typename tag<Geometry2>::type, multi_tag>::type,
  231. std::size_t DimensionCount = dimension<Geometry1>::type::value,
  232. bool UseAssignment = std::is_same<Geometry1, Geometry2>::value
  233. && !std::is_array<Geometry1>::value
  234. >
  235. struct convert
  236. : not_implemented
  237. <
  238. Tag1, Tag2,
  239. std::integral_constant<std::size_t, DimensionCount>
  240. >
  241. {};
  242. template
  243. <
  244. typename Geometry1, typename Geometry2,
  245. typename Tag,
  246. std::size_t DimensionCount
  247. >
  248. struct convert<Geometry1, Geometry2, Tag, Tag, DimensionCount, true>
  249. {
  250. // Same geometry type -> copy whole geometry
  251. static inline void apply(Geometry1 const& source, Geometry2& destination)
  252. {
  253. destination = source;
  254. }
  255. };
  256. template
  257. <
  258. typename Geometry1, typename Geometry2,
  259. std::size_t DimensionCount
  260. >
  261. struct convert<Geometry1, Geometry2, point_tag, point_tag, DimensionCount, false>
  262. : detail::conversion::point_to_point<Geometry1, Geometry2, 0, DimensionCount>
  263. {};
  264. template
  265. <
  266. typename Box1, typename Box2,
  267. std::size_t DimensionCount
  268. >
  269. struct convert<Box1, Box2, box_tag, box_tag, DimensionCount, false>
  270. : detail::conversion::indexed_to_indexed<Box1, Box2, 0, DimensionCount>
  271. {};
  272. template
  273. <
  274. typename Segment1, typename Segment2,
  275. std::size_t DimensionCount
  276. >
  277. struct convert<Segment1, Segment2, segment_tag, segment_tag, DimensionCount, false>
  278. : detail::conversion::indexed_to_indexed<Segment1, Segment2, 0, DimensionCount>
  279. {};
  280. template <typename Segment, typename LineString, std::size_t DimensionCount>
  281. struct convert<Segment, LineString, segment_tag, linestring_tag, DimensionCount, false>
  282. : detail::conversion::segment_to_range<Segment, LineString>
  283. {};
  284. template <typename Ring1, typename Ring2, std::size_t DimensionCount>
  285. struct convert<Ring1, Ring2, ring_tag, ring_tag, DimensionCount, false>
  286. : detail::conversion::range_to_range
  287. <
  288. Ring1,
  289. Ring2,
  290. geometry::point_order<Ring1>::value
  291. != geometry::point_order<Ring2>::value
  292. >
  293. {};
  294. template <typename LineString1, typename LineString2, std::size_t DimensionCount>
  295. struct convert<LineString1, LineString2, linestring_tag, linestring_tag, DimensionCount, false>
  296. : detail::conversion::range_to_range<LineString1, LineString2>
  297. {};
  298. template <typename Polygon1, typename Polygon2, std::size_t DimensionCount>
  299. struct convert<Polygon1, Polygon2, polygon_tag, polygon_tag, DimensionCount, false>
  300. : detail::conversion::polygon_to_polygon<Polygon1, Polygon2>
  301. {};
  302. template <typename Box, typename Ring>
  303. struct convert<Box, Ring, box_tag, ring_tag, 2, false>
  304. : detail::conversion::box_to_range
  305. <
  306. Box,
  307. Ring,
  308. geometry::closure<Ring>::value == closed,
  309. geometry::point_order<Ring>::value == counterclockwise
  310. >
  311. {};
  312. template <typename Box, typename Polygon>
  313. struct convert<Box, Polygon, box_tag, polygon_tag, 2, false>
  314. {
  315. static inline void apply(Box const& box, Polygon& polygon)
  316. {
  317. typedef typename ring_type<Polygon>::type ring_type;
  318. convert
  319. <
  320. Box, ring_type,
  321. box_tag, ring_tag,
  322. 2, false
  323. >::apply(box, exterior_ring(polygon));
  324. }
  325. };
  326. template <typename Point, typename Box, std::size_t DimensionCount>
  327. struct convert<Point, Box, point_tag, box_tag, DimensionCount, false>
  328. {
  329. static inline void apply(Point const& point, Box& box)
  330. {
  331. detail::conversion::point_to_box
  332. <
  333. Point, Box, min_corner, 0, DimensionCount
  334. >::apply(point, box);
  335. detail::conversion::point_to_box
  336. <
  337. Point, Box, max_corner, 0, DimensionCount
  338. >::apply(point, box);
  339. }
  340. };
  341. template <typename Ring, typename Polygon, std::size_t DimensionCount>
  342. struct convert<Ring, Polygon, ring_tag, polygon_tag, DimensionCount, false>
  343. {
  344. static inline void apply(Ring const& ring, Polygon& polygon)
  345. {
  346. typedef typename ring_type<Polygon>::type ring_type;
  347. convert
  348. <
  349. Ring, ring_type,
  350. ring_tag, ring_tag,
  351. DimensionCount, false
  352. >::apply(ring, exterior_ring(polygon));
  353. }
  354. };
  355. template <typename Polygon, typename Ring, std::size_t DimensionCount>
  356. struct convert<Polygon, Ring, polygon_tag, ring_tag, DimensionCount, false>
  357. {
  358. static inline void apply(Polygon const& polygon, Ring& ring)
  359. {
  360. typedef typename ring_type<Polygon>::type ring_type;
  361. convert
  362. <
  363. ring_type, Ring,
  364. ring_tag, ring_tag,
  365. DimensionCount, false
  366. >::apply(exterior_ring(polygon), ring);
  367. }
  368. };
  369. // Dispatch for multi <-> multi, specifying their single-version as policy.
  370. // Note that, even if the multi-types are mutually different, their single
  371. // version types might be the same and therefore we call std::is_same again
  372. template <typename Multi1, typename Multi2, std::size_t DimensionCount>
  373. struct convert<Multi1, Multi2, multi_tag, multi_tag, DimensionCount, false>
  374. : detail::conversion::multi_to_multi
  375. <
  376. Multi1,
  377. Multi2,
  378. convert
  379. <
  380. typename boost::range_value<Multi1>::type,
  381. typename boost::range_value<Multi2>::type,
  382. typename single_tag_of
  383. <
  384. typename tag<Multi1>::type
  385. >::type,
  386. typename single_tag_of
  387. <
  388. typename tag<Multi2>::type
  389. >::type,
  390. DimensionCount
  391. >
  392. >
  393. {};
  394. template <typename Single, typename Multi, typename SingleTag, std::size_t DimensionCount>
  395. struct convert<Single, Multi, SingleTag, multi_tag, DimensionCount, false>
  396. : detail::conversion::single_to_multi
  397. <
  398. Single,
  399. Multi,
  400. convert
  401. <
  402. Single,
  403. typename boost::range_value<Multi>::type,
  404. typename tag<Single>::type,
  405. typename single_tag_of
  406. <
  407. typename tag<Multi>::type
  408. >::type,
  409. DimensionCount,
  410. false
  411. >
  412. >
  413. {};
  414. } // namespace dispatch
  415. #endif // DOXYGEN_NO_DISPATCH
  416. namespace resolve_variant {
  417. template <typename Geometry1, typename Geometry2>
  418. struct convert
  419. {
  420. static inline void apply(Geometry1 const& geometry1, Geometry2& geometry2)
  421. {
  422. concepts::check_concepts_and_equal_dimensions<Geometry1 const, Geometry2>();
  423. dispatch::convert<Geometry1, Geometry2>::apply(geometry1, geometry2);
  424. }
  425. };
  426. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  427. struct convert<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  428. {
  429. struct visitor: static_visitor<void>
  430. {
  431. Geometry2& m_geometry2;
  432. visitor(Geometry2& geometry2)
  433. : m_geometry2(geometry2)
  434. {}
  435. template <typename Geometry1>
  436. inline void operator()(Geometry1 const& geometry1) const
  437. {
  438. convert<Geometry1, Geometry2>::apply(geometry1, m_geometry2);
  439. }
  440. };
  441. static inline void apply(
  442. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  443. Geometry2& geometry2
  444. )
  445. {
  446. boost::apply_visitor(visitor(geometry2), geometry1);
  447. }
  448. };
  449. }
  450. /*!
  451. \brief Converts one geometry to another geometry
  452. \details The convert algorithm converts one geometry, e.g. a BOX, to another
  453. geometry, e.g. a RING. This only works if it is possible and applicable.
  454. If the point-order is different, or the closure is different between two
  455. geometry types, it will be converted correctly by explicitly reversing the
  456. points or closing or opening the polygon rings.
  457. \ingroup convert
  458. \tparam Geometry1 \tparam_geometry
  459. \tparam Geometry2 \tparam_geometry
  460. \param geometry1 \param_geometry (source)
  461. \param geometry2 \param_geometry (target)
  462. \qbk{[include reference/algorithms/convert.qbk]}
  463. */
  464. template <typename Geometry1, typename Geometry2>
  465. inline void convert(Geometry1 const& geometry1, Geometry2& geometry2)
  466. {
  467. resolve_variant::convert<Geometry1, Geometry2>::apply(geometry1, geometry2);
  468. }
  469. #if defined(_MSC_VER)
  470. #pragma warning(pop)
  471. #endif
  472. }} // namespace boost::geometry
  473. #endif // BOOST_GEOMETRY_ALGORITHMS_CONVERT_HPP