circle.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // Copyright 2020 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
  3. //
  4. // Use, modification and distribution are subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_GIL_EXTENSION_RASTERIZATION_CIRCLE_HPP
  9. #define BOOST_GIL_EXTENSION_RASTERIZATION_CIRCLE_HPP
  10. #include <boost/gil/detail/math.hpp>
  11. #include <boost/gil/extension/rasterization/apply_rasterizer.hpp>
  12. #include <boost/gil/point.hpp>
  13. #include <cmath>
  14. #include <cstddef>
  15. #include <vector>
  16. namespace boost { namespace gil {
  17. struct circle_rasterizer_t{};
  18. /// \defgroup CircleRasterization
  19. /// \ingroup Rasterization
  20. /// \brief Circle rasterization algorithms
  21. ///
  22. /// The main problems are connectivity and equation following. Circle can be easily moved
  23. /// to new offset, and rotation has no effect on it (not recommended to do rotation).
  24. /// \ingroup CircleRasterization
  25. /// \brief Rasterize trigonometric circle according to radius by sine and radius by cosine
  26. ///
  27. /// This rasterizer is the one used that is used in standard Hough circle transform in
  28. /// the books. It is also quite expensive to compute.
  29. /// WARNING: the product of this rasterizer does not follow circle equation, even though it
  30. /// produces quite round like shapes.
  31. struct trigonometric_circle_rasterizer
  32. {
  33. using type = circle_rasterizer_t;
  34. /// \brief Creates a trigonometric circle rasterizer
  35. /// \param center_point - Point containing positive integer x co-ordinate and y co-ordinate of the
  36. /// center respectively.
  37. /// \param circle_radius - Radius of the circle
  38. trigonometric_circle_rasterizer(point_t center_point, std::ptrdiff_t circle_radius)
  39. : center(center_point), radius(circle_radius)
  40. {}
  41. /// \brief Calculates minimum angle step that is distinguishable when walking on circle
  42. ///
  43. /// It is important to not have disconnected circle and to not compute unnecessarily,
  44. /// thus the result of this function is used when rendering.
  45. double minimum_angle_step() const noexcept
  46. {
  47. const auto diameter = radius * 2 - 1;
  48. return std::atan2(1.0, diameter);
  49. }
  50. /// \brief Calculate the amount of points that rasterizer will output
  51. std::ptrdiff_t point_count() const noexcept
  52. {
  53. return 8 * static_cast<std::ptrdiff_t>(
  54. std::round(detail::pi / 4 / minimum_angle_step()) + 1);
  55. }
  56. /// \brief perform rasterization and output into d_first
  57. template <typename OutputIterator>
  58. void operator()(OutputIterator d_first) const
  59. {
  60. const double minimum_angle_step = std::atan2(1.0, radius);
  61. auto translate_mirror_points = [this, &d_first](point_t p) {
  62. *d_first++ = point_t{center.x + p.x, center.y + p.y};
  63. *d_first++ = point_t{center.x + p.x, center.y - p.y};
  64. *d_first++ = point_t{center.x - p.x, center.y + p.y};
  65. *d_first++ = point_t{center.x - p.x, center.y - p.y};
  66. *d_first++ = point_t{center.x + p.y, center.y + p.x};
  67. *d_first++ = point_t{center.x + p.y, center.y - p.x};
  68. *d_first++ = point_t{center.x - p.y, center.y + p.x};
  69. *d_first++ = point_t{center.x - p.y, center.y - p.x};
  70. };
  71. const std::ptrdiff_t iteration_count = point_count() / 8;
  72. double angle = 0;
  73. // do note that + 1 was done inside count estimation, thus <= is not needed, only <
  74. for (std::ptrdiff_t i = 0; i < iteration_count; ++i, angle += minimum_angle_step)
  75. {
  76. std::ptrdiff_t x = static_cast<std::ptrdiff_t>(std::round(radius * std::cos(angle)));
  77. std::ptrdiff_t y = static_cast<std::ptrdiff_t>(std::round(radius * std::sin(angle)));
  78. translate_mirror_points({x, y});
  79. }
  80. }
  81. point_t center;
  82. std::ptrdiff_t radius;
  83. };
  84. /// \ingroup CircleRasterization
  85. /// \brief Perform circle rasterization according to Midpoint algorithm
  86. ///
  87. /// This algorithm givess reasonable output and is cheap to compute.
  88. /// reference:
  89. /// https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
  90. struct midpoint_circle_rasterizer
  91. {
  92. using type = circle_rasterizer_t;
  93. /// \brief Creates a midpoint circle rasterizer
  94. /// \param center_point - Point containing positive integer x co-ordinate and y co-ordinate of the
  95. /// center respectively.
  96. /// \param circle_radius - Radius of the circle
  97. midpoint_circle_rasterizer(point_t center_point, std::ptrdiff_t circle_radius)
  98. : center(center_point), radius(circle_radius)
  99. {}
  100. /// \brief Calculate the amount of points that rasterizer will output
  101. std::ptrdiff_t point_count() const noexcept
  102. {
  103. // the reason for pulling 8 out is so that when the expression radius * cos(45 degrees)
  104. // is used, it would yield the same result as here
  105. // + 1 at the end is because the point at radius itself is computed as well
  106. return 8 * static_cast<std::ptrdiff_t>(
  107. std::round(radius * std::cos(boost::gil::detail::pi / 4)) + 1);
  108. }
  109. /// \brief perform rasterization and output into d_first
  110. template <typename OutputIterator>
  111. void operator()(OutputIterator d_first) const
  112. {
  113. auto translate_mirror_points = [this, &d_first](point_t p) {
  114. *d_first++ = point_t{center.x + p.x, center.y + p.y};
  115. *d_first++ = point_t{center.x + p.x, center.y - p.y};
  116. *d_first++ = point_t{center.x - p.x, center.y + p.y};
  117. *d_first++ = point_t{center.x - p.x, center.y - p.y};
  118. *d_first++ = point_t{center.x + p.y, center.y + p.x};
  119. *d_first++ = point_t{center.x + p.y, center.y - p.x};
  120. *d_first++ = point_t{center.x - p.y, center.y + p.x};
  121. *d_first++ = point_t{center.x - p.y, center.y - p.x};
  122. };
  123. std::ptrdiff_t iteration_distance = point_count() / 8;
  124. std::ptrdiff_t y_current = radius;
  125. std::ptrdiff_t r_squared = radius * radius;
  126. translate_mirror_points({0, y_current});
  127. for (std::ptrdiff_t x = 1; x < iteration_distance; ++x)
  128. {
  129. std::ptrdiff_t midpoint = x * x + y_current * y_current - y_current - r_squared;
  130. if (midpoint > 0)
  131. {
  132. --y_current;
  133. }
  134. translate_mirror_points({x, y_current});
  135. }
  136. }
  137. point_t center;
  138. std::ptrdiff_t radius;
  139. };
  140. namespace detail {
  141. template <typename View, typename Rasterizer, typename Pixel>
  142. struct apply_rasterizer_op<View, Rasterizer, Pixel, circle_rasterizer_t>
  143. {
  144. void operator()(
  145. View const& view, Rasterizer const& rasterizer, Pixel const& pixel)
  146. {
  147. std::vector<point_t> trajectory(rasterizer.point_count());
  148. rasterizer(std::begin(trajectory));
  149. for (auto const& point : trajectory)
  150. {
  151. view(point) = pixel;
  152. }
  153. }
  154. };
  155. } //namespace detail
  156. }} // namespace boost::gil
  157. #endif