buffer_end_round.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Boost.Geometry
  2. // Copyright (c) 2022 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_BUFFER_END_ROUND_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_BUFFER_END_ROUND_HPP
  8. #include <boost/range/value_type.hpp>
  9. #include <boost/geometry/core/radian_access.hpp>
  10. #include <boost/geometry/srs/spheroid.hpp>
  11. #include <boost/geometry/strategies/buffer.hpp>
  12. #include <boost/geometry/strategies/geographic/buffer_helper.hpp>
  13. #include <boost/geometry/strategies/geographic/parameters.hpp>
  14. #include <boost/geometry/util/math.hpp>
  15. #include <boost/geometry/util/select_calculation_type.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. namespace strategy { namespace buffer
  19. {
  20. template
  21. <
  22. typename FormulaPolicy = strategy::andoyer,
  23. typename Spheroid = srs::spheroid<double>,
  24. typename CalculationType = void
  25. >
  26. class geographic_end_round
  27. {
  28. public :
  29. //! \brief Constructs the strategy with a spheroid
  30. //! \param spheroid The spheroid to be used
  31. //! \param points_per_circle Number of points (minimum 4) that would be used for a full circle
  32. explicit inline geographic_end_round(Spheroid const& spheroid,
  33. std::size_t points_per_circle = default_points_per_circle)
  34. : m_spheroid(spheroid)
  35. , m_points_per_circle(get_point_count_for_end(points_per_circle))
  36. {}
  37. //! \brief Constructs the strategy
  38. //! \param points_per_circle Number of points (minimum 4) that would be used for a full circle
  39. explicit inline geographic_end_round(std::size_t points_per_circle = default_points_per_circle)
  40. : m_points_per_circle(get_point_count_for_end(points_per_circle))
  41. {}
  42. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  43. template <typename T, typename RangeOut>
  44. inline void generate(T lon_rad, T lat_rad, T distance, T azimuth, RangeOut& range_out) const
  45. {
  46. using helper = geographic_buffer_helper<FormulaPolicy, T>;
  47. std::size_t const n = m_points_per_circle / 2;
  48. T const angle_diff = geometry::math::pi<T>() / n;
  49. T azi = math::wrap_azimuth_in_radian(azimuth + angle_diff);
  50. // Generate points between 0 and n, not including them
  51. // because left and right are inserted before and after this range.
  52. for (std::size_t i = 1; i < n; i++)
  53. {
  54. helper::append_point(lon_rad, lat_rad, distance, azi, m_spheroid, range_out);
  55. azi = math::wrap_azimuth_in_radian(azi + angle_diff);
  56. }
  57. }
  58. //! Fills output_range with a round end
  59. template <typename Point, typename DistanceStrategy, typename RangeOut>
  60. inline void apply(Point const& penultimate_point, Point const& perp_left_point,
  61. Point const& ultimate_point, Point const& perp_right_point,
  62. buffer_side_selector side, DistanceStrategy const& distance,
  63. RangeOut& range_out) const
  64. {
  65. using calc_t = typename select_calculation_type
  66. <
  67. Point,
  68. typename boost::range_value<RangeOut>::type,
  69. CalculationType
  70. >::type;
  71. using helper = geographic_buffer_helper<FormulaPolicy, calc_t>;
  72. calc_t const lon_rad = get_as_radian<0>(ultimate_point);
  73. calc_t const lat_rad = get_as_radian<1>(ultimate_point);
  74. auto const azimuth = helper::azimuth(lon_rad, lat_rad, perp_left_point, m_spheroid);
  75. calc_t const dist_left = distance.apply(penultimate_point, ultimate_point, buffer_side_left);
  76. calc_t const dist_right = distance.apply(penultimate_point, ultimate_point, buffer_side_right);
  77. bool const reversed = (side == buffer_side_left && dist_right < 0 && -dist_right > dist_left)
  78. || (side == buffer_side_right && dist_left < 0 && -dist_left > dist_right)
  79. ;
  80. if (reversed)
  81. {
  82. range_out.push_back(perp_right_point);
  83. // generate
  84. range_out.push_back(perp_left_point);
  85. }
  86. else
  87. {
  88. range_out.push_back(perp_left_point);
  89. if (geometry::math::equals(dist_left, dist_right))
  90. {
  91. generate(lon_rad, lat_rad, dist_left, azimuth, range_out);
  92. }
  93. else
  94. {
  95. static calc_t const two = 2.0;
  96. calc_t const dist_average = (dist_left + dist_right) / two;
  97. calc_t const dist_half
  98. = (side == buffer_side_right
  99. ? (dist_right - dist_left)
  100. : (dist_left - dist_right)) / two;
  101. auto const shifted = helper::direct::apply(lon_rad, lat_rad, dist_half, azimuth, m_spheroid);
  102. generate(shifted.lon2, shifted.lat2, dist_average, azimuth, range_out);
  103. }
  104. range_out.push_back(perp_right_point);
  105. }
  106. }
  107. template <typename NumericType>
  108. static inline NumericType max_distance(NumericType const& distance)
  109. {
  110. return distance;
  111. }
  112. //! Returns the piece_type (round end)
  113. static inline piece_type get_piece_type()
  114. {
  115. return buffered_round_end;
  116. }
  117. #endif // DOXYGEN_SHOULD_SKIP_THIS
  118. private :
  119. Spheroid m_spheroid;
  120. std::size_t m_points_per_circle;
  121. };
  122. }} // namespace strategy::buffer
  123. }} // namespace boost::geometry
  124. #endif // BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_BUFFER_END_ROUND_HPP