xref: /AOO42X/main/basegfx/source/matrix/b2dhommatrix.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
30*cdf0e10cSrcweir #include <osl/diagnose.h>
31*cdf0e10cSrcweir #include <rtl/instance.hxx>
32*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
33*cdf0e10cSrcweir #include <hommatrixtemplate.hxx>
34*cdf0e10cSrcweir #include <basegfx/tuple/b2dtuple.hxx>
35*cdf0e10cSrcweir #include <basegfx/vector/b2dvector.hxx>
36*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace basegfx
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir     class Impl2DHomMatrix : public ::basegfx::internal::ImplHomMatrixTemplate< 3 >
43*cdf0e10cSrcweir     {
44*cdf0e10cSrcweir     };
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     namespace { struct IdentityMatrix : public rtl::Static< B2DHomMatrix::ImplType,
47*cdf0e10cSrcweir                                                             IdentityMatrix > {}; }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     B2DHomMatrix::B2DHomMatrix() :
50*cdf0e10cSrcweir         mpImpl( IdentityMatrix::get() ) // use common identity matrix
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir     }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     B2DHomMatrix::B2DHomMatrix(const B2DHomMatrix& rMat) :
55*cdf0e10cSrcweir         mpImpl(rMat.mpImpl)
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir     }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     B2DHomMatrix::~B2DHomMatrix()
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     B2DHomMatrix::B2DHomMatrix(double f_0x0, double f_0x1, double f_0x2, double f_1x0, double f_1x1, double f_1x2)
64*cdf0e10cSrcweir     :   mpImpl( IdentityMatrix::get() ) // use common identity matrix, will be made unique with 1st set-call
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir         mpImpl->set(0, 0, f_0x0);
67*cdf0e10cSrcweir         mpImpl->set(0, 1, f_0x1);
68*cdf0e10cSrcweir         mpImpl->set(0, 2, f_0x2);
69*cdf0e10cSrcweir         mpImpl->set(1, 0, f_1x0);
70*cdf0e10cSrcweir         mpImpl->set(1, 1, f_1x1);
71*cdf0e10cSrcweir         mpImpl->set(1, 2, f_1x2);
72*cdf0e10cSrcweir     }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     B2DHomMatrix& B2DHomMatrix::operator=(const B2DHomMatrix& rMat)
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         mpImpl = rMat.mpImpl;
77*cdf0e10cSrcweir         return *this;
78*cdf0e10cSrcweir     }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     void B2DHomMatrix::makeUnique()
81*cdf0e10cSrcweir     {
82*cdf0e10cSrcweir         mpImpl.make_unique();
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     double B2DHomMatrix::get(sal_uInt16 nRow, sal_uInt16 nColumn) const
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir         return mpImpl->get(nRow, nColumn);
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     void B2DHomMatrix::set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         mpImpl->set(nRow, nColumn, fValue);
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     void B2DHomMatrix::set3x2(double f_0x0, double f_0x1, double f_0x2, double f_1x0, double f_1x1, double f_1x2)
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         mpImpl->set(0, 0, f_0x0);
98*cdf0e10cSrcweir         mpImpl->set(0, 1, f_0x1);
99*cdf0e10cSrcweir         mpImpl->set(0, 2, f_0x2);
100*cdf0e10cSrcweir         mpImpl->set(1, 0, f_1x0);
101*cdf0e10cSrcweir         mpImpl->set(1, 1, f_1x1);
102*cdf0e10cSrcweir         mpImpl->set(1, 2, f_1x2);
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     bool B2DHomMatrix::isLastLineDefault() const
106*cdf0e10cSrcweir     {
107*cdf0e10cSrcweir         return mpImpl->isLastLineDefault();
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     bool B2DHomMatrix::isIdentity() const
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         if(mpImpl.same_object(IdentityMatrix::get()))
113*cdf0e10cSrcweir             return true;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         return mpImpl->isIdentity();
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     void B2DHomMatrix::identity()
119*cdf0e10cSrcweir     {
120*cdf0e10cSrcweir         mpImpl = IdentityMatrix::get();
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     bool B2DHomMatrix::isInvertible() const
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         return mpImpl->isInvertible();
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     bool B2DHomMatrix::invert()
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         Impl2DHomMatrix aWork(*mpImpl);
131*cdf0e10cSrcweir         sal_uInt16* pIndex = new sal_uInt16[mpImpl->getEdgeLength()];
132*cdf0e10cSrcweir         sal_Int16 nParity;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         if(aWork.ludcmp(pIndex, nParity))
135*cdf0e10cSrcweir         {
136*cdf0e10cSrcweir             mpImpl->doInvert(aWork, pIndex);
137*cdf0e10cSrcweir             delete[] pIndex;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir             return true;
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         delete[] pIndex;
143*cdf0e10cSrcweir         return false;
144*cdf0e10cSrcweir     }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     bool B2DHomMatrix::isNormalized() const
147*cdf0e10cSrcweir     {
148*cdf0e10cSrcweir         return mpImpl->isNormalized();
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     void B2DHomMatrix::normalize()
152*cdf0e10cSrcweir     {
153*cdf0e10cSrcweir         if(!const_cast<const B2DHomMatrix*>(this)->mpImpl->isNormalized())
154*cdf0e10cSrcweir             mpImpl->doNormalize();
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     double B2DHomMatrix::determinant() const
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir         return mpImpl->doDeterminant();
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     double B2DHomMatrix::trace() const
163*cdf0e10cSrcweir     {
164*cdf0e10cSrcweir         return mpImpl->doTrace();
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     void B2DHomMatrix::transpose()
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         mpImpl->doTranspose();
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     B2DHomMatrix& B2DHomMatrix::operator+=(const B2DHomMatrix& rMat)
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         mpImpl->doAddMatrix(*rMat.mpImpl);
175*cdf0e10cSrcweir         return *this;
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     B2DHomMatrix& B2DHomMatrix::operator-=(const B2DHomMatrix& rMat)
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         mpImpl->doSubMatrix(*rMat.mpImpl);
181*cdf0e10cSrcweir         return *this;
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     B2DHomMatrix& B2DHomMatrix::operator*=(double fValue)
185*cdf0e10cSrcweir     {
186*cdf0e10cSrcweir         const double fOne(1.0);
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir         if(!fTools::equal(fOne, fValue))
189*cdf0e10cSrcweir             mpImpl->doMulMatrix(fValue);
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         return *this;
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     B2DHomMatrix& B2DHomMatrix::operator/=(double fValue)
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir         const double fOne(1.0);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         if(!fTools::equal(fOne, fValue))
199*cdf0e10cSrcweir             mpImpl->doMulMatrix(1.0 / fValue);
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         return *this;
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     B2DHomMatrix& B2DHomMatrix::operator*=(const B2DHomMatrix& rMat)
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         if(!rMat.isIdentity())
207*cdf0e10cSrcweir             mpImpl->doMulMatrix(*rMat.mpImpl);
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         return *this;
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     bool B2DHomMatrix::operator==(const B2DHomMatrix& rMat) const
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir         if(mpImpl.same_object(rMat.mpImpl))
215*cdf0e10cSrcweir             return true;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         return mpImpl->isEqual(*rMat.mpImpl);
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     bool B2DHomMatrix::operator!=(const B2DHomMatrix& rMat) const
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir         return !(*this == rMat);
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     void B2DHomMatrix::rotate(double fRadiant)
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         if(!fTools::equalZero(fRadiant))
228*cdf0e10cSrcweir         {
229*cdf0e10cSrcweir             double fSin(0.0);
230*cdf0e10cSrcweir             double fCos(1.0);
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir             tools::createSinCosOrthogonal(fSin, fCos, fRadiant);
233*cdf0e10cSrcweir             Impl2DHomMatrix aRotMat;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir             aRotMat.set(0, 0, fCos);
236*cdf0e10cSrcweir             aRotMat.set(1, 1, fCos);
237*cdf0e10cSrcweir             aRotMat.set(1, 0, fSin);
238*cdf0e10cSrcweir             aRotMat.set(0, 1, -fSin);
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir             mpImpl->doMulMatrix(aRotMat);
241*cdf0e10cSrcweir         }
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     void B2DHomMatrix::translate(double fX, double fY)
245*cdf0e10cSrcweir     {
246*cdf0e10cSrcweir         if(!fTools::equalZero(fX) || !fTools::equalZero(fY))
247*cdf0e10cSrcweir         {
248*cdf0e10cSrcweir             Impl2DHomMatrix aTransMat;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir             aTransMat.set(0, 2, fX);
251*cdf0e10cSrcweir             aTransMat.set(1, 2, fY);
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir             mpImpl->doMulMatrix(aTransMat);
254*cdf0e10cSrcweir         }
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     void B2DHomMatrix::scale(double fX, double fY)
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         const double fOne(1.0);
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         if(!fTools::equal(fOne, fX) || !fTools::equal(fOne, fY))
262*cdf0e10cSrcweir         {
263*cdf0e10cSrcweir             Impl2DHomMatrix aScaleMat;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir             aScaleMat.set(0, 0, fX);
266*cdf0e10cSrcweir             aScaleMat.set(1, 1, fY);
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir             mpImpl->doMulMatrix(aScaleMat);
269*cdf0e10cSrcweir         }
270*cdf0e10cSrcweir     }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir     void B2DHomMatrix::shearX(double fSx)
273*cdf0e10cSrcweir     {
274*cdf0e10cSrcweir         // #i76239# do not test againt 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
275*cdf0e10cSrcweir         if(!fTools::equalZero(fSx))
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir             Impl2DHomMatrix aShearXMat;
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir             aShearXMat.set(0, 1, fSx);
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir             mpImpl->doMulMatrix(aShearXMat);
282*cdf0e10cSrcweir         }
283*cdf0e10cSrcweir     }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir     void B2DHomMatrix::shearY(double fSy)
286*cdf0e10cSrcweir     {
287*cdf0e10cSrcweir         // #i76239# do not test againt 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
288*cdf0e10cSrcweir         if(!fTools::equalZero(fSy))
289*cdf0e10cSrcweir         {
290*cdf0e10cSrcweir             Impl2DHomMatrix aShearYMat;
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir             aShearYMat.set(1, 0, fSy);
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir             mpImpl->doMulMatrix(aShearYMat);
295*cdf0e10cSrcweir         }
296*cdf0e10cSrcweir     }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir     /** Decomposition
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir        New, optimized version with local shearX detection. Old version (keeping
301*cdf0e10cSrcweir        below, is working well, too) used the 3D matrix decomposition when
302*cdf0e10cSrcweir        shear was used. Keeping old version as comment below since it may get
303*cdf0e10cSrcweir        necessary to add the determinant() test from there here, too.
304*cdf0e10cSrcweir     */
305*cdf0e10cSrcweir     bool B2DHomMatrix::decompose(B2DTuple& rScale, B2DTuple& rTranslate, double& rRotate, double& rShearX) const
306*cdf0e10cSrcweir     {
307*cdf0e10cSrcweir         // when perspective is used, decompose is not made here
308*cdf0e10cSrcweir         if(!mpImpl->isLastLineDefault())
309*cdf0e10cSrcweir         {
310*cdf0e10cSrcweir             return false;
311*cdf0e10cSrcweir         }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir         // reset rotate and shear and copy translation values in every case
314*cdf0e10cSrcweir         rRotate = rShearX = 0.0;
315*cdf0e10cSrcweir         rTranslate.setX(get(0, 2));
316*cdf0e10cSrcweir         rTranslate.setY(get(1, 2));
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir         // test for rotation and shear
319*cdf0e10cSrcweir         if(fTools::equalZero(get(0, 1)) && fTools::equalZero(get(1, 0)))
320*cdf0e10cSrcweir         {
321*cdf0e10cSrcweir             // no rotation and shear, copy scale values
322*cdf0e10cSrcweir             rScale.setX(get(0, 0));
323*cdf0e10cSrcweir             rScale.setY(get(1, 1));
324*cdf0e10cSrcweir         }
325*cdf0e10cSrcweir         else
326*cdf0e10cSrcweir         {
327*cdf0e10cSrcweir             // get the unit vectors of the transformation -> the perpendicular vectors
328*cdf0e10cSrcweir             B2DVector aUnitVecX(get(0, 0), get(1, 0));
329*cdf0e10cSrcweir             B2DVector aUnitVecY(get(0, 1), get(1, 1));
330*cdf0e10cSrcweir             const double fScalarXY(aUnitVecX.scalar(aUnitVecY));
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir             // Test if shear is zero. That's the case if the unit vectors in the matrix
333*cdf0e10cSrcweir             // are perpendicular -> scalar is zero. This is also the case when one of
334*cdf0e10cSrcweir             // the unit vectors is zero.
335*cdf0e10cSrcweir             if(fTools::equalZero(fScalarXY))
336*cdf0e10cSrcweir             {
337*cdf0e10cSrcweir                 // calculate unsigned scale values
338*cdf0e10cSrcweir                 rScale.setX(aUnitVecX.getLength());
339*cdf0e10cSrcweir                 rScale.setY(aUnitVecY.getLength());
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir                 // check unit vectors for zero lengths
342*cdf0e10cSrcweir                 const bool bXIsZero(fTools::equalZero(rScale.getX()));
343*cdf0e10cSrcweir                 const bool bYIsZero(fTools::equalZero(rScale.getY()));
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir                 if(bXIsZero || bYIsZero)
346*cdf0e10cSrcweir                 {
347*cdf0e10cSrcweir                     // still extract as much as possible. Scalings are already set
348*cdf0e10cSrcweir                     if(!bXIsZero)
349*cdf0e10cSrcweir                     {
350*cdf0e10cSrcweir                         // get rotation of X-Axis
351*cdf0e10cSrcweir                         rRotate = atan2(aUnitVecX.getY(), aUnitVecX.getX());
352*cdf0e10cSrcweir                     }
353*cdf0e10cSrcweir                     else if(!bYIsZero)
354*cdf0e10cSrcweir                     {
355*cdf0e10cSrcweir                         // get rotation of X-Axis. When assuming X and Y perpendicular
356*cdf0e10cSrcweir                         // and correct rotation, it's the Y-Axis rotation minus 90 degrees
357*cdf0e10cSrcweir                         rRotate = atan2(aUnitVecY.getY(), aUnitVecY.getX()) - M_PI_2;
358*cdf0e10cSrcweir                     }
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir                     // one or both unit vectors do not extist, determinant is zero, no decomposition possible.
361*cdf0e10cSrcweir                     // Eventually used rotations or shears are lost
362*cdf0e10cSrcweir                     return false;
363*cdf0e10cSrcweir                 }
364*cdf0e10cSrcweir                 else
365*cdf0e10cSrcweir                 {
366*cdf0e10cSrcweir                     // no shear
367*cdf0e10cSrcweir                     // calculate rotation of X unit vector relative to (1, 0)
368*cdf0e10cSrcweir                     rRotate = atan2(aUnitVecX.getY(), aUnitVecX.getX());
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir                     // use orientation to evtl. correct sign of Y-Scale
371*cdf0e10cSrcweir                     const double fCrossXY(aUnitVecX.cross(aUnitVecY));
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir                     if(fCrossXY < 0.0)
374*cdf0e10cSrcweir                     {
375*cdf0e10cSrcweir                         rScale.setY(-rScale.getY());
376*cdf0e10cSrcweir                     }
377*cdf0e10cSrcweir                 }
378*cdf0e10cSrcweir             }
379*cdf0e10cSrcweir             else
380*cdf0e10cSrcweir             {
381*cdf0e10cSrcweir                 // fScalarXY is not zero, thus both unit vectors exist. No need to handle that here
382*cdf0e10cSrcweir                 // shear, extract it
383*cdf0e10cSrcweir                 double fCrossXY(aUnitVecX.cross(aUnitVecY));
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir                 // get rotation by calculating angle of X unit vector relative to (1, 0).
386*cdf0e10cSrcweir                 // This is before the parallell test following the motto to extract
387*cdf0e10cSrcweir                 // as much as possible
388*cdf0e10cSrcweir                 rRotate = atan2(aUnitVecX.getY(), aUnitVecX.getX());
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir                 // get unsigned scale value for X. It will not change and is useful
391*cdf0e10cSrcweir                 // for further corrections
392*cdf0e10cSrcweir                 rScale.setX(aUnitVecX.getLength());
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir                 if(fTools::equalZero(fCrossXY))
395*cdf0e10cSrcweir                 {
396*cdf0e10cSrcweir                     // extract as much as possible
397*cdf0e10cSrcweir                     rScale.setY(aUnitVecY.getLength());
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir                     // unit vectors are parallel, thus not linear independent. No
400*cdf0e10cSrcweir                     // useful decomposition possible. This should not happen since
401*cdf0e10cSrcweir                     // the only way to get the unit vectors nearly parallell is
402*cdf0e10cSrcweir                     // a very big shearing. Anyways, be prepared for hand-filled
403*cdf0e10cSrcweir                     // matrices
404*cdf0e10cSrcweir                     // Eventually used rotations or shears are lost
405*cdf0e10cSrcweir                     return false;
406*cdf0e10cSrcweir                 }
407*cdf0e10cSrcweir                 else
408*cdf0e10cSrcweir                 {
409*cdf0e10cSrcweir                     // calculate the contained shear
410*cdf0e10cSrcweir                     rShearX = fScalarXY / fCrossXY;
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir                     if(!fTools::equalZero(rRotate))
413*cdf0e10cSrcweir                     {
414*cdf0e10cSrcweir                         // To be able to correct the shear for aUnitVecY, rotation needs to be
415*cdf0e10cSrcweir                         // removed first. Correction of aUnitVecX is easy, it will be rotated back to (1, 0).
416*cdf0e10cSrcweir                         aUnitVecX.setX(rScale.getX());
417*cdf0e10cSrcweir                         aUnitVecX.setY(0.0);
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir                         // for Y correction we rotate the UnitVecY back about -rRotate
420*cdf0e10cSrcweir                         const double fNegRotate(-rRotate);
421*cdf0e10cSrcweir                         const double fSin(sin(fNegRotate));
422*cdf0e10cSrcweir                         const double fCos(cos(fNegRotate));
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir                         const double fNewX(aUnitVecY.getX() * fCos - aUnitVecY.getY() * fSin);
425*cdf0e10cSrcweir                         const double fNewY(aUnitVecY.getX() * fSin + aUnitVecY.getY() * fCos);
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir                         aUnitVecY.setX(fNewX);
428*cdf0e10cSrcweir                         aUnitVecY.setY(fNewY);
429*cdf0e10cSrcweir                     }
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir                     // Correct aUnitVecY and fCrossXY to fShear=0. Rotation is already removed.
432*cdf0e10cSrcweir                     // Shear correction can only work with removed rotation
433*cdf0e10cSrcweir                     aUnitVecY.setX(aUnitVecY.getX() - (aUnitVecY.getY() * rShearX));
434*cdf0e10cSrcweir                     fCrossXY = aUnitVecX.cross(aUnitVecY);
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir                     // calculate unsigned scale value for Y, after the corrections since
437*cdf0e10cSrcweir                     // the shear correction WILL change the length of aUnitVecY
438*cdf0e10cSrcweir                     rScale.setY(aUnitVecY.getLength());
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir                     // use orientation to set sign of Y-Scale
441*cdf0e10cSrcweir                     if(fCrossXY < 0.0)
442*cdf0e10cSrcweir                     {
443*cdf0e10cSrcweir                         rScale.setY(-rScale.getY());
444*cdf0e10cSrcweir                     }
445*cdf0e10cSrcweir                 }
446*cdf0e10cSrcweir             }
447*cdf0e10cSrcweir         }
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir         return true;
450*cdf0e10cSrcweir     }
451*cdf0e10cSrcweir } // end of namespace basegfx
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
454*cdf0e10cSrcweir // eof
455