8 #ifndef BOOST_GIL_POINT_HPP 9 #define BOOST_GIL_POINT_HPP 11 #include <boost/gil/utilities.hpp> 13 #include <boost/config.hpp> 16 #include <type_traits> 18 namespace boost {
namespace gil {
39 template<std::
size_t D>
42 using coord_t = value_type;
45 static constexpr std::size_t num_dimensions = 2;
48 point(T px, T py) : x(px), y(py) {}
50 point operator<<(std::ptrdiff_t shift)
const 52 return point(x << shift, y << shift);
55 point operator>>(std::ptrdiff_t shift)
const 57 return point(x >> shift, y >> shift);
60 point& operator+=(point
const& p)
67 point& operator-=(point
const& p)
74 point& operator/=(
double d)
78 x =
static_cast<T
>(x / d);
79 y =
static_cast<T
>(y / d);
84 point& operator*=(
double d)
86 x =
static_cast<T
>(x * d);
87 y =
static_cast<T
>(y * d);
91 T
const& operator[](std::size_t i)
const 93 return this->*mem_array[i];
96 T& operator[](std::size_t i)
98 return this->*mem_array[i];
107 static T point<T>::*
const mem_array[num_dimensions];
111 template <
typename T>
112 using point2 = point<T>;
116 using point_t = point<std::ptrdiff_t>;
118 template <
typename T>
119 T point<T>::*
const point<T>::mem_array[point<T>::num_dimensions] =
126 template <
typename T>
128 bool operator==(
const point<T>& p1,
const point<T>& p2)
130 return p1.x == p2.x && p1.y == p2.y;
134 template <
typename T>
136 bool operator!=(
const point<T>& p1,
const point<T>& p2)
138 return p1.x != p2.x || p1.y != p2.y;
142 template <
typename T>
144 point<T> operator+(
const point<T>& p1,
const point<T>& p2)
146 return { p1.x + p2.x, p1.y + p2.y };
150 template <
typename T>
152 point<T> operator-(
const point<T>& p)
154 return { -p.x, -p.y };
158 template <
typename T>
160 point<T> operator-(
const point<T>& p1,
const point<T>& p2)
162 return { p1.x - p2.x, p1.y - p2.y };
166 template <
typename T,
typename D>
168 auto operator/(point<T>
const& p, D d) -> point<typename std::common_type<T, D>::type>
170 static_assert(std::is_arithmetic<D>::value,
"denominator is not arithmetic type");
171 using result_type =
typename std::common_type<T, D>::type;
174 double const x = p.x /
static_cast<double>(d);
175 double const y = p.y /
static_cast<double>(d);
176 return point<result_type>{
177 static_cast<result_type
>(iround(x)),
178 static_cast<result_type>(iround(y))};
182 return point<result_type>{0, 0};
187 template <
typename T,
typename M>
189 auto operator*(point<T>
const& p, M m) -> point<typename std::common_type<T, M>::type>
191 static_assert(std::is_arithmetic<M>::value,
"multiplier is not arithmetic type");
192 using result_type =
typename std::common_type<T, M>::type;
193 return point<result_type>{p.x * m, p.y * m};
197 template <
typename T,
typename M>
199 auto operator*(M m, point<T>
const& p) -> point<typename std::common_type<T, M>::type>
201 static_assert(std::is_arithmetic<M>::value,
"multiplier is not arithmetic type");
202 using result_type =
typename std::common_type<T, M>::type;
203 return point<result_type>{p.x * m, p.y * m};
207 template <std::
size_t K,
typename T>
209 T
const& axis_value(point<T>
const& p)
211 static_assert(K < point<T>::num_dimensions,
"axis index out of range");
216 template <std::
size_t K,
typename T>
218 T& axis_value(point<T>& p)
220 static_assert(K < point<T>::num_dimensions,
"axis index out of range");
232 template <
typename T>
233 inline point<std::ptrdiff_t> iround(point<T>
const& p)
235 static_assert(std::is_integral<T>::value,
"T is not integer");
236 return {
static_cast<std::ptrdiff_t
>(p.x), static_cast<std::ptrdiff_t>(p.y) };
240 inline point<std::ptrdiff_t> iround(point<float>
const& p)
242 return { iround(p.x), iround(p.y) };
246 inline point<std::ptrdiff_t> iround(point<double>
const& p)
248 return { iround(p.x), iround(p.y) };
252 inline point<std::ptrdiff_t> ifloor(point<float>
const& p)
254 return { ifloor(p.x), ifloor(p.y) };
258 inline point<std::ptrdiff_t> ifloor(point<double>
const& p)
260 return { ifloor(p.x), ifloor(p.y) };
264 inline point<std::ptrdiff_t> iceil(point<float>
const& p)
266 return { iceil(p.x), iceil(p.y) };
270 inline point<std::ptrdiff_t> iceil(point<double>
const& p)
272 return { iceil(p.x), iceil(p.y) };
Definition: algorithm.hpp:30