CVB++ 15.0
point_2d.hpp
1#pragma once
2
3#include <cmath>
4#include <cassert>
5
6#include "global.hpp"
7
8#include "angle.hpp"
9
10namespace Cvb
11{
12
13CVB_BEGIN_INLINE_NS
14
16
18template< class T, class ENABLE = void>
19class Point2D final
20{
21
22#ifndef CVB_DOXYGEN
23 static_assert(std::is_arithmetic<T>::value, "CVB: Unsupported data type - must be arithmetic!");
24};
25
26
27template <class T>
28class Point2D<T,
29 typename EnableIfArithmetic<T>::type> final
30{
31
32#endif
33
34 public:
35
36 using ValueType = T;
37
39
42 Point2D() noexcept = default;
43
44
46
51 Point2D(T x, T y) noexcept
52 : x_(x)
53 , y_(y)
54 {
55 }
56
58
65 Point2D(Angle phi, T r) noexcept
66 : Point2D(static_cast<T>(static_cast<double>(r) * Cos(phi)),
67 static_cast<T>(static_cast<double>(r) * Sin(phi)))
68 {
69 }
70
72
76 template<std::size_t N>
77 Point2D(const T(&list)[N]) noexcept // NOLINT
78 {
79 static_assert(N == 2, "CVB: Point2D must have 2 elements");
80 std::copy(std::begin(list), std::end(list), &x_);
81 }
82
84
88 T X() const noexcept
89 {
90 return x_;
91 }
92
94
98 void SetX(T x) noexcept
99 {
100 x_ = x;
101 }
102
104
108 T Y() const noexcept
109 {
110 return y_;
111 }
112
114
118 void SetY(T y)
119 {
120 y_ = y;
121 }
122
124
128 T Length() const noexcept
129 {
130 return static_cast<T>(sqrt(static_cast<double>(x_) * static_cast<double>(x_)
131 + static_cast<double>(y_) * static_cast<double>(y_)));
132 }
133
135
139 void SetLength(T length) noexcept
140 {
141 *this = Point2D<T>(Phi(), length);
142 }
143
145
149 Angle Phi() const noexcept
150 {
151 return Atan2(static_cast<double>(y_), static_cast<double>(x_));
152 }
153
155
159 void SetPhi(Angle phi) noexcept
160 {
161 *this = Point2D<T>(phi, Length());
162 }
163
164
165
166
168
173 bool operator==(const Point2D<T>& other) const noexcept
174 {
175 return (x_ == other.x_ && y_ == other.y_);
176 }
177
178
180
185 bool operator!=(const Point2D<T>& other) const noexcept
186 {
187 return !(*this == other);
188 }
189
191 template <class C>
192 explicit operator Point2D<C>() const noexcept
193 {
194 static_assert(std::is_floating_point<C>::value, "CVB: Unsupported data type - must be floating point!");
195 return Point2D<C>(static_cast<C>(X()), static_cast<C>(Y()));
196 }
197
199
204 Point2D<T>& operator +=(const Point2D<T> & point) noexcept
205 {
206 x_ += point.x_;
207 y_ += point.y_;
208 return *this;
209 }
210
212
217 Point2D<T>& operator -=(const Point2D<T> & point) noexcept
218 {
219 x_ -= point.x_;
220 y_ -= point.y_;
221 return *this;
222 }
223
225
230 Point2D<T>& operator *=(const T & value) noexcept
231 {
232 x_ *= value;
233 y_ *= value;
234 return *this;
235 }
236
238
243 Point2D<T>& operator /=(const T & value) noexcept
244 {
245 x_ /= value;
246 y_ /= value;
247 return *this;
248 }
249
250
252
257 const T& operator[](int index) const noexcept
258 {
259 assert(index < 2 && index >= 0);
260 return *(&x_ + index);
261 }
262
264
269 T& operator[](int index) noexcept
270 {
271 assert(index < 2 && index >= 0);
272 return *(&x_ + index);
273 }
274
275 private:
276
277 T x_ = static_cast<T>(0);
278 T y_ = static_cast<T>(0);
279
280
281};
282
284
292template <class T>
293inline Point2D<T> operator+(const Point2D<T> & lhs, const Point2D<T> & rhs)
294{
295 return Point2D<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
296}
297
299
307template <class T>
308inline Point2D<T> operator-(const Point2D<T> & lhs, const Point2D<T> & rhs)
309{
310 return Point2D<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
311}
312
314
322template <class T>
323inline T operator*(const Point2D<T> & lhs, const Point2D<T> & rhs)
324{
325 return lhs.X() * rhs.X() + lhs.Y() * rhs.Y();
326}
327
329
337template <class T>
338inline Point2D<T> operator*(const Point2D<T> & lhs, const T & rhs)
339{
340 return Point2D<T>(lhs.X() * rhs, lhs.Y() * rhs);
341}
342
344
352template <class T>
353inline Point2D<T> operator*(const T & lhs, const Point2D<T> & rhs)
354{
355 return rhs * lhs;
356}
357
359
367template <class T>
368inline Point2D<T> operator/(const Point2D<T> & lhs, const T & rhs)
369{
370 return Point2D<T>(lhs.X() / rhs, lhs.Y() / rhs);
371}
372
374
381template <class T>
382inline Point2D<int> Round(const Point2D<T> & rhs) noexcept
383{
384 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
385 return Point2D<int>(static_cast<int>(std::round(rhs.X())), static_cast<int>(std::round(rhs.Y())));
386}
387
389template<class T>
391
392CVB_END_INLINE_NS
393
394
395}
Object for convenient and type - safe handling of angles.
Definition: angle.hpp:19
Multi-purpose 2D vector class.
Definition: point_2d.hpp:20
Point2D< T > & operator/=(const T &value) noexcept
Divide by a scalar and assigns to this point.
Definition: point_2d.hpp:243
Point2D< T > & operator*=(const T &value) noexcept
Multiplies by a scalar and assigns to this point.
Definition: point_2d.hpp:230
Point2D< T > operator/(const Point2D< T > &lhs, const T &rhs)
Divide point by scalar.
Definition: point_2d.hpp:368
const T & operator[](int index) const noexcept
Index based element access.
Definition: point_2d.hpp:257
Point2D< T > & operator+=(const Point2D< T > &point) noexcept
Adds and assigns to this point.
Definition: point_2d.hpp:204
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:88
T operator*(const Point2D< T > &lhs, const Point2D< T > &rhs)
Inner product of two point vectors.
Definition: point_2d.hpp:323
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:108
void SetX(T x) noexcept
Sets the x-component of the point.
Definition: point_2d.hpp:98
void SetY(T y)
Sets the y-component of the point.
Definition: point_2d.hpp:118
Point2D(const T(&list)[N]) noexcept
Construct a point with an initializer list.
Definition: point_2d.hpp:77
Point2D() noexcept=default
Creates a default point at (0, 0).
Point2D< T > operator*(const T &lhs, const Point2D< T > &rhs)
Multiply scalar with point.
Definition: point_2d.hpp:353
Point2D< T > operator*(const Point2D< T > &lhs, const T &rhs)
Multiply point with scalar.
Definition: point_2d.hpp:338
bool operator!=(const Point2D< T > &other) const noexcept
Compares to an other point.
Definition: point_2d.hpp:185
Point2D< T > operator-(const Point2D< T > &lhs, const Point2D< T > &rhs)
Subtracts two points.
Definition: point_2d.hpp:308
T Length() const noexcept
Gets the length of the vector represented by this point object.
Definition: point_2d.hpp:128
Angle Phi() const noexcept
Gets the orientation of the vector represented by this point object.
Definition: point_2d.hpp:149
bool operator==(const Point2D< T > &other) const noexcept
Compares to an other point.
Definition: point_2d.hpp:173
Point2D< T > operator+(const Point2D< T > &lhs, const Point2D< T > &rhs)
Add two points.
Definition: point_2d.hpp:293
T & operator[](int index) noexcept
Index based element access.
Definition: point_2d.hpp:269
Point2D< T > & operator-=(const Point2D< T > &point) noexcept
Subtracts and assigns to this point.
Definition: point_2d.hpp:217
void SetLength(T length) noexcept
Sets the length of the vector represented by this point object.
Definition: point_2d.hpp:139
Point2D(Angle phi, T r) noexcept
Create a PointD vector from radial coordinates.
Definition: point_2d.hpp:65
void SetPhi(Angle phi) noexcept
Sets the orientation of the vector represented by this point object.
Definition: point_2d.hpp:159
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15
double Sin(Angle angle) noexcept
Returns the sine of an angle.
Definition: angle.hpp:445
Angle Atan2(double y, double x) noexcept
Returns the angle whose tangent is the quotient of two specified numbers.
Definition: angle.hpp:404
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:382
double Cos(Angle angle) noexcept
Returns the cosine of an angle.
Definition: angle.hpp:418