buffer_join_round.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2012-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2015.
  5. // Modifications copyright (c) 2015, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_HPP
  11. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_HPP
  12. #include <algorithm>
  13. #include <boost/geometry/core/cs.hpp>
  14. #include <boost/geometry/policies/compare.hpp>
  15. #include <boost/geometry/strategies/buffer.hpp>
  16. #include <boost/geometry/util/math.hpp>
  17. #include <boost/geometry/util/select_most_precise.hpp>
  18. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  19. #include <iostream>
  20. #include <boost/geometry/io/wkt/wkt.hpp>
  21. #endif
  22. namespace boost { namespace geometry
  23. {
  24. namespace strategy { namespace buffer
  25. {
  26. /*!
  27. \brief Let the buffer create rounded corners
  28. \ingroup strategies
  29. \details This strategy can be used as JoinStrategy for the buffer algorithm.
  30. It creates a rounded corners around each convex vertex. It can be applied
  31. for (multi)linestrings and (multi)polygons.
  32. This strategy is only applicable for Cartesian coordinate systems.
  33. \qbk{
  34. [heading Example]
  35. [buffer_join_round]
  36. [heading Output]
  37. [$img/strategies/buffer_join_round.png]
  38. [heading See also]
  39. \* [link geometry.reference.algorithms.buffer.buffer_7_with_strategies buffer (with strategies)]
  40. \* [link geometry.reference.strategies.strategy_buffer_join_miter join_miter]
  41. }
  42. */
  43. class join_round
  44. {
  45. public :
  46. //! \brief Constructs the strategy
  47. //! \param points_per_circle Number of points (minimum 4) that would be used for a full circle
  48. explicit inline join_round(std::size_t points_per_circle = default_points_per_circle)
  49. : m_points_per_circle(get_point_count_for_join(points_per_circle))
  50. {}
  51. private :
  52. template
  53. <
  54. typename PromotedType,
  55. typename Point,
  56. typename DistanceType,
  57. typename RangeOut
  58. >
  59. inline void generate_points(Point const& vertex,
  60. Point const& perp1, Point const& perp2,
  61. DistanceType const& buffer_distance,
  62. RangeOut& range_out) const
  63. {
  64. PromotedType const dx1 = get<0>(perp1) - get<0>(vertex);
  65. PromotedType const dy1 = get<1>(perp1) - get<1>(vertex);
  66. PromotedType const dx2 = get<0>(perp2) - get<0>(vertex);
  67. PromotedType const dy2 = get<1>(perp2) - get<1>(vertex);
  68. PromotedType const two_pi = geometry::math::two_pi<PromotedType>();
  69. PromotedType const angle1 = atan2(dy1, dx1);
  70. PromotedType angle2 = atan2(dy2, dx2);
  71. while (angle2 > angle1)
  72. {
  73. angle2 -= two_pi;
  74. }
  75. PromotedType const angle_diff = angle1 - angle2;
  76. // Divide the angle into an integer amount of steps to make it
  77. // visually correct also for a low number of points / circle
  78. // If a full circle is divided into 3 parts (e.g. angle is 125),
  79. // the one point in between must still be generated
  80. // The calculation below:
  81. // - generates 1 point in between for an angle of 125 based on 3 points
  82. // - generates 0 points in between for an angle of 90 based on 4 points
  83. std::size_t const n = (std::max)(static_cast<std::size_t>(
  84. ceil(m_points_per_circle * angle_diff / two_pi)), std::size_t(1));
  85. PromotedType const diff = angle_diff / static_cast<PromotedType>(n);
  86. PromotedType a = angle1 - diff;
  87. // Walk to n - 1 to avoid generating the last point
  88. for (std::size_t i = 0; i < n - 1; i++, a -= diff)
  89. {
  90. Point p;
  91. set<0>(p, get<0>(vertex) + buffer_distance * cos(a));
  92. set<1>(p, get<1>(vertex) + buffer_distance * sin(a));
  93. range_out.push_back(p);
  94. }
  95. }
  96. public :
  97. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  98. //! Fills output_range with a rounded shape around a vertex
  99. template <typename Point, typename DistanceType, typename RangeOut>
  100. inline bool apply(Point const& ip, Point const& vertex,
  101. Point const& perp1, Point const& perp2,
  102. DistanceType const& buffer_distance,
  103. RangeOut& range_out) const
  104. {
  105. typedef typename coordinate_type<Point>::type coordinate_type;
  106. typedef typename boost::range_value<RangeOut>::type output_point_type;
  107. typedef typename geometry::select_most_precise
  108. <
  109. typename geometry::select_most_precise
  110. <
  111. coordinate_type,
  112. typename geometry::coordinate_type<output_point_type>::type
  113. >::type,
  114. double
  115. >::type promoted_type;
  116. geometry::equal_to<Point> equals;
  117. if (equals(perp1, perp2))
  118. {
  119. boost::ignore_unused(ip);
  120. #ifdef BOOST_GEOMETRY_DEBUG_BUFFER_WARN
  121. std::cout << "Corner for equal points " << geometry::wkt(ip) << " " << geometry::wkt(perp1) << std::endl;
  122. #endif
  123. return false;
  124. }
  125. range_out.push_back(perp1);
  126. generate_points<promoted_type>(vertex, perp1, perp2, geometry::math::abs(buffer_distance), range_out);
  127. range_out.push_back(perp2);
  128. return true;
  129. }
  130. template <typename NumericType>
  131. static inline NumericType max_distance(NumericType const& distance)
  132. {
  133. return distance;
  134. }
  135. #endif // DOXYGEN_SHOULD_SKIP_THIS
  136. private :
  137. std::size_t m_points_per_circle;
  138. };
  139. }} // namespace strategy::buffer
  140. }} // namespace boost::geometry
  141. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BUFFER_JOIN_ROUND_HPP