CVB++ 15.0
affine_matrix_2d.hpp
1#pragma once
2
3#include "global.hpp"
4
5#include "matrix_2d.hpp"
6#include "point_2d.hpp"
7
8namespace Cvb
9{
10
11 CVB_BEGIN_INLINE_NS
12
14
16 class AffineMatrix2D final
17 {
18 public:
20
28
30
33 AffineMatrix2D() noexcept = default;
34
36
41 AffineMatrix2D(Cvb::Matrix2D matrix, Vector2D<double> translation) noexcept
42 : translation_(translation)
43 , matrix_(matrix)
44 {
45 }
46
48
52 Cvb::Matrix2D Matrix() const noexcept
53 {
54 return matrix_;
55 }
56
58
62 void SetMatrix(Cvb::Matrix2D matrix) noexcept
63 {
64 matrix_ = matrix;
65 }
66
68
73 {
74 return translation_;
75 }
76
78
82 void SetTranslation(Vector2D<double> translation) noexcept
83 {
84 translation_ = translation;
85 }
86
88
93 void Invert()
94 {
95 matrix_.Invert();
96 translation_ = matrix_ * (-1.0 * translation_);
97 }
98
100
107 {
108 auto matrix = *this;
109 matrix.Invert();
110 return matrix;
111 }
112
114
118 bool IsTranslation() const noexcept
119 {
120 return matrix_ == Cvb::Matrix2D::Identity();
121 }
122
124
129 bool operator==(const AffineMatrix2D &affineMatrix) const noexcept
130 {
131 return translation_ == affineMatrix.translation_ && matrix_ == affineMatrix.matrix_;
132 }
133
135
140 bool operator!=(const AffineMatrix2D &affineMatrix) const noexcept
141 {
142 return !(*this == affineMatrix);
143 }
144
146
151 AffineMatrix2D &operator+=(const AffineMatrix2D &affineMatrix) noexcept
152 {
153 matrix_ += affineMatrix.matrix_;
154 translation_ += affineMatrix.translation_;
155 return *this;
156 }
157
159
164 AffineMatrix2D &operator-=(const AffineMatrix2D &affineMatrix) noexcept
165 {
166 matrix_ -= affineMatrix.matrix_;
167 translation_ -= affineMatrix.translation_;
168 return *this;
169 }
170
172
177 AffineMatrix2D &operator*=(const AffineMatrix2D &affineMatrix) noexcept
178 {
179 *this = AffineMatrix2D(matrix_ * affineMatrix.matrix_, matrix_ * affineMatrix.translation_ + translation_);
180 return *this;
181 }
182
184
189 AffineMatrix2D &operator*=(const double &value) noexcept
190 {
191 matrix_ *= value;
192 translation_ *= value;
193 return *this;
194 }
195
197
202 AffineMatrix2D &operator/=(const double &value) noexcept
203 {
204 matrix_ /= value;
205 translation_ /= value;
206 return *this;
207 }
208
209 private:
210 Vector2D<double> translation_;
211 Cvb::Matrix2D matrix_;
212 };
213
215
223 inline AffineMatrix2D operator+(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
224 {
225 return AffineMatrix2D(lhs.Matrix() + rhs.Matrix(), lhs.Translation() + rhs.Translation());
226 }
227
229
237 inline AffineMatrix2D operator-(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
238 {
239 return AffineMatrix2D(lhs.Matrix() - rhs.Matrix(), lhs.Translation() - rhs.Translation());
240 }
241
243
251 inline AffineMatrix2D operator*(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
252 {
253 return AffineMatrix2D(lhs.Matrix() * rhs.Matrix(), lhs.Matrix() * rhs.Translation() + lhs.Translation());
254 }
255
257
265 inline Point2D<double> operator*(const AffineMatrix2D &lhs, const Point2D<double> &rhs) noexcept
266 {
267 return lhs.Matrix() * rhs + lhs.Translation();
268 }
269
271
279 inline AffineMatrix2D operator*(const AffineMatrix2D &lhs, const double &rhs) noexcept
280 {
281 return AffineMatrix2D(lhs.Matrix() * rhs, lhs.Translation() * rhs);
282 }
283
285
293 inline AffineMatrix2D operator*(const double &lhs, const AffineMatrix2D &rhs) noexcept
294 {
295 return rhs * lhs;
296 }
297
299
307 inline AffineMatrix2D operator/(const AffineMatrix2D &lhs, const double &rhs)
308 {
309 return AffineMatrix2D(lhs.Matrix() / rhs, lhs.Translation() / rhs);
310 }
311
312 CVB_END_INLINE_NS
313
314} // namespace Cvb
AffineMatrix2D & operator+=(const AffineMatrix2D &affineMatrix) noexcept
Adds and assigns to this affine matrix.
Definition affine_matrix_2d.hpp:151
Point2D< double > operator*(const AffineMatrix2D &lhs, const Point2D< double > &rhs) noexcept
Multiply affine matrix with 2D point.
Definition affine_matrix_2d.hpp:265
AffineMatrix2D & operator*=(const double &value) noexcept
Multiplies and assigns to this affine matrix.
Definition affine_matrix_2d.hpp:189
AffineMatrix2D operator+(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Add two affine matrices.
Definition affine_matrix_2d.hpp:223
AffineMatrix2D & operator/=(const double &value) noexcept
Divides each element of this affine matrix by the given value.
Definition affine_matrix_2d.hpp:202
AffineMatrix2D operator*(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Multiply two affine matrices.
Definition affine_matrix_2d.hpp:251
AffineMatrix2D operator/(const AffineMatrix2D &lhs, const double &rhs)
Divide affine matrix by scalar.
Definition affine_matrix_2d.hpp:307
AffineMatrix2D & operator*=(const AffineMatrix2D &affineMatrix) noexcept
Multiplies and assigns to this affine matrix.
Definition affine_matrix_2d.hpp:177
AffineMatrix2D operator*(const AffineMatrix2D &lhs, const double &rhs) noexcept
Multiply affine matrix with scalar.
Definition affine_matrix_2d.hpp:279
Cvb::Matrix2D Matrix() const noexcept
Get the transformation part of the affine matrix.
Definition affine_matrix_2d.hpp:52
void SetTranslation(Vector2D< double > translation) noexcept
Set the translation part of the affine matrix.
Definition affine_matrix_2d.hpp:82
bool IsTranslation() const noexcept
Check if this affine matrix only describes a translation.
Definition affine_matrix_2d.hpp:118
void SetMatrix(Cvb::Matrix2D matrix) noexcept
Set the transformation part of the affine matrix.
Definition affine_matrix_2d.hpp:62
AffineMatrix2D operator*(const double &lhs, const AffineMatrix2D &rhs) noexcept
Multiply scalar with affine matrix .
Definition affine_matrix_2d.hpp:293
bool operator!=(const AffineMatrix2D &affineMatrix) const noexcept
Compares to an other matrix.
Definition affine_matrix_2d.hpp:140
AffineMatrix2D operator-(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Subtract two affine matrices.
Definition affine_matrix_2d.hpp:237
AffineMatrix2D() noexcept=default
Default constructor for empty affine matrix.
AffineMatrix2D & operator-=(const AffineMatrix2D &affineMatrix) noexcept
Subtracts and assigns to this affine matrix.
Definition affine_matrix_2d.hpp:164
AffineMatrix2D Inverse()
Gets the inverse of this affine transformation if possible.
Definition affine_matrix_2d.hpp:106
static AffineMatrix2D Identity() noexcept
The identity element.
Definition affine_matrix_2d.hpp:24
void Invert()
Invert this affine transformation in-place if possible.
Definition affine_matrix_2d.hpp:93
bool operator==(const AffineMatrix2D &affineMatrix) const noexcept
Compares to an other matrix.
Definition affine_matrix_2d.hpp:129
Vector2D< double > Translation() const noexcept
Get the translation part of the affine matrix.
Definition affine_matrix_2d.hpp:72
Double precision 2x2 matrix class.
Definition matrix_2d.hpp:16
static Matrix2D Identity() noexcept
The identity element.
Definition matrix_2d.hpp:23
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
Point2D< T > Vector2D
Alias for Point2D.
Definition point_2d.hpp:379