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 .
22 #include <sal/types.h>
23 #include <basegfx/matrix/b2dhommatrix.hxx>
24 #include <basegfx/vector/b2dvector.hxx>
25 #include <basegfx/point/b2dpoint.hxx>
26 #include <basegfx/tuple/b2dtuple.hxx>
27 #include <basegfx/basegfxdllapi.h>
29 namespace basegfx
{ class B2DRange
; }
31 namespace basegfx::utils
33 /** If the rotation angle is an approximate multiple of pi/2,
34 force fSin/fCos to -1/0/1, to maintain orthogonality (which
35 might also be advantageous for the other cases, but: for
36 multiples of pi/2, the exact values _can_ be attained. It
37 would be largely unintuitive, if a 180 degrees rotation
38 would introduce slight roundoff errors, instead of exactly
39 mirroring the coordinate system)
41 BASEGFX_DLLPUBLIC
void createSinCosOrthogonal(double& o_rSin
, double& rCos
, double fRadiant
);
43 /** Tooling methods for on-the-fly matrix generation e.g. for inline
46 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleB2DHomMatrix(double fScaleX
, double fScaleY
);
47 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearXB2DHomMatrix(double fShearX
);
48 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearYB2DHomMatrix(double fShearY
);
49 BASEGFX_DLLPUBLIC B2DHomMatrix
createRotateB2DHomMatrix(double fRadiant
);
50 BASEGFX_DLLPUBLIC B2DHomMatrix
createTranslateB2DHomMatrix(double fTranslateX
, double fTranslateY
);
52 /// inline versions for parameters as tuples
53 inline B2DHomMatrix
createScaleB2DHomMatrix(const B2DTuple
& rScale
)
55 return createScaleB2DHomMatrix(rScale
.getX(), rScale
.getY());
58 inline B2DHomMatrix
createTranslateB2DHomMatrix(const B2DTuple
& rTranslate
)
60 return createTranslateB2DHomMatrix(rTranslate
.getX(), rTranslate
.getY());
63 /** Tooling methods for faster completely combined matrix creation
64 when scale, shearX, rotation and translation needs to be done in
65 exactly that order. It's faster since it directly calculates
66 each matrix value based on a symbolic calculation of the three
67 matrix multiplications.
68 Inline versions for parameters as tuples added, too.
70 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleShearXRotateTranslateB2DHomMatrix(
71 double fScaleX
, double fScaleY
,
74 double fTranslateX
, double fTranslateY
);
75 inline B2DHomMatrix
createScaleShearXRotateTranslateB2DHomMatrix(
76 const B2DTuple
& rScale
,
79 const B2DTuple
& rTranslate
)
81 return createScaleShearXRotateTranslateB2DHomMatrix(
82 rScale
.getX(), rScale
.getY(),
85 rTranslate
.getX(), rTranslate
.getY());
88 BASEGFX_DLLPUBLIC B2DHomMatrix
createShearXRotateTranslateB2DHomMatrix(
91 double fTranslateX
, double fTranslateY
);
92 inline B2DHomMatrix
createShearXRotateTranslateB2DHomMatrix(
95 const B2DTuple
& rTranslate
)
97 return createShearXRotateTranslateB2DHomMatrix(
100 rTranslate
.getX(), rTranslate
.getY());
103 BASEGFX_DLLPUBLIC B2DHomMatrix
createScaleTranslateB2DHomMatrix(
104 double fScaleX
, double fScaleY
,
105 double fTranslateX
, double fTranslateY
);
106 inline B2DHomMatrix
createScaleTranslateB2DHomMatrix(
107 const B2DTuple
& rScale
,
108 const B2DTuple
& rTranslate
)
110 return createScaleTranslateB2DHomMatrix(
111 rScale
.getX(), rScale
.getY(),
112 rTranslate
.getX(), rTranslate
.getY());
115 /// special for the often used case of rotation around a point
116 BASEGFX_DLLPUBLIC B2DHomMatrix
createRotateAroundPoint(
117 double fPointX
, double fPointY
,
119 inline B2DHomMatrix
createRotateAroundPoint(
120 const B2DTuple
& rPoint
,
123 return createRotateAroundPoint(
124 rPoint
.getX(), rPoint
.getY(),
128 /// special for creating a mapping for a Range rotated around it's center
129 /// while keeping AspectRatio unchanged and staying inside the given Range
130 /// by optimally using the available space (no overlap or outside allowed)
131 BASEGFX_DLLPUBLIC B2DHomMatrix
createRotateAroundCenterKeepAspectRatioStayInsideRange(
132 const basegfx::B2DRange
& rTargetRange
,
135 /// special for the case to map from source range to target range
136 BASEGFX_DLLPUBLIC B2DHomMatrix
createSourceRangeTargetRangeTransform(
137 const B2DRange
& rSourceRange
,
138 const B2DRange
& rTargetRange
);
140 /// create based on given CoordinateSystem which is defined by origin and x/yaxis
141 BASEGFX_DLLPUBLIC B2DHomMatrix
createCoordinateSystemTransform(
142 const B2DPoint
& rOrigin
,
144 const B2DVector
& rY
);
146 /// get column vector from B2dHomMatrix, e.g. to extract coordinate system origin and x/yaxis
147 BASEGFX_DLLPUBLIC B2DTuple
getColumn(const B2DHomMatrix
& rMatrix
, sal_uInt16 nCol
);
150 class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedDecompose
154 B2DVector maTranslate
;
159 B2DHomMatrixBufferedDecompose(const B2DHomMatrix
& rB2DHomMatrix
= B2DHomMatrix())
165 rB2DHomMatrix
.decompose(maScale
, maTranslate
, mfRotate
, mfShearX
);
169 B2DHomMatrix
getB2DHomMatrix() const
171 return createScaleShearXRotateTranslateB2DHomMatrix(
172 maScale
, mfShearX
, mfRotate
, maTranslate
);
175 const B2DVector
& getScale() const { return maScale
; }
176 const B2DVector
& getTranslate() const { return maTranslate
; }
177 double getRotate() const { return mfRotate
; }
178 double getShearX() const { return mfShearX
; }
181 class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedOnDemandDecompose
184 B2DHomMatrix maB2DHomMatrix
;
186 B2DVector maTranslate
;
190 bool mbDecomposed
: 1;
192 void impCheckDecompose()
196 maB2DHomMatrix
.decompose(maScale
, maTranslate
, mfRotate
, mfShearX
);
202 B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix
& rB2DHomMatrix
= B2DHomMatrix())
203 : maB2DHomMatrix(rB2DHomMatrix
),
213 const B2DHomMatrix
& getB2DHomMatrix() const { return maB2DHomMatrix
; }
214 const B2DVector
& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return maScale
; }
215 const B2DVector
& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return maTranslate
; }
216 double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return mfRotate
; }
217 double getShearX() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose
* >(this)->impCheckDecompose(); return mfShearX
; }
219 } // end of namespace basegfx::utils
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */