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
13 CVB_BEGIN_INLINE_NS
14
16
18 template <class T, class ENABLE = void>
19 class 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 template <class T>
27 class Point2D<T, typename EnableIfArithmetic<T>::type> final
28 {
29
30#endif
31
32 public:
33 using ValueType = T;
34
36
39 Point2D() noexcept = default;
40
42
47 Point2D(T x, T y) noexcept
48 : x_(x)
49 , y_(y)
50 {
51 }
52
54
62 Point2D(Angle phi, T r) noexcept
63 : Point2D(static_cast<T>(static_cast<double>(r) * Cos(phi)), static_cast<T>(static_cast<double>(r) * Sin(phi)))
64 {
65 }
66
68
72 template <std::size_t N>
73 Point2D(const T (&list)[N]) noexcept // NOLINT
74 {
75 static_assert(N == 2, "CVB: Point2D must have 2 elements");
76 std::copy(std::begin(list), std::end(list), &x_);
77 }
78
80
84 T X() const noexcept
85 {
86 return x_;
87 }
88
90
94 void SetX(T x) noexcept
95 {
96 x_ = x;
97 }
98
100
104 T Y() const noexcept
105 {
106 return y_;
107 }
108
110
114 void SetY(T y)
115 {
116 y_ = y;
117 }
118
120
124 T Length() const noexcept
125 {
126 return static_cast<T>(
127 sqrt(static_cast<double>(x_) * static_cast<double>(x_) + static_cast<double>(y_) * static_cast<double>(y_)));
128 }
129
131
135 void SetLength(T length) noexcept
136 {
137 *this = Point2D<T>(Phi(), length);
138 }
139
141
145 Angle Phi() const noexcept
146 {
147 return Atan2(static_cast<double>(y_), static_cast<double>(x_));
148 }
149
151
155 void SetPhi(Angle phi) noexcept
156 {
157 *this = Point2D<T>(phi, Length());
158 }
159
161
166 bool operator==(const Point2D<T> &other) const noexcept
167 {
168 return (x_ == other.x_ && y_ == other.y_);
169 }
170
172
177 bool operator!=(const Point2D<T> &other) const noexcept
178 {
179 return !(*this == other);
180 }
181
183 template <class C>
184 explicit operator Point2D<C>() const noexcept
185 {
187 "CVB: Unsupported data type - must be floating point!");
188 return Point2D<C>(static_cast<C>(X()), static_cast<C>(Y()));
189 }
190
192
197 Point2D<T> &operator+=(const Point2D<T> &point) noexcept
198 {
199 x_ += point.x_;
200 y_ += point.y_;
201 return *this;
202 }
203
205
210 Point2D<T> &operator-=(const Point2D<T> &point) noexcept
211 {
212 x_ -= point.x_;
213 y_ -= point.y_;
214 return *this;
215 }
216
218
223 Point2D<T> &operator*=(const T &value) noexcept
224 {
225 x_ *= value;
226 y_ *= value;
227 return *this;
228 }
229
231
236 Point2D<T> &operator/=(const T &value) noexcept
237 {
238 x_ /= value;
239 y_ /= value;
240 return *this;
241 }
242
244
249 const T &operator[](int index) const noexcept
250 {
251 assert(index < 2 && index >= 0);
252 return *(&x_ + index);
253 }
254
256
261 T &operator[](int index) noexcept
262 {
263 assert(index < 2 && index >= 0);
264 return *(&x_ + index);
265 }
266
267 private:
268 T x_ = static_cast<T>(0);
269 T y_ = static_cast<T>(0);
270 };
271
273
281 template <class T>
282 inline Point2D<T> operator+(const Point2D<T> &lhs, const Point2D<T> &rhs)
283 {
284 return Point2D<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
285 }
286
288
296 template <class T>
297 inline Point2D<T> operator-(const Point2D<T> &lhs, const Point2D<T> &rhs)
298 {
299 return Point2D<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
300 }
301
303
311 template <class T>
312 inline T operator*(const Point2D<T> &lhs, const Point2D<T> &rhs)
313 {
314 return lhs.X() * rhs.X() + lhs.Y() * rhs.Y();
315 }
316
318
326 template <class T>
327 inline Point2D<T> operator*(const Point2D<T> &lhs, const T &rhs)
328 {
329 return Point2D<T>(lhs.X() * rhs, lhs.Y() * rhs);
330 }
331
333
341 template <class T>
342 inline Point2D<T> operator*(const T &lhs, const Point2D<T> &rhs)
343 {
344 return rhs * lhs;
345 }
346
348
356 template <class T>
357 inline Point2D<T> operator/(const Point2D<T> &lhs, const T &rhs)
358 {
359 return Point2D<T>(lhs.X() / rhs, lhs.Y() / rhs);
360 }
361
363
370 template <class T>
371 inline Point2D<int> Round(const Point2D<T> &rhs) noexcept
372 {
373 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
374 return Point2D<int>(static_cast<int>(std::round(rhs.X())), static_cast<int>(std::round(rhs.Y())));
375 }
376
378 template <class T>
380
381 CVB_END_INLINE_NS
382
383} // namespace Cvb
T begin(T... args)
Object for convenient and type - safe handling of angles.
Definition angle.hpp:16
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:236
Point2D< T > & operator*=(const T &value) noexcept
Multiplies by a scalar and assigns to this point.
Definition point_2d.hpp:223
Point2D< T > operator/(const Point2D< T > &lhs, const T &rhs)
Divide point by scalar.
Definition point_2d.hpp:357
const T & operator[](int index) const noexcept
Index based element access.
Definition point_2d.hpp:249
Point2D< T > & operator+=(const Point2D< T > &point) noexcept
Adds and assigns to this point.
Definition point_2d.hpp:197
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T operator*(const Point2D< T > &lhs, const Point2D< T > &rhs)
Inner product of two point vectors.
Definition point_2d.hpp:312
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
void SetX(T x) noexcept
Sets the x-component of the point.
Definition point_2d.hpp:94
void SetY(T y)
Sets the y-component of the point.
Definition point_2d.hpp:114
Point2D(const T(&list)[N]) noexcept
Construct a point with an initializer list.
Definition point_2d.hpp:73
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:342
Point2D< T > operator*(const Point2D< T > &lhs, const T &rhs)
Multiply point with scalar.
Definition point_2d.hpp:327
bool operator!=(const Point2D< T > &other) const noexcept
Compares to an other point.
Definition point_2d.hpp:177
Point2D< T > operator-(const Point2D< T > &lhs, const Point2D< T > &rhs)
Subtracts two points.
Definition point_2d.hpp:297
T Length() const noexcept
Gets the length of the vector represented by this point object.
Definition point_2d.hpp:124
Angle Phi() const noexcept
Gets the orientation of the vector represented by this point object.
Definition point_2d.hpp:145
bool operator==(const Point2D< T > &other) const noexcept
Compares to an other point.
Definition point_2d.hpp:166
Point2D< T > operator+(const Point2D< T > &lhs, const Point2D< T > &rhs)
Add two points.
Definition point_2d.hpp:282
T & operator[](int index) noexcept
Index based element access.
Definition point_2d.hpp:261
Point2D< T > & operator-=(const Point2D< T > &point) noexcept
Subtracts and assigns to this point.
Definition point_2d.hpp:210
void SetLength(T length) noexcept
Sets the length of the vector represented by this point object.
Definition point_2d.hpp:135
Point2D(Angle phi, T r) noexcept
Create a PointD vector from radial coordinates.
Definition point_2d.hpp:62
void SetPhi(Angle phi) noexcept
Sets the orientation of the vector represented by this point object.
Definition point_2d.hpp:155
T copy(T... args)
T end(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
double Sin(Angle angle) noexcept
Returns the sine of an angle.
Definition angle.hpp:429
Angle Atan2(double y, double x) noexcept
Returns the angle whose tangent is the quotient of two specified numbers.
Definition angle.hpp:390
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition point_2d.hpp:371
double Cos(Angle angle) noexcept
Returns the cosine of an angle.
Definition angle.hpp:403
Point2D< T > Vector2D
Alias for Point2D.
Definition point_2d.hpp:379
T round(T... args)