buffer_point_circle.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Boost.Geometry
  2. // Copyright (c) 2018-2022 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2020-2021.
  4. // Modifications copyright (c) 2020-2021 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_BUFFER_POINT_CIRCLE_HPP
  10. #define BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_BUFFER_POINT_CIRCLE_HPP
  11. #include <cstddef>
  12. #include <boost/range/value_type.hpp>
  13. #include <boost/geometry/core/radian_access.hpp>
  14. #include <boost/geometry/srs/spheroid.hpp>
  15. #include <boost/geometry/strategies/buffer.hpp>
  16. #include <boost/geometry/strategies/geographic/buffer_helper.hpp>
  17. #include <boost/geometry/strategies/geographic/parameters.hpp>
  18. #include <boost/geometry/util/math.hpp>
  19. #include <boost/geometry/util/select_calculation_type.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. namespace strategy { namespace buffer
  23. {
  24. /*!
  25. \brief Create a circular buffer around a point, on the Earth
  26. \ingroup strategies
  27. \details This strategy can be used as PointStrategy for the buffer algorithm.
  28. It creates a circular buffer around a point, on the Earth. It can be applied
  29. for points and multi_points.
  30. \qbk{
  31. [heading Example]
  32. [buffer_geographic_point_circle]
  33. [buffer_geographic_point_circle_output]
  34. [heading See also]
  35. \* [link geometry.reference.algorithms.buffer.buffer_7_with_strategies buffer (with strategies)]
  36. \* [link geometry.reference.strategies.strategy_buffer_point_circle point_circle]
  37. \* [link geometry.reference.strategies.strategy_buffer_point_square point_square]
  38. }
  39. */
  40. template
  41. <
  42. typename FormulaPolicy = strategy::andoyer,
  43. typename Spheroid = srs::spheroid<double>,
  44. typename CalculationType = void
  45. >
  46. class geographic_point_circle
  47. {
  48. public :
  49. //! \brief Constructs the strategy with a spheroid
  50. //! \param spheroid The spheroid to be used
  51. //! \param count Number of points (minimum 3) for the created circle
  52. explicit inline geographic_point_circle(Spheroid const& spheroid,
  53. std::size_t count = default_points_per_circle)
  54. : m_spheroid(spheroid)
  55. , m_count(get_point_count_for_circle(count))
  56. {}
  57. //! \brief Constructs the strategy
  58. //! \param count Number of points (minimum 3) for the created circle
  59. explicit inline geographic_point_circle(std::size_t count = default_points_per_circle)
  60. : m_count(get_point_count_for_circle(count))
  61. {}
  62. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  63. //! Fills range_out with a circle around point using distance_strategy
  64. template
  65. <
  66. typename Point,
  67. typename DistanceStrategy,
  68. typename RangeOut
  69. >
  70. inline void apply(Point const& point,
  71. DistanceStrategy const& distance_strategy,
  72. RangeOut& range_out) const
  73. {
  74. using calc_t = typename select_calculation_type
  75. <
  76. Point,
  77. typename boost::range_value<RangeOut>::type,
  78. CalculationType
  79. >::type;
  80. using helper = geographic_buffer_helper<FormulaPolicy, calc_t>;
  81. calc_t const lon_rad = get_as_radian<0>(point);
  82. calc_t const lat_rad = get_as_radian<1>(point);
  83. calc_t const buffer_distance = distance_strategy.apply(point,
  84. point, strategy::buffer::buffer_side_left);
  85. calc_t const two_pi = geometry::math::two_pi<calc_t>();
  86. calc_t const pi = geometry::math::pi<calc_t>();
  87. calc_t const diff = two_pi / calc_t(m_count);
  88. calc_t angle = -pi;
  89. for (std::size_t i = 0; i < m_count; i++, angle += diff)
  90. {
  91. // If angle is zero, shift angle a tiny bit to avoid spikes.
  92. calc_t const eps = angle == 0 ? 1.0e-10 : 0.0;
  93. helper::append_point(lon_rad, lat_rad, buffer_distance, angle + eps, m_spheroid, range_out);
  94. }
  95. {
  96. // Close the range
  97. auto const p = range_out.front();
  98. range_out.push_back(p);
  99. }
  100. }
  101. #endif // DOXYGEN_SHOULD_SKIP_THIS
  102. private :
  103. Spheroid m_spheroid;
  104. std::size_t m_count;
  105. };
  106. }} // namespace strategy::buffer
  107. }} // namespace boost::geometry
  108. #endif // BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_BUFFER_POINT_CIRCLE_HPP