xref: /AOO41X/main/basegfx/inc/basegfx/matrix/b2dhommatrixtools.hxx (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 #ifndef _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
29*cdf0e10cSrcweir #define _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <sal/types.h>
32*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
33*cdf0e10cSrcweir #include <basegfx/vector/b2dvector.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir namespace rtl { class OUString; }
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir namespace basegfx
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir 	namespace tools
42*cdf0e10cSrcweir 	{
43*cdf0e10cSrcweir 		/** If the rotation angle is an approximate multiple of pi/2,
44*cdf0e10cSrcweir 			force fSin/fCos to -1/0/1, to maintain orthogonality (which
45*cdf0e10cSrcweir 			might also be advantageous for the other cases, but: for
46*cdf0e10cSrcweir 			multiples of pi/2, the exact values _can_ be attained. It
47*cdf0e10cSrcweir 			would be largely unintuitive, if a 180 degrees rotation
48*cdf0e10cSrcweir 			would introduce slight roundoff errors, instead of exactly
49*cdf0e10cSrcweir 			mirroring the coordinate system)
50*cdf0e10cSrcweir 		 */
51*cdf0e10cSrcweir 		void createSinCosOrthogonal(double& o_rSin, double& rCos, double fRadiant);
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 		/** Tooling methods for on-the-fly matrix generation e.g. for inline
54*cdf0e10cSrcweir 			multiplications
55*cdf0e10cSrcweir 		 */
56*cdf0e10cSrcweir 		B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY);
57*cdf0e10cSrcweir 		B2DHomMatrix createShearXB2DHomMatrix(double fShearX);
58*cdf0e10cSrcweir 		B2DHomMatrix createShearYB2DHomMatrix(double fShearY);
59*cdf0e10cSrcweir 		B2DHomMatrix createRotateB2DHomMatrix(double fRadiant);
60*cdf0e10cSrcweir 		B2DHomMatrix createTranslateB2DHomMatrix(double fTranslateX, double fTranslateY);
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 		/// inline versions for parameters as tuples
63*cdf0e10cSrcweir 		inline B2DHomMatrix createScaleB2DHomMatrix(const B2DTuple& rScale)
64*cdf0e10cSrcweir 		{
65*cdf0e10cSrcweir 			return createScaleB2DHomMatrix(rScale.getX(), rScale.getY());
66*cdf0e10cSrcweir 		}
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 		inline B2DHomMatrix createTranslateB2DHomMatrix(const B2DTuple& rTranslate)
69*cdf0e10cSrcweir 		{
70*cdf0e10cSrcweir 			return createTranslateB2DHomMatrix(rTranslate.getX(), rTranslate.getY());
71*cdf0e10cSrcweir 		}
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 		/** Tooling methods for faster completely combined matrix creation
74*cdf0e10cSrcweir 			when scale, shearX, rotation and translation needs to be done in
75*cdf0e10cSrcweir 			exactly that order. It's faster since it direcly calculates
76*cdf0e10cSrcweir 			each matrix value based on a symbolic calculation of the three
77*cdf0e10cSrcweir 			matrix multiplications.
78*cdf0e10cSrcweir 			Inline versions for parameters as tuples added, too.
79*cdf0e10cSrcweir 		 */
80*cdf0e10cSrcweir 		B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
81*cdf0e10cSrcweir 			double fScaleX, double fScaleY,
82*cdf0e10cSrcweir 			double fShearX,
83*cdf0e10cSrcweir 			double fRadiant,
84*cdf0e10cSrcweir 			double fTranslateX, double fTranslateY);
85*cdf0e10cSrcweir 		inline B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
86*cdf0e10cSrcweir 			const B2DTuple& rScale,
87*cdf0e10cSrcweir 			double fShearX,
88*cdf0e10cSrcweir 			double fRadiant,
89*cdf0e10cSrcweir 			const B2DTuple& rTranslate)
90*cdf0e10cSrcweir 		{
91*cdf0e10cSrcweir 			return createScaleShearXRotateTranslateB2DHomMatrix(
92*cdf0e10cSrcweir 				rScale.getX(), rScale.getY(),
93*cdf0e10cSrcweir 				fShearX,
94*cdf0e10cSrcweir 				fRadiant,
95*cdf0e10cSrcweir 				rTranslate.getX(), rTranslate.getY());
96*cdf0e10cSrcweir 		}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 		B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
99*cdf0e10cSrcweir 			double fShearX,
100*cdf0e10cSrcweir 			double fRadiant,
101*cdf0e10cSrcweir 			double fTranslateX, double fTranslateY);
102*cdf0e10cSrcweir 		inline B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
103*cdf0e10cSrcweir 			double fShearX,
104*cdf0e10cSrcweir 			double fRadiant,
105*cdf0e10cSrcweir 			const B2DTuple& rTranslate)
106*cdf0e10cSrcweir 		{
107*cdf0e10cSrcweir 			return createShearXRotateTranslateB2DHomMatrix(
108*cdf0e10cSrcweir 				fShearX,
109*cdf0e10cSrcweir 				fRadiant,
110*cdf0e10cSrcweir 				rTranslate.getX(), rTranslate.getY());
111*cdf0e10cSrcweir 		}
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		B2DHomMatrix createScaleTranslateB2DHomMatrix(
114*cdf0e10cSrcweir 			double fScaleX, double fScaleY,
115*cdf0e10cSrcweir 			double fTranslateX, double fTranslateY);
116*cdf0e10cSrcweir 		inline B2DHomMatrix createScaleTranslateB2DHomMatrix(
117*cdf0e10cSrcweir 			const B2DTuple& rScale,
118*cdf0e10cSrcweir 			const B2DTuple& rTranslate)
119*cdf0e10cSrcweir 		{
120*cdf0e10cSrcweir 			return createScaleTranslateB2DHomMatrix(
121*cdf0e10cSrcweir 				rScale.getX(), rScale.getY(),
122*cdf0e10cSrcweir 				rTranslate.getX(), rTranslate.getY());
123*cdf0e10cSrcweir 		}
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         /// special for the often used case of rotation around a point
126*cdf0e10cSrcweir         B2DHomMatrix createRotateAroundPoint(
127*cdf0e10cSrcweir             double fPointX, double fPointY,
128*cdf0e10cSrcweir             double fRadiant);
129*cdf0e10cSrcweir         inline B2DHomMatrix createRotateAroundPoint(
130*cdf0e10cSrcweir             const B2DTuple& rPoint,
131*cdf0e10cSrcweir             double fRadiant)
132*cdf0e10cSrcweir         {
133*cdf0e10cSrcweir             return createRotateAroundPoint(
134*cdf0e10cSrcweir                 rPoint.getX(), rPoint.getY(),
135*cdf0e10cSrcweir                 fRadiant);
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     } // end of namespace tools
139*cdf0e10cSrcweir } // end of namespace basegfx
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir namespace basegfx
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	namespace tools
146*cdf0e10cSrcweir 	{
147*cdf0e10cSrcweir 		class B2DHomMatrixBufferedDecompose
148*cdf0e10cSrcweir 		{
149*cdf0e10cSrcweir 		private:
150*cdf0e10cSrcweir 			B2DVector              maScale;
151*cdf0e10cSrcweir 			B2DVector              maTranslate;
152*cdf0e10cSrcweir 			double                 mfRotate;
153*cdf0e10cSrcweir 			double                 mfShearX;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 		public:
156*cdf0e10cSrcweir 			B2DHomMatrixBufferedDecompose(const B2DHomMatrix& rB2DHomMatrix)
157*cdf0e10cSrcweir 			:   maScale(),
158*cdf0e10cSrcweir 				maTranslate(),
159*cdf0e10cSrcweir 				mfRotate(0.0),
160*cdf0e10cSrcweir 				mfShearX(0.0)
161*cdf0e10cSrcweir 			{
162*cdf0e10cSrcweir 				rB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
163*cdf0e10cSrcweir 			}
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 			// data access
166*cdf0e10cSrcweir 			B2DHomMatrix getB2DHomMatrix() const
167*cdf0e10cSrcweir 			{
168*cdf0e10cSrcweir 				return createScaleShearXRotateTranslateB2DHomMatrix(
169*cdf0e10cSrcweir 					maScale, mfShearX, mfRotate, maTranslate);
170*cdf0e10cSrcweir 			}
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 			const B2DVector& getScale() const { return maScale; }
173*cdf0e10cSrcweir 			const B2DVector& getTranslate() const { return maTranslate; }
174*cdf0e10cSrcweir 			double getRotate() const { return mfRotate; }
175*cdf0e10cSrcweir 			double getShearX() const { return mfShearX; }
176*cdf0e10cSrcweir 		};
177*cdf0e10cSrcweir     } // end of namespace tools
178*cdf0e10cSrcweir } // end of namespace basegfx
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir namespace basegfx
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	namespace tools
185*cdf0e10cSrcweir 	{
186*cdf0e10cSrcweir 		class B2DHomMatrixBufferedOnDemandDecompose
187*cdf0e10cSrcweir 		{
188*cdf0e10cSrcweir 		private:
189*cdf0e10cSrcweir 			B2DHomMatrix           maB2DHomMatrix;
190*cdf0e10cSrcweir 			B2DVector              maScale;
191*cdf0e10cSrcweir 			B2DVector              maTranslate;
192*cdf0e10cSrcweir 			double                 mfRotate;
193*cdf0e10cSrcweir 			double                 mfShearX;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 			// bitfield
196*cdf0e10cSrcweir 			unsigned               mbDecomposed : 1;
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 			void impCheckDecompose()
199*cdf0e10cSrcweir 			{
200*cdf0e10cSrcweir 				if(!mbDecomposed)
201*cdf0e10cSrcweir 				{
202*cdf0e10cSrcweir 					maB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
203*cdf0e10cSrcweir 					mbDecomposed = true;
204*cdf0e10cSrcweir 				}
205*cdf0e10cSrcweir 			}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		public:
208*cdf0e10cSrcweir 			B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix& rB2DHomMatrix)
209*cdf0e10cSrcweir 			:   maB2DHomMatrix(rB2DHomMatrix),
210*cdf0e10cSrcweir 				maScale(),
211*cdf0e10cSrcweir 				maTranslate(),
212*cdf0e10cSrcweir 				mfRotate(0.0),
213*cdf0e10cSrcweir 				mfShearX(0.0),
214*cdf0e10cSrcweir 				mbDecomposed(false)
215*cdf0e10cSrcweir 			{
216*cdf0e10cSrcweir 			}
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 			// data access
219*cdf0e10cSrcweir 			const B2DHomMatrix& getB2DHomMatrix() const { return maB2DHomMatrix; }
220*cdf0e10cSrcweir 			const B2DVector& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maScale; }
221*cdf0e10cSrcweir 			const B2DVector& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maTranslate; }
222*cdf0e10cSrcweir 			double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfRotate; }
223*cdf0e10cSrcweir 			double getShearX() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfShearX; }
224*cdf0e10cSrcweir 		};
225*cdf0e10cSrcweir     } // end of namespace tools
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     /// Returns a string with svg's "matrix(m00,m10,m01,m11,m02,m12)" representation
228*cdf0e10cSrcweir     ::rtl::OUString exportToSvg( const B2DHomMatrix& rMatrix );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir } // end of namespace basegfx
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir #endif /* _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX */
235