CVB++ 14.0
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
13namespace Cvb
14{
15
16
17
18CVB_BEGIN_INLINE_NS
19
20class Matrix3DH;
21
23
31inline Matrix3DH operator+(const Matrix3DH & lhs, const Matrix3DH & rhs);
32
33
35
43inline Matrix3DH operator-(const Matrix3DH & lhs, const Matrix3DH & rhs);
44
46
49class 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
237};
238
239
240
241
242inline 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
254inline 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
276inline 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
296inline 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
316inline Matrix3DH operator*(const double & lhs, const Matrix3DH & rhs)
317{
318 return rhs * lhs;
319}
320
322
330inline 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
346CVB_END_INLINE_NS
347
348}
Double precision row-major 4x4 matrix.
Definition: matrix_3d_h.hpp:50
const double & At(int row, int column) const noexcept
Index based element access.
Definition: matrix_3d_h.hpp:134
Matrix3DH operator*(const double &lhs, const Matrix3DH &rhs)
Multiply scalar with matrix .
Definition: matrix_3d_h.hpp:316
const double * operator[](int row) const noexcept
Index based element access.
Definition: matrix_3d_h.hpp:110
double * operator[](int row) noexcept
Index based element access.
Definition: matrix_3d_h.hpp:122
Matrix3DH(const Matrix3D &matrix) noexcept
Create a homogeneous matrix from a matrix.
Definition: matrix_3d_h.hpp:91
Matrix3DH & operator/=(const double &value) noexcept
Divides each element of this matrix by the given value.
Definition: matrix_3d_h.hpp:224
Matrix3DH() noexcept=default
Default constructor for empty matrix.
Matrix3DH operator/(const Matrix3DH &lhs, const double &rhs)
Divide matrix by scalar.
Definition: matrix_3d_h.hpp:330
Matrix3DH & operator-=(const Matrix3DH &matrix) noexcept
Subtracts and assigns to this matrix.
Definition: matrix_3d_h.hpp:196
Matrix3DH & operator+=(const Matrix3DH &matrix) noexcept
Adds and assigns to this matrix.
Definition: matrix_3d_h.hpp:184
Matrix3DH operator*(const Matrix3DH &lhs, const double &rhs)
Multiply matrix with scalar.
Definition: matrix_3d_h.hpp:296
bool operator!=(const Matrix3DH &matrix) const noexcept
Compares to an other matrix.
Definition: matrix_3d_h.hpp:173
double & At(int row, int column) noexcept
Index based element access.
Definition: matrix_3d_h.hpp:146
Matrix3DH & operator*=(const double &value) noexcept
Multiplies and assigns to this matrix.
Definition: matrix_3d_h.hpp:210
Point3DH< double > operator*(const Matrix3DH &lhs, const Point3DH< double > &rhs)
Multiply matrix with 3D point (homogeneous).
Definition: matrix_3d_h.hpp:276
static Matrix3DH Identity() noexcept
The identity element.
Definition: matrix_3d_h.hpp:58
bool operator==(const Matrix3DH &matrix) const noexcept
Compares to an other matrix.
Definition: matrix_3d_h.hpp:162
Double precision 3x3 matrix class.
Definition: matrix_3d.hpp:59
Multi-purpose 3D vector class (homogeneous).
Definition: point_3d_h.hpp:22
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
AffineMatrix2D operator+(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Add two affine matrices.
Definition: affine_matrix_2d.hpp:217
AffineMatrix2D operator-(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Subtract two affine matrices.
Definition: affine_matrix_2d.hpp:231