1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_MATRIX_B3DHOMMATRIX_HXX
25 #define _BGFX_MATRIX_B3DHOMMATRIX_HXX
26 
27 #include <sal/types.h>
28 #include <basegfx/point/b3dpoint.hxx>
29 #include <basegfx/vector/b3dvector.hxx>
30 #include <o3tl/cow_wrapper.hxx>
31 
32 namespace basegfx
33 {
34 	class B3DTuple;
35     class Impl3DHomMatrix;
36 
37 	class B3DHomMatrix
38 	{
39     public:
40         typedef o3tl::cow_wrapper< Impl3DHomMatrix > ImplType;
41 
42 	private:
43         ImplType                                     mpImpl;
44 
45 	public:
46 		B3DHomMatrix();
47 		B3DHomMatrix(const B3DHomMatrix& rMat);
48 		~B3DHomMatrix();
49 
50         /// unshare this matrix with all internally shared instances
51         void makeUnique();
52 
53 		double get(sal_uInt16 nRow, sal_uInt16 nColumn) const;
54 		void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue);
55 
56 		// test if last line is default to see if last line needs to be
57 		// involved in calculations
58 		bool isLastLineDefault() const;
59 
60 		bool isIdentity() const;
61 		/// Reset to the identity matrix
62 		void identity();
63 
64 		bool isInvertible() const;
65 		/// Invert the matrix (if possible)
66 		bool invert();
67 
68 		bool isNormalized() const;
69 		/// Normalize (i.e. force w=1) the matrix
70 		void normalize();
71 
72 		/// Calc the matrix determinant
73 		double determinant() const;
74 
75 		/// Calc the matrix trace
76 		double trace() const;
77 
78 		/// Transpose the matrix
79 		void transpose();
80 
81 		/// Rotation
82 		void rotate(double fAngleX,double fAngleY,double fAngleZ);
83 
84 		/// Translation
85 		void translate(double fX, double fY, double fZ);
86 
87 		/// Scaling
88 		void scale(double fX, double fY, double fZ);
89 
90 		// Shearing-Matrices
91 		void shearXY(double fSx, double fSy);
92 		void shearYZ(double fSy, double fSz);
93 		void shearXZ(double fSx, double fSz);
94 
95 		// Projection matrices, used for converting between eye and
96 		// clip coordinates
97 		void frustum(double fLeft = -1.0, double fRight = 1.0,
98 			double fBottom = -1.0, double fTop = 1.0,
99 			double fNear = 0.001, double fFar = 1.0);
100 
101 		void ortho(double fLeft = -1.0, double fRight = 1.0,
102 			double fBottom = -1.0, double fTop = 1.0,
103 			double fNear = 0.0, double fFar = 1.0);
104 
105 		// build orientation matrix
106 		void orientation(
107 			B3DPoint aVRP = B3DPoint(0.0,0.0,1.0),
108 			B3DVector aVPN = B3DVector(0.0,0.0,1.0),
109 			B3DVector aVUV = B3DVector(0.0,1.0,0.0));
110 
111 		// addition, subtraction
112 		B3DHomMatrix& operator+=(const B3DHomMatrix& rMat);
113 		B3DHomMatrix& operator-=(const B3DHomMatrix& rMat);
114 
115 		// comparison
116 		bool operator==(const B3DHomMatrix& rMat) const;
117 		bool operator!=(const B3DHomMatrix& rMat) const;
118 
119 		// multiplication, division by constant value
120 		B3DHomMatrix& operator*=(double fValue);
121 		B3DHomMatrix& operator/=(double fValue);
122 
123 		// matrix multiplication (from the left)
124 		B3DHomMatrix& operator*=(const B3DHomMatrix& rMat);
125 
126 		// assignment operator
127 		B3DHomMatrix& operator=(const B3DHomMatrix& rMat);
128 
129 		// decomposition
130 		bool decompose(B3DTuple& rScale, B3DTuple& rTranslate, B3DTuple& rRotate, B3DTuple& rShear) const;
131 	};
132 
133 	// addition, subtraction
operator +(const B3DHomMatrix & rMatA,const B3DHomMatrix & rMatB)134 	inline B3DHomMatrix	operator+(const B3DHomMatrix& rMatA, const B3DHomMatrix& rMatB)
135 	{
136 		B3DHomMatrix aSum(rMatA);
137 		aSum += rMatB;
138 		return aSum;
139 	}
140 
operator -(const B3DHomMatrix & rMatA,const B3DHomMatrix & rMatB)141 	inline B3DHomMatrix	operator-(const B3DHomMatrix& rMatA, const B3DHomMatrix& rMatB)
142 	{
143 		B3DHomMatrix aDiv(rMatA);
144 		aDiv -= rMatB;
145 		return aDiv;
146 	}
147 
148 	// multiplication, division by constant value
operator *(const B3DHomMatrix & rMat,double fValue)149 	inline B3DHomMatrix	operator*(const B3DHomMatrix& rMat, double fValue)
150 	{
151 		B3DHomMatrix aNew(rMat);
152 		aNew *= fValue;
153 		return aNew;
154 	}
155 
operator /(const B3DHomMatrix & rMat,double fValue)156 	inline B3DHomMatrix	operator/(const B3DHomMatrix& rMat, double fValue)
157 	{
158 		B3DHomMatrix aNew(rMat);
159 		aNew *= 1.0 / fValue;
160 		return aNew;
161 	}
162 
operator *(const B3DHomMatrix & rMatA,const B3DHomMatrix & rMatB)163 	inline B3DHomMatrix	operator*(const B3DHomMatrix& rMatA, const B3DHomMatrix& rMatB)
164 	{
165 		B3DHomMatrix aMul(rMatB);
166 		aMul *= rMatA;
167 		return aMul;
168 	}
169 } // end of namespace basegfx
170 
171 #endif /* _BGFX_MATRIX_B3DHOMMATRIX_HXX */
172