transform.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_TRANSFORM_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_TRANSFORM_HPP
  12. #include <cmath>
  13. #include <iterator>
  14. #include <boost/range.hpp>
  15. #include <boost/typeof/typeof.hpp>
  16. #include <boost/geometry/algorithms/assign.hpp>
  17. #include <boost/geometry/algorithms/clear.hpp>
  18. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  19. #include <boost/geometry/core/cs.hpp>
  20. #include <boost/geometry/core/exterior_ring.hpp>
  21. #include <boost/geometry/core/interior_rings.hpp>
  22. #include <boost/geometry/core/mutable_range.hpp>
  23. #include <boost/geometry/core/ring_type.hpp>
  24. #include <boost/geometry/core/tag_cast.hpp>
  25. #include <boost/geometry/geometries/concepts/check.hpp>
  26. #include <boost/geometry/strategies/transform.hpp>
  27. namespace boost { namespace geometry
  28. {
  29. #ifndef DOXYGEN_NO_DETAIL
  30. namespace detail { namespace transform
  31. {
  32. struct transform_point
  33. {
  34. template <typename Point1, typename Point2, typename Strategy>
  35. static inline bool apply(Point1 const& p1, Point2& p2,
  36. Strategy const& strategy)
  37. {
  38. return strategy.apply(p1, p2);
  39. }
  40. };
  41. struct transform_box
  42. {
  43. template <typename Box1, typename Box2, typename Strategy>
  44. static inline bool apply(Box1 const& b1, Box2& b2,
  45. Strategy const& strategy)
  46. {
  47. typedef typename point_type<Box1>::type point_type1;
  48. typedef typename point_type<Box2>::type point_type2;
  49. point_type1 lower_left, upper_right;
  50. detail::assign::assign_box_2d_corner<min_corner, min_corner>(
  51. b1, lower_left);
  52. detail::assign::assign_box_2d_corner<max_corner, max_corner>(
  53. b1, upper_right);
  54. point_type2 p1, p2;
  55. if (strategy.apply(lower_left, p1) && strategy.apply(upper_right, p2))
  56. {
  57. // Create a valid box and therefore swap if necessary
  58. typedef typename coordinate_type<point_type2>::type coordinate_type;
  59. coordinate_type x1 = geometry::get<0>(p1)
  60. , y1 = geometry::get<1>(p1)
  61. , x2 = geometry::get<0>(p2)
  62. , y2 = geometry::get<1>(p2);
  63. if (x1 > x2) { std::swap(x1, x2); }
  64. if (y1 > y2) { std::swap(y1, y2); }
  65. set<min_corner, 0>(b2, x1);
  66. set<min_corner, 1>(b2, y1);
  67. set<max_corner, 0>(b2, x2);
  68. set<max_corner, 1>(b2, y2);
  69. return true;
  70. }
  71. return false;
  72. }
  73. };
  74. struct transform_box_or_segment
  75. {
  76. template <typename Geometry1, typename Geometry2, typename Strategy>
  77. static inline bool apply(Geometry1 const& source, Geometry2& target,
  78. Strategy const& strategy)
  79. {
  80. typedef typename point_type<Geometry1>::type point_type1;
  81. typedef typename point_type<Geometry2>::type point_type2;
  82. point_type1 source_point[2];
  83. geometry::detail::assign_point_from_index<0>(source, source_point[0]);
  84. geometry::detail::assign_point_from_index<1>(source, source_point[1]);
  85. point_type2 target_point[2];
  86. if (strategy.apply(source_point[0], target_point[0])
  87. && strategy.apply(source_point[1], target_point[1]))
  88. {
  89. geometry::detail::assign_point_to_index<0>(target_point[0], target);
  90. geometry::detail::assign_point_to_index<1>(target_point[1], target);
  91. return true;
  92. }
  93. return false;
  94. }
  95. };
  96. template
  97. <
  98. typename PointOut,
  99. typename OutputIterator,
  100. typename Range,
  101. typename Strategy
  102. >
  103. inline bool transform_range_out(Range const& range,
  104. OutputIterator out, Strategy const& strategy)
  105. {
  106. PointOut point_out;
  107. for(typename boost::range_iterator<Range const>::type
  108. it = boost::begin(range);
  109. it != boost::end(range);
  110. ++it)
  111. {
  112. if (! transform_point::apply(*it, point_out, strategy))
  113. {
  114. return false;
  115. }
  116. *out++ = point_out;
  117. }
  118. return true;
  119. }
  120. struct transform_polygon
  121. {
  122. template <typename Polygon1, typename Polygon2, typename Strategy>
  123. static inline bool apply(Polygon1 const& poly1, Polygon2& poly2,
  124. Strategy const& strategy)
  125. {
  126. typedef typename ring_type<Polygon1>::type ring1_type;
  127. typedef typename ring_type<Polygon2>::type ring2_type;
  128. typedef typename point_type<Polygon2>::type point2_type;
  129. geometry::clear(poly2);
  130. if (!transform_range_out<point2_type>(exterior_ring(poly1),
  131. std::back_inserter(exterior_ring(poly2)), strategy))
  132. {
  133. return false;
  134. }
  135. // Note: here a resizeable container is assumed.
  136. traits::resize
  137. <
  138. typename boost::remove_reference
  139. <
  140. typename traits::interior_mutable_type<Polygon2>::type
  141. >::type
  142. >::apply(interior_rings(poly2), num_interior_rings(poly1));
  143. typename interior_return_type<Polygon1 const>::type rings1
  144. = interior_rings(poly1);
  145. typename interior_return_type<Polygon2>::type rings2
  146. = interior_rings(poly2);
  147. BOOST_AUTO_TPL(it1, boost::begin(rings1));
  148. BOOST_AUTO_TPL(it2, boost::begin(rings2));
  149. for ( ; it1 != boost::end(interior_rings(poly1)); ++it1, ++it2)
  150. {
  151. if (!transform_range_out<point2_type>(*it1,
  152. std::back_inserter(*it2), strategy))
  153. {
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. };
  160. template <typename Point1, typename Point2>
  161. struct select_strategy
  162. {
  163. typedef typename strategy::transform::services::default_strategy
  164. <
  165. typename cs_tag<Point1>::type,
  166. typename cs_tag<Point2>::type,
  167. typename coordinate_system<Point1>::type,
  168. typename coordinate_system<Point2>::type,
  169. dimension<Point1>::type::value,
  170. dimension<Point2>::type::value,
  171. typename point_type<Point1>::type,
  172. typename point_type<Point2>::type
  173. >::type type;
  174. };
  175. struct transform_range
  176. {
  177. template <typename Range1, typename Range2, typename Strategy>
  178. static inline bool apply(Range1 const& range1,
  179. Range2& range2, Strategy const& strategy)
  180. {
  181. typedef typename point_type<Range2>::type point_type;
  182. // Should NOT be done here!
  183. // geometry::clear(range2);
  184. return transform_range_out<point_type>(range1,
  185. std::back_inserter(range2), strategy);
  186. }
  187. };
  188. }} // namespace detail::transform
  189. #endif // DOXYGEN_NO_DETAIL
  190. #ifndef DOXYGEN_NO_DISPATCH
  191. namespace dispatch
  192. {
  193. template
  194. <
  195. typename Geometry1, typename Geometry2,
  196. typename Tag1 = typename tag_cast<typename tag<Geometry1>::type, multi_tag>::type,
  197. typename Tag2 = typename tag_cast<typename tag<Geometry2>::type, multi_tag>::type
  198. >
  199. struct transform {};
  200. template <typename Point1, typename Point2>
  201. struct transform<Point1, Point2, point_tag, point_tag>
  202. : detail::transform::transform_point
  203. {
  204. };
  205. template <typename Linestring1, typename Linestring2>
  206. struct transform
  207. <
  208. Linestring1, Linestring2,
  209. linestring_tag, linestring_tag
  210. >
  211. : detail::transform::transform_range
  212. {
  213. };
  214. template <typename Range1, typename Range2>
  215. struct transform<Range1, Range2, ring_tag, ring_tag>
  216. : detail::transform::transform_range
  217. {
  218. };
  219. template <typename Polygon1, typename Polygon2>
  220. struct transform<Polygon1, Polygon2, polygon_tag, polygon_tag>
  221. : detail::transform::transform_polygon
  222. {
  223. };
  224. template <typename Box1, typename Box2>
  225. struct transform<Box1, Box2, box_tag, box_tag>
  226. : detail::transform::transform_box
  227. {
  228. };
  229. template <typename Segment1, typename Segment2>
  230. struct transform<Segment1, Segment2, segment_tag, segment_tag>
  231. : detail::transform::transform_box_or_segment
  232. {
  233. };
  234. } // namespace dispatch
  235. #endif // DOXYGEN_NO_DISPATCH
  236. /*!
  237. \brief Transforms from one geometry to another geometry \brief_strategy
  238. \ingroup transform
  239. \tparam Geometry1 \tparam_geometry
  240. \tparam Geometry2 \tparam_geometry
  241. \tparam Strategy strategy
  242. \param geometry1 \param_geometry
  243. \param geometry2 \param_geometry
  244. \param strategy The strategy to be used for transformation
  245. \return True if the transformation could be done
  246. \qbk{distinguish,with strategy}
  247. \qbk{[include reference/algorithms/transform_with_strategy.qbk]}
  248. */
  249. template <typename Geometry1, typename Geometry2, typename Strategy>
  250. inline bool transform(Geometry1 const& geometry1, Geometry2& geometry2,
  251. Strategy const& strategy)
  252. {
  253. concept::check<Geometry1 const>();
  254. concept::check<Geometry2>();
  255. typedef dispatch::transform<Geometry1, Geometry2> transform_type;
  256. return transform_type::apply(geometry1, geometry2, strategy);
  257. }
  258. /*!
  259. \brief Transforms from one geometry to another geometry using a strategy
  260. \ingroup transform
  261. \tparam Geometry1 \tparam_geometry
  262. \tparam Geometry2 \tparam_geometry
  263. \param geometry1 \param_geometry
  264. \param geometry2 \param_geometry
  265. \return True if the transformation could be done
  266. \qbk{[include reference/algorithms/transform.qbk]}
  267. */
  268. template <typename Geometry1, typename Geometry2>
  269. inline bool transform(Geometry1 const& geometry1, Geometry2& geometry2)
  270. {
  271. concept::check<Geometry1 const>();
  272. concept::check<Geometry2>();
  273. typename detail::transform::select_strategy<Geometry1, Geometry2>::type strategy;
  274. return transform(geometry1, geometry2, strategy);
  275. }
  276. }} // namespace boost::geometry
  277. #endif // BOOST_GEOMETRY_ALGORITHMS_TRANSFORM_HPP