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
11CVB_BEGIN_INLINE_NS
12
13
15
17class AffineMatrix2D final
18{
19 public:
20
22
26 static AffineMatrix2D Identity() noexcept
27 {
29 }
30
32
35 AffineMatrix2D() noexcept = default;
36
38
43 AffineMatrix2D(Cvb::Matrix2D matrix, Vector2D<double> translation) noexcept
44 : translation_(translation)
45 , matrix_(matrix)
46 {
47 }
48
50
54 Cvb::Matrix2D Matrix() const noexcept
55 {
56 return matrix_;
57 }
58
60
64 void SetMatrix(Cvb::Matrix2D matrix) noexcept
65 {
66 matrix_ = matrix;
67 }
68
70
75 {
76 return translation_;
77 }
78
79
81
85 void SetTranslation(Vector2D<double> translation) noexcept
86 {
87 translation_ = translation;
88 }
89
91
96 void Invert()
97 {
98 matrix_.Invert();
99 translation_ = matrix_ * (-1.0 * translation_);
100 }
101
103
110 {
111 auto matrix = *this;
112 matrix.Invert();
113 return matrix;
114 }
115
117
121 bool IsTranslation() const noexcept
122 {
123 return matrix_ == Cvb::Matrix2D::Identity();
124 }
125
127
132 bool operator==(const AffineMatrix2D& affineMatrix) const noexcept
133 {
134 return translation_ == affineMatrix.translation_
135 && matrix_ == affineMatrix.matrix_;
136 }
137
139
144 bool operator!=(const AffineMatrix2D& affineMatrix) const noexcept
145 {
146 return !(*this == affineMatrix);
147 }
148
149
151
156 AffineMatrix2D& operator +=(const AffineMatrix2D & affineMatrix) noexcept
157 {
158 matrix_ += affineMatrix.matrix_;
159 translation_ += affineMatrix.translation_;
160 return *this;
161 }
162
164
169 AffineMatrix2D& operator -=(const AffineMatrix2D & affineMatrix) noexcept
170 {
171 matrix_ -= affineMatrix.matrix_;
172 translation_ -= affineMatrix.translation_;
173 return *this;
174 }
175
177
182 AffineMatrix2D& operator *=(const AffineMatrix2D & affineMatrix) noexcept
183 {
184 *this = AffineMatrix2D(matrix_ * affineMatrix.matrix_, matrix_ * affineMatrix.translation_ + translation_);
185 return *this;
186 }
187
189
194 AffineMatrix2D& operator *=(const double & value) noexcept
195 {
196 matrix_ *= value;
197 translation_ *= value;
198 return *this;
199 }
200
202
207 AffineMatrix2D& operator /=(const double & value) noexcept
208 {
209 matrix_ /= value;
210 translation_ /= value;
211 return *this;
212 }
213
214 private:
215
216 Vector2D<double> translation_;
217 Cvb::Matrix2D matrix_;
218
219};
220
221
223
231inline AffineMatrix2D operator+(const AffineMatrix2D & lhs, const AffineMatrix2D & rhs) noexcept
232{
233 return AffineMatrix2D(lhs.Matrix() + rhs.Matrix(), lhs.Translation() + rhs.Translation());
234}
235
237
245inline AffineMatrix2D operator-(const AffineMatrix2D & lhs, const AffineMatrix2D & rhs) noexcept
246{
247 return AffineMatrix2D(lhs.Matrix() - rhs.Matrix(), lhs.Translation() - rhs.Translation());
248}
249
251
259inline AffineMatrix2D operator*(const AffineMatrix2D & lhs, const AffineMatrix2D & rhs) noexcept
260{
261 return AffineMatrix2D(lhs.Matrix() * rhs.Matrix(), lhs.Matrix() * rhs.Translation() + lhs.Translation());
262}
263
265
273inline Point2D<double> operator*(const AffineMatrix2D & lhs, const Point2D<double> & rhs) noexcept
274{
275 return lhs.Matrix() * rhs + lhs.Translation();
276}
277
279
287inline AffineMatrix2D operator*(const AffineMatrix2D & lhs, const double & rhs) noexcept
288{
289 return AffineMatrix2D(lhs.Matrix() * rhs, lhs.Translation() * rhs);
290}
291
293
301inline AffineMatrix2D operator*(const double & lhs, const AffineMatrix2D & rhs) noexcept
302{
303 return rhs * lhs;
304}
305
307
315inline AffineMatrix2D operator/(const AffineMatrix2D & lhs, const double & rhs)
316{
317 return AffineMatrix2D(lhs.Matrix() / rhs, lhs.Translation() / rhs);
318}
319
320CVB_END_INLINE_NS
321
322}
Compacted affine matrix describing the Common Vision Blox coordinate system.
Definition: affine_matrix_2d.hpp:18
AffineMatrix2D & operator+=(const AffineMatrix2D &affineMatrix) noexcept
Adds and assigns to this affine matrix.
Definition: affine_matrix_2d.hpp:156
Point2D< double > operator*(const AffineMatrix2D &lhs, const Point2D< double > &rhs) noexcept
Multiply affine matrix with 2D point.
Definition: affine_matrix_2d.hpp:273
AffineMatrix2D operator+(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Add two affine matrices.
Definition: affine_matrix_2d.hpp:231
AffineMatrix2D & operator/=(const double &value) noexcept
Divides each element of this affine matrix by the given value.
Definition: affine_matrix_2d.hpp:207
AffineMatrix2D operator*(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Multiply two affine matrices.
Definition: affine_matrix_2d.hpp:259
AffineMatrix2D operator/(const AffineMatrix2D &lhs, const double &rhs)
Divide affine matrix by scalar.
Definition: affine_matrix_2d.hpp:315
AffineMatrix2D & operator*=(const AffineMatrix2D &affineMatrix) noexcept
Multiplies and assigns to this affine matrix.
Definition: affine_matrix_2d.hpp:182
AffineMatrix2D operator*(const AffineMatrix2D &lhs, const double &rhs) noexcept
Multiply affine matrix with scalar.
Definition: affine_matrix_2d.hpp:287
Cvb::Matrix2D Matrix() const noexcept
Get the transformation part of the affine matrix.
Definition: affine_matrix_2d.hpp:54
void SetTranslation(Vector2D< double > translation) noexcept
Set the translation part of the affine matrix.
Definition: affine_matrix_2d.hpp:85
bool IsTranslation() const noexcept
Check if this affine matrix only describes a translation.
Definition: affine_matrix_2d.hpp:121
void SetMatrix(Cvb::Matrix2D matrix) noexcept
Set the transformation part of the affine matrix.
Definition: affine_matrix_2d.hpp:64
AffineMatrix2D operator*(const double &lhs, const AffineMatrix2D &rhs) noexcept
Multiply scalar with affine matrix .
Definition: affine_matrix_2d.hpp:301
bool operator!=(const AffineMatrix2D &affineMatrix) const noexcept
Compares to an other matrix.
Definition: affine_matrix_2d.hpp:144
AffineMatrix2D operator-(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Subtract two affine matrices.
Definition: affine_matrix_2d.hpp:245
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:169
AffineMatrix2D Inverse()
Gets the inverse of this affine transformation if possible.
Definition: affine_matrix_2d.hpp:109
static AffineMatrix2D Identity() noexcept
The identity element.
Definition: affine_matrix_2d.hpp:26
void Invert()
Invert this affine transformation in-place if possible.
Definition: affine_matrix_2d.hpp:96
bool operator==(const AffineMatrix2D &affineMatrix) const noexcept
Compares to an other matrix.
Definition: affine_matrix_2d.hpp:132
Vector2D< double > Translation() const noexcept
Get the translation part of the affine matrix.
Definition: affine_matrix_2d.hpp:74
Double precision 2x2 matrix class.
Definition: matrix_2d.hpp:16
static Matrix2D Identity() noexcept
The identity element.
Definition: matrix_2d.hpp:24
void Invert()
Inverts this matrix in-place if possible.
Definition: matrix_2d.hpp:257
Multi-purpose 2D vector class.
Definition: point_2d.hpp:20
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24