transform.hpp 14 KB

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