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 : namespace rtl { class OUString; }
29 :
30 : ///////////////////////////////////////////////////////////////////////////////
31 :
32 : namespace basegfx
33 : {
34 : namespace tools
35 : {
36 : /** If the rotation angle is an approximate multiple of pi/2,
37 : force fSin/fCos to -1/0/1, to maintain orthogonality (which
38 : might also be advantageous for the other cases, but: for
39 : multiples of pi/2, the exact values _can_ be attained. It
40 : would be largely unintuitive, if a 180 degrees rotation
41 : would introduce slight roundoff errors, instead of exactly
42 : mirroring the coordinate system)
43 : */
44 : BASEGFX_DLLPUBLIC void createSinCosOrthogonal(double& o_rSin, double& rCos, double fRadiant);
45 :
46 : /** Tooling methods for on-the-fly matrix generation e.g. for inline
47 : multiplications
48 : */
49 : BASEGFX_DLLPUBLIC B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY);
50 : BASEGFX_DLLPUBLIC B2DHomMatrix createShearXB2DHomMatrix(double fShearX);
51 : BASEGFX_DLLPUBLIC B2DHomMatrix createShearYB2DHomMatrix(double fShearY);
52 : BASEGFX_DLLPUBLIC B2DHomMatrix createRotateB2DHomMatrix(double fRadiant);
53 : BASEGFX_DLLPUBLIC B2DHomMatrix createTranslateB2DHomMatrix(double fTranslateX, double fTranslateY);
54 :
55 : /// inline versions for parameters as tuples
56 : inline B2DHomMatrix createScaleB2DHomMatrix(const B2DTuple& rScale)
57 : {
58 : return createScaleB2DHomMatrix(rScale.getX(), rScale.getY());
59 : }
60 :
61 0 : inline B2DHomMatrix createTranslateB2DHomMatrix(const B2DTuple& rTranslate)
62 : {
63 0 : return createTranslateB2DHomMatrix(rTranslate.getX(), rTranslate.getY());
64 : }
65 :
66 : /** Tooling methods for faster completely combined matrix creation
67 : when scale, shearX, rotation and translation needs to be done in
68 : exactly that order. It's faster since it direcly calculates
69 : each matrix value based on a symbolic calculation of the three
70 : matrix multiplications.
71 : Inline versions for parameters as tuples added, too.
72 : */
73 : BASEGFX_DLLPUBLIC B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
74 : double fScaleX, double fScaleY,
75 : double fShearX,
76 : double fRadiant,
77 : double fTranslateX, double fTranslateY);
78 1700 : inline B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(
79 : const B2DTuple& rScale,
80 : double fShearX,
81 : double fRadiant,
82 : const B2DTuple& rTranslate)
83 : {
84 : return createScaleShearXRotateTranslateB2DHomMatrix(
85 : rScale.getX(), rScale.getY(),
86 : fShearX,
87 : fRadiant,
88 1700 : rTranslate.getX(), rTranslate.getY());
89 : }
90 :
91 : BASEGFX_DLLPUBLIC B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
92 : double fShearX,
93 : double fRadiant,
94 : double fTranslateX, double fTranslateY);
95 84 : inline B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(
96 : double fShearX,
97 : double fRadiant,
98 : const B2DTuple& rTranslate)
99 : {
100 : return createShearXRotateTranslateB2DHomMatrix(
101 : fShearX,
102 : fRadiant,
103 84 : rTranslate.getX(), rTranslate.getY());
104 : }
105 :
106 : BASEGFX_DLLPUBLIC B2DHomMatrix createScaleTranslateB2DHomMatrix(
107 : double fScaleX, double fScaleY,
108 : double fTranslateX, double fTranslateY);
109 15 : inline B2DHomMatrix createScaleTranslateB2DHomMatrix(
110 : const B2DTuple& rScale,
111 : const B2DTuple& rTranslate)
112 : {
113 : return createScaleTranslateB2DHomMatrix(
114 : rScale.getX(), rScale.getY(),
115 15 : rTranslate.getX(), rTranslate.getY());
116 : }
117 :
118 : /// special for the often used case of rotation around a point
119 : BASEGFX_DLLPUBLIC B2DHomMatrix createRotateAroundPoint(
120 : double fPointX, double fPointY,
121 : double fRadiant);
122 0 : inline B2DHomMatrix createRotateAroundPoint(
123 : const B2DTuple& rPoint,
124 : double fRadiant)
125 : {
126 : return createRotateAroundPoint(
127 : rPoint.getX(), rPoint.getY(),
128 0 : fRadiant);
129 : }
130 :
131 : } // end of namespace tools
132 : } // end of namespace basegfx
133 :
134 : ///////////////////////////////////////////////////////////////////////////////
135 :
136 : namespace basegfx
137 : {
138 : namespace tools
139 : {
140 0 : class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedDecompose
141 : {
142 : private:
143 : B2DVector maScale;
144 : B2DVector maTranslate;
145 : double mfRotate;
146 : double mfShearX;
147 :
148 : public:
149 0 : B2DHomMatrixBufferedDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
150 : : maScale(),
151 : maTranslate(),
152 : mfRotate(0.0),
153 0 : mfShearX(0.0)
154 : {
155 0 : rB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
156 0 : }
157 :
158 : // data access
159 0 : B2DHomMatrix getB2DHomMatrix() const
160 : {
161 : return createScaleShearXRotateTranslateB2DHomMatrix(
162 0 : maScale, mfShearX, mfRotate, maTranslate);
163 : }
164 :
165 0 : const B2DVector& getScale() const { return maScale; }
166 0 : const B2DVector& getTranslate() const { return maTranslate; }
167 0 : double getRotate() const { return mfRotate; }
168 0 : double getShearX() const { return mfShearX; }
169 : };
170 : } // end of namespace tools
171 : } // end of namespace basegfx
172 :
173 : ///////////////////////////////////////////////////////////////////////////////
174 :
175 : namespace basegfx
176 : {
177 : namespace tools
178 : {
179 0 : class BASEGFX_DLLPUBLIC B2DHomMatrixBufferedOnDemandDecompose
180 : {
181 : private:
182 : B2DHomMatrix maB2DHomMatrix;
183 : B2DVector maScale;
184 : B2DVector maTranslate;
185 : double mfRotate;
186 : double mfShearX;
187 :
188 : // bitfield
189 : unsigned mbDecomposed : 1;
190 :
191 0 : void impCheckDecompose()
192 : {
193 0 : if(!mbDecomposed)
194 : {
195 0 : maB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX);
196 0 : mbDecomposed = true;
197 : }
198 0 : }
199 :
200 : public:
201 0 : B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix())
202 : : maB2DHomMatrix(rB2DHomMatrix),
203 : maScale(),
204 : maTranslate(),
205 : mfRotate(0.0),
206 : mfShearX(0.0),
207 0 : mbDecomposed(false)
208 : {
209 0 : }
210 :
211 : // data access
212 0 : const B2DHomMatrix& getB2DHomMatrix() const { return maB2DHomMatrix; }
213 0 : const B2DVector& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maScale; }
214 0 : const B2DVector& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maTranslate; }
215 0 : double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfRotate; }
216 : double getShearX() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfShearX; }
217 : };
218 : } // end of namespace tools
219 :
220 : } // end of namespace basegfx
221 :
222 : ///////////////////////////////////////////////////////////////////////////////
223 :
224 : #endif /* _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX */
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|