1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef INCLUDED_BASEGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
21 #define INCLUDED_BASEGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
23 #include <sal/types.h>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <basegfx/vector/b2dvector.hxx>
26 #include <basegfx/range/b2drange.hxx>
27 #include <basegfx/basegfxdllapi.h>
34 /** If the rotation angle is an approximate multiple of pi/2,
35 force fSin/fCos to -1/0/1, to maintain orthogonality (which
36 might also be advantageous for the other cases, but: for
37 multiples of pi/2, the exact values _can_ be attained. It
38 would be largely unintuitive, if a 180 degrees rotation
39 would introduce slight roundoff errors, instead of exactly
40 mirroring the coordinate system)
42 BASEGFX_DLLPUBLIC
void createSinCosOrthogonal(double& o_rSin
, double& rCos
, double fRadiant
);
44 /** Tooling methods for on-the-fly matrix generation e.g. for inline
47 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleB2DHomMatrix(double fScaleX
, double fScaleY
);
48 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearXB2DHomMatrix(double fShearX
);
49 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearYB2DHomMatrix(double fShearY
);
50 BASEGFX_DLLPUBLIC B2DHomMatrix
createRotateB2DHomMatrix(double fRadiant
);
51 BASEGFX_DLLPUBLIC B2DHomMatrix
createTranslateB2DHomMatrix(double fTranslateX
, double fTranslateY
);
53 /// inline versions for parameters as tuples
54 inline B2DHomMatrix
createScaleB2DHomMatrix(const B2DTuple
& rScale
)
56 return createScaleB2DHomMatrix(rScale
.getX(), rScale
.getY());
59 inline B2DHomMatrix
createTranslateB2DHomMatrix(const B2DTuple
& rTranslate
)
61 return createTranslateB2DHomMatrix(rTranslate
.getX(), rTranslate
.getY());
64 /** Tooling methods for faster completely combined matrix creation
65 when scale, shearX, rotation and translation needs to be done in
66 exactly that order. It's faster since it direcly calculates
67 each matrix value based on a symbolic calculation of the three
68 matrix multiplications.
69 Inline versions for parameters as tuples added, too.
71 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleShearXRotateTranslateB2DHomMatrix(
72 double fScaleX
, double fScaleY
,
75 double fTranslateX
, double fTranslateY
);
76 inline B2DHomMatrix
createScaleShearXRotateTranslateB2DHomMatrix(
77 const B2DTuple
& rScale
,
80 const B2DTuple
& rTranslate
)
82 return createScaleShearXRotateTranslateB2DHomMatrix(
83 rScale
.getX(), rScale
.getY(),
86 rTranslate
.getX(), rTranslate
.getY());
89 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearXRotateTranslateB2DHomMatrix(
92 double fTranslateX
, double fTranslateY
);
93 inline B2DHomMatrix
createShearXRotateTranslateB2DHomMatrix(
96 const B2DTuple
& rTranslate
)
98 return createShearXRotateTranslateB2DHomMatrix(
101 rTranslate
.getX(), rTranslate
.getY());
104 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleTranslateB2DHomMatrix(
105 double fScaleX
, double fScaleY
,
106 double fTranslateX
, double fTranslateY
);
107 inline B2DHomMatrix
createScaleTranslateB2DHomMatrix(
108 const B2DTuple
& rScale
,
109 const B2DTuple
& rTranslate
)
111 return createScaleTranslateB2DHomMatrix(
112 rScale
.getX(), rScale
.getY(),
113 rTranslate
.getX(), rTranslate
.getY());
116 /// special for the often used case of rotation around a point
117 BASEGFX_DLLPUBLIC B2DHomMatrix
createRotateAroundPoint(
118 double fPointX
, double fPointY
,
120 inline B2DHomMatrix
createRotateAroundPoint(
121 const B2DTuple
& rPoint
,
124 return createRotateAroundPoint(
125 rPoint
.getX(), rPoint
.getY(),
129 /// special for the case to map from source range to target range
130 BASEGFX_DLLPUBLIC B2DHomMatrix
createSourceRangeTargetRangeTransform(
131 const B2DRange
& rSourceRange
,
132 const B2DRange
& rTargetRange
);
134 } // end of namespace tools
135 } // end of namespace basegfx
142 class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedDecompose
146 B2DVector maTranslate
;
151 B2DHomMatrixBufferedDecompose(const B2DHomMatrix
& rB2DHomMatrix
= B2DHomMatrix())
157 rB2DHomMatrix
.decompose(maScale
, maTranslate
, mfRotate
, mfShearX
);
161 B2DHomMatrix
getB2DHomMatrix() const
163 return createScaleShearXRotateTranslateB2DHomMatrix(
164 maScale
, mfShearX
, mfRotate
, maTranslate
);
167 const B2DVector
& getScale() const { return maScale
; }
168 const B2DVector
& getTranslate() const { return maTranslate
; }
169 double getRotate() const { return mfRotate
; }
170 double getShearX() const { return mfShearX
; }
172 } // end of namespace tools
173 } // end of namespace basegfx
180 class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedOnDemandDecompose
183 B2DHomMatrix maB2DHomMatrix
;
185 B2DVector maTranslate
;
189 bool mbDecomposed
: 1;
191 void impCheckDecompose()
195 maB2DHomMatrix
.decompose(maScale
, maTranslate
, mfRotate
, mfShearX
);
201 B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix
& rB2DHomMatrix
= B2DHomMatrix())
202 : maB2DHomMatrix(rB2DHomMatrix
),
212 const B2DHomMatrix
& getB2DHomMatrix() const { return maB2DHomMatrix
; }
213 const B2DVector
& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return maScale
; }
214 const B2DVector
& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return maTranslate
; }
215 double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return mfRotate
; }
217 } // end of namespace tools
219 } // end of namespace basegfx
222 #endif // INCLUDED_BASEGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */