CVB++ 15.0
point_3d.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
15#pragma pack(push, 4)
16
18
20 template <class T, class ENABLE = void>
21 class Point3D final
22 {
23
24#ifndef CVB_DOXYGEN
25 static_assert(std::is_arithmetic<T>::value, "CVB: Unsupported data type - must be arithmetic!");
26 };
27
28 template <class T>
29 class Point3D<T, typename EnableIfArithmetic<T>::type> final
30 {
31
32#endif
33
34 public:
35 using ValueType = T;
36
38
41 Point3D() noexcept = default;
42
44
50 Point3D(T x, T y, T z) noexcept
51 : x_(x)
52 , y_(y)
53 , z_(z)
54 {
55 }
56
58
62 template <std::size_t N>
63 Point3D(const T (&list)[N]) noexcept // NOLINT
64 {
65 static_assert(N == 3, "CVB: Point3D must have 3 elements");
66 std::copy(std::begin(list), std::end(list), &x_);
67 }
68
70
74 T X() const noexcept
75 {
76 return x_;
77 }
78
80
84 void SetX(T x) noexcept
85 {
86 x_ = x;
87 }
88
90
94 T Y() const noexcept
95 {
96 return y_;
97 }
98
100
104 void SetY(T y)
105 {
106 y_ = y;
107 }
108
110
114 T Z() const noexcept
115 {
116 return z_;
117 }
118
120
124 void SetZ(T z)
125 {
126 z_ = z;
127 }
128
130
134 T Length() const noexcept
135 {
136 return sqrt(x_ * x_ + y_ * y_ + z_ * z_);
137 }
138
140
145 bool operator==(const Point3D<T> &other) const noexcept
146 {
147 return (x_ == other.x_ && y_ == other.y_ && z_ == other.z_);
148 }
149
151
156 bool operator!=(const Point3D<T> &other) const noexcept
157 {
158 return !(*this == other);
159 }
160
162 template <class C>
163 explicit operator Point3D<C>() const noexcept
164 {
165 static_assert(std::is_floating_point<C>::value, "CVB: Unsupported data type - must be floating point!");
166 return Point3D<C>(static_cast<C>(X()), static_cast<C>(Y()), static_cast<C>(Z()));
167 }
168
170
175 Point3D<T> &operator+=(const Point3D<T> &point) noexcept
176 {
177 x_ += point.x_;
178 y_ += point.y_;
179 z_ += point.z_;
180 return *this;
181 }
182
184
189 Point3D<T> &operator-=(const Point3D<T> &point) noexcept
190 {
191 x_ -= point.x_;
192 y_ -= point.y_;
193 z_ -= point.z_;
194 return *this;
195 }
196
198
203 Point3D<T> &operator*=(const T &value) noexcept
204 {
205 x_ *= value;
206 y_ *= value;
207 z_ *= value;
208 return *this;
209 }
210
212
217 Point3D<T> &operator/=(const T &value) noexcept
218 {
219 x_ /= value;
220 y_ /= value;
221 z_ /= value;
222 return *this;
223 }
224
226
231 const T &operator[](int index) const noexcept
232 {
233 assert(index < 3 && index >= 0); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
234 return *(&x_ + index);
235 }
236
238
243 T &operator[](int index) noexcept
244 {
245 assert(index < 3 && index >= 0); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
246 return *(&x_ + index);
247 }
248
249 private:
250 T x_ = static_cast<T>(0.0);
251 T y_ = static_cast<T>(0.0);
252 T z_ = static_cast<T>(0.0);
253 };
254
256
264 template <class T>
265 inline Point3D<T> operator+(const Point3D<T> &lhs, const Point3D<T> &rhs)
266 {
267 return Point3D<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y(), lhs.Z() + rhs.Z());
268 }
269
271
279 template <class T>
280 inline Point3D<T> operator-(const Point3D<T> &lhs, const Point3D<T> &rhs)
281 {
282 return Point3D<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y(), lhs.Z() - rhs.Z());
283 }
284
286
294 template <class T>
295 inline T operator*(const Point3D<T> &lhs, const Point3D<T> &rhs)
296 {
297 return lhs.X() * rhs.X() + lhs.Y() * rhs.Y() + lhs.Z() * rhs.Z();
298 }
299
301
309 template <class T>
310 inline Point3D<T> operator*(const Point3D<T> &lhs, const T &rhs)
311 {
312 return Point3D<T>(lhs.X() * rhs, lhs.Y() * rhs, lhs.Z() * rhs);
313 }
314
316
324 template <class T>
325 inline Point3D<T> operator*(const T &lhs, const Point3D<T> &rhs)
326 {
327 return rhs * lhs;
328 }
329
331
339 template <class T>
340 inline Point3D<T> operator/(const Point3D<T> &lhs, const T &rhs)
341 {
342 return Point3D<T>(lhs.X() / rhs, lhs.Y() / rhs, lhs.Z() / rhs);
343 }
344
346
353 template <class T>
354 inline Point3D<int> Round(const Point3D<T> &rhs) noexcept
355 {
356 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
357 return Point3D<int>(static_cast<int>(std::round(rhs.X())), static_cast<int>(std::round(rhs.Y())),
358 static_cast<int>(std::round(rhs.Z())));
359 }
360
362 template <class T>
364
365#pragma pack(pop)
366
367 CVB_END_INLINE_NS
368
369} // namespace Cvb
Multi-purpose 3D vector class.
Definition: point_3d.hpp:22
const T & operator[](int index) const noexcept
Index based element access.
Definition: point_3d.hpp:231
T X() const noexcept
Get the x component of the point.
Definition: point_3d.hpp:74
bool operator!=(const Point3D< T > &other) const noexcept
Compares to an other point.
Definition: point_3d.hpp:156
T Y() const noexcept
Get the y component of the point.
Definition: point_3d.hpp:94
void SetX(T x) noexcept
Set the x component of the point.
Definition: point_3d.hpp:84
Point3D< T > operator/(const Point3D< T > &lhs, const T &rhs)
Divide point by scalar.
Definition: point_3d.hpp:340
void SetY(T y)
Set the y component of the point.
Definition: point_3d.hpp:104
Point3D< T > operator*(const Point3D< T > &lhs, const T &rhs)
Multiply point with scalar.
Definition: point_3d.hpp:310
T Z() const noexcept
Get the z component of the point.
Definition: point_3d.hpp:114
Point3D< T > & operator+=(const Point3D< T > &point) noexcept
Adds and assigns to this point.
Definition: point_3d.hpp:175
Point3D< T > & operator-=(const Point3D< T > &point) noexcept
Subtracts and assigns to this point.
Definition: point_3d.hpp:189
bool operator==(const Point3D< T > &other) const noexcept
Compares to an other point.
Definition: point_3d.hpp:145
Point3D< T > operator*(const T &lhs, const Point3D< T > &rhs)
Multiply scalar with point.
Definition: point_3d.hpp:325
Point3D(const T(&list)[N]) noexcept
Construct a point with an initializer list.
Definition: point_3d.hpp:63
T Length() const noexcept
Gets the length of this point.
Definition: point_3d.hpp:134
Point3D< T > operator-(const Point3D< T > &lhs, const Point3D< T > &rhs)
Subtracts two points.
Definition: point_3d.hpp:280
Point3D< T > & operator*=(const T &value) noexcept
Multiplies by a scalar and assigns to this point.
Definition: point_3d.hpp:203
T operator*(const Point3D< T > &lhs, const Point3D< T > &rhs)
Inner product of two point vectors.
Definition: point_3d.hpp:295
Point3D< T > operator+(const Point3D< T > &lhs, const Point3D< T > &rhs)
Add two points.
Definition: point_3d.hpp:265
Point3D< T > & operator/=(const T &value) noexcept
Divide by a scalar and assigns to this point.
Definition: point_3d.hpp:217
T & operator[](int index) noexcept
Index based element access.
Definition: point_3d.hpp:243
Point3D() noexcept=default
Creates a default point at (0, 0).
void SetZ(T z)
Set the z component of the point.
Definition: point_3d.hpp:124
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:371