ACF $AcfVersion:0$
CMatrix2d.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// ACF includes
6#include <imath/TMatrix.h>
7
8#include <i2d/CVector2d.h>
9
10
11namespace i2d
12{
13
14
18class CMatrix2d: public imath::TMatrix<2, 2>
19{
20public:
22
26 CMatrix2d();
27 CMatrix2d(const CMatrix2d& transform);
31 CMatrix2d(const CVector2d& axisX, const CVector2d& axisY);
35 CMatrix2d(double m11, double m12, double m21, double m22);
36
40 void Reset();
44 void Reset(double angle, double scale = 1.0);
48 void Reset(double angle, const CVector2d& scale);
49
50 // operations
52 CVector2d GetMultiplied(const CVector2d& position) const;
53 CMatrix2d GetMultiplied(const CMatrix2d& matrix) const;
54 void Multiply(const CMatrix2d& matrix);
55 void MultiplyLeft(const CMatrix2d& matrix);
56
60 CVector2d GetAxisX() const;
64 CVector2d GetAxisY() const;
65
73 void GetAxesLengths(CVector2d& result) const;
78 double GetApproxAngle() const;
82 double GetApproxScale() const;
83
91 bool GetInvMultiplied(const i2d::CVector2d& position, i2d::CVector2d& result) const;
92
96 CMatrix2d GetInverted() const;
100 bool GetInverted(CMatrix2d& result) const;
101
106 CMatrix2d GetTransposed() const;
107
112 bool GetEigenVectors(i2d::CVector2d& vector1, i2d::CVector2d& vector2, double& value1, double& value2) const;
113
117 double GetDet() const;
118
119 // operators
123 CMatrix2d& operator=(const CMatrix2d& matrix);
124
125 CMatrix2d operator+(const BaseClass& matrix) const;
126 CMatrix2d operator-(const BaseClass& matrix) const;
131 CMatrix2d operator*(double scale) const;
135 CMatrix2d operator/(double scale) const;
136
137 // static methods
138 static const CMatrix2d& GetIdentity();
139
140private:
141 // static members
142 static CMatrix2d s_identity;
143};
144
145
146// inline methods
147
149{
150}
151
152
153inline CMatrix2d::CMatrix2d(const CMatrix2d& matrix)
154: BaseClass(matrix)
155{
156}
157
158
159inline CMatrix2d::CMatrix2d(const CVector2d& axisX, const CVector2d& axisY)
160{
161 SetAt(0, 0, axisX[0]);
162 SetAt(0, 1, axisX[1]);
163 SetAt(1, 0, axisY[0]);
164 SetAt(1, 1, axisY[1]);
165}
166
167
168inline CMatrix2d::CMatrix2d(double m11, double m12, double m21, double m22)
169{
170 SetAt(0, 0, m11);
171 SetAt(0, 1, m12);
172 SetAt(1, 0, m21);
173 SetAt(1, 1, m22);
174}
175
176
177inline CVector2d CMatrix2d::GetMultiplied(const CVector2d& position) const
178{
179 CVector2d retVal;
180
181 GetMultiplied(position, retVal);
182
183 return retVal;
184}
185
186
188{
189 CMatrix2d retVal;
190
191 GetMultiplied(matrix, retVal);
192
193 return retVal;
194}
195
196
198{
199 return CVector2d(GetAt(0, 0), GetAt(0, 1));
200}
201
202
204{
205 return CVector2d(GetAt(1, 0), GetAt(1, 1));
206}
207
208
210{
211 CVector2d result;
212
213 GetAxesLengths(result);
214
215 return result;
216}
217
218
219inline void CMatrix2d::GetAxesLengths(CVector2d& result) const
220{
221 result.SetX(GetAxisX().GetLength());
222 result.SetY(GetAxisY().GetLength());
223}
224
225
227{
228 CMatrix2d result;
229
230 GetInverted(result);
231
232 return result;
233}
234
235
237{
238 return CMatrix2d(GetAt(0, 0), GetAt(1, 0), GetAt(0, 1), GetAt(1, 1));
239}
240
241
242inline double CMatrix2d::GetDet() const
243{
244 return GetAt(0, 0) * GetAt(1, 1) - GetAt(0, 1) * GetAt(1, 0);
245}
246
247
248// operators
249
250inline CMatrix2d CMatrix2d::operator+(const BaseClass& matrix) const
251{
252 CMatrix2d retVal;
253
254 BaseClass::GetAdded(matrix, retVal);
255
256 return retVal;
257}
258
259
260inline CMatrix2d CMatrix2d::operator-(const BaseClass& matrix) const
261{
262 CMatrix2d retVal;
263
264 BaseClass::GetSubstracted(matrix, retVal);
265
266 return retVal;
267}
268
269
271{
272 CMatrix2d retVal;
273
274 BaseClass::GetNegated(retVal);
275
276 return retVal;
277}
278
279
281{
282 BaseClass::operator=(matrix);
283
284 return *this;
285}
286
287
288inline CMatrix2d CMatrix2d::operator*(double scale) const
289{
290 CMatrix2d retVal;
291
292 GetScaled(scale, retVal);
293
294 return retVal;
295}
296
297
298inline CMatrix2d CMatrix2d::operator/(double scale) const
299{
300 return CMatrix2d(GetAt(0, 0) / scale, GetAt(0, 1) / scale, GetAt(1, 0) / scale, GetAt(1, 1) / scale);
301}
302
303
304// static methods
305
307{
308 return s_identity;
309}
310
311
312} // namespace i2d
313
314
315
316
2D matrix.
Definition CMatrix2d.h:19
void Reset(double angle, double scale=1.0)
Set the matrix using specified angle (in radians) and uniform scale.
CMatrix2d operator-()
Definition CMatrix2d.h:270
void Multiply(const CMatrix2d &matrix)
CVector2d GetAxesLengths() const
Get lengths of axes vectors.
Definition CMatrix2d.h:209
void Reset()
Default reset to identity.
CMatrix2d GetTransposed() const
Calculate transposed matrix.
Definition CMatrix2d.h:236
CVector2d GetAxisX() const
Get axis X vector.
Definition CMatrix2d.h:197
CVector2d GetMultiplied(const CVector2d &position) const
Definition CMatrix2d.h:177
void MultiplyLeft(const CMatrix2d &matrix)
CMatrix2d GetInverted() const
Calculate inverted matrix.
Definition CMatrix2d.h:226
CVector2d GetAxisY() const
Get axis Y vector.
Definition CMatrix2d.h:203
imath::TMatrix< 2, 2 > BaseClass
Definition CMatrix2d.h:21
CMatrix2d operator*(double scale) const
Multiplication by scalar number.
Definition CMatrix2d.h:288
CMatrix2d operator+(const BaseClass &matrix) const
Definition CMatrix2d.h:250
i2d::CVector2d GetInvMultiplied(const i2d::CVector2d &position) const
Inverted operation to GetApply().
static const CMatrix2d & GetIdentity()
Definition CMatrix2d.h:306
CMatrix2d()
Constructor with no member initialization.
Definition CMatrix2d.h:148
double GetApproxAngle() const
Get angle of axis X in radians.
void Reset(double angle, const CVector2d &scale)
Set the deformation matrix using specified angle (in radians) and scale determined for both axis sepa...
bool GetEigenVectors(i2d::CVector2d &vector1, i2d::CVector2d &vector2, double &value1, double &value2) const
Calculate eigen vectors and eigen values.
CMatrix2d & operator=(const CMatrix2d &matrix)
Copy operator.
Definition CMatrix2d.h:280
CMatrix2d operator/(double scale) const
Division by scalar number.
Definition CMatrix2d.h:298
double GetDet() const
Calculate determinant of deformation matrix.
Definition CMatrix2d.h:242
double GetApproxScale() const
Get approximated scale.
bool GetInvMultiplied(const i2d::CVector2d &position, i2d::CVector2d &result) const
Inverted operation to GetApply().
bool GetInverted(CMatrix2d &result) const
Calculate inverted matrix.
Definition of position or mathematical vector on 2D plane.
Definition CVector2d.h:29
void SetY(double y)
Set Y position of this vector.
Definition CVector2d.h:190
void SetX(double x)
Set X position of this vector.
Definition CVector2d.h:178
Definition of mathematical matrix with fixed dimensions.
Definition TMatrix.h:27
TMatrix< Height, Width, double > GetTransposed() const
Get transposed matrix.
Definition TMatrix.h:343
void GetAdded(const TMatrix< Width, Height, double > &matrix, TMatrix< Width, Height, double > &result) const
Get sum of two matrices.
Definition TMatrix.h:731
void GetMultiplied(const TMatrix< SecondWidth, Width, double > &matrix, TMatrix< SecondWidth, Height, double > &result) const
Get result of multiplication of two matrices.
Definition TMatrix.h:194
void GetNegated(TMatrix< Width, Height, double > &result)
Get result matrix with negated all elements.
Definition TMatrix.h:720
TMatrix< Width, Height, double > & operator=(const TMatrix< Width, Height, double > &matrix)=default
void GetScaled(double value, TMatrix< Width, Height, double > &result) const
Get result of multiplication of this matrix with scalar value.
Definition TMatrix.h:767
void SetAt(const IndexType &index, const ElementType &value)
Set element at specified index.
Definition TMatrix.h:652
const ElementType & GetAt(const IndexType &index) const
Get element stored at specified index.
Definition TMatrix.h:624
void GetSubstracted(const TMatrix< Width, Height, double > &matrix, TMatrix< Width, Height, double > &result) const
Get result of substraction of two matrices.
Definition TMatrix.h:742
Contains the 2D objects.
Definition CAffine2d.h:11