recalculate.hpp 6.6 KB

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