implementation.hpp 7.6 KB

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