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
13 CVB_BEGIN_INLINE_NS
14
15#pragma pack(push, 4)
16
18
20 template <class T, class ENABLE = void>
21 class 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 template <class T>
29 class Point3DH<T, typename EnableIfArithmetic<T>::type> final
30 {
31
32#endif
33
34 public:
35 using ValueType = T;
36
38
41 Point3DH() noexcept = default;
42
44
50 Point3DH(T x, T y, T z) noexcept
51 : x_(x)
52 , y_(y)
53 , z_(z)
54 {
55 }
56
58
65 Point3DH(T x, T y, T z, T w) noexcept
66 : x_(x)
67 , y_(y)
68 , z_(z)
69 , w_(w)
70 {
71 }
72
74
78 explicit Point3DH(Point3D<T> point3d) noexcept
79 : Point3DH(point3d.X(), point3d.Y(), point3d.Z())
80 {
81 }
82
84
88 template <std::size_t N>
89 Point3DH(const T (&list)[N]) noexcept // NOLINT
90 {
91 static_assert(N == 4, "CVB: Point3DH must have 4 elements");
92 std::copy(std::begin(list), std::end(list), &x_);
93 }
94
96
100 T X() const noexcept
101 {
102 return x_;
103 }
104
106
110 void SetX(T x) noexcept
111 {
112 x_ = x;
113 }
114
116
120 T Y() const noexcept
121 {
122 return y_;
123 }
124
126
130 void SetY(T y)
131 {
132 y_ = y;
133 }
134
136
140 T Z() const noexcept
141 {
142 return z_;
143 }
144
146
150 void SetZ(T z)
151 {
152 z_ = z;
153 }
154
156
160 T W() const noexcept
161 {
162 return w_;
163 }
164
166
170 void SetW(T w)
171 {
172 w_ = w;
173 }
174
176
181 void Normalize() noexcept
182 {
183 x_ /= w_;
184 y_ /= w_;
185 z_ /= w_;
186 w_ = 1.0;
187 }
188
190
195 bool operator==(const Point3DH<T> &other) const noexcept
196 {
197 return (x_ == other.x_ && y_ == other.y_ && z_ == other.z_ && w_ == other.w_);
198 }
199
201
206 bool operator!=(const Point3DH<T> &other) const noexcept
207 {
208 return !(*this == other);
209 }
210
212 template <class C>
213 explicit operator Point3DH<C>() const noexcept
214 {
215 static_assert(std::is_floating_point<C>::value, "CVB: Unsupported data type - must be floating point!");
216 return Point3DH<C>(static_cast<C>(X()), static_cast<C>(Y()), static_cast<C>(Z()), static_cast<C>(W()));
217 }
218
220
225 Point3DH<T> &operator+=(const Point3DH<T> &point) noexcept
226 {
227 x_ += point.x_;
228 y_ += point.y_;
229 z_ += point.z_;
230 w_ += point.w_;
231 return *this;
232 }
233
235
240 Point3DH<T> &operator-=(const Point3DH<T> &point) noexcept
241 {
242 x_ -= point.x_;
243 y_ -= point.y_;
244 z_ -= point.z_;
245 w_ -= point.w_;
246 return *this;
247 }
248
250
255 Point3DH<T> &operator*=(const T &value) noexcept
256 {
257 x_ *= value;
258 y_ *= value;
259 z_ *= value;
260 w_ *= value;
261 return *this;
262 }
263
265
270 Point3DH<T> &operator/=(const T &value) noexcept
271 {
272 x_ /= value;
273 y_ /= value;
274 z_ /= value;
275 w_ /= value;
276 return *this;
277 }
278
280
285 const T &operator[](int index) const noexcept
286 {
287 assert(index < 4 && index >= 0); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
288 return *(&x_ + index);
289 }
290
292
297 T &operator[](int index) noexcept
298 {
299 assert(index < 4 && index >= 0); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
300 return *(&x_ + index);
301 }
302
303 private:
304 T x_ = static_cast<T>(0.0);
305 T y_ = static_cast<T>(0.0);
306 T z_ = static_cast<T>(0.0);
307 T w_ = static_cast<T>(1.0);
308 };
309
311
319 template <class T>
320 inline Point3DH<T> operator+(const Point3DH<T> &lhs, const Point3DH<T> &rhs)
321 {
322 return Point3DH<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y(), lhs.Z() + rhs.Z(), lhs.W() + rhs.W());
323 }
324
326
334 template <class T>
335 inline Point3DH<T> operator-(const Point3DH<T> &lhs, const Point3DH<T> &rhs)
336 {
337 return Point3DH<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y(), lhs.Z() - rhs.Z(), lhs.W() - rhs.W());
338 }
339
341
349 template <class T>
350 inline T operator*(const Point3DH<T> &lhs, const Point3DH<T> &rhs)
351 {
352 return lhs.X() * rhs.X() + lhs.Y() * rhs.Y() + lhs.Z() * rhs.Z() + lhs.W() * rhs.W();
353 }
354
356
364 template <class T>
365 inline Point3DH<T> operator*(const Point3DH<T> &lhs, const T &rhs)
366 {
367 return Point3DH<T>(lhs.X() * rhs, lhs.Y() * rhs, lhs.Z() * rhs, lhs.W() * rhs);
368 }
369
371
379 template <class T>
380 inline Point3DH<T> operator*(const T &lhs, const Point3DH<T> &rhs)
381 {
382 return rhs * lhs;
383 }
384
386
394 template <class T>
395 inline Point3DH<T> operator/(const Point3DH<T> &lhs, const T &rhs)
396 {
397 return Point3DH<T>(lhs.X() / rhs, lhs.Y() / rhs, lhs.Z() / rhs, lhs.W() / rhs);
398 }
399
401 template <class T>
403
404#pragma pack(pop)
405
406 CVB_END_INLINE_NS
407
408} // namespace Cvb
T begin(T... args)
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:365
T operator*(const Point3DH< T > &lhs, const Point3DH< T > &rhs)
Inner product of two point vectors.
Definition point_3d_h.hpp:350
Point3DH< T > & operator*=(const T &value) noexcept
Multiplies by a scalar and assigns to this point.
Definition point_3d_h.hpp:255
const T & operator[](int index) const noexcept
Index based element access.
Definition point_3d_h.hpp:285
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:65
T X() const noexcept
Gets the x-component of the point.
Definition point_3d_h.hpp:100
T W() const noexcept
Gets the w-component of the point.
Definition point_3d_h.hpp:160
Point3DH< T > & operator-=(const Point3DH< T > &point) noexcept
Subtracts and assigns to this point.
Definition point_3d_h.hpp:240
T Y() const noexcept
Gets the y-component of the point.
Definition point_3d_h.hpp:120
void SetX(T x) noexcept
Sets the x-component of the point.
Definition point_3d_h.hpp:110
Point3DH< T > & operator/=(const T &value) noexcept
Divide by a scalar and assigns to this point.
Definition point_3d_h.hpp:270
void SetY(T y)
Sets the y-component of the point.
Definition point_3d_h.hpp:130
Point3DH< T > operator-(const Point3DH< T > &lhs, const Point3DH< T > &rhs)
Subtracts two points.
Definition point_3d_h.hpp:335
Point3DH< T > operator+(const Point3DH< T > &lhs, const Point3DH< T > &rhs)
Add two points.
Definition point_3d_h.hpp:320
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:140
Point3DH(const T(&list)[N]) noexcept
Construct a point with an initializer list.
Definition point_3d_h.hpp:89
Point3DH< T > & operator+=(const Point3DH< T > &point) noexcept
Adds and assigns to this point.
Definition point_3d_h.hpp:225
void SetW(T w)
Sets the w-component of the point.
Definition point_3d_h.hpp:170
Point3DH(Point3D< T > point3d) noexcept
Create a homogeneous point from a point.
Definition point_3d_h.hpp:78
bool operator==(const Point3DH< T > &other) const noexcept
Compares to an other point.
Definition point_3d_h.hpp:195
void Normalize() noexcept
Normalize the x-, y-, and z- with the w-component.
Definition point_3d_h.hpp:181
T & operator[](int index) noexcept
Index based element access.
Definition point_3d_h.hpp:297
void SetZ(T z)
Sets the z-component of the point.
Definition point_3d_h.hpp:150
Point3DH< T > operator/(const Point3DH< T > &lhs, const T &rhs)
Divide point by scalar.
Definition point_3d_h.hpp:395
bool operator!=(const Point3DH< T > &other) const noexcept
Compares to an other point.
Definition point_3d_h.hpp:206
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:380
T copy(T... args)
T end(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
Point3DH< T > Vector3DH
Alias for Point3D.
Definition point_3d_h.hpp:402