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 _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
21 #define _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
23 #include <sal/types.h>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <basegfx/vector/b2dvector.hxx>
26 #include <basegfx/basegfxdllapi.h>
29 ///////////////////////////////////////////////////////////////////////////////
35 /** If the rotation angle is an approximate multiple of pi/2,
36 force fSin/fCos to -1/0/1, to maintain orthogonality (which
37 might also be advantageous for the other cases, but: for
38 multiples of pi/2, the exact values _can_ be attained. It
39 would be largely unintuitive, if a 180 degrees rotation
40 would introduce slight roundoff errors, instead of exactly
41 mirroring the coordinate system)
43 BASEGFX_DLLPUBLIC
void createSinCosOrthogonal(double& o_rSin
, double& rCos
, double fRadiant
);
45 /** Tooling methods for on-the-fly matrix generation e.g. for inline
48 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleB2DHomMatrix(double fScaleX
, double fScaleY
);
49 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearXB2DHomMatrix(double fShearX
);
50 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearYB2DHomMatrix(double fShearY
);
51 BASEGFX_DLLPUBLIC B2DHomMatrix
createRotateB2DHomMatrix(double fRadiant
);
52 BASEGFX_DLLPUBLIC B2DHomMatrix
createTranslateB2DHomMatrix(double fTranslateX
, double fTranslateY
);
54 /// inline versions for parameters as tuples
55 inline B2DHomMatrix
createScaleB2DHomMatrix(const B2DTuple
& rScale
)
57 return createScaleB2DHomMatrix(rScale
.getX(), rScale
.getY());
60 inline B2DHomMatrix
createTranslateB2DHomMatrix(const B2DTuple
& rTranslate
)
62 return createTranslateB2DHomMatrix(rTranslate
.getX(), rTranslate
.getY());
65 /** Tooling methods for faster completely combined matrix creation
66 when scale, shearX, rotation and translation needs to be done in
67 exactly that order. It's faster since it direcly calculates
68 each matrix value based on a symbolic calculation of the three
69 matrix multiplications.
70 Inline versions for parameters as tuples added, too.
72 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleShearXRotateTranslateB2DHomMatrix(
73 double fScaleX
, double fScaleY
,
76 double fTranslateX
, double fTranslateY
);
77 inline B2DHomMatrix
createScaleShearXRotateTranslateB2DHomMatrix(
78 const B2DTuple
& rScale
,
81 const B2DTuple
& rTranslate
)
83 return createScaleShearXRotateTranslateB2DHomMatrix(
84 rScale
.getX(), rScale
.getY(),
87 rTranslate
.getX(), rTranslate
.getY());
90 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearXRotateTranslateB2DHomMatrix(
93 double fTranslateX
, double fTranslateY
);
94 inline B2DHomMatrix
createShearXRotateTranslateB2DHomMatrix(
97 const B2DTuple
& rTranslate
)
99 return createShearXRotateTranslateB2DHomMatrix(
102 rTranslate
.getX(), rTranslate
.getY());
105 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleTranslateB2DHomMatrix(
106 double fScaleX
, double fScaleY
,
107 double fTranslateX
, double fTranslateY
);
108 inline B2DHomMatrix
createScaleTranslateB2DHomMatrix(
109 const B2DTuple
& rScale
,
110 const B2DTuple
& rTranslate
)
112 return createScaleTranslateB2DHomMatrix(
113 rScale
.getX(), rScale
.getY(),
114 rTranslate
.getX(), rTranslate
.getY());
117 /// special for the often used case of rotation around a point
118 BASEGFX_DLLPUBLIC B2DHomMatrix
createRotateAroundPoint(
119 double fPointX
, double fPointY
,
121 inline B2DHomMatrix
createRotateAroundPoint(
122 const B2DTuple
& rPoint
,
125 return createRotateAroundPoint(
126 rPoint
.getX(), rPoint
.getY(),
130 } // end of namespace tools
131 } // end of namespace basegfx
133 ///////////////////////////////////////////////////////////////////////////////
139 class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedDecompose
143 B2DVector maTranslate
;
148 B2DHomMatrixBufferedDecompose(const B2DHomMatrix
& rB2DHomMatrix
= B2DHomMatrix())
154 rB2DHomMatrix
.decompose(maScale
, maTranslate
, mfRotate
, mfShearX
);
158 B2DHomMatrix
getB2DHomMatrix() const
160 return createScaleShearXRotateTranslateB2DHomMatrix(
161 maScale
, mfShearX
, mfRotate
, maTranslate
);
164 const B2DVector
& getScale() const { return maScale
; }
165 const B2DVector
& getTranslate() const { return maTranslate
; }
166 double getRotate() const { return mfRotate
; }
167 double getShearX() const { return mfShearX
; }
169 } // end of namespace tools
170 } // end of namespace basegfx
172 ///////////////////////////////////////////////////////////////////////////////
178 class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedOnDemandDecompose
181 B2DHomMatrix maB2DHomMatrix
;
183 B2DVector maTranslate
;
188 unsigned mbDecomposed
: 1;
190 void impCheckDecompose()
194 maB2DHomMatrix
.decompose(maScale
, maTranslate
, mfRotate
, mfShearX
);
200 B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix
& rB2DHomMatrix
= B2DHomMatrix())
201 : maB2DHomMatrix(rB2DHomMatrix
),
211 const B2DHomMatrix
& getB2DHomMatrix() const { return maB2DHomMatrix
; }
212 const B2DVector
& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return maScale
; }
213 const B2DVector
& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return maTranslate
; }
214 double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return mfRotate
; }
215 double getShearX() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return mfShearX
; }
217 } // end of namespace tools
219 } // end of namespace basegfx
221 ///////////////////////////////////////////////////////////////////////////////
223 #endif /* _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX */
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */