affine_matrix_2d.hpp
1 #pragma once
2 
3 #include "global.hpp"
4 
5 #include "matrix_2d.hpp"
6 #include "point_2d.hpp"
7 
8 namespace Cvb
9 {
10 
11 CVB_BEGIN_INLINE_NS
12 
13 
15 
17 class 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 
74  Vector2D<double> Translation() const noexcept
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 
107  bool IsTranslation() const noexcept
108  {
109  return matrix_ == Cvb::Matrix2D::Identity();
110  }
111 
113 
118  bool operator==(const AffineMatrix2D& affineMatrix) const noexcept
119  {
120  return translation_ == affineMatrix.translation_
121  && matrix_ == affineMatrix.matrix_;
122  }
123 
125 
130  bool operator!=(const AffineMatrix2D& affineMatrix) const noexcept
131  {
132  return !(*this == affineMatrix);
133  }
134 
135 
137 
142  AffineMatrix2D& operator +=(const AffineMatrix2D & affineMatrix) noexcept
143  {
144  matrix_ += affineMatrix.matrix_;
145  translation_ += affineMatrix.translation_;
146  return *this;
147  }
148 
150 
155  AffineMatrix2D& operator -=(const AffineMatrix2D & affineMatrix) noexcept
156  {
157  matrix_ -= affineMatrix.matrix_;
158  translation_ -= affineMatrix.translation_;
159  return *this;
160  }
161 
163 
168  AffineMatrix2D& operator *=(const AffineMatrix2D & affineMatrix) noexcept
169  {
170  *this = AffineMatrix2D(matrix_ * affineMatrix.matrix_, matrix_ * affineMatrix.translation_ + translation_);
171  return *this;
172  }
173 
175 
180  AffineMatrix2D& operator *=(const double & value) noexcept
181  {
182  matrix_ *= value;
183  translation_ *= value;
184  return *this;
185  }
186 
188 
193  AffineMatrix2D& operator /=(const double & value) noexcept
194  {
195  matrix_ /= value;
196  translation_ /= value;
197  return *this;
198  }
199 
200  private:
201 
202  Vector2D<double> translation_;
203  Cvb::Matrix2D matrix_;
204 
205 };
206 
207 
209 
217 inline AffineMatrix2D operator+(const AffineMatrix2D & lhs, const AffineMatrix2D & rhs) noexcept
218 {
219  return AffineMatrix2D(lhs.Matrix() + rhs.Matrix(), lhs.Translation() + rhs.Translation());
220 }
221 
223 
231 inline AffineMatrix2D operator-(const AffineMatrix2D & lhs, const AffineMatrix2D & rhs) noexcept
232 {
233  return AffineMatrix2D(lhs.Matrix() - rhs.Matrix(), lhs.Translation() - rhs.Translation());
234 }
235 
237 
245 inline AffineMatrix2D operator*(const AffineMatrix2D & lhs, const AffineMatrix2D & rhs) noexcept
246 {
247  return AffineMatrix2D(lhs.Matrix() * rhs.Matrix(), lhs.Matrix() * rhs.Translation() + lhs.Translation());
248 }
249 
251 
259 inline Point2D<double> operator*(const AffineMatrix2D & lhs, const Point2D<double> & rhs) noexcept
260 {
261  return lhs.Matrix() * rhs + lhs.Translation();
262 }
263 
265 
273 inline AffineMatrix2D operator*(const AffineMatrix2D & lhs, const double & rhs) noexcept
274 {
275  return AffineMatrix2D(lhs.Matrix() * rhs, lhs.Translation() * rhs);
276 }
277 
279 
287 inline AffineMatrix2D operator*(const double & lhs, const AffineMatrix2D & rhs) noexcept
288 {
289  return rhs * lhs;
290 }
291 
293 
301 inline AffineMatrix2D operator/(const AffineMatrix2D & lhs, const double & rhs)
302 {
303  return AffineMatrix2D(lhs.Matrix() / rhs, lhs.Translation() / rhs);
304 }
305 
306 CVB_END_INLINE_NS
307 
308 }
AffineMatrix2D & operator/=(const double &value) noexcept
Divides each element of this affine matrix by the given value.
Definition: affine_matrix_2d.hpp:193
AffineMatrix2D() noexcept=default
Default constructor for empty affine matrix.
void SetTranslation(Vector2D< double > translation) noexcept
Set the translation part of the affine matrix.
Definition: affine_matrix_2d.hpp:85
static AffineMatrix2D Identity() noexcept
The identity element.
Definition: affine_matrix_2d.hpp:26
AffineMatrix2D operator+(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Add two affine matrices.
Definition: affine_matrix_2d.hpp:217
bool operator!=(const AffineMatrix2D &affineMatrix) const noexcept
Compares to an other matrix.
Definition: affine_matrix_2d.hpp:130
Compacted affine matrix describing the Common Vision Blox coordinate system.
Definition: affine_matrix_2d.hpp:17
static Matrix2D Identity() noexcept
The identity element.
Definition: matrix_2d.hpp:24
AffineMatrix2D & operator *=(const double &value) noexcept
Multiplies and assigns to this affine matrix.
Definition: affine_matrix_2d.hpp:180
Cvb::Matrix2D Matrix() const noexcept
Get the transformation part of the affine matrix.
Definition: affine_matrix_2d.hpp:54
AffineMatrix2D operator/(const AffineMatrix2D &lhs, const double &rhs)
Divide affine matrix by scalar.
Definition: affine_matrix_2d.hpp:301
Double precision 2x2 matrix class.
Definition: matrix_2d.hpp:15
Root namespace for the Image Manager interface.
Definition: version.hpp:11
bool operator==(const AffineMatrix2D &affineMatrix) const noexcept
Compares to an other matrix.
Definition: affine_matrix_2d.hpp:118
bool IsTranslation() const noexcept
Check if this affine matrix only describes a translation.
Definition: affine_matrix_2d.hpp:107
void Invert()
Invert this affine matrix if possible.
Definition: affine_matrix_2d.hpp:96
Multi-purpose 2D vector class.
Definition: point_2d.hpp:19
AffineMatrix2D operator *(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Multiply two affine matrices.
Definition: affine_matrix_2d.hpp:245
void Invert()
Inverts this matrix if possible.
Definition: matrix_2d.hpp:257
AffineMatrix2D & operator+=(const AffineMatrix2D &affineMatrix) noexcept
Adds and assigns to this affine matrix.
Definition: affine_matrix_2d.hpp:142
AffineMatrix2D & operator -=(const AffineMatrix2D &affineMatrix) noexcept
Subtracts and assigns to this affine matrix.
Definition: affine_matrix_2d.hpp:155
AffineMatrix2D & operator *=(const AffineMatrix2D &affineMatrix) noexcept
Multiplies and assigns to this affine matrix.
Definition: affine_matrix_2d.hpp:168
void SetMatrix(Cvb::Matrix2D matrix) noexcept
Set the transformation part of the affine matrix.
Definition: affine_matrix_2d.hpp:64
Vector2D< double > Translation() const noexcept
Get the translation part of the affine matrix.
Definition: affine_matrix_2d.hpp:74
AffineMatrix2D operator-(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Subtract two affine matrices.
Definition: affine_matrix_2d.hpp:231