1ce9c7ef7SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3ce9c7ef7SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4ce9c7ef7SAndrew Rist * or more contributor license agreements. See the NOTICE file 5ce9c7ef7SAndrew Rist * distributed with this work for additional information 6ce9c7ef7SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7ce9c7ef7SAndrew Rist * to you under the Apache License, Version 2.0 (the 8ce9c7ef7SAndrew Rist * "License"); you may not use this file except in compliance 9ce9c7ef7SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11ce9c7ef7SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13ce9c7ef7SAndrew Rist * Unless required by applicable law or agreed to in writing, 14ce9c7ef7SAndrew Rist * software distributed under the License is distributed on an 15ce9c7ef7SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ce9c7ef7SAndrew Rist * KIND, either express or implied. See the License for the 17ce9c7ef7SAndrew Rist * specific language governing permissions and limitations 18ce9c7ef7SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20ce9c7ef7SAndrew Rist *************************************************************/ 21ce9c7ef7SAndrew Rist 22ce9c7ef7SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX 25cdf0e10cSrcweir #define _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <sal/types.h> 28cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx> 29cdf0e10cSrcweir #include <basegfx/vector/b2dvector.hxx> 30*d8ed516eSArmin Le Grand #include <basegfx/range/b2drange.hxx> 31cdf0e10cSrcweir 32cdf0e10cSrcweir namespace rtl { class OUString; } 33cdf0e10cSrcweir 34cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////// 35cdf0e10cSrcweir 36cdf0e10cSrcweir namespace basegfx 37cdf0e10cSrcweir { 38cdf0e10cSrcweir namespace tools 39cdf0e10cSrcweir { 40cdf0e10cSrcweir /** If the rotation angle is an approximate multiple of pi/2, 41cdf0e10cSrcweir force fSin/fCos to -1/0/1, to maintain orthogonality (which 42cdf0e10cSrcweir might also be advantageous for the other cases, but: for 43cdf0e10cSrcweir multiples of pi/2, the exact values _can_ be attained. It 44cdf0e10cSrcweir would be largely unintuitive, if a 180 degrees rotation 45cdf0e10cSrcweir would introduce slight roundoff errors, instead of exactly 46cdf0e10cSrcweir mirroring the coordinate system) 47cdf0e10cSrcweir */ 48cdf0e10cSrcweir void createSinCosOrthogonal(double& o_rSin, double& rCos, double fRadiant); 49cdf0e10cSrcweir 50cdf0e10cSrcweir /** Tooling methods for on-the-fly matrix generation e.g. for inline 51cdf0e10cSrcweir multiplications 52cdf0e10cSrcweir */ 53cdf0e10cSrcweir B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY); 54cdf0e10cSrcweir B2DHomMatrix createShearXB2DHomMatrix(double fShearX); 55cdf0e10cSrcweir B2DHomMatrix createShearYB2DHomMatrix(double fShearY); 56cdf0e10cSrcweir B2DHomMatrix createRotateB2DHomMatrix(double fRadiant); 57cdf0e10cSrcweir B2DHomMatrix createTranslateB2DHomMatrix(double fTranslateX, double fTranslateY); 58cdf0e10cSrcweir 59cdf0e10cSrcweir /// inline versions for parameters as tuples createScaleB2DHomMatrix(const B2DTuple & rScale)60cdf0e10cSrcweir inline B2DHomMatrix createScaleB2DHomMatrix(const B2DTuple& rScale) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir return createScaleB2DHomMatrix(rScale.getX(), rScale.getY()); 63cdf0e10cSrcweir } 64cdf0e10cSrcweir createTranslateB2DHomMatrix(const B2DTuple & rTranslate)65cdf0e10cSrcweir inline B2DHomMatrix createTranslateB2DHomMatrix(const B2DTuple& rTranslate) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir return createTranslateB2DHomMatrix(rTranslate.getX(), rTranslate.getY()); 68cdf0e10cSrcweir } 69cdf0e10cSrcweir 70cdf0e10cSrcweir /** Tooling methods for faster completely combined matrix creation 71cdf0e10cSrcweir when scale, shearX, rotation and translation needs to be done in 72cdf0e10cSrcweir exactly that order. It's faster since it direcly calculates 73cdf0e10cSrcweir each matrix value based on a symbolic calculation of the three 74cdf0e10cSrcweir matrix multiplications. 75cdf0e10cSrcweir Inline versions for parameters as tuples added, too. 76cdf0e10cSrcweir */ 77cdf0e10cSrcweir B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix( 78cdf0e10cSrcweir double fScaleX, double fScaleY, 79cdf0e10cSrcweir double fShearX, 80cdf0e10cSrcweir double fRadiant, 81cdf0e10cSrcweir double fTranslateX, double fTranslateY); createScaleShearXRotateTranslateB2DHomMatrix(const B2DTuple & rScale,double fShearX,double fRadiant,const B2DTuple & rTranslate)82cdf0e10cSrcweir inline B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix( 83cdf0e10cSrcweir const B2DTuple& rScale, 84cdf0e10cSrcweir double fShearX, 85cdf0e10cSrcweir double fRadiant, 86cdf0e10cSrcweir const B2DTuple& rTranslate) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir return createScaleShearXRotateTranslateB2DHomMatrix( 89cdf0e10cSrcweir rScale.getX(), rScale.getY(), 90cdf0e10cSrcweir fShearX, 91cdf0e10cSrcweir fRadiant, 92cdf0e10cSrcweir rTranslate.getX(), rTranslate.getY()); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir B2DHomMatrix createShearXRotateTranslateB2DHomMatrix( 96cdf0e10cSrcweir double fShearX, 97cdf0e10cSrcweir double fRadiant, 98cdf0e10cSrcweir double fTranslateX, double fTranslateY); createShearXRotateTranslateB2DHomMatrix(double fShearX,double fRadiant,const B2DTuple & rTranslate)99cdf0e10cSrcweir inline B2DHomMatrix createShearXRotateTranslateB2DHomMatrix( 100cdf0e10cSrcweir double fShearX, 101cdf0e10cSrcweir double fRadiant, 102cdf0e10cSrcweir const B2DTuple& rTranslate) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir return createShearXRotateTranslateB2DHomMatrix( 105cdf0e10cSrcweir fShearX, 106cdf0e10cSrcweir fRadiant, 107cdf0e10cSrcweir rTranslate.getX(), rTranslate.getY()); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir B2DHomMatrix createScaleTranslateB2DHomMatrix( 111cdf0e10cSrcweir double fScaleX, double fScaleY, 112cdf0e10cSrcweir double fTranslateX, double fTranslateY); createScaleTranslateB2DHomMatrix(const B2DTuple & rScale,const B2DTuple & rTranslate)113cdf0e10cSrcweir inline B2DHomMatrix createScaleTranslateB2DHomMatrix( 114cdf0e10cSrcweir const B2DTuple& rScale, 115cdf0e10cSrcweir const B2DTuple& rTranslate) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir return createScaleTranslateB2DHomMatrix( 118cdf0e10cSrcweir rScale.getX(), rScale.getY(), 119cdf0e10cSrcweir rTranslate.getX(), rTranslate.getY()); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir /// special for the often used case of rotation around a point 123cdf0e10cSrcweir B2DHomMatrix createRotateAroundPoint( 124cdf0e10cSrcweir double fPointX, double fPointY, 125cdf0e10cSrcweir double fRadiant); createRotateAroundPoint(const B2DTuple & rPoint,double fRadiant)126cdf0e10cSrcweir inline B2DHomMatrix createRotateAroundPoint( 127cdf0e10cSrcweir const B2DTuple& rPoint, 128cdf0e10cSrcweir double fRadiant) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir return createRotateAroundPoint( 131cdf0e10cSrcweir rPoint.getX(), rPoint.getY(), 132cdf0e10cSrcweir fRadiant); 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135*d8ed516eSArmin Le Grand /// special for the case to map from source range to target range 136*d8ed516eSArmin Le Grand B2DHomMatrix createSourceRangeTargetRangeTransform( 137*d8ed516eSArmin Le Grand const B2DRange& rSourceRange, 138*d8ed516eSArmin Le Grand const B2DRange& rTargetRange); 139*d8ed516eSArmin Le Grand 140cdf0e10cSrcweir } // end of namespace tools 141cdf0e10cSrcweir } // end of namespace basegfx 142cdf0e10cSrcweir 143cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////// 144cdf0e10cSrcweir 145cdf0e10cSrcweir namespace basegfx 146cdf0e10cSrcweir { 147cdf0e10cSrcweir namespace tools 148cdf0e10cSrcweir { 149cdf0e10cSrcweir class B2DHomMatrixBufferedDecompose 150cdf0e10cSrcweir { 151cdf0e10cSrcweir private: 152cdf0e10cSrcweir B2DVector maScale; 153cdf0e10cSrcweir B2DVector maTranslate; 154cdf0e10cSrcweir double mfRotate; 155cdf0e10cSrcweir double mfShearX; 156cdf0e10cSrcweir 157cdf0e10cSrcweir public: B2DHomMatrixBufferedDecompose(const B2DHomMatrix & rB2DHomMatrix=B2DHomMatrix ())158ddde725dSArmin Le Grand B2DHomMatrixBufferedDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix()) 159cdf0e10cSrcweir : maScale(), 160cdf0e10cSrcweir maTranslate(), 161cdf0e10cSrcweir mfRotate(0.0), 162cdf0e10cSrcweir mfShearX(0.0) 163cdf0e10cSrcweir { 164cdf0e10cSrcweir rB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX); 165cdf0e10cSrcweir } 166cdf0e10cSrcweir 167cdf0e10cSrcweir // data access getB2DHomMatrix() const168cdf0e10cSrcweir B2DHomMatrix getB2DHomMatrix() const 169cdf0e10cSrcweir { 170cdf0e10cSrcweir return createScaleShearXRotateTranslateB2DHomMatrix( 171cdf0e10cSrcweir maScale, mfShearX, mfRotate, maTranslate); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir getScale() const174cdf0e10cSrcweir const B2DVector& getScale() const { return maScale; } getTranslate() const175cdf0e10cSrcweir const B2DVector& getTranslate() const { return maTranslate; } getRotate() const176cdf0e10cSrcweir double getRotate() const { return mfRotate; } getShearX() const177cdf0e10cSrcweir double getShearX() const { return mfShearX; } 178cdf0e10cSrcweir }; 179cdf0e10cSrcweir } // end of namespace tools 180cdf0e10cSrcweir } // end of namespace basegfx 181cdf0e10cSrcweir 182cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////// 183cdf0e10cSrcweir 184cdf0e10cSrcweir namespace basegfx 185cdf0e10cSrcweir { 186cdf0e10cSrcweir namespace tools 187cdf0e10cSrcweir { 188cdf0e10cSrcweir class B2DHomMatrixBufferedOnDemandDecompose 189cdf0e10cSrcweir { 190cdf0e10cSrcweir private: 191cdf0e10cSrcweir B2DHomMatrix maB2DHomMatrix; 192cdf0e10cSrcweir B2DVector maScale; 193cdf0e10cSrcweir B2DVector maTranslate; 194cdf0e10cSrcweir double mfRotate; 195cdf0e10cSrcweir double mfShearX; 196cdf0e10cSrcweir 197cdf0e10cSrcweir // bitfield 198cdf0e10cSrcweir unsigned mbDecomposed : 1; 199cdf0e10cSrcweir impCheckDecompose()200cdf0e10cSrcweir void impCheckDecompose() 201cdf0e10cSrcweir { 202cdf0e10cSrcweir if(!mbDecomposed) 203cdf0e10cSrcweir { 204cdf0e10cSrcweir maB2DHomMatrix.decompose(maScale, maTranslate, mfRotate, mfShearX); 205cdf0e10cSrcweir mbDecomposed = true; 206cdf0e10cSrcweir } 207cdf0e10cSrcweir } 208cdf0e10cSrcweir 209cdf0e10cSrcweir public: B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix & rB2DHomMatrix=B2DHomMatrix ())210ddde725dSArmin Le Grand B2DHomMatrixBufferedOnDemandDecompose(const B2DHomMatrix& rB2DHomMatrix = B2DHomMatrix()) 211cdf0e10cSrcweir : maB2DHomMatrix(rB2DHomMatrix), 212cdf0e10cSrcweir maScale(), 213cdf0e10cSrcweir maTranslate(), 214cdf0e10cSrcweir mfRotate(0.0), 215cdf0e10cSrcweir mfShearX(0.0), 216cdf0e10cSrcweir mbDecomposed(false) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir // data access getB2DHomMatrix() const221cdf0e10cSrcweir const B2DHomMatrix& getB2DHomMatrix() const { return maB2DHomMatrix; } getScale() const222cdf0e10cSrcweir const B2DVector& getScale() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maScale; } getTranslate() const223cdf0e10cSrcweir const B2DVector& getTranslate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return maTranslate; } getRotate() const224cdf0e10cSrcweir double getRotate() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfRotate; } getShearX() const225cdf0e10cSrcweir double getShearX() const { const_cast< B2DHomMatrixBufferedOnDemandDecompose* >(this)->impCheckDecompose(); return mfShearX; } 226cdf0e10cSrcweir }; 227cdf0e10cSrcweir } // end of namespace tools 228cdf0e10cSrcweir 229cdf0e10cSrcweir /// Returns a string with svg's "matrix(m00,m10,m01,m11,m02,m12)" representation 230cdf0e10cSrcweir ::rtl::OUString exportToSvg( const B2DHomMatrix& rMatrix ); 231cdf0e10cSrcweir 232cdf0e10cSrcweir } // end of namespace basegfx 233cdf0e10cSrcweir 234cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////// 235cdf0e10cSrcweir 236cdf0e10cSrcweir #endif /* _BGFX_MATRIX_B2DHOMMATRIXTOOLS_HXX */ 237