Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
21 : #define _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX
22 :
23 : #include <sal/types.h>
24 : #include <basegfx/matrix/b2dhommatrix.hxx>
25 : #include <basegfx/vector/b2dvector.hxx>
26 : #include <basegfx/basegfxdllapi.h>
27 :
28 :
29 : ///////////////////////////////////////////////////////////////////////////////
30 :
31 : namespace basegfx
32 : {
33 : namespace tools
34 : {
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)
42 : */
43 : BASEGFX_DLLPUBLIC void createSinCosOrthogonal(double& o_rSin, double& rCos, double fRadiant);
44 :
45 : /** Tooling methods for on-the-fly matrix generation e.g. for inline
46 : multiplications
47 : */
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);
53 :
54 : /// inline versions for parameters as tuples
55 0 : inline B2DHomMatrix createScaleB2DHomMatrix(const B2DTuple& rScale)
56 : {
57 0 : return createScaleB2DHomMatrix(rScale.getX(), rScale.getY());
58 : }
59 :
60 0 : inline B2DHomMatrix createTranslateB2DHomMatrix(const B2DTuple& rTranslate)
61 : {
62 0 : return createTranslateB2DHomMatrix(rTranslate.getX(), rTranslate.getY());
63 : }
64 :
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.
71 : */
72 : BASEGFX_DLLPUBLIC B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
73 : double fScaleX, double fScaleY,
74 : double fShearX,
75 : double fRadiant,
76 : double fTranslateX, double fTranslateY);
77 26748 : inline B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
78 : const B2DTuple& rScale,
79 : double fShearX,
80 : double fRadiant,
81 : const B2DTuple& rTranslate)
82 : {
83 : return createScaleShearXRotateTranslateB2DHomMatrix(
84 : rScale.getX(), rScale.getY(),
85 : fShearX,
86 : fRadiant,
87 26748 : rTranslate.getX(), rTranslate.getY());
88 : }
89 :
90 : BASEGFX_DLLPUBLIC B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
91 : double fShearX,
92 : double fRadiant,
93 : double fTranslateX, double fTranslateY);
94 10418 : inline B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
95 : double fShearX,
96 : double fRadiant,
97 : const B2DTuple& rTranslate)
98 : {
99 : return createShearXRotateTranslateB2DHomMatrix(
100 : fShearX,
101 : fRadiant,
102 10418 : rTranslate.getX(), rTranslate.getY());
103 : }
104 :
105 : BASEGFX_DLLPUBLIC B2DHomMatrix createScaleTranslateB2DHomMatrix(
106 : double fScaleX, double fScaleY,
107 : double fTranslateX, double fTranslateY);
108 172 : inline B2DHomMatrix createScaleTranslateB2DHomMatrix(
109 : const B2DTuple& rScale,
110 : const B2DTuple& rTranslate)
111 : {
112 : return createScaleTranslateB2DHomMatrix(
113 : rScale.getX(), rScale.getY(),
114 172 : rTranslate.getX(), rTranslate.getY());
115 : }
116 :
117 : /// special for the often used case of rotation around a point
118 : BASEGFX_DLLPUBLIC B2DHomMatrix createRotateAroundPoint(
119 : double fPointX, double fPointY,
120 : double fRadiant);
121 158 : inline B2DHomMatrix createRotateAroundPoint(
122 : const B2DTuple& rPoint,
123 : double fRadiant)
124 : {
125 : return createRotateAroundPoint(
126 : rPoint.getX(), rPoint.getY(),
127 158 : fRadiant);
128 : }
129 :
130 : } // end of namespace tools
131 : } // end of namespace basegfx
132 :
133 : ///////////////////////////////////////////////////////////////////////////////
134 :
135 : namespace basegfx
136 : {
137 : namespace tools
138 : {
139 0 : class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedDecompose
140 : {
141 : private:
142 : B2DVector maScale;
143 : B2DVector maTranslate;
144 : double mfRotate;
145 : double mfShearX;
146 :
147 : public:
148 0 : B2DHomMatrixBufferedDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
149 : : maScale(),
150 : maTranslate(),
151 : mfRotate(0.0),
152 0 : mfShearX(0.0)
153 : {
154 0 : rB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
155 0 : }
156 :
157 : // data access
158 0 : B2DHomMatrix getB2DHomMatrix() const
159 : {
160 : return createScaleShearXRotateTranslateB2DHomMatrix(
161 0 : maScale, mfShearX, mfRotate, maTranslate);
162 : }
163 :
164 0 : const B2DVector& getScale() const { return maScale; }
165 0 : const B2DVector& getTranslate() const { return maTranslate; }
166 0 : double getRotate() const { return mfRotate; }
167 0 : double getShearX() const { return mfShearX; }
168 : };
169 : } // end of namespace tools
170 : } // end of namespace basegfx
171 :
172 : ///////////////////////////////////////////////////////////////////////////////
173 :
174 : namespace basegfx
175 : {
176 : namespace tools
177 : {
178 3726 : class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedOnDemandDecompose
179 : {
180 : private:
181 : B2DHomMatrix maB2DHomMatrix;
182 : B2DVector maScale;
183 : B2DVector maTranslate;
184 : double mfRotate;
185 : double mfShearX;
186 :
187 : // bitfield
188 : unsigned mbDecomposed : 1;
189 :
190 9922 : void impCheckDecompose()
191 : {
192 9922 : if(!mbDecomposed)
193 : {
194 1777 : maB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
195 1777 : mbDecomposed = true;
196 : }
197 9922 : }
198 :
199 : public:
200 3038 : B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
201 : : maB2DHomMatrix(rB2DHomMatrix),
202 : maScale(),
203 : maTranslate(),
204 : mfRotate(0.0),
205 : mfShearX(0.0),
206 3038 : mbDecomposed(false)
207 : {
208 3038 : }
209 :
210 : // data access
211 5462 : const B2DHomMatrix& getB2DHomMatrix() const { return maB2DHomMatrix; }
212 7126 : const B2DVector& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maScale; }
213 1398 : const B2DVector& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maTranslate; }
214 1398 : double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfRotate; }
215 : double getShearX() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfShearX; }
216 : };
217 : } // end of namespace tools
218 :
219 : } // end of namespace basegfx
220 :
221 : ///////////////////////////////////////////////////////////////////////////////
222 :
223 : #endif /* _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX */
224 :
225 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|