convert.hpp 17 KB

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