Bump version to 21.06.18.1
[LibreOffice.git] / include / basegfx / matrix / b2dhommatrixtools.hxx
blobb7159aef6165c34b69935e7e5b3b8a108db41d10
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 #pragma once
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
44 multiplications
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,
72 double fShearX,
73 double fRadiant,
74 double fTranslateX, double fTranslateY);
75 inline B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
76 const B2DTuple& rScale,
77 double fShearX,
78 double fRadiant,
79 const B2DTuple& rTranslate)
81 return createScaleShearXRotateTranslateB2DHomMatrix(
82 rScale.getX(), rScale.getY(),
83 fShearX,
84 fRadiant,
85 rTranslate.getX(), rTranslate.getY());
88 BASEGFX_DLLPUBLIC B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
89 double fShearX,
90 double fRadiant,
91 double fTranslateX, double fTranslateY);
92 inline B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
93 double fShearX,
94 double fRadiant,
95 const B2DTuple& rTranslate)
97 return createShearXRotateTranslateB2DHomMatrix(
98 fShearX,
99 fRadiant,
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,
118 double fRadiant);
119 inline B2DHomMatrix createRotateAroundPoint(
120 const B2DTuple& rPoint,
121 double fRadiant)
123 return createRotateAroundPoint(
124 rPoint.getX(), rPoint.getY(),
125 fRadiant);
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,
133 double fRotate);
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,
143 const B2DVector& rX,
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
152 private:
153 B2DVector maScale;
154 B2DVector maTranslate;
155 double mfRotate;
156 double mfShearX;
158 public:
159 B2DHomMatrixBufferedDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
160 : maScale(),
161 maTranslate(),
162 mfRotate(0.0),
163 mfShearX(0.0)
165 rB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
168 // data access
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
183 private:
184 B2DHomMatrix maB2DHomMatrix;
185 B2DVector maScale;
186 B2DVector maTranslate;
187 double mfRotate;
188 double mfShearX;
190 bool mbDecomposed : 1;
192 void impCheckDecompose()
194 if(!mbDecomposed)
196 maB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
197 mbDecomposed = true;
201 public:
202 B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
203 : maB2DHomMatrix(rB2DHomMatrix),
204 maScale(),
205 maTranslate(),
206 mfRotate(0.0),
207 mfShearX(0.0),
208 mbDecomposed(false)
212 // data access
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: */