###

#

point.hpp
//
// Copyright 2005-2007 Adobe Systems Incorporated
//
// Distributed under 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_POINT_HPP
#define BOOST_GIL_POINT_HPP
#include <boost/gil/utilities.hpp>
#include <boost/gil/detail/std_common_type.hpp>
#include <boost/config.hpp>
#include <cstddef>
#include <type_traits>
namespace boost { namespace gil {
template <typename T>
class point
{
public:
using value_type = T;
template<std::size_t D>
struct axis
{
using coord_t = value_type;
};
static constexpr std::size_t num_dimensions = 2;
point() = default;
point(T px, T py) : x(px), y(py) {}
point operator<<(std::ptrdiff_t shift) const
{
return point(x << shift, y << shift);
}
point operator>>(std::ptrdiff_t shift) const
{
return point(x >> shift, y >> shift);
}
point& operator+=(point const& p)
{
x += p.x;
y += p.y;
return *this;
}
point& operator-=(point const& p)
{
x -= p.x;
y -= p.y;
return *this;
}
point& operator/=(double d)
{
if (d < 0 || 0 < d)
{
x = static_cast<T>(x / d);
y = static_cast<T>(y / d);
}
return *this;
}
point& operator*=(double d)
{
x = static_cast<T>(x * d);
y = static_cast<T>(y * d);
return *this;
}
T const& operator[](std::size_t i) const
{
return this->*mem_array[i];
}
T& operator[](std::size_t i)
{
return this->*mem_array[i];
}
T x{0};
T y{0};
private:
// this static array of pointers to member variables makes operator[] safe
// and doesn't seem to exhibit any performance penalty.
static T point<T>::* const mem_array[num_dimensions];
};
template <typename T>
using point2 = point<T>;
using point_t = point<std::ptrdiff_t>;
template <typename T>
T point<T>::* const point<T>::mem_array[point<T>::num_dimensions] =
{
&point<T>::x,
&point<T>::y
};
template <typename T>
BOOST_FORCEINLINE
bool operator==(const point<T>& p1, const point<T>& p2)
{
return p1.x == p2.x && p1.y == p2.y;
}
template <typename T>
BOOST_FORCEINLINE
bool operator!=(const point<T>& p1, const point<T>& p2)
{
return p1.x != p2.x || p1.y != p2.y;
}
template <typename T>
BOOST_FORCEINLINE
point<T> operator+(const point<T>& p1, const point<T>& p2)
{
return { p1.x + p2.x, p1.y + p2.y };
}
template <typename T>
BOOST_FORCEINLINE
point<T> operator-(const point<T>& p)
{
return { -p.x, -p.y };
}
template <typename T>
BOOST_FORCEINLINE
point<T> operator-(const point<T>& p1, const point<T>& p2)
{
return { p1.x - p2.x, p1.y - p2.y };
}
template <typename T, typename D>
BOOST_FORCEINLINE
auto operator/(point<T> const& p, D d)
-> typename std::enable_if
<
std::is_arithmetic<D>::value,
point<typename detail::std_common_type<T, D>::type>
>::type
{
static_assert(std::is_arithmetic<D>::value, "denominator is not arithmetic type");
using result_type = typename detail::std_common_type<T, D>::type;
if (d < 0 || 0 < d)
{
double const x = static_cast<double>(p.x) / static_cast<double>(d);
double const y = static_cast<double>(p.y) / static_cast<double>(d);
return point<result_type>{
static_cast<result_type>(iround(x)),
static_cast<result_type>(iround(y))};
}
else
{
return point<result_type>{0, 0};
}
}
template <typename T, typename M>
BOOST_FORCEINLINE
auto operator*(point<T> const& p, M m)
-> typename std::enable_if
<
std::is_arithmetic<M>::value,
point<typename detail::std_common_type<T, M>::type>
>::type
{
static_assert(std::is_arithmetic<M>::value, "multiplier is not arithmetic type");
using result_type = typename detail::std_common_type<T, M>::type;
return point<result_type>{p.x * m, p.y * m};
}
template <typename T, typename M>
BOOST_FORCEINLINE
auto operator*(M m, point<T> const& p)
-> typename std::enable_if
<
std::is_arithmetic<M>::value,
point<typename detail::std_common_type<T, M>::type>
>::type
{
static_assert(std::is_arithmetic<M>::value, "multiplier is not arithmetic type");
using result_type = typename detail::std_common_type<T, M>::type;
return point<result_type>{p.x * m, p.y * m};
}
template <std::size_t K, typename T>
BOOST_FORCEINLINE
T const& axis_value(point<T> const& p)
{
static_assert(K < point<T>::num_dimensions, "axis index out of range");
return p[K];
}
template <std::size_t K, typename T>
BOOST_FORCEINLINE
T& axis_value(point<T>& p)
{
static_assert(K < point<T>::num_dimensions, "axis index out of range");
return p[K];
}
template <typename T>
inline auto iround(point<T> const& p) -> point<std::ptrdiff_t>
{
static_assert(std::is_integral<T>::value, "T is not integer");
return { static_cast<std::ptrdiff_t>(p.x), static_cast<std::ptrdiff_t>(p.y) };
}
inline auto iround(point<float> const& p) -> point<std::ptrdiff_t>
{
return { iround(p.x), iround(p.y) };
}
inline auto iround(point<double> const& p) -> point<std::ptrdiff_t>
{
return { iround(p.x), iround(p.y) };
}
inline auto ifloor(point<float> const& p) -> point<std::ptrdiff_t>
{
return { ifloor(p.x), ifloor(p.y) };
}
inline auto ifloor(point<double> const& p) -> point<std::ptrdiff_t>
{
return { ifloor(p.x), ifloor(p.y) };
}
inline auto iceil(point<float> const& p) -> point<std::ptrdiff_t>
{
return { iceil(p.x), iceil(p.y) };
}
inline auto iceil(point<double> const& p) -> point<std::ptrdiff_t>
{
return { iceil(p.x), iceil(p.y) };
}
}} // namespace boost::gil
#endif
boost::gil::point
2D point both axes of which have the same dimension typeModels: Point2DConcept
Definition: point.hpp:36
boost
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36