CVB++ 15.0
point_3d_h.hpp
1#pragma once
2
3#include <cmath>
4
5#include "global.hpp"
6
7#include "angle.hpp"
8#include "point_3d.hpp"
9
10namespace Cvb
11{
12
13CVB_BEGIN_INLINE_NS
14
15#pragma pack ( push, 4 )
16
18
20template< class T, class ENABLE = void>
21class Point3DH 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
29template <class T>
30class Point3DH<T,
31 typename EnableIfArithmetic<T>::type> final
32{
33
34#endif
35
36 public:
37
38 using ValueType = T;
39
41
44 Point3DH() noexcept = default;
45
46
48
54 Point3DH(T x, T y, T z) noexcept
55 : x_(x)
56 , y_(y)
57 , z_(z)
58 {
59 }
60
62
69 Point3DH(T x, T y, T z, T w) noexcept
70 : x_(x)
71 , y_(y)
72 , z_(z)
73 , w_(w)
74 {
75 }
76
77
79
83 explicit Point3DH(Point3D<T> point3d) noexcept
84 : Point3DH(point3d.X(), point3d.Y(), point3d.Z())
85 {
86 }
87
89
93 template<std::size_t N>
94 Point3DH(const T(&list)[N]) noexcept // NOLINT
95 {
96 static_assert(N == 4, "CVB: Point3DH must have 4 elements");
97 std::copy(std::begin(list), std::end(list), &x_);
98 }
99
100
101
103
107 T X() const noexcept
108 {
109 return x_;
110 }
111
113
117 void SetX(T x) noexcept
118 {
119 x_ = x;
120 }
121
123
127 T Y() const noexcept
128 {
129 return y_;
130 }
131
133
137 void SetY(T y)
138 {
139 y_ = y;
140 }
141
143
147 T Z() const noexcept
148 {
149 return z_;
150 }
151
153
157 void SetZ(T z)
158 {
159 z_ = z;
160 }
161
163
167 T W() const noexcept
168 {
169 return w_;
170 }
171
173
177 void SetW(T w)
178 {
179 w_ = w;
180 }
181
182
184
189 void Normalize() noexcept
190 {
191 x_ /= w_;
192 y_ /= w_;
193 z_ /= w_;
194 w_ = 1.0;
195 }
196
197
199
204 bool operator==(const Point3DH<T>& other) const noexcept
205 {
206 return (x_ == other.x_ && y_ == other.y_ && z_ == other.z_ && w_ == other.w_);
207 }
208
209
211
216 bool operator!=(const Point3DH<T>& other) const noexcept
217 {
218 return !(*this == other);
219 }
220
222 template <class C>
223 explicit operator Point3DH<C>() const noexcept
224 {
225 static_assert(std::is_floating_point<C>::value, "CVB: Unsupported data type - must be floating point!");
226 return Point3DH<C>(static_cast<C>(X()), static_cast<C>(Y()), static_cast<C>(Z()), static_cast<C>(W()));
227 }
228
230
235 Point3DH<T>& operator +=(const Point3DH<T> & point) noexcept
236 {
237 x_ += point.x_;
238 y_ += point.y_;
239 z_ += point.z_;
240 w_ += point.w_;
241 return *this;
242 }
243
245
250 Point3DH<T>& operator -=(const Point3DH<T> & point) noexcept
251 {
252 x_ -= point.x_;
253 y_ -= point.y_;
254 z_ -= point.z_;
255 w_ -= point.w_;
256 return *this;
257 }
258
260
265 Point3DH<T>& operator *=(const T & value) noexcept
266 {
267 x_ *= value;
268 y_ *= value;
269 z_ *= value;
270 w_ *= value;
271 return *this;
272 }
273
275
280 Point3DH<T>& operator /=(const T & value) noexcept
281 {
282 x_ /= value;
283 y_ /= value;
284 z_ /= value;
285 w_ /= value;
286 return *this;
287 }
288
289
291
296 const T& operator[](int index) const noexcept
297 {
298 assert(index < 4 && index >= 0); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
299 return *(&x_ + index);
300 }
301
303
308 T& operator[](int index) noexcept
309 {
310 assert(index < 4 && index >= 0); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
311 return *(&x_ + index);
312 }
313
314 private:
315
316 T x_ = static_cast<T>(0.0);
317 T y_ = static_cast<T>(0.0);
318 T z_ = static_cast<T>(0.0);
319 T w_ = static_cast<T>(1.0);
320
321
322};
323
325
333template <class T>
334inline Point3DH<T> operator+(const Point3DH<T> & lhs, const Point3DH<T> & rhs)
335{
336 return Point3DH<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y(), lhs.Z() + rhs.Z(), lhs.W() + rhs.W());
337}
338
340
348template <class T>
349inline Point3DH<T> operator-(const Point3DH<T> & lhs, const Point3DH<T> & rhs)
350{
351 return Point3DH<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y(), lhs.Z() - rhs.Z(), lhs.W() - rhs.W());
352}
353
355
363template <class T>
364inline T operator*(const Point3DH<T> & lhs, const Point3DH<T> & rhs)
365{
366 return lhs.X() * rhs.X() + lhs.Y() * rhs.Y() + lhs.Z() * rhs.Z() + lhs.W() * rhs.W();
367}
368
370
378template <class T>
379inline Point3DH<T> operator*(const Point3DH<T> & lhs, const T & rhs)
380{
381 return Point3DH<T>(lhs.X() * rhs, lhs.Y() * rhs, lhs.Z() * rhs, lhs.W() * rhs);
382}
383
385
393template <class T>
394inline Point3DH<T> operator*(const T & lhs, const Point3DH<T> & rhs)
395{
396 return rhs * lhs;
397}
398
400
408template <class T>
409inline Point3DH<T> operator/(const Point3DH<T> & lhs, const T & rhs)
410{
411 return Point3DH<T>(lhs.X() / rhs, lhs.Y() / rhs, lhs.Z() / rhs, lhs.W() / rhs);
412}
413
414
416template<class T>
418
419#pragma pack ( pop )
420
421
422CVB_END_INLINE_NS
423
424
425}
Multi-purpose 3D vector class (homogeneous).
Definition: point_3d_h.hpp:22
Point3DH< T > operator*(const Point3DH< T > &lhs, const T &rhs)
Multiply point with scalar.
Definition: point_3d_h.hpp:379
T operator*(const Point3DH< T > &lhs, const Point3DH< T > &rhs)
Inner product of two point vectors.
Definition: point_3d_h.hpp:364
Point3DH< T > & operator*=(const T &value) noexcept
Multiplies by a scalar and assigns to this point.
Definition: point_3d_h.hpp:265
const T & operator[](int index) const noexcept
Index based element access.
Definition: point_3d_h.hpp:296
Point3DH(T x, T y, T z, T w) noexcept
Create a point from the x-, y-, z- and w-component.
Definition: point_3d_h.hpp:69
T X() const noexcept
Gets the x-component of the point.
Definition: point_3d_h.hpp:107
T W() const noexcept
Gets the w-component of the point.
Definition: point_3d_h.hpp:167
Point3DH< T > & operator-=(const Point3DH< T > &point) noexcept
Subtracts and assigns to this point.
Definition: point_3d_h.hpp:250
T Y() const noexcept
Gets the y-component of the point.
Definition: point_3d_h.hpp:127
void SetX(T x) noexcept
Sets the x-component of the point.
Definition: point_3d_h.hpp:117
Point3DH< T > & operator/=(const T &value) noexcept
Divide by a scalar and assigns to this point.
Definition: point_3d_h.hpp:280
void SetY(T y)
Sets the y-component of the point.
Definition: point_3d_h.hpp:137
Point3DH< T > operator-(const Point3DH< T > &lhs, const Point3DH< T > &rhs)
Subtracts two points.
Definition: point_3d_h.hpp:349
Point3DH< T > operator+(const Point3DH< T > &lhs, const Point3DH< T > &rhs)
Add two points.
Definition: point_3d_h.hpp:334
Point3DH() noexcept=default
Creates a default point at (0, 0).
T Z() const noexcept
Gets the z-component of the point.
Definition: point_3d_h.hpp:147
Point3DH(const T(&list)[N]) noexcept
Construct a point with an initializer list.
Definition: point_3d_h.hpp:94
Point3DH< T > & operator+=(const Point3DH< T > &point) noexcept
Adds and assigns to this point.
Definition: point_3d_h.hpp:235
void SetW(T w)
Sets the w-component of the point.
Definition: point_3d_h.hpp:177
Point3DH(Point3D< T > point3d) noexcept
Create a homogeneous point from a point.
Definition: point_3d_h.hpp:83
bool operator==(const Point3DH< T > &other) const noexcept
Compares to an other point.
Definition: point_3d_h.hpp:204
void Normalize() noexcept
Normalize the x-, y-, and z- with the w-component.
Definition: point_3d_h.hpp:189
T & operator[](int index) noexcept
Index based element access.
Definition: point_3d_h.hpp:308
void SetZ(T z)
Sets the z-component of the point.
Definition: point_3d_h.hpp:157
Point3DH< T > operator/(const Point3DH< T > &lhs, const T &rhs)
Divide point by scalar.
Definition: point_3d_h.hpp:409
bool operator!=(const Point3DH< T > &other) const noexcept
Compares to an other point.
Definition: point_3d_h.hpp:216
Multi-purpose 3D vector class.
Definition: point_3d.hpp:22
Point3DH< T > operator*(const T &lhs, const Point3DH< T > &rhs)
Multiply scalar with point.
Definition: point_3d_h.hpp:394
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24