buffer_join_round.hpp 6.1 KB

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