transform.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 2020-2023.
  7. // Modifications copyright (c) 2020-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_TRANSFORM_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_TRANSFORM_HPP
  17. #include <type_traits>
  18. #include <boost/range/begin.hpp>
  19. #include <boost/range/end.hpp>
  20. #include <boost/range/size.hpp>
  21. #include <boost/range/value_type.hpp>
  22. #include <boost/variant/static_visitor.hpp>
  23. #include <boost/variant/variant_fwd.hpp>
  24. #include <boost/geometry/algorithms/clear.hpp>
  25. #include "boost/geometry/algorithms/detail/assign_indexed_point.hpp"
  26. #include "boost/geometry/algorithms/detail/assign_values.hpp"
  27. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  28. #include <boost/geometry/core/cs.hpp>
  29. #include <boost/geometry/core/exterior_ring.hpp>
  30. #include <boost/geometry/core/interior_rings.hpp>
  31. #include <boost/geometry/core/mutable_range.hpp>
  32. #include <boost/geometry/core/tag_cast.hpp>
  33. #include <boost/geometry/core/tags.hpp>
  34. #include <boost/geometry/geometries/concepts/check.hpp>
  35. #include <boost/geometry/strategies/default_strategy.hpp>
  36. #include <boost/geometry/strategies/transform.hpp>
  37. namespace boost { namespace geometry
  38. {
  39. #ifndef DOXYGEN_NO_DETAIL
  40. namespace detail { namespace transform
  41. {
  42. struct transform_point
  43. {
  44. template <typename Point1, typename Point2, typename Strategy>
  45. static inline bool apply(Point1 const& p1, Point2& p2,
  46. Strategy const& strategy)
  47. {
  48. return strategy.apply(p1, p2);
  49. }
  50. };
  51. struct transform_box
  52. {
  53. template <typename Box1, typename Box2, typename Strategy>
  54. static inline bool apply(Box1 const& b1, Box2& b2,
  55. Strategy const& strategy)
  56. {
  57. typedef typename point_type<Box1>::type point_type1;
  58. typedef typename point_type<Box2>::type point_type2;
  59. point_type1 lower_left, upper_right;
  60. geometry::detail::assign::assign_box_2d_corner<min_corner, min_corner>(
  61. b1, lower_left);
  62. geometry::detail::assign::assign_box_2d_corner<max_corner, max_corner>(
  63. b1, upper_right);
  64. point_type2 p1, p2;
  65. if (strategy.apply(lower_left, p1) && strategy.apply(upper_right, p2))
  66. {
  67. // Create a valid box and therefore swap if necessary
  68. typedef typename coordinate_type<point_type2>::type coordinate_type;
  69. coordinate_type x1 = geometry::get<0>(p1)
  70. , y1 = geometry::get<1>(p1)
  71. , x2 = geometry::get<0>(p2)
  72. , y2 = geometry::get<1>(p2);
  73. if (x1 > x2) { std::swap(x1, x2); }
  74. if (y1 > y2) { std::swap(y1, y2); }
  75. geometry::set<min_corner, 0>(b2, x1);
  76. geometry::set<min_corner, 1>(b2, y1);
  77. geometry::set<max_corner, 0>(b2, x2);
  78. geometry::set<max_corner, 1>(b2, y2);
  79. return true;
  80. }
  81. return false;
  82. }
  83. };
  84. struct transform_box_or_segment
  85. {
  86. template <typename Geometry1, typename Geometry2, typename Strategy>
  87. static inline bool apply(Geometry1 const& source, Geometry2& target,
  88. Strategy const& strategy)
  89. {
  90. typedef typename point_type<Geometry1>::type point_type1;
  91. typedef typename point_type<Geometry2>::type point_type2;
  92. point_type1 source_point[2];
  93. geometry::detail::assign_point_from_index<0>(source, source_point[0]);
  94. geometry::detail::assign_point_from_index<1>(source, source_point[1]);
  95. point_type2 target_point[2];
  96. if (strategy.apply(source_point[0], target_point[0])
  97. && strategy.apply(source_point[1], target_point[1]))
  98. {
  99. geometry::detail::assign_point_to_index<0>(target_point[0], target);
  100. geometry::detail::assign_point_to_index<1>(target_point[1], target);
  101. return true;
  102. }
  103. return false;
  104. }
  105. };
  106. template
  107. <
  108. typename PointOut,
  109. typename OutputIterator,
  110. typename Range,
  111. typename Strategy
  112. >
  113. inline bool transform_range_out(Range const& range,
  114. OutputIterator out, Strategy const& strategy)
  115. {
  116. PointOut point_out;
  117. for (auto it = boost::begin(range); it != boost::end(range); ++it)
  118. {
  119. if (! transform_point::apply(*it, point_out, strategy))
  120. {
  121. return false;
  122. }
  123. *out++ = point_out;
  124. }
  125. return true;
  126. }
  127. struct transform_polygon
  128. {
  129. template <typename Polygon1, typename Polygon2, typename Strategy>
  130. static inline bool apply(Polygon1 const& poly1, Polygon2& poly2,
  131. Strategy const& strategy)
  132. {
  133. typedef typename point_type<Polygon2>::type point2_type;
  134. geometry::clear(poly2);
  135. if (!transform_range_out<point2_type>(geometry::exterior_ring(poly1),
  136. range::back_inserter(geometry::exterior_ring(poly2)), strategy))
  137. {
  138. return false;
  139. }
  140. // Note: here a resizeable container is assumed.
  141. traits::resize
  142. <
  143. typename std::remove_reference
  144. <
  145. typename traits::interior_mutable_type<Polygon2>::type
  146. >::type
  147. >::apply(geometry::interior_rings(poly2),
  148. geometry::num_interior_rings(poly1));
  149. auto const& rings1 = geometry::interior_rings(poly1);
  150. auto&& rings2 = geometry::interior_rings(poly2);
  151. auto it1 = boost::begin(rings1);
  152. auto it2 = boost::begin(rings2);
  153. for ( ; it1 != boost::end(rings1); ++it1, ++it2)
  154. {
  155. if ( ! transform_range_out<point2_type>(*it1,
  156. range::back_inserter(*it2),
  157. strategy) )
  158. {
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. };
  165. template <typename Point1, typename Point2>
  166. struct select_strategy
  167. {
  168. typedef typename strategy::transform::services::default_strategy
  169. <
  170. typename cs_tag<Point1>::type,
  171. typename cs_tag<Point2>::type,
  172. typename coordinate_system<Point1>::type,
  173. typename coordinate_system<Point2>::type,
  174. dimension<Point1>::type::value,
  175. dimension<Point2>::type::value,
  176. typename point_type<Point1>::type,
  177. typename point_type<Point2>::type
  178. >::type type;
  179. };
  180. struct transform_range
  181. {
  182. template <typename Range1, typename Range2, typename Strategy>
  183. static inline bool apply(Range1 const& range1,
  184. Range2& range2, Strategy const& strategy)
  185. {
  186. typedef typename point_type<Range2>::type point_type;
  187. // Should NOT be done here!
  188. // geometry::clear(range2);
  189. return transform_range_out<point_type>(range1,
  190. range::back_inserter(range2), strategy);
  191. }
  192. };
  193. /*!
  194. \brief Is able to transform any multi-geometry, calling the single-version as policy
  195. */
  196. template <typename Policy>
  197. struct transform_multi
  198. {
  199. template <typename Multi1, typename Multi2, typename S>
  200. static inline bool apply(Multi1 const& multi1, Multi2& multi2, S const& strategy)
  201. {
  202. traits::resize<Multi2>::apply(multi2, boost::size(multi1));
  203. auto it1 = boost::begin(multi1);
  204. auto it2 = boost::begin(multi2);
  205. for (; it1 != boost::end(multi1); ++it1, ++it2)
  206. {
  207. if (! Policy::apply(*it1, *it2, strategy))
  208. {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }
  214. };
  215. }} // namespace detail::transform
  216. #endif // DOXYGEN_NO_DETAIL
  217. #ifndef DOXYGEN_NO_DISPATCH
  218. namespace dispatch
  219. {
  220. template
  221. <
  222. typename Geometry1, typename Geometry2,
  223. typename Tag1 = typename tag_cast<typename tag<Geometry1>::type, multi_tag>::type,
  224. typename Tag2 = typename tag_cast<typename tag<Geometry2>::type, multi_tag>::type
  225. >
  226. struct transform {};
  227. template <typename Point1, typename Point2>
  228. struct transform<Point1, Point2, point_tag, point_tag>
  229. : detail::transform::transform_point
  230. {
  231. };
  232. template <typename Linestring1, typename Linestring2>
  233. struct transform
  234. <
  235. Linestring1, Linestring2,
  236. linestring_tag, linestring_tag
  237. >
  238. : detail::transform::transform_range
  239. {
  240. };
  241. template <typename Range1, typename Range2>
  242. struct transform<Range1, Range2, ring_tag, ring_tag>
  243. : detail::transform::transform_range
  244. {
  245. };
  246. template <typename Polygon1, typename Polygon2>
  247. struct transform<Polygon1, Polygon2, polygon_tag, polygon_tag>
  248. : detail::transform::transform_polygon
  249. {
  250. };
  251. template <typename Box1, typename Box2>
  252. struct transform<Box1, Box2, box_tag, box_tag>
  253. : detail::transform::transform_box
  254. {
  255. };
  256. template <typename Segment1, typename Segment2>
  257. struct transform<Segment1, Segment2, segment_tag, segment_tag>
  258. : detail::transform::transform_box_or_segment
  259. {
  260. };
  261. template <typename Multi1, typename Multi2>
  262. struct transform
  263. <
  264. Multi1, Multi2,
  265. multi_tag, multi_tag
  266. >
  267. : detail::transform::transform_multi
  268. <
  269. dispatch::transform
  270. <
  271. typename boost::range_value<Multi1>::type,
  272. typename boost::range_value<Multi2>::type
  273. >
  274. >
  275. {};
  276. } // namespace dispatch
  277. #endif // DOXYGEN_NO_DISPATCH
  278. namespace resolve_strategy {
  279. struct transform
  280. {
  281. template <typename Geometry1, typename Geometry2, typename Strategy>
  282. static inline bool apply(Geometry1 const& geometry1,
  283. Geometry2& geometry2,
  284. Strategy const& strategy)
  285. {
  286. concepts::check<Geometry1 const>();
  287. concepts::check<Geometry2>();
  288. return dispatch::transform<Geometry1, Geometry2>::apply(
  289. geometry1,
  290. geometry2,
  291. strategy
  292. );
  293. }
  294. template <typename Geometry1, typename Geometry2>
  295. static inline bool apply(Geometry1 const& geometry1,
  296. Geometry2& geometry2,
  297. default_strategy)
  298. {
  299. return apply(
  300. geometry1,
  301. geometry2,
  302. typename detail::transform::select_strategy<Geometry1, Geometry2>::type()
  303. );
  304. }
  305. };
  306. } // namespace resolve_strategy
  307. namespace resolve_variant {
  308. template <typename Geometry1, typename Geometry2>
  309. struct transform
  310. {
  311. template <typename Strategy>
  312. static inline bool apply(Geometry1 const& geometry1,
  313. Geometry2& geometry2,
  314. Strategy const& strategy)
  315. {
  316. return resolve_strategy::transform::apply(
  317. geometry1,
  318. geometry2,
  319. strategy
  320. );
  321. }
  322. };
  323. template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
  324. struct transform<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
  325. {
  326. template <typename Strategy>
  327. struct visitor: static_visitor<bool>
  328. {
  329. Geometry2& m_geometry2;
  330. Strategy const& m_strategy;
  331. visitor(Geometry2& geometry2, Strategy const& strategy)
  332. : m_geometry2(geometry2)
  333. , m_strategy(strategy)
  334. {}
  335. template <typename Geometry1>
  336. inline bool operator()(Geometry1 const& geometry1) const
  337. {
  338. return transform<Geometry1, Geometry2>::apply(
  339. geometry1,
  340. m_geometry2,
  341. m_strategy
  342. );
  343. }
  344. };
  345. template <typename Strategy>
  346. static inline bool apply(
  347. boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
  348. Geometry2& geometry2,
  349. Strategy const& strategy
  350. )
  351. {
  352. return boost::apply_visitor(visitor<Strategy>(geometry2, strategy), geometry1);
  353. }
  354. };
  355. } // namespace resolve_variant
  356. /*!
  357. \brief Transforms from one geometry to another geometry \brief_strategy
  358. \ingroup transform
  359. \tparam Geometry1 \tparam_geometry
  360. \tparam Geometry2 \tparam_geometry
  361. \tparam Strategy strategy
  362. \param geometry1 \param_geometry
  363. \param geometry2 \param_geometry
  364. \param strategy The strategy to be used for transformation
  365. \return True if the transformation could be done
  366. \qbk{distinguish,with strategy}
  367. \qbk{[include reference/algorithms/transform_with_strategy.qbk]}
  368. */
  369. template <typename Geometry1, typename Geometry2, typename Strategy>
  370. inline bool transform(Geometry1 const& geometry1, Geometry2& geometry2,
  371. Strategy const& strategy)
  372. {
  373. return resolve_variant::transform<Geometry1, Geometry2>
  374. ::apply(geometry1, geometry2, strategy);
  375. }
  376. /*!
  377. \brief Transforms from one geometry to another geometry using a strategy
  378. \ingroup transform
  379. \tparam Geometry1 \tparam_geometry
  380. \tparam Geometry2 \tparam_geometry
  381. \param geometry1 \param_geometry
  382. \param geometry2 \param_geometry
  383. \return True if the transformation could be done
  384. \qbk{[include reference/algorithms/transform.qbk]}
  385. */
  386. template <typename Geometry1, typename Geometry2>
  387. inline bool transform(Geometry1 const& geometry1, Geometry2& geometry2)
  388. {
  389. return geometry::transform(geometry1, geometry2, default_strategy());
  390. }
  391. }} // namespace boost::geometry
  392. #endif // BOOST_GEOMETRY_ALGORITHMS_TRANSFORM_HPP