convert.hpp 17 KB

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