CVB++ 14.1
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Friends Modules Pages
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
37
40 Point2D() noexcept = default;
41
42
44
49 Point2D(T x, T y) noexcept
50 : x_(x)
51 , y_(y)
52 {
53 }
54
56
63 Point2D(Angle phi, T r) noexcept
64 : Point2D(static_cast<T>(static_cast<double>(r) * Cos(phi)),
65 static_cast<T>(static_cast<double>(r) * Sin(phi)))
66 {
67 }
68
70
74 template<std::size_t N>
75 Point2D(const T(&list)[N]) noexcept // NOLINT
76 {
77 static_assert(N == 2, "CVB: Point2D must have 2 elements");
78 std::copy(std::begin(list), std::end(list), &x_);
79 }
80
82
86 T X() const noexcept
87 {
88 return x_;
89 }
90
92
96 void SetX(T x) noexcept
97 {
98 x_ = x;
99 }
100
102
106 T Y() const noexcept
107 {
108 return y_;
109 }
110
112
116 void SetY(T y)
117 {
118 y_ = y;
119 }
120
122
126 T Length() const noexcept
127 {
128 return static_cast<T>(sqrt(static_cast<double>(x_) * static_cast<double>(x_)
129 + static_cast<double>(y_) * static_cast<double>(y_)));
130 }
131
133
137 void SetLength(T length) noexcept
138 {
139 *this = Point2D<T>(Phi(), length);
140 }
141
143
147 Angle Phi() const noexcept
148 {
149 return Atan2(static_cast<double>(y_), static_cast<double>(x_));
150 }
151
153
157 void SetPhi(Angle phi) noexcept
158 {
159 *this = Point2D<T>(phi, Length());
160 }
161
162
163
164
166
171 bool operator==(const Point2D<T>& other) const noexcept
172 {
173 return (x_ == other.x_ && y_ == other.y_);
174 }
175
176
178
183 bool operator!=(const Point2D<T>& other) const noexcept
184 {
185 return !(*this == other);
186 }
187
189 template <class C>
190 explicit operator Point2D<C>() const noexcept
191 {
193 "CVB: Unsupported data type - must be floating point!");
194 return Point2D<C>(static_cast<C>(X()), static_cast<C>(Y()));
195 }
196
198
203 Point2D<T>& operator +=(const Point2D<T> & point) noexcept
204 {
205 x_ += point.x_;
206 y_ += point.y_;
207 return *this;
208 }
209
211
216 Point2D<T>& operator -=(const Point2D<T> & point) noexcept
217 {
218 x_ -= point.x_;
219 y_ -= point.y_;
220 return *this;
221 }
222
224
229 Point2D<T>& operator *=(const T & value) noexcept
230 {
231 x_ *= value;
232 y_ *= value;
233 return *this;
234 }
235
237
242 Point2D<T>& operator /=(const T & value) noexcept
243 {
244 x_ /= value;
245 y_ /= value;
246 return *this;
247 }
248
249
251
256 const T& operator[](int index) const noexcept
257 {
258 assert(index < 2 && index >= 0);
259 return *(&x_ + index);
260 }
261
263
268 T& operator[](int index) noexcept
269 {
270 assert(index < 2 && index >= 0);
271 return *(&x_ + index);
272 }
273
274 private:
275
276 T x_ = static_cast<T>(0);
277 T y_ = static_cast<T>(0);
278
279
280};
281
283
291template <class T>
292inline Point2D<T> operator+(const Point2D<T> & lhs, const Point2D<T> & rhs)
293{
294 return Point2D<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
295}
296
298
306template <class T>
307inline Point2D<T> operator-(const Point2D<T> & lhs, const Point2D<T> & rhs)
308{
309 return Point2D<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
310}
311
313
321template <class T>
322inline T operator*(const Point2D<T> & lhs, const Point2D<T> & rhs)
323{
324 return lhs.X() * rhs.X() + lhs.Y() * rhs.Y();
325}
326
328
336template <class T>
337inline Point2D<T> operator*(const Point2D<T> & lhs, const T & rhs)
338{
339 return Point2D<T>(lhs.X() * rhs, lhs.Y() * rhs);
340}
341
343
351template <class T>
352inline Point2D<T> operator*(const T & lhs, const Point2D<T> & rhs)
353{
354 return rhs * lhs;
355}
356
358
366template <class T>
367inline Point2D<T> operator/(const Point2D<T> & lhs, const T & rhs)
368{
369 return Point2D<T>(lhs.X() / rhs, lhs.Y() / rhs);
370}
371
373
380template <class T>
381inline Point2D<int> Round(const Point2D<T> & rhs) noexcept
382{
383 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
384 return Point2D<int>(static_cast<int>(std::round(rhs.X())), static_cast<int>(std::round(rhs.Y())));
385}
386
388template<class T>
390
391CVB_END_INLINE_NS
392
393
394}
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:242
Point2D< T > & operator*=(const T &value) noexcept
Multiplies by a scalar and assigns to this point.
Definition: point_2d.hpp:229
Point2D< T > operator/(const Point2D< T > &lhs, const T &rhs)
Divide point by scalar.
Definition: point_2d.hpp:367
const T & operator[](int index) const noexcept
Index based element access.
Definition: point_2d.hpp:256
Point2D< T > & operator+=(const Point2D< T > &point) noexcept
Adds and assigns to this point.
Definition: point_2d.hpp:203
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
T operator*(const Point2D< T > &lhs, const Point2D< T > &rhs)
Inner product of two point vectors.
Definition: point_2d.hpp:322
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
void SetX(T x) noexcept
Sets the x-component of the point.
Definition: point_2d.hpp:96
void SetY(T y)
Sets the y-component of the point.
Definition: point_2d.hpp:116
Point2D(const T(&list)[N]) noexcept
Construct a point with an initializer list.
Definition: point_2d.hpp:75
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:352
Point2D< T > operator*(const Point2D< T > &lhs, const T &rhs)
Multiply point with scalar.
Definition: point_2d.hpp:337
bool operator!=(const Point2D< T > &other) const noexcept
Compares to an other point.
Definition: point_2d.hpp:183
Point2D< T > operator-(const Point2D< T > &lhs, const Point2D< T > &rhs)
Subtracts two points.
Definition: point_2d.hpp:307
T Length() const noexcept
Gets the length of the vector represented by this point object.
Definition: point_2d.hpp:126
Angle Phi() const noexcept
Gets the orientation of the vector represented by this point object.
Definition: point_2d.hpp:147
bool operator==(const Point2D< T > &other) const noexcept
Compares to an other point.
Definition: point_2d.hpp:171
Point2D< T > operator+(const Point2D< T > &lhs, const Point2D< T > &rhs)
Add two points.
Definition: point_2d.hpp:292
T & operator[](int index) noexcept
Index based element access.
Definition: point_2d.hpp:268
Point2D< T > & operator-=(const Point2D< T > &point) noexcept
Subtracts and assigns to this point.
Definition: point_2d.hpp:216
void SetLength(T length) noexcept
Sets the length of the vector represented by this point object.
Definition: point_2d.hpp:137
Point2D(Angle phi, T r) noexcept
Create a PointD vector from radial coordinates.
Definition: point_2d.hpp:63
void SetPhi(Angle phi) noexcept
Sets the orientation of the vector represented by this point object.
Definition: point_2d.hpp:157
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
double Sin(Angle angle) noexcept
Returns the sine of an angle.
Definition: angle.hpp:438
Angle Atan2(double y, double x) noexcept
Returns the angle whose tangent is the quotient of two specified numbers.
Definition: angle.hpp:397
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:381
double Cos(Angle angle) noexcept
Returns the cosine of an angle.
Definition: angle.hpp:411