transform.hpp 13 KB

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