LibreOffice Module basegfx (master) 1
b2dhommatrix.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
25#include <memory>
26
27namespace basegfx
28{
29 constexpr int RowSize = 3;
30
31 void B2DHomMatrix::set3x2(double f_0x0, double f_0x1, double f_0x2, double f_1x0, double f_1x1, double f_1x2)
32 {
33 mfValues[0][0] = f_0x0;
34 mfValues[0][1] = f_0x1;
35 mfValues[0][2] = f_0x2;
36 mfValues[1][0] = f_1x0;
37 mfValues[1][1] = f_1x1;
38 mfValues[1][2] = f_1x2;
39 }
40
42 {
43 for(sal_uInt16 a(0); a < RowSize - 1; a++)
44 {
45 for(sal_uInt16 b(0); b < RowSize; b++)
46 {
47 const double fDefault(internal::implGetDefaultValue(a, b));
48 const double fValueAB(get(a, b));
49
50 if(!::basegfx::fTools::equal(fDefault, fValueAB))
51 {
52 return false;
53 }
54 }
55 }
56
57 return true;
58 }
59
61 {
62 for(sal_uInt16 a(0); a < RowSize - 1; a++)
63 {
64 for(sal_uInt16 b(0); b < RowSize; b++)
66 }
67 }
68
70 {
71 double dst[6];
72 /* Compute adjoint: */
73 computeAdjoint(dst);
74 /* Compute determinant: */
75 double det = computeDeterminant(dst);
76 if (fTools::equalZero(det))
77 return false;
78 return true;
79 }
80
82 {
83 if(isIdentity())
84 return true;
85
86 double dst[6];
87
88 /* Compute adjoint: */
89 computeAdjoint(dst);
90
91 /* Compute determinant: */
92 double det = computeDeterminant(dst);
93 if (fTools::equalZero(det))
94 return false;
95
96 /* Multiply adjoint with reciprocal of determinant: */
97 det = 1.0 / det;
98 mfValues[0][0] = dst[0] * det;
99 mfValues[0][1] = dst[1] * det;
100 mfValues[0][2] = dst[2] * det;
101 mfValues[1][0] = dst[3] * det;
102 mfValues[1][1] = dst[4] * det;
103 mfValues[1][2] = dst[5] * det;
104
105 return true;
106 }
107
108 /* Compute adjoint, optimised for the case where the last (not stored) row is { 0, 0, 1 } */
109 void B2DHomMatrix::computeAdjoint(double (&dst)[6]) const
110 {
111 dst[0] = + get(1, 1);
112 dst[1] = - get(0, 1);
113 dst[2] = + get(0, 1) * get(1, 2) - get(0, 2) * get(1, 1);
114 dst[3] = - get(1, 0);
115 dst[4] = + get(0, 0);
116 dst[5] = - get(0, 0) * get(1, 2) + get(0, 2) * get(1, 0);
117 }
118
119 /* Compute the determinant, given the adjoint matrix */
120 double B2DHomMatrix::computeDeterminant(double (&dst)[6]) const
121 {
122 return mfValues[0][0] * dst[0] + mfValues[0][1] * dst[3];
123 }
124
126 {
127 if(rMat.isIdentity())
128 {
129 // multiply with identity, no change -> nothing to do
130 }
131 else if(isIdentity())
132 {
133 // we are identity, result will be rMat -> assign
134 *this = rMat;
135 }
136 else
137 {
138 // multiply
139 doMulMatrix(rMat);
140 }
141
142 return *this;
143 }
144
146 {
147 // create a copy as source for the original values
148 const B2DHomMatrix aCopy(*this);
149
150 for(sal_uInt16 a(0); a < 2; ++a)
151 {
152 for(sal_uInt16 b(0); b < 3; ++b)
153 {
154 double fValue = 0.0;
155
156 for(sal_uInt16 c(0); c < 2; ++c)
157 fValue += aCopy.mfValues[c][b] * rMat.mfValues[a][c];
158
159 mfValues[a][b] = fValue;
160 }
161 mfValues[a][2] += rMat.mfValues[a][2];
162 }
163 }
164
166 {
167 if (&rMat == this)
168 return true;
169 for(sal_uInt16 a(0); a < 2; a++)
170 {
171 for(sal_uInt16 b(0); b < 3; b++)
172 {
173 const double fValueA(mfValues[a][b]);
174 const double fValueB(rMat.mfValues[a][b]);
175
176 if(!::basegfx::fTools::equal(fValueA, fValueB))
177 {
178 return false;
179 }
180 }
181 }
182 return true;
183 }
184
186 {
187 return !(*this == rMat);
188 }
189
190 void B2DHomMatrix::rotate(double fRadiant)
191 {
192 if(fTools::equalZero(fRadiant))
193 return;
194
195 double fSin(0.0);
196 double fCos(1.0);
197
198 utils::createSinCosOrthogonal(fSin, fCos, fRadiant);
199 B2DHomMatrix aRotMat;
200
201 aRotMat.set(0, 0, fCos);
202 aRotMat.set(1, 1, fCos);
203 aRotMat.set(1, 0, fSin);
204 aRotMat.set(0, 1, -fSin);
205
206 doMulMatrix(aRotMat);
207 }
208
209 void B2DHomMatrix::translate(double fX, double fY)
210 {
211 if(!fTools::equalZero(fX) || !fTools::equalZero(fY))
212 {
213 B2DHomMatrix aTransMat;
214
215 aTransMat.set(0, 2, fX);
216 aTransMat.set(1, 2, fY);
217
218 doMulMatrix(aTransMat);
219 }
220 }
221
223 {
224 translate(rTuple.getX(), rTuple.getY());
225 }
226
227 void B2DHomMatrix::scale(double fX, double fY)
228 {
229 const double fOne(1.0);
230
231 if(!fTools::equal(fOne, fX) || !fTools::equal(fOne, fY))
232 {
233 B2DHomMatrix aScaleMat;
234
235 aScaleMat.set(0, 0, fX);
236 aScaleMat.set(1, 1, fY);
237
238 doMulMatrix(aScaleMat);
239 }
240 }
241
242 void B2DHomMatrix::scale(const B2DTuple& rTuple)
243 {
244 scale(rTuple.getX(), rTuple.getY());
245 }
246
247 void B2DHomMatrix::shearX(double fSx)
248 {
249 // #i76239# do not test against 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
250 if(!fTools::equalZero(fSx))
251 {
252 B2DHomMatrix aShearXMat;
253
254 aShearXMat.set(0, 1, fSx);
255
256 doMulMatrix(aShearXMat);
257 }
258 }
259
260 void B2DHomMatrix::shearY(double fSy)
261 {
262 // #i76239# do not test against 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
263 if(!fTools::equalZero(fSy))
264 {
265 B2DHomMatrix aShearYMat;
266
267 aShearYMat.set(1, 0, fSy);
268
269 doMulMatrix(aShearYMat);
270 }
271 }
272
280 bool B2DHomMatrix::decompose(B2DTuple& rScale, B2DTuple& rTranslate, double& rRotate, double& rShearX) const
281 {
282 // reset rotate and shear and copy translation values in every case
283 rRotate = rShearX = 0.0;
284 rTranslate.setX(get(0, 2));
285 rTranslate.setY(get(1, 2));
286
287 // test for rotation and shear
288 if(fTools::equalZero(get(0, 1)) && fTools::equalZero(get(1, 0)))
289 {
290 // no rotation and shear, copy scale values
291 rScale.setX(get(0, 0));
292 rScale.setY(get(1, 1));
293
294 // or is there?
295 if( rScale.getX() < 0 && rScale.getY() < 0 )
296 {
297 // there is - 180 degree rotated
298 rScale *= -1;
299 rRotate = M_PI;
300 }
301 }
302 else
303 {
304 // get the unit vectors of the transformation -> the perpendicular vectors
305 B2DVector aUnitVecX(get(0, 0), get(1, 0));
306 B2DVector aUnitVecY(get(0, 1), get(1, 1));
307 const double fScalarXY(aUnitVecX.scalar(aUnitVecY));
308
309 // Test if shear is zero. That's the case if the unit vectors in the matrix
310 // are perpendicular -> scalar is zero. This is also the case when one of
311 // the unit vectors is zero.
312 if(fTools::equalZero(fScalarXY))
313 {
314 // calculate unsigned scale values
315 rScale.setX(aUnitVecX.getLength());
316 rScale.setY(aUnitVecY.getLength());
317
318 // check unit vectors for zero lengths
319 const bool bXIsZero(fTools::equalZero(rScale.getX()));
320 const bool bYIsZero(fTools::equalZero(rScale.getY()));
321
322 if(bXIsZero || bYIsZero)
323 {
324 // still extract as much as possible. Scalings are already set
325 if(!bXIsZero)
326 {
327 // get rotation of X-Axis
328 rRotate = atan2(aUnitVecX.getY(), aUnitVecX.getX());
329 }
330 else if(!bYIsZero)
331 {
332 // get rotation of X-Axis. When assuming X and Y perpendicular
333 // and correct rotation, it's the Y-Axis rotation minus 90 degrees
334 rRotate = atan2(aUnitVecY.getY(), aUnitVecY.getX()) - M_PI_2;
335 }
336
337 // one or both unit vectors do not exist, determinant is zero, no decomposition possible.
338 // Eventually used rotations or shears are lost
339 return false;
340 }
341 else
342 {
343 // no shear
344 // calculate rotation of X unit vector relative to (1, 0)
345 rRotate = atan2(aUnitVecX.getY(), aUnitVecX.getX());
346
347 // use orientation to evtl. correct sign of Y-Scale
348 const double fCrossXY(aUnitVecX.cross(aUnitVecY));
349
350 if(fCrossXY < 0.0)
351 {
352 rScale.setY(-rScale.getY());
353 }
354 }
355 }
356 else
357 {
358 // fScalarXY is not zero, thus both unit vectors exist. No need to handle that here
359 // shear, extract it
360 double fCrossXY(aUnitVecX.cross(aUnitVecY));
361
362 // get rotation by calculating angle of X unit vector relative to (1, 0).
363 // This is before the parallel test following the motto to extract
364 // as much as possible
365 rRotate = atan2(aUnitVecX.getY(), aUnitVecX.getX());
366
367 // get unsigned scale value for X. It will not change and is useful
368 // for further corrections
369 rScale.setX(aUnitVecX.getLength());
370
371 if(fTools::equalZero(fCrossXY))
372 {
373 // extract as much as possible
374 rScale.setY(aUnitVecY.getLength());
375
376 // unit vectors are parallel, thus not linear independent. No
377 // useful decomposition possible. This should not happen since
378 // the only way to get the unit vectors nearly parallel is
379 // a very big shearing. Anyways, be prepared for hand-filled
380 // matrices
381 // Eventually used rotations or shears are lost
382 return false;
383 }
384 else
385 {
386 // calculate the contained shear
387 rShearX = fScalarXY / fCrossXY;
388
389 if(!fTools::equalZero(rRotate))
390 {
391 // To be able to correct the shear for aUnitVecY, rotation needs to be
392 // removed first. Correction of aUnitVecX is easy, it will be rotated back to (1, 0).
393 aUnitVecX.setX(rScale.getX());
394 aUnitVecX.setY(0.0);
395
396 // for Y correction we rotate the UnitVecY back about -rRotate
397 const double fNegRotate(-rRotate);
398 const double fSin(sin(fNegRotate));
399 const double fCos(cos(fNegRotate));
400
401 const double fNewX(aUnitVecY.getX() * fCos - aUnitVecY.getY() * fSin);
402 const double fNewY(aUnitVecY.getX() * fSin + aUnitVecY.getY() * fCos);
403
404 aUnitVecY.setX(fNewX);
405 aUnitVecY.setY(fNewY);
406 }
407
408 // Correct aUnitVecY and fCrossXY to fShear=0. Rotation is already removed.
409 // Shear correction can only work with removed rotation
410 aUnitVecY.setX(aUnitVecY.getX() - (aUnitVecY.getY() * rShearX));
411 fCrossXY = aUnitVecX.cross(aUnitVecY);
412
413 // calculate unsigned scale value for Y, after the corrections since
414 // the shear correction WILL change the length of aUnitVecY
415 rScale.setY(aUnitVecY.getLength());
416
417 // use orientation to set sign of Y-Scale
418 if(fCrossXY < 0.0)
419 {
420 rScale.setY(-rScale.getY());
421 }
422 }
423 }
424 }
425
426 return true;
427 }
428} // end of namespace basegfx
429
430/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator!=(const B2DHomMatrix &rMat) const
bool decompose(B2DTuple &rScale, B2DTuple &rTranslate, double &rRotate, double &rShearX) const
Help routine to decompose given homogen 3x3 matrix to components.
void computeAdjoint(double(&dst)[6]) const
bool isInvertible() const
void shearX(double fSx)
void set3x2(double f_0x0, double f_0x1, double f_0x2, double f_1x0, double f_1x1, double f_1x2)
allow setting all needed values for a 3x2 matrix in one call.
bool operator==(const B2DHomMatrix &rMat) const
double computeDeterminant(double(&dst)[6]) const
void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
void rotate(double fRadiant)
B2DHomMatrix & operator*=(const B2DHomMatrix &rMat)
void translate(double fX, double fY)
std::array< std::array< double, 3 >, 2 > mfValues
double get(sal_uInt16 nRow, sal_uInt16 nColumn) const
void scale(double fX, double fY)
void doMulMatrix(const B2DHomMatrix &rMat)
void shearY(double fSy)
bool isIdentity() const
Base class for all Points/Vectors with two double values.
Definition: b2dtuple.hxx:39
Base Point class with two double values.
Definition: b2dvector.hxx:40
double scalar(const B2DVector &rVec) const
Calculate the Scalar with another 2D Vector.
Definition: b2dvector.hxx:134
double cross(const B2DVector &rVec) const
Calculate the length of the cross product with another 2D Vector.
Definition: b2dvector.hxx:151
double getLength() const
Calculate the length of this 2D Vector.
Definition: b2dvector.cxx:54
TYPE getX() const
Get X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:63
void setY(TYPE fY)
Set Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:72
TYPE getY() const
Get Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:66
void setX(TYPE fX)
Set X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:69
bool equalZero(const T &rfVal)
Compare against small value.
Definition: ftools.hxx:156
bool equal(T const &rfValA, T const &rfValB)
Definition: ftools.hxx:169
constexpr double implGetDefaultValue(sal_uInt16 nRow, sal_uInt16 nColumn)
void createSinCosOrthogonal(double &o_rSin, double &o_rCos, double fRadiant)
If the rotation angle is an approximate multiple of pi/2, force fSin/fCos to -1/0/1,...
constexpr int RowSize