xref: /AOO41X/main/basegfx/source/matrix/b3dhommatrix.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 
31*cdf0e10cSrcweir #include <rtl/instance.hxx>
32*cdf0e10cSrcweir #include <basegfx/matrix/b3dhommatrix.hxx>
33*cdf0e10cSrcweir #include <hommatrixtemplate.hxx>
34*cdf0e10cSrcweir #include <basegfx/vector/b3dvector.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir namespace basegfx
37*cdf0e10cSrcweir {
38*cdf0e10cSrcweir     class Impl3DHomMatrix : public ::basegfx::internal::ImplHomMatrixTemplate< 4 >
39*cdf0e10cSrcweir     {
40*cdf0e10cSrcweir     };
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir     namespace { struct IdentityMatrix : public rtl::Static< B3DHomMatrix::ImplType,
43*cdf0e10cSrcweir                                                             IdentityMatrix > {}; }
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 	B3DHomMatrix::B3DHomMatrix() :
46*cdf0e10cSrcweir         mpImpl( IdentityMatrix::get() ) // use common identity matrix
47*cdf0e10cSrcweir 	{
48*cdf0e10cSrcweir 	}
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 	B3DHomMatrix::B3DHomMatrix(const B3DHomMatrix& rMat) :
51*cdf0e10cSrcweir         mpImpl(rMat.mpImpl)
52*cdf0e10cSrcweir 	{
53*cdf0e10cSrcweir 	}
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 	B3DHomMatrix::~B3DHomMatrix()
56*cdf0e10cSrcweir 	{
57*cdf0e10cSrcweir 	}
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	B3DHomMatrix& B3DHomMatrix::operator=(const B3DHomMatrix& rMat)
60*cdf0e10cSrcweir 	{
61*cdf0e10cSrcweir         mpImpl = rMat.mpImpl;
62*cdf0e10cSrcweir 		return *this;
63*cdf0e10cSrcweir 	}
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     void B3DHomMatrix::makeUnique()
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir         mpImpl.make_unique();
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	double B3DHomMatrix::get(sal_uInt16 nRow, sal_uInt16 nColumn) const
71*cdf0e10cSrcweir 	{
72*cdf0e10cSrcweir 		return mpImpl->get(nRow, nColumn);
73*cdf0e10cSrcweir 	}
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	void B3DHomMatrix::set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
76*cdf0e10cSrcweir 	{
77*cdf0e10cSrcweir 		mpImpl->set(nRow, nColumn, fValue);
78*cdf0e10cSrcweir 	}
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	bool B3DHomMatrix::isLastLineDefault() const
81*cdf0e10cSrcweir 	{
82*cdf0e10cSrcweir 		return mpImpl->isLastLineDefault();
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	bool B3DHomMatrix::isIdentity() const
86*cdf0e10cSrcweir 	{
87*cdf0e10cSrcweir 		if(mpImpl.same_object(IdentityMatrix::get()))
88*cdf0e10cSrcweir 			return true;
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 		return mpImpl->isIdentity();
91*cdf0e10cSrcweir 	}
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	void B3DHomMatrix::identity()
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir         mpImpl = IdentityMatrix::get();
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	bool B3DHomMatrix::isInvertible() const
99*cdf0e10cSrcweir 	{
100*cdf0e10cSrcweir 		return mpImpl->isInvertible();
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	bool B3DHomMatrix::invert()
104*cdf0e10cSrcweir 	{
105*cdf0e10cSrcweir 		Impl3DHomMatrix aWork(*mpImpl);
106*cdf0e10cSrcweir 		sal_uInt16* pIndex = new sal_uInt16[mpImpl->getEdgeLength()];
107*cdf0e10cSrcweir 		sal_Int16 nParity;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 		if(aWork.ludcmp(pIndex, nParity))
110*cdf0e10cSrcweir 		{
111*cdf0e10cSrcweir 			mpImpl->doInvert(aWork, pIndex);
112*cdf0e10cSrcweir 			delete[] pIndex;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 			return true;
115*cdf0e10cSrcweir 		}
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 		delete[] pIndex;
118*cdf0e10cSrcweir 		return false;
119*cdf0e10cSrcweir 	}
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	bool B3DHomMatrix::isNormalized() const
122*cdf0e10cSrcweir 	{
123*cdf0e10cSrcweir 		return mpImpl->isNormalized();
124*cdf0e10cSrcweir 	}
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	void B3DHomMatrix::normalize()
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		if(!const_cast<const B3DHomMatrix*>(this)->mpImpl->isNormalized())
129*cdf0e10cSrcweir 			mpImpl->doNormalize();
130*cdf0e10cSrcweir 	}
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	double B3DHomMatrix::determinant() const
133*cdf0e10cSrcweir 	{
134*cdf0e10cSrcweir 		return mpImpl->doDeterminant();
135*cdf0e10cSrcweir 	}
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	double B3DHomMatrix::trace() const
138*cdf0e10cSrcweir 	{
139*cdf0e10cSrcweir 		return mpImpl->doTrace();
140*cdf0e10cSrcweir 	}
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	void B3DHomMatrix::transpose()
143*cdf0e10cSrcweir 	{
144*cdf0e10cSrcweir 		mpImpl->doTranspose();
145*cdf0e10cSrcweir 	}
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	B3DHomMatrix& B3DHomMatrix::operator+=(const B3DHomMatrix& rMat)
148*cdf0e10cSrcweir 	{
149*cdf0e10cSrcweir 		mpImpl->doAddMatrix(*rMat.mpImpl);
150*cdf0e10cSrcweir 		return *this;
151*cdf0e10cSrcweir 	}
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 	B3DHomMatrix& B3DHomMatrix::operator-=(const B3DHomMatrix& rMat)
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		mpImpl->doSubMatrix(*rMat.mpImpl);
156*cdf0e10cSrcweir 		return *this;
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	B3DHomMatrix& B3DHomMatrix::operator*=(double fValue)
160*cdf0e10cSrcweir 	{
161*cdf0e10cSrcweir 		const double fOne(1.0);
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 		if(!fTools::equal(fOne, fValue))
164*cdf0e10cSrcweir 			mpImpl->doMulMatrix(fValue);
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 		return *this;
167*cdf0e10cSrcweir 	}
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 	B3DHomMatrix& B3DHomMatrix::operator/=(double fValue)
170*cdf0e10cSrcweir 	{
171*cdf0e10cSrcweir 		const double fOne(1.0);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 		if(!fTools::equal(fOne, fValue))
174*cdf0e10cSrcweir 			mpImpl->doMulMatrix(1.0 / fValue);
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 		return *this;
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	B3DHomMatrix& B3DHomMatrix::operator*=(const B3DHomMatrix& rMat)
180*cdf0e10cSrcweir 	{
181*cdf0e10cSrcweir 		if(!rMat.isIdentity())
182*cdf0e10cSrcweir 			mpImpl->doMulMatrix(*rMat.mpImpl);
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 		return *this;
185*cdf0e10cSrcweir 	}
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	bool B3DHomMatrix::operator==(const B3DHomMatrix& rMat) const
188*cdf0e10cSrcweir 	{
189*cdf0e10cSrcweir 		if(mpImpl.same_object(rMat.mpImpl))
190*cdf0e10cSrcweir 			return true;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 		return mpImpl->isEqual(*rMat.mpImpl);
193*cdf0e10cSrcweir 	}
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 	bool B3DHomMatrix::operator!=(const B3DHomMatrix& rMat) const
196*cdf0e10cSrcweir 	{
197*cdf0e10cSrcweir         return !(*this == rMat);
198*cdf0e10cSrcweir 	}
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 	void B3DHomMatrix::rotate(double fAngleX,double fAngleY,double fAngleZ)
201*cdf0e10cSrcweir 	{
202*cdf0e10cSrcweir 		if(!fTools::equalZero(fAngleX) || !fTools::equalZero(fAngleY) || !fTools::equalZero(fAngleZ))
203*cdf0e10cSrcweir 		{
204*cdf0e10cSrcweir 			if(!fTools::equalZero(fAngleX))
205*cdf0e10cSrcweir 			{
206*cdf0e10cSrcweir 				Impl3DHomMatrix aRotMatX;
207*cdf0e10cSrcweir 				double fSin(sin(fAngleX));
208*cdf0e10cSrcweir 				double fCos(cos(fAngleX));
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 				aRotMatX.set(1, 1, fCos);
211*cdf0e10cSrcweir 				aRotMatX.set(2, 2, fCos);
212*cdf0e10cSrcweir 				aRotMatX.set(2, 1, fSin);
213*cdf0e10cSrcweir 				aRotMatX.set(1, 2, -fSin);
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 				mpImpl->doMulMatrix(aRotMatX);
216*cdf0e10cSrcweir 			}
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 			if(!fTools::equalZero(fAngleY))
219*cdf0e10cSrcweir 			{
220*cdf0e10cSrcweir 				Impl3DHomMatrix aRotMatY;
221*cdf0e10cSrcweir 				double fSin(sin(fAngleY));
222*cdf0e10cSrcweir 				double fCos(cos(fAngleY));
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 				aRotMatY.set(0, 0, fCos);
225*cdf0e10cSrcweir 				aRotMatY.set(2, 2, fCos);
226*cdf0e10cSrcweir 				aRotMatY.set(0, 2, fSin);
227*cdf0e10cSrcweir 				aRotMatY.set(2, 0, -fSin);
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 				mpImpl->doMulMatrix(aRotMatY);
230*cdf0e10cSrcweir 			}
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 			if(!fTools::equalZero(fAngleZ))
233*cdf0e10cSrcweir 			{
234*cdf0e10cSrcweir 				Impl3DHomMatrix aRotMatZ;
235*cdf0e10cSrcweir 				double fSin(sin(fAngleZ));
236*cdf0e10cSrcweir 				double fCos(cos(fAngleZ));
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 				aRotMatZ.set(0, 0, fCos);
239*cdf0e10cSrcweir 				aRotMatZ.set(1, 1, fCos);
240*cdf0e10cSrcweir 				aRotMatZ.set(1, 0, fSin);
241*cdf0e10cSrcweir 				aRotMatZ.set(0, 1, -fSin);
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 				mpImpl->doMulMatrix(aRotMatZ);
244*cdf0e10cSrcweir 			}
245*cdf0e10cSrcweir 		}
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 	void B3DHomMatrix::translate(double fX, double fY, double fZ)
249*cdf0e10cSrcweir 	{
250*cdf0e10cSrcweir 		if(!fTools::equalZero(fX) || !fTools::equalZero(fY) || !fTools::equalZero(fZ))
251*cdf0e10cSrcweir 		{
252*cdf0e10cSrcweir 			Impl3DHomMatrix aTransMat;
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 			aTransMat.set(0, 3, fX);
255*cdf0e10cSrcweir 			aTransMat.set(1, 3, fY);
256*cdf0e10cSrcweir 			aTransMat.set(2, 3, fZ);
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 			mpImpl->doMulMatrix(aTransMat);
259*cdf0e10cSrcweir 		}
260*cdf0e10cSrcweir 	}
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	void B3DHomMatrix::scale(double fX, double fY, double fZ)
263*cdf0e10cSrcweir 	{
264*cdf0e10cSrcweir 		const double fOne(1.0);
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 		if(!fTools::equal(fOne, fX) || !fTools::equal(fOne, fY) ||!fTools::equal(fOne, fZ))
267*cdf0e10cSrcweir 		{
268*cdf0e10cSrcweir 			Impl3DHomMatrix aScaleMat;
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 			aScaleMat.set(0, 0, fX);
271*cdf0e10cSrcweir 			aScaleMat.set(1, 1, fY);
272*cdf0e10cSrcweir 			aScaleMat.set(2, 2, fZ);
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 			mpImpl->doMulMatrix(aScaleMat);
275*cdf0e10cSrcweir 		}
276*cdf0e10cSrcweir 	}
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	void B3DHomMatrix::shearXY(double fSx, double fSy)
279*cdf0e10cSrcweir 	{
280*cdf0e10cSrcweir 		// #i76239# do not test againt 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
281*cdf0e10cSrcweir 		if(!fTools::equalZero(fSx) || !fTools::equalZero(fSy))
282*cdf0e10cSrcweir 		{
283*cdf0e10cSrcweir 			Impl3DHomMatrix aShearXYMat;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 			aShearXYMat.set(0, 2, fSx);
286*cdf0e10cSrcweir 			aShearXYMat.set(1, 2, fSy);
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 			mpImpl->doMulMatrix(aShearXYMat);
289*cdf0e10cSrcweir 		}
290*cdf0e10cSrcweir 	}
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 	void B3DHomMatrix::shearYZ(double fSy, double fSz)
293*cdf0e10cSrcweir 	{
294*cdf0e10cSrcweir 		// #i76239# do not test againt 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
295*cdf0e10cSrcweir 		if(!fTools::equalZero(fSy) || !fTools::equalZero(fSz))
296*cdf0e10cSrcweir 		{
297*cdf0e10cSrcweir 			Impl3DHomMatrix aShearYZMat;
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir 			aShearYZMat.set(1, 0, fSy);
300*cdf0e10cSrcweir 			aShearYZMat.set(2, 0, fSz);
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 			mpImpl->doMulMatrix(aShearYZMat);
303*cdf0e10cSrcweir 		}
304*cdf0e10cSrcweir 	}
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 	void B3DHomMatrix::shearXZ(double fSx, double fSz)
307*cdf0e10cSrcweir 	{
308*cdf0e10cSrcweir 		// #i76239# do not test againt 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
309*cdf0e10cSrcweir 		if(!fTools::equalZero(fSx) || !fTools::equalZero(fSz))
310*cdf0e10cSrcweir 		{
311*cdf0e10cSrcweir 			Impl3DHomMatrix aShearXZMat;
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 			aShearXZMat.set(0, 1, fSx);
314*cdf0e10cSrcweir 			aShearXZMat.set(2, 1, fSz);
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir 			mpImpl->doMulMatrix(aShearXZMat);
317*cdf0e10cSrcweir 		}
318*cdf0e10cSrcweir 	}
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 	void B3DHomMatrix::frustum(double fLeft, double fRight, double fBottom, double fTop, double fNear, double fFar)
321*cdf0e10cSrcweir 	{
322*cdf0e10cSrcweir 		const double fZero(0.0);
323*cdf0e10cSrcweir 		const double fOne(1.0);
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 		if(!fTools::more(fNear, fZero))
326*cdf0e10cSrcweir 		{
327*cdf0e10cSrcweir 			fNear = 0.001;
328*cdf0e10cSrcweir 		}
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir 		if(!fTools::more(fFar, fZero))
331*cdf0e10cSrcweir 		{
332*cdf0e10cSrcweir 			fFar = fOne;
333*cdf0e10cSrcweir 		}
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 		if(fTools::equal(fNear, fFar))
336*cdf0e10cSrcweir 		{
337*cdf0e10cSrcweir 			fFar = fNear + fOne;
338*cdf0e10cSrcweir 		}
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 		if(fTools::equal(fLeft, fRight))
341*cdf0e10cSrcweir 		{
342*cdf0e10cSrcweir 			fLeft -= fOne;
343*cdf0e10cSrcweir 			fRight += fOne;
344*cdf0e10cSrcweir 		}
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir 		if(fTools::equal(fTop, fBottom))
347*cdf0e10cSrcweir 		{
348*cdf0e10cSrcweir 			fBottom -= fOne;
349*cdf0e10cSrcweir 			fTop += fOne;
350*cdf0e10cSrcweir 		}
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 		Impl3DHomMatrix aFrustumMat;
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir 		aFrustumMat.set(0, 0, 2.0 * fNear / (fRight - fLeft));
355*cdf0e10cSrcweir 		aFrustumMat.set(1, 1, 2.0 * fNear / (fTop - fBottom));
356*cdf0e10cSrcweir 		aFrustumMat.set(0, 2, (fRight + fLeft) / (fRight - fLeft));
357*cdf0e10cSrcweir 		aFrustumMat.set(1, 2, (fTop + fBottom) / (fTop - fBottom));
358*cdf0e10cSrcweir 		aFrustumMat.set(2, 2, -fOne * ((fFar + fNear) / (fFar - fNear)));
359*cdf0e10cSrcweir 		aFrustumMat.set(3, 2, -fOne);
360*cdf0e10cSrcweir 		aFrustumMat.set(2, 3, -fOne * ((2.0 * fFar * fNear) / (fFar - fNear)));
361*cdf0e10cSrcweir 		aFrustumMat.set(3, 3, fZero);
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir 		mpImpl->doMulMatrix(aFrustumMat);
364*cdf0e10cSrcweir 	}
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir 	void B3DHomMatrix::ortho(double fLeft, double fRight, double fBottom, double fTop, double fNear, double fFar)
367*cdf0e10cSrcweir 	{
368*cdf0e10cSrcweir 		if(fTools::equal(fNear, fFar))
369*cdf0e10cSrcweir 		{
370*cdf0e10cSrcweir 			fFar = fNear + 1.0;
371*cdf0e10cSrcweir 		}
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir 		if(fTools::equal(fLeft, fRight))
374*cdf0e10cSrcweir 		{
375*cdf0e10cSrcweir 			fLeft -= 1.0;
376*cdf0e10cSrcweir 			fRight += 1.0;
377*cdf0e10cSrcweir 		}
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 		if(fTools::equal(fTop, fBottom))
380*cdf0e10cSrcweir 		{
381*cdf0e10cSrcweir 			fBottom -= 1.0;
382*cdf0e10cSrcweir 			fTop += 1.0;
383*cdf0e10cSrcweir 		}
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 		Impl3DHomMatrix aOrthoMat;
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir 		aOrthoMat.set(0, 0, 2.0 / (fRight - fLeft));
388*cdf0e10cSrcweir 		aOrthoMat.set(1, 1, 2.0 / (fTop - fBottom));
389*cdf0e10cSrcweir 		aOrthoMat.set(2, 2, -1.0 * (2.0 / (fFar - fNear)));
390*cdf0e10cSrcweir 		aOrthoMat.set(0, 3, -1.0 * ((fRight + fLeft) / (fRight - fLeft)));
391*cdf0e10cSrcweir 		aOrthoMat.set(1, 3, -1.0 * ((fTop + fBottom) / (fTop - fBottom)));
392*cdf0e10cSrcweir 		aOrthoMat.set(2, 3, -1.0 * ((fFar + fNear) / (fFar - fNear)));
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir 		mpImpl->doMulMatrix(aOrthoMat);
395*cdf0e10cSrcweir 	}
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir 	void B3DHomMatrix::orientation(B3DPoint aVRP, B3DVector aVPN, B3DVector aVUV)
398*cdf0e10cSrcweir 	{
399*cdf0e10cSrcweir 		Impl3DHomMatrix aOrientationMat;
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir 		// translate -VRP
402*cdf0e10cSrcweir 		aOrientationMat.set(0, 3, -aVRP.getX());
403*cdf0e10cSrcweir 		aOrientationMat.set(1, 3, -aVRP.getY());
404*cdf0e10cSrcweir 		aOrientationMat.set(2, 3, -aVRP.getZ());
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir 		// build rotation
407*cdf0e10cSrcweir 		aVUV.normalize();
408*cdf0e10cSrcweir 		aVPN.normalize();
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir 		// build x-axis as peroendicular fron aVUV and aVPN
411*cdf0e10cSrcweir 		B3DVector aRx(aVUV.getPerpendicular(aVPN));
412*cdf0e10cSrcweir 		aRx.normalize();
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 		// y-axis perpendicular to that
415*cdf0e10cSrcweir 		B3DVector aRy(aVPN.getPerpendicular(aRx));
416*cdf0e10cSrcweir 		aRy.normalize();
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 		// the calculated normals are the line vectors of the rotation matrix,
419*cdf0e10cSrcweir 		// set them to create rotation
420*cdf0e10cSrcweir 		aOrientationMat.set(0, 0, aRx.getX());
421*cdf0e10cSrcweir 		aOrientationMat.set(0, 1, aRx.getY());
422*cdf0e10cSrcweir 		aOrientationMat.set(0, 2, aRx.getZ());
423*cdf0e10cSrcweir 		aOrientationMat.set(1, 0, aRy.getX());
424*cdf0e10cSrcweir 		aOrientationMat.set(1, 1, aRy.getY());
425*cdf0e10cSrcweir 		aOrientationMat.set(1, 2, aRy.getZ());
426*cdf0e10cSrcweir 		aOrientationMat.set(2, 0, aVPN.getX());
427*cdf0e10cSrcweir 		aOrientationMat.set(2, 1, aVPN.getY());
428*cdf0e10cSrcweir 		aOrientationMat.set(2, 2, aVPN.getZ());
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir 		mpImpl->doMulMatrix(aOrientationMat);
431*cdf0e10cSrcweir 	}
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir 	bool B3DHomMatrix::decompose(B3DTuple& rScale, B3DTuple& rTranslate, B3DTuple& rRotate, B3DTuple& rShear) const
434*cdf0e10cSrcweir 	{
435*cdf0e10cSrcweir 		// when perspective is used, decompose is not made here
436*cdf0e10cSrcweir 		if(!mpImpl->isLastLineDefault())
437*cdf0e10cSrcweir 			return false;
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir 		// If determinant is zero, decomposition is not possible
440*cdf0e10cSrcweir 		if(0.0 == determinant())
441*cdf0e10cSrcweir 			return false;
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 		// isolate translation
444*cdf0e10cSrcweir 		rTranslate.setX(mpImpl->get(0, 3));
445*cdf0e10cSrcweir 		rTranslate.setY(mpImpl->get(1, 3));
446*cdf0e10cSrcweir 		rTranslate.setZ(mpImpl->get(2, 3));
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir 		// correct translate values
449*cdf0e10cSrcweir 		rTranslate.correctValues();
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir 		// get scale and shear
452*cdf0e10cSrcweir 		B3DVector aCol0(mpImpl->get(0, 0), mpImpl->get(1, 0), mpImpl->get(2, 0));
453*cdf0e10cSrcweir 		B3DVector aCol1(mpImpl->get(0, 1), mpImpl->get(1, 1), mpImpl->get(2, 1));
454*cdf0e10cSrcweir 		B3DVector aCol2(mpImpl->get(0, 2), mpImpl->get(1, 2), mpImpl->get(2, 2));
455*cdf0e10cSrcweir 		B3DVector aTemp;
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir 		// get ScaleX
458*cdf0e10cSrcweir 		rScale.setX(aCol0.getLength());
459*cdf0e10cSrcweir 		aCol0.normalize();
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir 		// get ShearXY
462*cdf0e10cSrcweir 		rShear.setX(aCol0.scalar(aCol1));
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir 		if(fTools::equalZero(rShear.getX()))
465*cdf0e10cSrcweir 		{
466*cdf0e10cSrcweir 			rShear.setX(0.0);
467*cdf0e10cSrcweir 		}
468*cdf0e10cSrcweir 		else
469*cdf0e10cSrcweir 		{
470*cdf0e10cSrcweir 			aTemp.setX(aCol1.getX() - rShear.getX() * aCol0.getX());
471*cdf0e10cSrcweir 			aTemp.setY(aCol1.getY() - rShear.getX() * aCol0.getY());
472*cdf0e10cSrcweir 			aTemp.setZ(aCol1.getZ() - rShear.getX() * aCol0.getZ());
473*cdf0e10cSrcweir 			aCol1 = aTemp;
474*cdf0e10cSrcweir 		}
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir 		// get ScaleY
477*cdf0e10cSrcweir 		rScale.setY(aCol1.getLength());
478*cdf0e10cSrcweir 		aCol1.normalize();
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir 		const double fShearX(rShear.getX());
481*cdf0e10cSrcweir 
482*cdf0e10cSrcweir 		if(!fTools::equalZero(fShearX))
483*cdf0e10cSrcweir 		{
484*cdf0e10cSrcweir 			rShear.setX(rShear.getX() / rScale.getY());
485*cdf0e10cSrcweir 		}
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir 		// get ShearXZ
488*cdf0e10cSrcweir 		rShear.setY(aCol0.scalar(aCol2));
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir 		if(fTools::equalZero(rShear.getY()))
491*cdf0e10cSrcweir 		{
492*cdf0e10cSrcweir 			rShear.setY(0.0);
493*cdf0e10cSrcweir 		}
494*cdf0e10cSrcweir 		else
495*cdf0e10cSrcweir 		{
496*cdf0e10cSrcweir 			aTemp.setX(aCol2.getX() - rShear.getY() * aCol0.getX());
497*cdf0e10cSrcweir 			aTemp.setY(aCol2.getY() - rShear.getY() * aCol0.getY());
498*cdf0e10cSrcweir 			aTemp.setZ(aCol2.getZ() - rShear.getY() * aCol0.getZ());
499*cdf0e10cSrcweir 			aCol2 = aTemp;
500*cdf0e10cSrcweir 		}
501*cdf0e10cSrcweir 
502*cdf0e10cSrcweir 		// get ShearYZ
503*cdf0e10cSrcweir 		rShear.setZ(aCol1.scalar(aCol2));
504*cdf0e10cSrcweir 
505*cdf0e10cSrcweir 		if(fTools::equalZero(rShear.getZ()))
506*cdf0e10cSrcweir 		{
507*cdf0e10cSrcweir 			rShear.setZ(0.0);
508*cdf0e10cSrcweir 		}
509*cdf0e10cSrcweir 		else
510*cdf0e10cSrcweir 		{
511*cdf0e10cSrcweir 			aTemp.setX(aCol2.getX() - rShear.getZ() * aCol1.getX());
512*cdf0e10cSrcweir 			aTemp.setY(aCol2.getY() - rShear.getZ() * aCol1.getY());
513*cdf0e10cSrcweir 			aTemp.setZ(aCol2.getZ() - rShear.getZ() * aCol1.getZ());
514*cdf0e10cSrcweir 			aCol2 = aTemp;
515*cdf0e10cSrcweir 		}
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir 		// get ScaleZ
518*cdf0e10cSrcweir 		rScale.setZ(aCol2.getLength());
519*cdf0e10cSrcweir 		aCol2.normalize();
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir 		const double fShearY(rShear.getY());
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir 		if(!fTools::equalZero(fShearY))
524*cdf0e10cSrcweir 		{
525*cdf0e10cSrcweir 			rShear.setY(rShear.getY() / rScale.getZ());
526*cdf0e10cSrcweir 		}
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir 		const double fShearZ(rShear.getZ());
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir 		if(!fTools::equalZero(fShearZ))
531*cdf0e10cSrcweir 		{
532*cdf0e10cSrcweir 			rShear.setZ(rShear.getZ() / rScale.getZ());
533*cdf0e10cSrcweir 		}
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir 		// correct shear values
536*cdf0e10cSrcweir 		rShear.correctValues();
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir 		// Coordinate system flip?
539*cdf0e10cSrcweir 		if(0.0 > aCol0.scalar(aCol1.getPerpendicular(aCol2)))
540*cdf0e10cSrcweir 		{
541*cdf0e10cSrcweir 			rScale = -rScale;
542*cdf0e10cSrcweir 			aCol0 = -aCol0;
543*cdf0e10cSrcweir 			aCol1 = -aCol1;
544*cdf0e10cSrcweir 			aCol2 = -aCol2;
545*cdf0e10cSrcweir 		}
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir 		// correct scale values
548*cdf0e10cSrcweir 		rScale.correctValues(1.0);
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir 		// Get rotations
551*cdf0e10cSrcweir         {
552*cdf0e10cSrcweir             double fy=0;
553*cdf0e10cSrcweir             double cy=0;
554*cdf0e10cSrcweir 
555*cdf0e10cSrcweir             if( ::basegfx::fTools::equal( aCol0.getZ(), 1.0 )
556*cdf0e10cSrcweir                 || aCol0.getZ() > 1.0 )
557*cdf0e10cSrcweir             {
558*cdf0e10cSrcweir                 fy = -F_PI/2.0;
559*cdf0e10cSrcweir                 cy = 0.0;
560*cdf0e10cSrcweir             }
561*cdf0e10cSrcweir             else if( ::basegfx::fTools::equal( aCol0.getZ(), -1.0 )
562*cdf0e10cSrcweir                 || aCol0.getZ() < -1.0 )
563*cdf0e10cSrcweir             {
564*cdf0e10cSrcweir                 fy = F_PI/2.0;
565*cdf0e10cSrcweir                 cy = 0.0;
566*cdf0e10cSrcweir             }
567*cdf0e10cSrcweir             else
568*cdf0e10cSrcweir             {
569*cdf0e10cSrcweir                 fy = asin( -aCol0.getZ() );
570*cdf0e10cSrcweir                 cy = cos(fy);
571*cdf0e10cSrcweir             }
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir             rRotate.setY(fy);
574*cdf0e10cSrcweir             if( ::basegfx::fTools::equalZero( cy ) )
575*cdf0e10cSrcweir             {
576*cdf0e10cSrcweir                 if( aCol0.getZ() > 0.0 )
577*cdf0e10cSrcweir                     rRotate.setX(atan2(-1.0*aCol1.getX(), aCol1.getY()));
578*cdf0e10cSrcweir                 else
579*cdf0e10cSrcweir                     rRotate.setX(atan2(aCol1.getX(), aCol1.getY()));
580*cdf0e10cSrcweir                 rRotate.setZ(0.0);
581*cdf0e10cSrcweir             }
582*cdf0e10cSrcweir             else
583*cdf0e10cSrcweir             {
584*cdf0e10cSrcweir                 rRotate.setX(atan2(aCol1.getZ(), aCol2.getZ()));
585*cdf0e10cSrcweir                 rRotate.setZ(atan2(aCol0.getY(), aCol0.getX()));
586*cdf0e10cSrcweir             }
587*cdf0e10cSrcweir 
588*cdf0e10cSrcweir             // corrcet rotate values
589*cdf0e10cSrcweir             rRotate.correctValues();
590*cdf0e10cSrcweir         }
591*cdf0e10cSrcweir 
592*cdf0e10cSrcweir 		return true;
593*cdf0e10cSrcweir 	}
594*cdf0e10cSrcweir } // end of namespace basegfx
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir // eof
597