convert.hpp 17 KB

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