recalculate.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2013 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2013 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013 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. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_RECALCULATE_HPP
  13. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RECALCULATE_HPP
  14. #include <cstddef>
  15. #include <boost/concept/requires.hpp>
  16. #include <boost/concept_check.hpp>
  17. #include <boost/numeric/conversion/bounds.hpp>
  18. #include <boost/numeric/conversion/cast.hpp>
  19. #include <boost/range/begin.hpp>
  20. #include <boost/range/end.hpp>
  21. #include <boost/range/size.hpp>
  22. #include <boost/geometry/arithmetic/arithmetic.hpp>
  23. #include <boost/geometry/algorithms/append.hpp>
  24. #include <boost/geometry/algorithms/clear.hpp>
  25. #include <boost/geometry/core/access.hpp>
  26. #include <boost/geometry/core/interior_rings.hpp>
  27. #include <boost/geometry/core/exterior_ring.hpp>
  28. #include <boost/geometry/core/tags.hpp>
  29. #include <boost/geometry/geometries/concepts/check.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail { namespace recalculate
  34. {
  35. template <std::size_t Dimension>
  36. struct recalculate_point
  37. {
  38. template <typename Point1, typename Point2, typename Strategy>
  39. static inline void apply(Point1& point1, Point2 const& point2, Strategy const& strategy)
  40. {
  41. std::size_t const dim = Dimension - 1;
  42. geometry::set<dim>(point1, strategy.template apply<dim>(geometry::get<dim>(point2)));
  43. recalculate_point<dim>::apply(point1, point2, strategy);
  44. }
  45. };
  46. template <>
  47. struct recalculate_point<0>
  48. {
  49. template <typename Point1, typename Point2, typename Strategy>
  50. static inline void apply(Point1&, Point2 const&, Strategy const&)
  51. {
  52. }
  53. };
  54. template <std::size_t Dimension>
  55. struct recalculate_indexed
  56. {
  57. template <typename Geometry1, typename Geometry2, typename Strategy>
  58. static inline void apply(Geometry1& geometry1, Geometry2 const& geometry2, Strategy const& strategy)
  59. {
  60. // Do it for both indices in one dimension
  61. static std::size_t const dim = Dimension - 1;
  62. geometry::set<0, dim>(geometry1, strategy.template apply<dim>(geometry::get<0, dim>(geometry2)));
  63. geometry::set<1, dim>(geometry1, strategy.template apply<dim>(geometry::get<1, dim>(geometry2)));
  64. recalculate_indexed<dim>::apply(geometry1, geometry2, strategy);
  65. }
  66. };
  67. template <>
  68. struct recalculate_indexed<0>
  69. {
  70. template <typename Geometry1, typename Geometry2, typename Strategy>
  71. static inline void apply(Geometry1& , Geometry2 const& , Strategy const& )
  72. {
  73. }
  74. };
  75. struct range_to_range
  76. {
  77. template
  78. <
  79. typename Range1,
  80. typename Range2,
  81. typename Strategy
  82. >
  83. static inline void apply(Range1& destination, Range2 const& source,
  84. Strategy const& strategy)
  85. {
  86. typedef typename geometry::point_type<Range2>::type point_type;
  87. typedef recalculate_point<geometry::dimension<point_type>::value> per_point;
  88. geometry::clear(destination);
  89. for (typename boost::range_iterator<Range2 const>::type it
  90. = boost::begin(source);
  91. it != boost::end(source);
  92. ++it)
  93. {
  94. point_type p;
  95. per_point::apply(p, *it, strategy);
  96. geometry::append(destination, p);
  97. }
  98. }
  99. };
  100. struct polygon_to_polygon
  101. {
  102. private:
  103. template
  104. <
  105. typename IteratorIn,
  106. typename IteratorOut,
  107. typename Strategy
  108. >
  109. static inline void iterate(IteratorIn begin, IteratorIn end,
  110. IteratorOut it_out,
  111. Strategy const& strategy)
  112. {
  113. for (IteratorIn it_in = begin; it_in != end; ++it_in, ++it_out)
  114. {
  115. range_to_range::apply(*it_out, *it_in, strategy);
  116. }
  117. }
  118. template
  119. <
  120. typename InteriorRingsOut,
  121. typename InteriorRingsIn,
  122. typename Strategy
  123. >
  124. static inline void apply_interior_rings(
  125. InteriorRingsOut& interior_rings_out,
  126. InteriorRingsIn const& interior_rings_in,
  127. Strategy const& strategy)
  128. {
  129. traits::resize<InteriorRingsOut>::apply(interior_rings_out,
  130. boost::size(interior_rings_in));
  131. iterate(
  132. boost::begin(interior_rings_in), boost::end(interior_rings_in),
  133. boost::begin(interior_rings_out),
  134. strategy);
  135. }
  136. public:
  137. template
  138. <
  139. typename Polygon1,
  140. typename Polygon2,
  141. typename Strategy
  142. >
  143. static inline void apply(Polygon1& destination, Polygon2 const& source,
  144. Strategy const& strategy)
  145. {
  146. range_to_range::apply(geometry::exterior_ring(destination),
  147. geometry::exterior_ring(source), strategy);
  148. apply_interior_rings(geometry::interior_rings(destination),
  149. geometry::interior_rings(source), strategy);
  150. }
  151. };
  152. }} // namespace detail::recalculate
  153. #endif // DOXYGEN_NO_DETAIL
  154. #ifndef DOXYGEN_NO_DISPATCH
  155. namespace dispatch
  156. {
  157. template
  158. <
  159. typename Geometry1,
  160. typename Geometry2,
  161. typename Tag1 = typename geometry::tag<Geometry1>::type,
  162. typename Tag2 = typename geometry::tag<Geometry2>::type
  163. >
  164. struct recalculate : not_implemented<Tag1, Tag2>
  165. {};
  166. template <typename Point1, typename Point2>
  167. struct recalculate<Point1, Point2, point_tag, point_tag>
  168. : detail::recalculate::recalculate_point<geometry::dimension<Point1>::value>
  169. {};
  170. template <typename Box1, typename Box2>
  171. struct recalculate<Box1, Box2, box_tag, box_tag>
  172. : detail::recalculate::recalculate_indexed<geometry::dimension<Box1>::value>
  173. {};
  174. template <typename Segment1, typename Segment2>
  175. struct recalculate<Segment1, Segment2, segment_tag, segment_tag>
  176. : detail::recalculate::recalculate_indexed<geometry::dimension<Segment1>::value>
  177. {};
  178. template <typename Polygon1, typename Polygon2>
  179. struct recalculate<Polygon1, Polygon2, polygon_tag, polygon_tag>
  180. : detail::recalculate::polygon_to_polygon
  181. {};
  182. } // namespace dispatch
  183. #endif // DOXYGEN_NO_DISPATCH
  184. template <typename Geometry1, typename Geometry2, typename Strategy>
  185. inline void recalculate(Geometry1& geometry1, Geometry2 const& geometry2, Strategy const& strategy)
  186. {
  187. concepts::check<Geometry1>();
  188. concepts::check<Geometry2 const>();
  189. // static assert dimensions (/types) are the same
  190. dispatch::recalculate<Geometry1, Geometry2>::apply(geometry1, geometry2, strategy);
  191. }
  192. }} // namespace boost::geometry
  193. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RECALCULATE_HPP