xref: /AOO41X/main/basegfx/inc/basegfx/matrix/b2dhommatrixtools.hxx (revision ddde725d65c83fe3ba1186d46f6e3e08f12ba47e)
1ce9c7ef7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ce9c7ef7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ce9c7ef7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ce9c7ef7SAndrew Rist  * distributed with this work for additional information
6ce9c7ef7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ce9c7ef7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ce9c7ef7SAndrew Rist  * "License"); you may not use this file except in compliance
9ce9c7ef7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11ce9c7ef7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13ce9c7ef7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ce9c7ef7SAndrew Rist  * software distributed under the License is distributed on an
15ce9c7ef7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ce9c7ef7SAndrew Rist  * KIND, either express or implied.  See the License for the
17ce9c7ef7SAndrew Rist  * specific language governing permissions and limitations
18ce9c7ef7SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20ce9c7ef7SAndrew Rist  *************************************************************/
21ce9c7ef7SAndrew Rist 
22ce9c7ef7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
25cdf0e10cSrcweir #define _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
29cdf0e10cSrcweir #include <basegfx/vector/b2dvector.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace rtl { class OUString; }
32cdf0e10cSrcweir 
33cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace basegfx
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 	namespace tools
38cdf0e10cSrcweir 	{
39cdf0e10cSrcweir 		/** If the rotation angle is an approximate multiple of pi/2,
40cdf0e10cSrcweir 			force fSin/fCos to -1/0/1, to maintain orthogonality (which
41cdf0e10cSrcweir 			might also be advantageous for the other cases, but: for
42cdf0e10cSrcweir 			multiples of pi/2, the exact values _can_ be attained. It
43cdf0e10cSrcweir 			would be largely unintuitive, if a 180 degrees rotation
44cdf0e10cSrcweir 			would introduce slight roundoff errors, instead of exactly
45cdf0e10cSrcweir 			mirroring the coordinate system)
46cdf0e10cSrcweir 		 */
47cdf0e10cSrcweir 		void createSinCosOrthogonal(double& o_rSin, double& rCos, double fRadiant);
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 		/** Tooling methods for on-the-fly matrix generation e.g. for inline
50cdf0e10cSrcweir 			multiplications
51cdf0e10cSrcweir 		 */
52cdf0e10cSrcweir 		B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY);
53cdf0e10cSrcweir 		B2DHomMatrix createShearXB2DHomMatrix(double fShearX);
54cdf0e10cSrcweir 		B2DHomMatrix createShearYB2DHomMatrix(double fShearY);
55cdf0e10cSrcweir 		B2DHomMatrix createRotateB2DHomMatrix(double fRadiant);
56cdf0e10cSrcweir 		B2DHomMatrix createTranslateB2DHomMatrix(double fTranslateX, double fTranslateY);
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 		/// inline versions for parameters as tuples
59cdf0e10cSrcweir 		inline B2DHomMatrix createScaleB2DHomMatrix(const B2DTuple& rScale)
60cdf0e10cSrcweir 		{
61cdf0e10cSrcweir 			return createScaleB2DHomMatrix(rScale.getX(), rScale.getY());
62cdf0e10cSrcweir 		}
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 		inline B2DHomMatrix createTranslateB2DHomMatrix(const B2DTuple& rTranslate)
65cdf0e10cSrcweir 		{
66cdf0e10cSrcweir 			return createTranslateB2DHomMatrix(rTranslate.getX(), rTranslate.getY());
67cdf0e10cSrcweir 		}
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 		/** Tooling methods for faster completely combined matrix creation
70cdf0e10cSrcweir 			when scale, shearX, rotation and translation needs to be done in
71cdf0e10cSrcweir 			exactly that order. It's faster since it direcly calculates
72cdf0e10cSrcweir 			each matrix value based on a symbolic calculation of the three
73cdf0e10cSrcweir 			matrix multiplications.
74cdf0e10cSrcweir 			Inline versions for parameters as tuples added, too.
75cdf0e10cSrcweir 		 */
76cdf0e10cSrcweir 		B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
77cdf0e10cSrcweir 			double fScaleX, double fScaleY,
78cdf0e10cSrcweir 			double fShearX,
79cdf0e10cSrcweir 			double fRadiant,
80cdf0e10cSrcweir 			double fTranslateX, double fTranslateY);
81cdf0e10cSrcweir 		inline B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
82cdf0e10cSrcweir 			const B2DTuple& rScale,
83cdf0e10cSrcweir 			double fShearX,
84cdf0e10cSrcweir 			double fRadiant,
85cdf0e10cSrcweir 			const B2DTuple& rTranslate)
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			return createScaleShearXRotateTranslateB2DHomMatrix(
88cdf0e10cSrcweir 				rScale.getX(), rScale.getY(),
89cdf0e10cSrcweir 				fShearX,
90cdf0e10cSrcweir 				fRadiant,
91cdf0e10cSrcweir 				rTranslate.getX(), rTranslate.getY());
92cdf0e10cSrcweir 		}
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 		B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
95cdf0e10cSrcweir 			double fShearX,
96cdf0e10cSrcweir 			double fRadiant,
97cdf0e10cSrcweir 			double fTranslateX, double fTranslateY);
98cdf0e10cSrcweir 		inline B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
99cdf0e10cSrcweir 			double fShearX,
100cdf0e10cSrcweir 			double fRadiant,
101cdf0e10cSrcweir 			const B2DTuple& rTranslate)
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir 			return createShearXRotateTranslateB2DHomMatrix(
104cdf0e10cSrcweir 				fShearX,
105cdf0e10cSrcweir 				fRadiant,
106cdf0e10cSrcweir 				rTranslate.getX(), rTranslate.getY());
107cdf0e10cSrcweir 		}
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		B2DHomMatrix createScaleTranslateB2DHomMatrix(
110cdf0e10cSrcweir 			double fScaleX, double fScaleY,
111cdf0e10cSrcweir 			double fTranslateX, double fTranslateY);
112cdf0e10cSrcweir 		inline B2DHomMatrix createScaleTranslateB2DHomMatrix(
113cdf0e10cSrcweir 			const B2DTuple& rScale,
114cdf0e10cSrcweir 			const B2DTuple& rTranslate)
115cdf0e10cSrcweir 		{
116cdf0e10cSrcweir 			return createScaleTranslateB2DHomMatrix(
117cdf0e10cSrcweir 				rScale.getX(), rScale.getY(),
118cdf0e10cSrcweir 				rTranslate.getX(), rTranslate.getY());
119cdf0e10cSrcweir 		}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         /// special for the often used case of rotation around a point
122cdf0e10cSrcweir         B2DHomMatrix createRotateAroundPoint(
123cdf0e10cSrcweir             double fPointX, double fPointY,
124cdf0e10cSrcweir             double fRadiant);
125cdf0e10cSrcweir         inline B2DHomMatrix createRotateAroundPoint(
126cdf0e10cSrcweir             const B2DTuple& rPoint,
127cdf0e10cSrcweir             double fRadiant)
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             return createRotateAroundPoint(
130cdf0e10cSrcweir                 rPoint.getX(), rPoint.getY(),
131cdf0e10cSrcweir                 fRadiant);
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     } // end of namespace tools
135cdf0e10cSrcweir } // end of namespace basegfx
136cdf0e10cSrcweir 
137cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
138cdf0e10cSrcweir 
139cdf0e10cSrcweir namespace basegfx
140cdf0e10cSrcweir {
141cdf0e10cSrcweir 	namespace tools
142cdf0e10cSrcweir 	{
143cdf0e10cSrcweir 		class B2DHomMatrixBufferedDecompose
144cdf0e10cSrcweir 		{
145cdf0e10cSrcweir 		private:
146cdf0e10cSrcweir 			B2DVector              maScale;
147cdf0e10cSrcweir 			B2DVector              maTranslate;
148cdf0e10cSrcweir 			double                 mfRotate;
149cdf0e10cSrcweir 			double                 mfShearX;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		public:
152*ddde725dSArmin Le Grand 			B2DHomMatrixBufferedDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
153cdf0e10cSrcweir 			:   maScale(),
154cdf0e10cSrcweir 				maTranslate(),
155cdf0e10cSrcweir 				mfRotate(0.0),
156cdf0e10cSrcweir 				mfShearX(0.0)
157cdf0e10cSrcweir 			{
158cdf0e10cSrcweir 				rB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
159cdf0e10cSrcweir 			}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 			// data access
162cdf0e10cSrcweir 			B2DHomMatrix getB2DHomMatrix() const
163cdf0e10cSrcweir 			{
164cdf0e10cSrcweir 				return createScaleShearXRotateTranslateB2DHomMatrix(
165cdf0e10cSrcweir 					maScale, mfShearX, mfRotate, maTranslate);
166cdf0e10cSrcweir 			}
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 			const B2DVector& getScale() const { return maScale; }
169cdf0e10cSrcweir 			const B2DVector& getTranslate() const { return maTranslate; }
170cdf0e10cSrcweir 			double getRotate() const { return mfRotate; }
171cdf0e10cSrcweir 			double getShearX() const { return mfShearX; }
172cdf0e10cSrcweir 		};
173cdf0e10cSrcweir     } // end of namespace tools
174cdf0e10cSrcweir } // end of namespace basegfx
175cdf0e10cSrcweir 
176cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
177cdf0e10cSrcweir 
178cdf0e10cSrcweir namespace basegfx
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	namespace tools
181cdf0e10cSrcweir 	{
182cdf0e10cSrcweir 		class B2DHomMatrixBufferedOnDemandDecompose
183cdf0e10cSrcweir 		{
184cdf0e10cSrcweir 		private:
185cdf0e10cSrcweir 			B2DHomMatrix           maB2DHomMatrix;
186cdf0e10cSrcweir 			B2DVector              maScale;
187cdf0e10cSrcweir 			B2DVector              maTranslate;
188cdf0e10cSrcweir 			double                 mfRotate;
189cdf0e10cSrcweir 			double                 mfShearX;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			// bitfield
192cdf0e10cSrcweir 			unsigned               mbDecomposed : 1;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 			void impCheckDecompose()
195cdf0e10cSrcweir 			{
196cdf0e10cSrcweir 				if(!mbDecomposed)
197cdf0e10cSrcweir 				{
198cdf0e10cSrcweir 					maB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
199cdf0e10cSrcweir 					mbDecomposed = true;
200cdf0e10cSrcweir 				}
201cdf0e10cSrcweir 			}
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 		public:
204*ddde725dSArmin Le Grand 			B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
205cdf0e10cSrcweir 			:   maB2DHomMatrix(rB2DHomMatrix),
206cdf0e10cSrcweir 				maScale(),
207cdf0e10cSrcweir 				maTranslate(),
208cdf0e10cSrcweir 				mfRotate(0.0),
209cdf0e10cSrcweir 				mfShearX(0.0),
210cdf0e10cSrcweir 				mbDecomposed(false)
211cdf0e10cSrcweir 			{
212cdf0e10cSrcweir 			}
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 			// data access
215cdf0e10cSrcweir 			const B2DHomMatrix& getB2DHomMatrix() const { return maB2DHomMatrix; }
216cdf0e10cSrcweir 			const B2DVector& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maScale; }
217cdf0e10cSrcweir 			const B2DVector& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maTranslate; }
218cdf0e10cSrcweir 			double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfRotate; }
219cdf0e10cSrcweir 			double getShearX() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfShearX; }
220cdf0e10cSrcweir 		};
221cdf0e10cSrcweir     } // end of namespace tools
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     /// Returns a string with svg's "matrix(m00,m10,m01,m11,m02,m12)" representation
224cdf0e10cSrcweir     ::rtl::OUString exportToSvg( const B2DHomMatrix& rMatrix );
225cdf0e10cSrcweir 
226cdf0e10cSrcweir } // end of namespace basegfx
227cdf0e10cSrcweir 
228cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
229cdf0e10cSrcweir 
230cdf0e10cSrcweir #endif /* _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX */
231