123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #ifndef BOOST_GIL_EXTENSION_RASTERIZATION_CIRCLE_HPP
- #define BOOST_GIL_EXTENSION_RASTERIZATION_CIRCLE_HPP
- #include <boost/gil/detail/math.hpp>
- #include <boost/gil/extension/rasterization/apply_rasterizer.hpp>
- #include <boost/gil/point.hpp>
- #include <cmath>
- #include <cstddef>
- #include <vector>
- namespace boost { namespace gil {
- struct circle_rasterizer_t{};
- struct trigonometric_circle_rasterizer
- {
- using type = circle_rasterizer_t;
-
-
-
-
- trigonometric_circle_rasterizer(point_t center_point, std::ptrdiff_t circle_radius)
- : center(center_point), radius(circle_radius)
- {}
-
-
-
-
- double minimum_angle_step() const noexcept
- {
- const auto diameter = radius * 2 - 1;
- return std::atan2(1.0, diameter);
- }
-
- std::ptrdiff_t point_count() const noexcept
- {
- return 8 * static_cast<std::ptrdiff_t>(
- std::round(detail::pi / 4 / minimum_angle_step()) + 1);
- }
-
- template <typename OutputIterator>
- void operator()(OutputIterator d_first) const
- {
- const double minimum_angle_step = std::atan2(1.0, radius);
- auto translate_mirror_points = [this, &d_first](point_t p) {
- *d_first++ = point_t{center.x + p.x, center.y + p.y};
- *d_first++ = point_t{center.x + p.x, center.y - p.y};
- *d_first++ = point_t{center.x - p.x, center.y + p.y};
- *d_first++ = point_t{center.x - p.x, center.y - p.y};
- *d_first++ = point_t{center.x + p.y, center.y + p.x};
- *d_first++ = point_t{center.x + p.y, center.y - p.x};
- *d_first++ = point_t{center.x - p.y, center.y + p.x};
- *d_first++ = point_t{center.x - p.y, center.y - p.x};
- };
- const std::ptrdiff_t iteration_count = point_count() / 8;
- double angle = 0;
-
- for (std::ptrdiff_t i = 0; i < iteration_count; ++i, angle += minimum_angle_step)
- {
- std::ptrdiff_t x = static_cast<std::ptrdiff_t>(std::round(radius * std::cos(angle)));
- std::ptrdiff_t y = static_cast<std::ptrdiff_t>(std::round(radius * std::sin(angle)));
- translate_mirror_points({x, y});
- }
- }
- point_t center;
- std::ptrdiff_t radius;
- };
- struct midpoint_circle_rasterizer
- {
- using type = circle_rasterizer_t;
-
-
-
-
- midpoint_circle_rasterizer(point_t center_point, std::ptrdiff_t circle_radius)
- : center(center_point), radius(circle_radius)
- {}
-
- std::ptrdiff_t point_count() const noexcept
- {
-
-
-
- return 8 * static_cast<std::ptrdiff_t>(
- std::round(radius * std::cos(boost::gil::detail::pi / 4)) + 1);
- }
-
- template <typename OutputIterator>
- void operator()(OutputIterator d_first) const
- {
- auto translate_mirror_points = [this, &d_first](point_t p) {
- *d_first++ = point_t{center.x + p.x, center.y + p.y};
- *d_first++ = point_t{center.x + p.x, center.y - p.y};
- *d_first++ = point_t{center.x - p.x, center.y + p.y};
- *d_first++ = point_t{center.x - p.x, center.y - p.y};
- *d_first++ = point_t{center.x + p.y, center.y + p.x};
- *d_first++ = point_t{center.x + p.y, center.y - p.x};
- *d_first++ = point_t{center.x - p.y, center.y + p.x};
- *d_first++ = point_t{center.x - p.y, center.y - p.x};
- };
- std::ptrdiff_t iteration_distance = point_count() / 8;
- std::ptrdiff_t y_current = radius;
- std::ptrdiff_t r_squared = radius * radius;
- translate_mirror_points({0, y_current});
- for (std::ptrdiff_t x = 1; x < iteration_distance; ++x)
- {
- std::ptrdiff_t midpoint = x * x + y_current * y_current - y_current - r_squared;
- if (midpoint > 0)
- {
- --y_current;
- }
- translate_mirror_points({x, y_current});
- }
- }
- point_t center;
- std::ptrdiff_t radius;
- };
- namespace detail {
- template <typename View, typename Rasterizer, typename Pixel>
- struct apply_rasterizer_op<View, Rasterizer, Pixel, circle_rasterizer_t>
- {
- void operator()(
- View const& view, Rasterizer const& rasterizer, Pixel const& pixel)
- {
- std::vector<point_t> trajectory(rasterizer.point_count());
- rasterizer(std::begin(trajectory));
- for (auto const& point : trajectory)
- {
- view(point) = pixel;
- }
- }
- };
- }
- }}
- #endif
|