update boost

This commit is contained in:
2023-11-24 12:56:13 -06:00
parent cfc99971af
commit 19d727037a
9260 changed files with 849256 additions and 299957 deletions

View File

@@ -0,0 +1,28 @@
#ifndef BOOST_GIL_EXTENSION_RASTERIZATION_APPLY_RASTERIZER
#define BOOST_GIL_EXTENSION_RASTERIZATION_APPLY_RASTERIZER
namespace boost { namespace gil {
namespace detail {
template <typename View, typename Rasterizer, typename Pixel, typename Tag>
struct apply_rasterizer_op
{
void operator()(
View const& view, Rasterizer const& rasterizer, Pixel const& pixel);
};
} // namespace detail
template <typename View, typename Rasterizer, typename Pixel>
void apply_rasterizer(
View const& view, Rasterizer const& rasterizer, Pixel const& pixel)
{
using tag_t = typename Rasterizer::type;
detail::apply_rasterizer_op<View, Rasterizer, Pixel, tag_t>{}(
view, rasterizer, pixel);
}
}} // namespace boost::gil
#endif

View File

@@ -0,0 +1,179 @@
//
// Copyright 2020 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
//
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#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{};
/// \defgroup CircleRasterization
/// \ingroup Rasterization
/// \brief Circle rasterization algorithms
///
/// The main problems are connectivity and equation following. Circle can be easily moved
/// to new offset, and rotation has no effect on it (not recommended to do rotation).
/// \ingroup CircleRasterization
/// \brief Rasterize trigonometric circle according to radius by sine and radius by cosine
///
/// This rasterizer is the one used that is used in standard Hough circle transform in
/// the books. It is also quite expensive to compute.
/// WARNING: the product of this rasterizer does not follow circle equation, even though it
/// produces quite round like shapes.
struct trigonometric_circle_rasterizer
{
using type = circle_rasterizer_t;
/// \brief Creates a trigonometric circle rasterizer
/// \param center_point - Point containing positive integer x co-ordinate and y co-ordinate of the
/// center respectively.
/// \param circle_radius - Radius of the circle
trigonometric_circle_rasterizer(point_t center_point, std::ptrdiff_t circle_radius)
: center(center_point), radius(circle_radius)
{}
/// \brief Calculates minimum angle step that is distinguishable when walking on circle
///
/// It is important to not have disconnected circle and to not compute unnecessarily,
/// thus the result of this function is used when rendering.
double minimum_angle_step() const noexcept
{
const auto diameter = radius * 2 - 1;
return std::atan2(1.0, diameter);
}
/// \brief Calculate the amount of points that rasterizer will output
std::ptrdiff_t point_count() const noexcept
{
return 8 * static_cast<std::ptrdiff_t>(
std::round(detail::pi / 4 / minimum_angle_step()) + 1);
}
/// \brief perform rasterization and output into d_first
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;
// do note that + 1 was done inside count estimation, thus <= is not needed, only <
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;
};
/// \ingroup CircleRasterization
/// \brief Perform circle rasterization according to Midpoint algorithm
///
/// This algorithm givess reasonable output and is cheap to compute.
/// reference:
/// https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
struct midpoint_circle_rasterizer
{
using type = circle_rasterizer_t;
/// \brief Creates a midpoint circle rasterizer
/// \param center_point - Point containing positive integer x co-ordinate and y co-ordinate of the
/// center respectively.
/// \param circle_radius - Radius of the circle
midpoint_circle_rasterizer(point_t center_point, std::ptrdiff_t circle_radius)
: center(center_point), radius(circle_radius)
{}
/// \brief Calculate the amount of points that rasterizer will output
std::ptrdiff_t point_count() const noexcept
{
// the reason for pulling 8 out is so that when the expression radius * cos(45 degrees)
// is used, it would yield the same result as here
// + 1 at the end is because the point at radius itself is computed as well
return 8 * static_cast<std::ptrdiff_t>(
std::round(radius * std::cos(boost::gil::detail::pi / 4)) + 1);
}
/// \brief perform rasterization and output into d_first
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;
}
}
};
} //namespace detail
}} // namespace boost::gil
#endif

View File

