recalculate.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 (auto const& source_point : source)
  90. {
  91. point_type p;
  92. per_point::apply(p, source_point, strategy);
  93. geometry::append(destination, p);
  94. }
  95. }
  96. };
  97. struct polygon_to_polygon
  98. {
  99. private:
  100. template
  101. <
  102. typename IteratorIn,
  103. typename IteratorOut,
  104. typename Strategy
  105. >
  106. static inline void iterate(IteratorIn begin, IteratorIn end,
  107. IteratorOut it_out,
  108. Strategy const& strategy)
  109. {
  110. for (IteratorIn it_in = begin; it_in != end; ++it_in, ++it_out)
  111. {
  112. range_to_range::apply(*it_out, *it_in, strategy);
  113. }
  114. }
  115. template
  116. <
  117. typename InteriorRingsOut,
  118. typename InteriorRingsIn,
  119. typename Strategy
  120. >
  121. static inline void apply_interior_rings(
  122. InteriorRingsOut& interior_rings_out,
  123. InteriorRingsIn const& interior_rings_in,
  124. Strategy const& strategy)
  125. {
  126. traits::resize<InteriorRingsOut>::apply(interior_rings_out,
  127. boost::size(interior_rings_in));
  128. iterate(
  129. boost::begin(interior_rings_in), boost::end(interior_rings_in),
  130. boost::begin(interior_rings_out),
  131. strategy);
  132. }
  133. public:
  134. template
  135. <
  136. typename Polygon1,
  137. typename Polygon2,
  138. typename Strategy
  139. >
  140. static inline void apply(Polygon1& destination, Polygon2 const& source,
  141. Strategy const& strategy)
  142. {
  143. range_to_range::apply(geometry::exterior_ring(destination),
  144. geometry::exterior_ring(source), strategy);
  145. apply_interior_rings(geometry::interior_rings(destination),
  146. geometry::interior_rings(source), strategy);
  147. }
  148. };
  149. }} // namespace detail::recalculate
  150. #endif // DOXYGEN_NO_DETAIL
  151. #ifndef DOXYGEN_NO_DISPATCH
  152. namespace dispatch
  153. {
  154. template
  155. <
  156. typename Geometry1,
  157. typename Geometry2,
  158. typename Tag1 = typename geometry::tag<Geometry1>::type,
  159. typename Tag2 = typename geometry::tag<Geometry2>::type
  160. >
  161. struct recalculate : not_implemented<Tag1, Tag2>
  162. {};
  163. template <typename Point1, typename Point2>
  164. struct recalculate<Point1, Point2, point_tag, point_tag>
  165. : detail::recalculate::recalculate_point<geometry::dimension<Point1>::value>
  166. {};
  167. template <typename Box1, typename Box2>
  168. struct recalculate<Box1, Box2, box_tag, box_tag>
  169. : detail::recalculate::recalculate_indexed<geometry::dimension<Box1>::value>
  170. {};
  171. template <typename Segment1, typename Segment2>
  172. struct recalculate<Segment1, Segment2, segment_tag, segment_tag>
  173. : detail::recalculate::recalculate_indexed<geometry::dimension<Segment1>::value>
  174. {};
  175. template <typename Polygon1, typename Polygon2>
  176. struct recalculate<Polygon1, Polygon2, polygon_tag, polygon_tag>
  177. : detail::recalculate::polygon_to_polygon
  178. {};
  179. } // namespace dispatch
  180. #endif // DOXYGEN_NO_DISPATCH
  181. template <typename Geometry1, typename Geometry2, typename Strategy>
  182. inline void recalculate(Geometry1& geometry1, Geometry2 const& geometry2, Strategy const& strategy)
  183. {
  184. concepts::check<Geometry1>();
  185. concepts::check<Geometry2 const>();
  186. // static assert dimensions (/types) are the same
  187. dispatch::recalculate<Geometry1, Geometry2>::apply(geometry1, geometry2, strategy);
  188. }
  189. }} // namespace boost::geometry
  190. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RECALCULATE_HPP