implementation.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // This file was modified by Oracle on 2017-2022.
  6. // Modifications copyright (c) 2017-2022 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_IMPLEMENTATION_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_IMPLEMENTATION_HPP
  15. #include <boost/range/value_type.hpp>
  16. #include <boost/geometry/algorithms/detail/buffer/buffer_box.hpp>
  17. #include <boost/geometry/algorithms/detail/buffer/buffer_inserter.hpp>
  18. #include <boost/geometry/algorithms/detail/buffer/interface.hpp>
  19. #include <boost/geometry/algorithms/detail/visit.hpp> // for GC
  20. #include <boost/geometry/algorithms/envelope.hpp>
  21. #include <boost/geometry/algorithms/is_empty.hpp>
  22. #include <boost/geometry/algorithms/union.hpp> // for GC
  23. #include <boost/geometry/arithmetic/arithmetic.hpp>
  24. #include <boost/geometry/geometries/box.hpp>
  25. #include <boost/geometry/strategies/buffer/cartesian.hpp>
  26. #include <boost/geometry/strategies/buffer/geographic.hpp>
  27. #include <boost/geometry/strategies/buffer/spherical.hpp>
  28. #include <boost/geometry/util/math.hpp>
  29. #include <boost/geometry/util/range.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DISPATCH
  33. namespace dispatch
  34. {
  35. template <typename BoxIn, typename BoxOut>
  36. struct buffer_dc<BoxIn, BoxOut, box_tag, box_tag>
  37. {
  38. template <typename Distance>
  39. static inline void apply(BoxIn const& box_in, BoxOut& box_out,
  40. Distance const& distance, Distance const& )
  41. {
  42. detail::buffer::buffer_box(box_in, distance, box_out);
  43. }
  44. };
  45. template <typename Input, typename Output, typename TagIn>
  46. struct buffer_all<Input, Output, TagIn, multi_polygon_tag>
  47. {
  48. template
  49. <
  50. typename DistanceStrategy,
  51. typename SideStrategy,
  52. typename JoinStrategy,
  53. typename EndStrategy,
  54. typename PointStrategy,
  55. typename Strategies
  56. >
  57. static inline void apply(Input const& geometry_in,
  58. Output& geometry_out,
  59. DistanceStrategy const& distance_strategy,
  60. SideStrategy const& side_strategy,
  61. JoinStrategy const& join_strategy,
  62. EndStrategy const& end_strategy,
  63. PointStrategy const& point_strategy,
  64. Strategies const& strategies)
  65. {
  66. typedef typename boost::range_value<Output>::type polygon_type;
  67. typedef typename point_type<Input>::type point_type;
  68. typedef typename rescale_policy_type
  69. <
  70. point_type,
  71. typename geometry::cs_tag<point_type>::type
  72. >::type rescale_policy_type;
  73. if (geometry::is_empty(geometry_in))
  74. {
  75. // Then output geometry is kept empty as well
  76. return;
  77. }
  78. model::box<point_type> box;
  79. geometry::envelope(geometry_in, box);
  80. geometry::buffer(box, box, distance_strategy.max_distance(join_strategy, end_strategy));
  81. rescale_policy_type rescale_policy
  82. = boost::geometry::get_rescale_policy<rescale_policy_type>(
  83. box, strategies);
  84. detail::buffer::buffer_inserter<polygon_type>(geometry_in,
  85. range::back_inserter(geometry_out),
  86. distance_strategy,
  87. side_strategy,
  88. join_strategy,
  89. end_strategy,
  90. point_strategy,
  91. strategies,
  92. rescale_policy);
  93. }
  94. };
  95. template <typename Input, typename Output>
  96. struct buffer_all<Input, Output, geometry_collection_tag, multi_polygon_tag>
  97. {
  98. template
  99. <
  100. typename DistanceStrategy,
  101. typename SideStrategy,
  102. typename JoinStrategy,
  103. typename EndStrategy,
  104. typename PointStrategy,
  105. typename Strategies
  106. >
  107. static inline void apply(Input const& geometry_in,
  108. Output& geometry_out,
  109. DistanceStrategy const& distance_strategy,
  110. SideStrategy const& side_strategy,
  111. JoinStrategy const& join_strategy,
  112. EndStrategy const& end_strategy,
  113. PointStrategy const& point_strategy,
  114. Strategies const& strategies)
  115. {
  116. // NOTE: The buffer normally calculates everything at once (by pieces) and traverses all
  117. // of them to apply the union operation. Not even by merging elements. But that is
  118. // complex and has led to issues as well. Here intermediate results are calculated
  119. // with buffer and the results are merged afterwards.
  120. // NOTE: This algorithm merges partial results iteratively.
  121. // We could first gather all of the results and after that
  122. // use some more optimal method like merge_elements().
  123. detail::visit_breadth_first([&](auto const& g)
  124. {
  125. Output buffer_result;
  126. buffer_all
  127. <
  128. util::remove_cref_t<decltype(g)>, Output
  129. >::apply(g, buffer_result, distance_strategy, side_strategy,
  130. join_strategy, end_strategy, point_strategy, strategies);
  131. if (! geometry::is_empty(buffer_result))
  132. {
  133. Output union_result;
  134. geometry::union_(geometry_out, buffer_result, union_result, strategies);
  135. geometry_out = std::move(union_result);
  136. }
  137. return true;
  138. }, geometry_in);
  139. }
  140. };
  141. template <typename Input, typename Output>
  142. struct buffer_all<Input, Output, geometry_collection_tag, geometry_collection_tag>
  143. {
  144. template
  145. <
  146. typename DistanceStrategy,
  147. typename SideStrategy,
  148. typename JoinStrategy,
  149. typename EndStrategy,
  150. typename PointStrategy,
  151. typename Strategies
  152. >
  153. static inline void apply(Input const& geometry_in,
  154. Output& geometry_out,
  155. DistanceStrategy const& distance_strategy,
  156. SideStrategy const& side_strategy,
  157. JoinStrategy const& join_strategy,
  158. EndStrategy const& end_strategy,
  159. PointStrategy const& point_strategy,
  160. Strategies const& strategies)
  161. {
  162. // NOTE: We could also allow returning GC containing only polygons.
  163. // We'd have to wrap them in model::multi_polygon and then
  164. // iteratively emplace_back() into the GC.
  165. using mpo_t = typename util::sequence_find_if
  166. <
  167. typename traits::geometry_types<Output>::type,
  168. util::is_multi_polygon
  169. >::type;
  170. mpo_t result;
  171. buffer_all
  172. <
  173. Input, mpo_t
  174. >::apply(geometry_in, result, distance_strategy, side_strategy,
  175. join_strategy, end_strategy, point_strategy, strategies);
  176. range::emplace_back(geometry_out, std::move(result));
  177. }
  178. };
  179. template <typename Input, typename Output, typename TagIn>
  180. struct buffer_all<Input, Output, TagIn, geometry_collection_tag>
  181. : buffer_all<Input, Output, geometry_collection_tag, geometry_collection_tag>
  182. {};
  183. } // namespace dispatch
  184. #endif // DOXYGEN_NO_DISPATCH
  185. }} // namespace boost::geometry
  186. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_IMPLEMENTATION_HPP