@@ -0,0 +1,209 @@
//
// Copyright 2021 Prathamesh Tagore <prathameshtagore@gmail.com>
//
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_GIL_EXTENSION_RASTERIZATION_ELLIPSE_HPP
#define BOOST_GIL_EXTENSION_RASTERIZATION_ELLIPSE_HPP
#include <boost/gil/concepts/pixel.hpp>
#include <boost/gil/extension/rasterization/apply_rasterizer.hpp>
#include <boost/gil/point.hpp>
#include <array>
#include <stdexcept>
#include <vector>
namespace boost { namespace gil {
struct ellipse_rasterizer_t{};
/// \defgroup EllipseRasterization
/// \ingroup Rasterization
/// \brief Ellipse rasterization algorithms.
/// \ingroup EllipseRasterization
/// \brief Performs ellipse rasterization using midpoint algorithm. Initially, program considers
/// origin as center of ellipse and obtains first quadrant trajectory points. After that,
/// it shifts origin to provided co-ordinates of center and then draws the curve.
struct midpoint_ellipse_rasterizer
{
using type = ellipse_rasterizer_t;
/// \brief Creates a midpoint ellipse rasterizer
/// \param center - Point containing positive integer x co-ordinate and y co-ordinate of the
/// center respectively.
/// \param semi_axes - Point containing positive integer lengths of horizontal semi-axis
/// and vertical semi-axis respectively.
midpoint_ellipse_rasterizer(point<unsigned int> center_point,
point<unsigned int> semi_axes_values)
: center(center_point)
, semi_axes(semi_axes_values)
{}
/// \brief Returns a vector containing co-ordinates of first quadrant points which lie on
/// rasterizer trajectory of the ellipse.
auto obtain_trajectory() const
-> std::vector<point_t>
{
// Citation : J. Van Aken, "An Efficient Ellipse-Drawing Algorithm" in IEEE Computer
// Graphics and Applications, vol. 4, no. 09, pp. 24-35, 1984.
// doi: 10.1109/MCG.1984.275994
// keywords: {null}
// url: https://doi.ieeecomputersociety.org/10.1109/MCG.1984.275994
std::vector<point_t> trajectory_points;
std::ptrdiff_t x = semi_axes[0], y = 0;
// Variables declared on following lines are temporary variables used for improving
// performance since they help in converting all multiplicative operations inside the while
// loop into additive/subtractive operations.
long long int const t1 = semi_axes[0] * semi_axes[0];
long long int const t4 = semi_axes[1] * semi_axes[1];
long long int t2, t3, t5, t6, t8, t9;
t2 = 2 * t1, t3 = 2 * t2;
t5 = 2 * t4, t6 = 2 * t5;
long long int const t7 = semi_axes[0] * t5;
t8 = 2 * t7, t9 = 0;
// Following variables serve as decision parameters and help in choosing the right point
// to be included in rasterizer trajectory.
long long int d1, d2;
d1 = t2 - t7 + t4 / 2, d2 = t1 / 2 - t8 + t5;
while (d2 < 0)
{
trajectory_points.push_back({x, y});
y += 1;
t9 += t3;
if (d1 < 0)
{
d1 += t9 + t2;
d2 += t9;
}
else
{
x -= 1;
t8 -= t6;
d1 += t9 + t2 - t8;
d2 += t5 + t9 - t8;
}
}
while (x >= 0)
{
trajectory_points.push_back({x, y});
x -= 1;
t8 -= t6;
if (d2 < 0)
{
y += 1;
t9 += t3;
d2 += t5 + t9 - t8;
}
else
{
d2 += t5 - t8;
}
}
return trajectory_points;
}
/// \brief Fills pixels returned by function 'obtain_trajectory' as well as pixels
/// obtained from their reflection along major axis, minor axis and line passing through
/// center with slope -1 using colours provided by user.
/// \param view - Gil view of image on which the elliptical curve is to be drawn.
/// \param pixel - Pixel value for the elliptical curve to be drawn.
/// \param trajectory_points - Constant vector specifying pixel co-ordinates of points lying
/// on rasterizer trajectory.
/// \tparam View - Type of input image view.
/// \tparam Pixel - Type of pixel. Must be compatible to the pixel type of the image view
template<typename View, typename Pixel>
void draw_curve(View& view, Pixel const& pixel,
std::vector<point_t> const& trajectory_points) const
{
using pixel_t = typename View::value_type;
if (!pixels_are_compatible<pixel_t, Pixel>())
{
throw std::runtime_error("Pixel type of the given image is not compatible to the "
"type of the provided pixel.");
}
// mutable center copy
point<unsigned int> center2(center);
--center2[0], --center2[1]; // For converting center co-ordinate values to zero based indexing.
for (point_t pnt : trajectory_points)
{
std::array<std::ptrdiff_t, 4> co_ords = {center2[0] + pnt[0],
center2[0] - pnt[0], center2[1] + pnt[1], center2[1] - pnt[1]
};
bool validity[4]{};
if (co_ords[0] < view.width())
{
validity[0] = true;
}
if (co_ords[1] >= 0 && co_ords[1] < view.width())
{
validity[1] = true;
}
if (co_ords[2] < view.height())
{
validity[2] = true;
}
if (co_ords[3] >= 0 && co_ords[3] < view.height())
{
validity[3] = true;
}
if (validity[0] && validity[2])
{
view(co_ords[0], co_ords[2]) = pixel;
}
if (validity[1] && validity[2])
{
view(co_ords[1], co_ords[2]) = pixel;
}
if (validity[1] && validity[3])
{
view(co_ords[1], co_ords[3]) = pixel;
}
if (validity[0] && validity[3])
{
view(co_ords[0], co_ords[3]) = pixel;
}
}
}
/// \brief Calls the function 'obtain_trajectory' and then passes obtained trajectory points
/// in the function 'draw_curve' for drawing the desired ellipse.
/// \param view - Gil view of image on which the elliptical curve is to be drawn.
/// \param pixel - Pixel value for the elliptical curve to be drawn.
/// \tparam View - Type of input image view.
/// \tparam Pixel - Type of pixel. Must be compatible to the pixel type of the image view
template<typename View, typename Pixel>
void operator()(View& view, Pixel const& pixel) const
{
draw_curve(view, pixel, obtain_trajectory());
}
point<unsigned int> center;
point<unsigned int> semi_axes;
};
namespace detail {
template <typename View, typename Rasterizer, typename Pixel>
struct apply_rasterizer_op<View, Rasterizer, Pixel, ellipse_rasterizer_t>
{
void operator()(
View const& view, Rasterizer const& rasterizer, Pixel const& pixel)
{
rasterizer(view, pixel);
}
};
} //namespace detail
}} // namespace boost::gil
#endif

