Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / matrix / b2dhommatrixtools.cxx
blob3e39fd5e4df6a1dc19dd26f4baf2f8437efc1521
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basegfx/matrix/b2dhommatrixtools.hxx>
21 #include <rtl/ustring.hxx>
22 #include <rtl/ustrbuf.hxx>
24 ///////////////////////////////////////////////////////////////////////////////
26 namespace basegfx
28 namespace tools
30 void createSinCosOrthogonal(double& o_rSin, double& o_rCos, double fRadiant)
32 if( fTools::equalZero( fmod( fRadiant, F_PI2 ) ) )
34 // determine quadrant
35 const sal_Int32 nQuad(
36 (4 + fround( 4/F_2PI*fmod( fRadiant, F_2PI ) )) % 4 );
37 switch( nQuad )
39 case 0: // -2pi,0,2pi
40 o_rSin = 0.0;
41 o_rCos = 1.0;
42 break;
44 case 1: // -3/2pi,1/2pi
45 o_rSin = 1.0;
46 o_rCos = 0.0;
47 break;
49 case 2: // -pi,pi
50 o_rSin = 0.0;
51 o_rCos = -1.0;
52 break;
54 case 3: // -1/2pi,3/2pi
55 o_rSin = -1.0;
56 o_rCos = 0.0;
57 break;
59 default:
60 OSL_FAIL( "createSinCos: Impossible case reached" );
63 else
65 // TODO(P1): Maybe use glibc's sincos here (though
66 // that's kinda non-portable...)
67 o_rSin = sin(fRadiant);
68 o_rCos = cos(fRadiant);
72 B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY)
74 B2DHomMatrix aRetval;
75 const double fOne(1.0);
77 if(!fTools::equal(fScaleX, fOne))
79 aRetval.set(0, 0, fScaleX);
82 if(!fTools::equal(fScaleY, fOne))
84 aRetval.set(1, 1, fScaleY);
87 return aRetval;
90 B2DHomMatrix createShearXB2DHomMatrix(double fShearX)
92 B2DHomMatrix aRetval;
94 if(!fTools::equalZero(fShearX))
96 aRetval.set(0, 1, fShearX);
99 return aRetval;
102 B2DHomMatrix createShearYB2DHomMatrix(double fShearY)
104 B2DHomMatrix aRetval;
106 if(!fTools::equalZero(fShearY))
108 aRetval.set(1, 0, fShearY);
111 return aRetval;
114 B2DHomMatrix createRotateB2DHomMatrix(double fRadiant)
116 B2DHomMatrix aRetval;
118 if(!fTools::equalZero(fRadiant))
120 double fSin(0.0);
121 double fCos(1.0);
123 createSinCosOrthogonal(fSin, fCos, fRadiant);
124 aRetval.set(0, 0, fCos);
125 aRetval.set(1, 1, fCos);
126 aRetval.set(1, 0, fSin);
127 aRetval.set(0, 1, -fSin);
130 return aRetval;
133 B2DHomMatrix createTranslateB2DHomMatrix(double fTranslateX, double fTranslateY)
135 B2DHomMatrix aRetval;
137 if(!(fTools::equalZero(fTranslateX) && fTools::equalZero(fTranslateY)))
139 aRetval.set(0, 2, fTranslateX);
140 aRetval.set(1, 2, fTranslateY);
143 return aRetval;
146 B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
147 double fScaleX, double fScaleY,
148 double fShearX,
149 double fRadiant,
150 double fTranslateX, double fTranslateY)
152 const double fOne(1.0);
154 if(fTools::equal(fScaleX, fOne) && fTools::equal(fScaleY, fOne))
156 /// no scale, take shortcut
157 return createShearXRotateTranslateB2DHomMatrix(fShearX, fRadiant, fTranslateX, fTranslateY);
159 else
161 /// scale used
162 if(fTools::equalZero(fShearX))
164 /// no shear
165 if(fTools::equalZero(fRadiant))
167 /// no rotate, take shortcut
168 return createScaleTranslateB2DHomMatrix(fScaleX, fScaleY, fTranslateX, fTranslateY);
170 else
172 /// rotate and scale used, no shear
173 double fSin(0.0);
174 double fCos(1.0);
176 createSinCosOrthogonal(fSin, fCos, fRadiant);
178 B2DHomMatrix aRetval(
179 /* Row 0, Column 0 */ fCos * fScaleX,
180 /* Row 0, Column 1 */ fScaleY * -fSin,
181 /* Row 0, Column 2 */ fTranslateX,
182 /* Row 1, Column 0 */ fSin * fScaleX,
183 /* Row 1, Column 1 */ fScaleY * fCos,
184 /* Row 1, Column 2 */ fTranslateY);
186 return aRetval;
189 else
191 /// scale and shear used
192 if(fTools::equalZero(fRadiant))
194 /// scale and shear, but no rotate
195 B2DHomMatrix aRetval(
196 /* Row 0, Column 0 */ fScaleX,
197 /* Row 0, Column 1 */ fScaleY * fShearX,
198 /* Row 0, Column 2 */ fTranslateX,
199 /* Row 1, Column 0 */ 0.0,
200 /* Row 1, Column 1 */ fScaleY,
201 /* Row 1, Column 2 */ fTranslateY);
203 return aRetval;
205 else
207 /// scale, shear and rotate used
208 double fSin(0.0);
209 double fCos(1.0);
211 createSinCosOrthogonal(fSin, fCos, fRadiant);
213 B2DHomMatrix aRetval(
214 /* Row 0, Column 0 */ fCos * fScaleX,
215 /* Row 0, Column 1 */ fScaleY * ((fCos * fShearX) - fSin),
216 /* Row 0, Column 2 */ fTranslateX,
217 /* Row 1, Column 0 */ fSin * fScaleX,
218 /* Row 1, Column 1 */ fScaleY * ((fSin * fShearX) + fCos),
219 /* Row 1, Column 2 */ fTranslateY);
221 return aRetval;
227 B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
228 double fShearX,
229 double fRadiant,
230 double fTranslateX, double fTranslateY)
232 if(fTools::equalZero(fShearX))
234 /// no shear
235 if(fTools::equalZero(fRadiant))
237 /// no shear, no rotate, take shortcut
238 return createTranslateB2DHomMatrix(fTranslateX, fTranslateY);
240 else
242 /// no shear, but rotate used
243 double fSin(0.0);
244 double fCos(1.0);
246 createSinCosOrthogonal(fSin, fCos, fRadiant);
248 B2DHomMatrix aRetval(
249 /* Row 0, Column 0 */ fCos,
250 /* Row 0, Column 1 */ -fSin,
251 /* Row 0, Column 2 */ fTranslateX,
252 /* Row 1, Column 0 */ fSin,
253 /* Row 1, Column 1 */ fCos,
254 /* Row 1, Column 2 */ fTranslateY);
256 return aRetval;
259 else
261 /// shear used
262 if(fTools::equalZero(fRadiant))
264 /// no rotate, but shear used
265 B2DHomMatrix aRetval(
266 /* Row 0, Column 0 */ 1.0,
267 /* Row 0, Column 1 */ fShearX,
268 /* Row 0, Column 2 */ fTranslateX,
269 /* Row 1, Column 0 */ 0.0,
270 /* Row 1, Column 1 */ 1.0,
271 /* Row 1, Column 2 */ fTranslateY);
273 return aRetval;
275 else
277 /// shear and rotate used
278 double fSin(0.0);
279 double fCos(1.0);
281 createSinCosOrthogonal(fSin, fCos, fRadiant);
283 B2DHomMatrix aRetval(
284 /* Row 0, Column 0 */ fCos,
285 /* Row 0, Column 1 */ (fCos * fShearX) - fSin,
286 /* Row 0, Column 2 */ fTranslateX,
287 /* Row 1, Column 0 */ fSin,
288 /* Row 1, Column 1 */ (fSin * fShearX) + fCos,
289 /* Row 1, Column 2 */ fTranslateY);
291 return aRetval;
296 B2DHomMatrix createScaleTranslateB2DHomMatrix(
297 double fScaleX, double fScaleY,
298 double fTranslateX, double fTranslateY)
300 const double fOne(1.0);
302 if(fTools::equal(fScaleX, fOne) && fTools::equal(fScaleY, fOne))
304 /// no scale, take shortcut
305 return createTranslateB2DHomMatrix(fTranslateX, fTranslateY);
307 else
309 /// scale used
310 if(fTools::equalZero(fTranslateX) && fTools::equalZero(fTranslateY))
312 /// no translate, but scale.
313 B2DHomMatrix aRetval;
315 aRetval.set(0, 0, fScaleX);
316 aRetval.set(1, 1, fScaleY);
318 return aRetval;
320 else
322 /// translate and scale
323 B2DHomMatrix aRetval(
324 /* Row 0, Column 0 */ fScaleX,
325 /* Row 0, Column 1 */ 0.0,
326 /* Row 0, Column 2 */ fTranslateX,
327 /* Row 1, Column 0 */ 0.0,
328 /* Row 1, Column 1 */ fScaleY,
329 /* Row 1, Column 2 */ fTranslateY);
331 return aRetval;
336 B2DHomMatrix createRotateAroundPoint(
337 double fPointX, double fPointY,
338 double fRadiant)
340 B2DHomMatrix aRetval;
342 if(!fTools::equalZero(fRadiant))
344 double fSin(0.0);
345 double fCos(1.0);
347 createSinCosOrthogonal(fSin, fCos, fRadiant);
349 aRetval.set3x2(
350 /* Row 0, Column 0 */ fCos,
351 /* Row 0, Column 1 */ -fSin,
352 /* Row 0, Column 2 */ (fPointX * (1.0 - fCos)) + (fSin * fPointY),
353 /* Row 1, Column 0 */ fSin,
354 /* Row 1, Column 1 */ fCos,
355 /* Row 1, Column 2 */ (fPointY * (1.0 - fCos)) - (fSin * fPointX));
358 return aRetval;
361 /// special for the case to map from source range to target range
362 B2DHomMatrix createSourceRangeTargetRangeTransform(
363 const B2DRange& rSourceRange,
364 const B2DRange& rTargetRange)
366 B2DHomMatrix aRetval;
368 if(&rSourceRange == &rTargetRange)
370 return aRetval;
373 if(!fTools::equalZero(rSourceRange.getMinX()) || !fTools::equalZero(rSourceRange.getMinY()))
375 aRetval.set(0, 2, -rSourceRange.getMinX());
376 aRetval.set(1, 2, -rSourceRange.getMinY());
379 const double fSourceW(rSourceRange.getWidth());
380 const double fSourceH(rSourceRange.getHeight());
381 const bool bDivX(!fTools::equalZero(fSourceW) && !fTools::equal(fSourceW, 1.0));
382 const bool bDivY(!fTools::equalZero(fSourceH) && !fTools::equal(fSourceH, 1.0));
383 const double fScaleX(bDivX ? rTargetRange.getWidth() / fSourceW : rTargetRange.getWidth());
384 const double fScaleY(bDivY ? rTargetRange.getHeight() / fSourceH : rTargetRange.getHeight());
386 if(!fTools::equalZero(fScaleX) || !fTools::equalZero(fScaleY))
388 aRetval.scale(fScaleX, fScaleY);
391 if(!fTools::equalZero(rTargetRange.getMinX()) || !fTools::equalZero(rTargetRange.getMinY()))
393 aRetval.translate(
394 rTargetRange.getMinX(),
395 rTargetRange.getMinY());
398 return aRetval;
401 } // end of namespace tools
402 } // end of namespace basegfx
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */