matrix_3d_h.hpp
1 #pragma once
2 
3 
4 
5 #include "_cexports/c_core_3d.h"
6 
7 #include "global.hpp"
8 
9 
10 #include "point_3d_h.hpp"
11 #include "matrix_3d.hpp"
12 
13 namespace Cvb
14 {
15 
16 
17 
18 CVB_BEGIN_INLINE_NS
19 
20 class Matrix3DH;
21 
23 
31 inline Matrix3DH operator+(const Matrix3DH & lhs, const Matrix3DH & rhs);
32 
33 
35 
43 inline Matrix3DH operator-(const Matrix3DH & lhs, const Matrix3DH & rhs);
44 
46 
49 class Matrix3DH final
50 {
51  public:
52 
54 
58  static Matrix3DH Identity() noexcept
59  {
60  return {{
61  1.0, 0.0, 0.0, 0.0,
62  0.0, 1.0, 0.0, 0.0,
63  0.0, 0.0, 1.0, 0.0,
64  0.0, 0.0, 0.0, 1.0
65  }};
66  }
67 
69 
72  Matrix3DH() noexcept = default;
73 
75 
79  template<std::size_t N>
80  Matrix3DH(const double(&list)[N]) noexcept
81  {
82  static_assert(N == 16, "CVB: Matrix3D must have 16 elements");
83  std::copy(std::begin(list), std::end(list), matrix_.data()->data());
84  }
85 
87 
91  Matrix3DH(const Matrix3D & matrix) noexcept
92  {
93  *this = {{
94  matrix[0][0], matrix[0][1], matrix[0][2], 0.0,
95  matrix[1][0], matrix[1][1], matrix[1][2], 0.0,
96  matrix[2][0], matrix[2][1], matrix[2][2], 0.0,
97  0.0, 0.0, 0.0, 1.0,
98  }};
99  }
100 
101 
102 
103 
105 
110  const double* operator[](int row) const noexcept
111  {
112  return matrix_[row].data();
113  }
114 
115 
117 
122  double* operator[](int row) noexcept
123  {
124  return matrix_[row].data();
125  }
126 
128 
134  const double& At(int row, int column) const noexcept
135  {
136  return (*this)[row][column];
137  }
138 
140 
146  double& At(int row, int column) noexcept
147  {
148  return (*this)[row][column];
149  }
150 
151 
152 
153 
154 
155 
157 
162  bool operator==(const Matrix3DH& matrix) const noexcept
163  {
164  return matrix_ == matrix.matrix_;
165  }
166 
168 
173  bool operator!=(const Matrix3DH& matrix) const noexcept
174  {
175  return !(*this == matrix);
176  }
177 
179 
184  Matrix3DH& operator +=(const Matrix3DH & matrix) noexcept
185  {
186  *this = *this + matrix;
187  return *this;
188  }
189 
191 
196  Matrix3DH& operator -=(const Matrix3DH & matrix) noexcept
197  {
198  *this = *this - matrix;
199  return *this;
200  }
201 
202 
203 
205 
210  Matrix3DH& operator *=(const double & value) noexcept
211  {
212  for (auto & line : matrix_)
213  for (auto & element : line)
214  element *= value;
215  return *this;
216  }
217 
219 
224  Matrix3DH& operator /=(const double & value) noexcept
225  {
226  for(auto & line : matrix_)
227  for(auto & element : line)
228  element /= value;
229  return *this;
230  }
231 
232 
233 
234  private:
235 
236  std::array<std::array<double, 4>, 4> matrix_{};
237 };
238 
239 
240 
241 
242 inline Matrix3DH operator+(const Matrix3DH & lhs, const Matrix3DH & rhs)
243 {
244  return
245  {{
246  lhs[0][0] + rhs[0][0], lhs[0][1] + rhs[0][1], lhs[0][2] + rhs[0][2], lhs[0][3] + rhs[0][3],
247  lhs[1][0] + rhs[1][0], lhs[1][1] + rhs[1][1], lhs[1][2] + rhs[1][2], lhs[1][3] + rhs[1][3],
248  lhs[2][0] + rhs[2][0], lhs[2][1] + rhs[2][1], lhs[2][2] + rhs[2][2], lhs[2][3] + rhs[2][3],
249  lhs[3][0] + rhs[3][0], lhs[3][1] + rhs[3][1], lhs[3][2] + rhs[3][2], lhs[3][3] + rhs[3][3],
250  }};
251 }
252 
253 
254 inline Matrix3DH operator-(const Matrix3DH & lhs, const Matrix3DH & rhs)
255 {
256  return
257  {{
258  lhs[0][0] - rhs[0][0], lhs[0][1] - rhs[0][1], lhs[0][2] - rhs[0][2], lhs[0][3] - rhs[0][3],
259  lhs[1][0] - rhs[1][0], lhs[1][1] - rhs[1][1], lhs[1][2] - rhs[1][2], lhs[1][3] - rhs[1][3],
260  lhs[2][0] - rhs[2][0], lhs[2][1] - rhs[2][1], lhs[2][2] - rhs[2][2], lhs[2][3] - rhs[2][3],
261  lhs[3][0] - rhs[3][0], lhs[3][1] - rhs[3][1], lhs[3][2] - rhs[3][2], lhs[3][3] - rhs[3][3],
262  }};
263 }
264 
265 
266 
268 
276 inline Point3DH<double> operator*(const Matrix3DH & lhs, const Point3DH<double> & rhs)
277 {
278  return
279  {{
280  lhs[0][0] * rhs[0] + lhs[0][1] * rhs[1] + lhs[0][2] * rhs[2] + lhs[0][3] * rhs[3],
281  lhs[1][0] * rhs[0] + lhs[1][1] * rhs[1] + lhs[1][2] * rhs[2] + lhs[1][3] * rhs[3],
282  lhs[2][0] * rhs[0] + lhs[2][1] * rhs[1] + lhs[2][2] * rhs[2] + lhs[2][3] * rhs[3],
283  lhs[3][0] * rhs[0] + lhs[3][1] * rhs[1] + lhs[3][2] * rhs[2] + lhs[3][3] * rhs[3],
284  }};
285 }
286 
288 
296 inline Matrix3DH operator*(const Matrix3DH & lhs, const double & rhs)
297 {
298  return
299  {{
300  lhs[0][0] * rhs, lhs[0][1] * rhs, lhs[0][2] * rhs, lhs[0][3] * rhs,
301  lhs[1][0] * rhs, lhs[1][1] * rhs, lhs[1][2] * rhs, lhs[1][3] * rhs,
302  lhs[2][0] * rhs, lhs[2][1] * rhs, lhs[2][2] * rhs, lhs[2][3] * rhs,
303  lhs[3][0] * rhs, lhs[3][1] * rhs, lhs[3][2] * rhs, lhs[3][3] * rhs,
304  }};
305 }
306 
308 
316 inline Matrix3DH operator*(const double & lhs, const Matrix3DH & rhs)
317 {
318  return rhs * lhs;
319 }
320 
322 
330 inline Matrix3DH operator/(const Matrix3DH & lhs, const double & rhs)
331 {
332  return
333  {{
334  lhs[0][0] / rhs, lhs[0][1] / rhs, lhs[0][2] / rhs, lhs[0][3] / rhs,
335  lhs[1][0] / rhs, lhs[1][1] / rhs, lhs[1][2] / rhs, lhs[1][3] / rhs,
336  lhs[2][0] / rhs, lhs[2][1] / rhs, lhs[2][2] / rhs, lhs[2][3] / rhs,
337  lhs[3][0] / rhs, lhs[3][1] / rhs, lhs[3][2] / rhs, lhs[3][3] / rhs
338  }};
339 }
340 
341 
342 
343 
344 
345 
346 CVB_END_INLINE_NS
347 
348 }
AffineMatrix2D operator+(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Add two affine matrices.
Definition: affine_matrix_2d.hpp:217
Double precision row-major 4x4 matrix.
Definition: matrix_3d_h.hpp:49
double * operator[](int row) noexcept
Index based element access.
Definition: matrix_3d_h.hpp:122
bool operator==(const Matrix3DH &matrix) const noexcept
Compares to an other matrix.
Definition: matrix_3d_h.hpp:162
Matrix3DH & operator/=(const double &value) noexcept
Divides each element of this matrix by the given value.
Definition: matrix_3d_h.hpp:224
double & At(int row, int column) noexcept
Index based element access.
Definition: matrix_3d_h.hpp:146
AffineMatrix2D operator/(const AffineMatrix2D &lhs, const double &rhs)
Divide affine matrix by scalar.
Definition: affine_matrix_2d.hpp:301
Double precision 3x3 matrix class.
Definition: matrix_3d.hpp:58
Matrix3DH & operator+=(const Matrix3DH &matrix) noexcept
Adds and assigns to this matrix.
Definition: matrix_3d_h.hpp:184
Root namespace for the Image Manager interface.
Definition: version.hpp:11
const double * operator[](int row) const noexcept
Index based element access.
Definition: matrix_3d_h.hpp:110
const double & At(int row, int column) const noexcept
Index based element access.
Definition: matrix_3d_h.hpp:134
AffineMatrix2D operator *(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Multiply two affine matrices.
Definition: affine_matrix_2d.hpp:245
Matrix3DH() noexcept=default
Default constructor for empty matrix.
bool operator!=(const Matrix3DH &matrix) const noexcept
Compares to an other matrix.
Definition: matrix_3d_h.hpp:173
Matrix3DH & operator -=(const Matrix3DH &matrix) noexcept
Subtracts and assigns to this matrix.
Definition: matrix_3d_h.hpp:196
STL class.
static Matrix3DH Identity() noexcept
The identity element.
Definition: matrix_3d_h.hpp:58
Matrix3DH(const Matrix3D &matrix) noexcept
Create a homogeneous matrix from a matrix.
Definition: matrix_3d_h.hpp:91
Multi-purpose 3D vector class (homogeneous).
Definition: point_3d_h.hpp:21
AffineMatrix2D operator-(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Subtract two affine matrices.
Definition: affine_matrix_2d.hpp:231
Matrix3DH & operator *=(const double &value) noexcept
Multiplies and assigns to this matrix.
Definition: matrix_3d_h.hpp:210