View File

@@ -0,0 +1,132 @@
//
// Copyright 2020 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
//
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_GIL_EXTENSION_RASTERIZATION_LINE_HPP
#define BOOST_GIL_EXTENSION_RASTERIZATION_LINE_HPP
#include <boost/gil/extension/rasterization/apply_rasterizer.hpp>
#include <boost/gil/point.hpp>
#include <cmath>
#include <cstddef>
#include <iterator>
#include <vector>
namespace boost { namespace gil {
struct line_rasterizer_t{};
/// \defgroup Rasterization
/// \brief A set of functions to rasterize shapes
///
/// Due to images being discrete, most shapes require specialized algorithms to
/// handle rasterization efficiently and solve problem of connectivity and being
/// close to the original shape.
/// \defgroup LineRasterization
/// \ingroup Rasterization
/// \brief A set of rasterizers for lines
///
/// The main problem with line rasterization is to do it efficiently, e.g. less
/// floating point operations. There are multiple algorithms that on paper
/// should reach the same result, but due to quirks of IEEE-754 they don't.
/// Please select one and stick to it if possible. At the moment only Bresenham
/// rasterizer is implemented.
/// \ingroup LineRasterization
/// \brief Rasterize a line according to Bresenham algorithm
///
/// Do note that if either width or height is 1, slope is set to zero.
/// reference:
/// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#:~:text=Bresenham's%20line%20algorithm%20is%20a,straight%20line%20between%20two%20points.
struct bresenham_line_rasterizer
{
using type = line_rasterizer_t;
bresenham_line_rasterizer(point_t start, point_t end)
: start_point(start), end_point(end)
{}
std::ptrdiff_t point_count() const noexcept
{
const auto abs_width = std::abs(end_point.x - start_point.x) + 1;
const auto abs_height = std::abs(end_point.y - start_point.y) + 1;
return abs_width > abs_height ? abs_width : abs_height;
}
template <typename OutputIterator>
void operator()(OutputIterator d_first) const
{
// mutable stack copies
point_t start = start_point;
point_t end = end_point;
if (start == end)
{
// put the point and immediately exit, as later on division by zero will
// occur
*d_first = start;
return;
}
auto width = std::abs(end.x - start.x) + 1;
auto height = std::abs(end.y - start.y) + 1;
bool const needs_flip = width < height;
if (needs_flip)
{
// transpose the coordinate system if uncomfortable angle detected
std::swap(width, height);
std::swap(start.x, start.y);
std::swap(end.x, end.y);
}
std::ptrdiff_t const x_increment = end.x >= start.x ? 1 : -1;
std::ptrdiff_t const y_increment = end.y >= start.y ? 1 : -1;
double const slope =
height == 1 ? 0 : static_cast<double>(height) / static_cast<double>(width);
std::ptrdiff_t y = start.y;
double error_term = 0;
for (std::ptrdiff_t x = start.x; x != end.x; x += x_increment)
{
// transpose coordinate system back to proper form if needed
*d_first++ = needs_flip ? point_t{y, x} : point_t{x, y};
error_term += slope;
if (error_term >= 0.5)
{
--error_term;
y += y_increment;
}
}
*d_first++ = needs_flip ? point_t{end.y, end.x} : end;
}
point_t start_point;
point_t end_point;
};
namespace detail {
template <typename View, typename Rasterizer, typename Pixel>
struct apply_rasterizer_op<View, Rasterizer, Pixel, line_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;
}
}
};
} //namespace detail
}} // namespace boost::gil
#endif