• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MATRIX_H
17 #define MATRIX_H
18 
19 #include <array>
20 #include <iostream>
21 
22 #include "drawing/engine_adapter/impl_interface/matrix_impl.h"
23 #include "utils/drawing_macros.h"
24 #include "utils/matrix44.h"
25 #include "utils/scalar.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 enum class ScaleToFit {
31     FILL_SCALETOFIT,
32     START_SCALETOFIT,
33     CENTER_SCALETOFIT,
34     END_SCALETOFIT,
35 };
36 
37 class DRAWING_API Matrix {
38 public:
39     // Matrix is a 3x3 float type matrix.
40     static constexpr int MATRIX_SIZE = 9;
41     static constexpr size_t ROW0 = 0;
42     static constexpr size_t ROW1 = 1;
43     static constexpr size_t ROW2 = 2;
44     static constexpr size_t ROW3 = 3;
45     static constexpr size_t COL0 = 0;
46     static constexpr size_t COL1 = 1;
47     static constexpr size_t COL2 = 2;
48     static constexpr size_t COL3 = 3;
49     using Buffer = std::array<scalar, MATRIX_SIZE>;
50 
51     enum Index {
52         SCALE_X,
53         SKEW_X,
54         TRANS_X,
55         SKEW_Y,
56         SCALE_Y,
57         TRANS_Y,
58         PERSP_0,
59         PERSP_1,
60         PERSP_2,
61     };
62 
63     Matrix();
64     Matrix(const Matrix& matrix);
65     Matrix& operator=(const Matrix& matrix);
~Matrix()66     virtual ~Matrix() {}
Skew(scalar kx,scalar ky)67     static Matrix Skew(scalar kx, scalar ky)
68     {
69         Matrix m;
70         m.SetSkew(kx, ky);
71         return m;
72     }
73 
74     void Rotate(scalar degree, scalar px, scalar py);
75     void Translate(scalar dx, scalar dy);
76     void Scale(scalar sx, scalar sy, scalar px, scalar py);
77 
78     /**
79      * @brief    Sets Matrix to Matrix a multiplied by Matrix b.
80      * @param a  Matrix on left side of multiply expression.
81      * @param b  Matrix on right side of multiply expression.
82      */
83     void SetConcat(const Matrix& a, const Matrix& b);
84 
85     void SetScale(scalar sx, scalar sy);
86     void SetScaleTranslate(scalar sx, scalar sy, scalar dx, scalar dy);
87 
88     /**
89      * @brief Sets Matrix to rotate around a specified point (px, py) using the given sine and cosine values.
90      *
91      * @param sinValue Indicates the sine of the angle of rotation.
92      * @param cosValue Indicates the cosine of the angle of rotation.
93      * @param px Indicates the x-coordinate of the point around which to rotate.
94      * @param py Indicates the y-coordinate of the point around which to rotate.
95      */
96     void SetSinCos(scalar sinValue, scalar cosValue, scalar px, scalar py);
97 
98     void SetSkew(scalar kx, scalar ky);
99     void SetSkew(scalar kx, scalar ky, scalar px, scalar py);
100     /**
101      * @brief         Sets Matrix to Matrix multiplied by Matrix constructed
102      *                from rotating by degrees about pivot point(0,0).
103      * @param degree  Angle of axes relative to upright axes.
104      */
105     void PreRotate(scalar degree);
106 
107     void PostRotate(scalar degree);
108 
109     /**
110      * @brief         Sets Matrix to Matrix constructed from rotating by degrees
111      *                about pivot point(px,py), multiplied by Matrix.
112      * @param degree  Angle of axes relative to upright axes.
113      * @param px      pivot on x-axis
114      * @param px      pivot on y-axis
115      */
116     void PostRotate(scalar degree, scalar px, scalar py);
117 
118     /**
119      * @brief     Sets Matrix to Matrix constructed from translation (dx, dy) multiplied by Matrix.
120      * @param dx  X-axis translation after applying Matrix.
121      * @param dy  Y-axis translation after applying Matrix.
122      */
123     void PreTranslate(scalar dx, scalar dy);
124 
125     void PostTranslate(scalar dx, scalar dy);
126 
127     /**
128      * @brief     Sets Matrix to Matrix multiplied by Matrix constructed
129      *            from scaling by (sx, sy) about pivot point (0, 0).
130      * @param sx  Horizontal scale factor.
131      * @param sy  Vertical scale factor.
132      */
133     void PreScale(scalar sx, scalar sy);
134 
135     void PostScale(scalar sx, scalar sy);
136 
137     /**
138      * @brief         Sets Matrix to Matrix constructed from scaling by (sx, sy)
139      *                about pivot point(px,py), multiplied by Matrix.
140      * @param sx      horizontal scale factor
141      * @param sy      vertical scale factor
142      * @param px      pivot on x-axis
143      * @param px      pivot on y-axis
144      */
145     void PostScale(scalar sx, scalar sy, scalar px, scalar py);
146 
147     /**
148      * @brief     Sets Matrix to Matrix multiplied by Matrix constructed
149      *            from skewing by (kx, ky) about pivot point (0, 0).
150      * @param kx  Horizontal skew factor.
151      * @param ky  Vertical skew factor.
152      */
153     void PreSkew(scalar kx, scalar ky);
154 
155     /**
156      * @brief     Sets Matrix to Matrix constructed from skewing by (kx, ky)
157      *            about pivot point(0, 0), multiplied by Matrix.
158      * @param kx  Horizontal skew factor.
159      * @param ky  Vertical skew factor.
160      */
161     void PostSkew(scalar kx, scalar ky);
162 
163     /**
164      * @brief         Sets Matrix to Matrix multiplied by Matrix constructed
165      *                from skewing by (kx, ky) about pivot point (px, py).
166      * @param kx      horizontal skew factor
167      * @param ky      vertical skew factor
168      * @param px      pivot on x-axis
169      * @param py      pivot on y-axis
170      */
171     void PreSkew(scalar kx, scalar ky, scalar px, scalar py);
172 
173     /**
174      * @brief         Sets Matrix to Matrix constructed from skewing by (kx, ky)
175      *                about pivot point(px, py), multiplied by Matrix.
176      * @param kx      horizontal skew factor
177      * @param ky      vertical skew factor
178      * @param px      pivot on x-axis
179      * @param py      pivot on y-axis
180      */
181     void PostSkew(scalar kx, scalar ky, scalar px, scalar py);
182 
183     /**
184      * @brief         Sets Matrix to Matrix other multiplied by Matrix.
185      * @param other   Matrix on left side of multiply expression.
186      */
187     void PreConcat(const Matrix& other);
188 
189     /**
190      * @brief            Sets Matrix to Matrix other multiplied by Matrix44.
191      * @param matrix44   Matrix on left side of multiply expression.
192      */
193     void PreConcat(const Matrix44& matrix44);
194 
195     /**
196      * @brief         Sets Matrix to Matrix other multiplied by Matrix.
197      * @param other   Matrix on right side of multiply expression.
198      */
199     void PostConcat(const Matrix& other);
200 
201     /**
202      * @brief         Sets Matrix to Matrix other multiplied by Matrix44.
203      * @param other   Matrix on right side of multiply expression.
204      */
205     void PostConcat(const Matrix44& matrix44);
206 
207     /**
208      * @brief           Sets inverse to the inverse of Matrix.
209      * @param inverse   To store to inverse Matrix, may be nullptr.
210      * @return          Return true if Matrix can be inverted, otherwise return false.
211      */
212     bool Invert(Matrix& inverse) const;
213     Matrix operator*(const Matrix& other);
214 
215     /**
216      * @brief         Compares Matrix and other.
217      * @param other   To compare Matrix.
218      * @return        True if Matrix and other are numerically equal.
219      */
220     bool operator==(const Matrix& other) const;
221     void SetMatrix(scalar scaleX, scalar skewX, scalar transX, scalar skewY, scalar scaleY, scalar transY,
222         scalar persp0, scalar persp1, scalar persp2);
223 
224     /**
225      * @brief Sets matrix to scale and translate src rect to dst rect.
226      *
227      * @param src Rect to map from
228      * @param dst Rect to map to
229      * @param stf Describes how matrix is constructed to map one rect to another
230      * @return Returns true if dst is empty, and sets matrix to:
231                | 0 0 0 |
232                | 0 0 0 |
233                | 0 0 1 |.
234      */
235     bool SetRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf);
236 
237     void MapPoints(std::vector<Point>& dst, const std::vector<Point>& src, uint32_t count) const;
238 
239     /**
240      * @brief Returns geometric mean radius of ellipse formed by constructing circle of size radius,
241      *        and mapping constructed circle with Matrix.
242      *
243      * @param radius  circle size to map.
244      * @return        average mapped radius.
245      */
246     scalar MapRadius(scalar radius) const;
247 
248     /**
249      * @brief       Sets dst to bounds of src corners mapped by Matrix.
250      * @param dst   Storage for bounds of map.
251      * @param src   To map.
252      * @return      True if dst is equivalent to mapped src.
253      */
254     bool MapRect(Rect& dst, const Rect& src) const;
255 
256     /**
257      * @brief       Sets Matrix to map src to dst. count must be zero or greater, and four or less.
258      * @param src   Point to map from
259      * @param dst   Point to map to
260      * @param count Number of Point in src and dst
261      * @return      True if Matrix was constructed successfully
262      */
263     bool SetPolyToPoly(const Point src[], const Point dst[], uint32_t count);
264 
265     /**
266      * @brief         Sets Matrix value.
267      * @param index   One of Index.
268      * @param value   Scalar to store in Matrix.
269      */
270     void Set(Index index, scalar value);
271     scalar Get(int index) const;
272 
273     /**
274      * @brief         Copies nine scalar values contained by Matrix into buffer.
275      * @param buffer  Storage for nine scalar values
276      */
277     void GetAll(Buffer& buffer) const;
278 
279     /**
280      * @brief         Copies nine scalar values contained by Matrix from buffer.
281      * @param buffer  Storage for nine scalar values
282      */
283     void SetAll(Buffer& buffer);
284 
285     template<typename T>
GetImpl()286     T* GetImpl() const
287     {
288         return matrixImplPtr->DowncastingTo<T>();
289     }
290 
291     /**
292      * @brief Checks if the current transformation matrix is an affine transformation.
293      *
294      * @return If the rectangle is affine, return true; Otherwise, returns false.
295      */
296     bool IsAffine() const;
297 
298     /**
299      * @brief Returns true if matrix is Identity. Identity matrix is:
300      *    | 1 0 0 |
301      *    | 0 1 0 |
302      *    | 0 0 1 |
303      * @return true if is identity
304      */
305     bool IsIdentity() const;
306 
307     void PreRotate(scalar degree, scalar px, scalar py);
308     void PreScale(scalar sx, scalar sy, scalar px, scalar py);
309 
310     /**
311      * @brief Checks whether a rectangle will be mapped to another rectangle after the specified matrix transformation.
312      *
313      * @return If a rectangle is mapped to another rectangle, return true; Otherwise, return false.
314      */
315     bool RectStaysRect() const;
316 
317     void Reset();
318 
319     bool GetMinMaxScales(scalar scaleFactors[2]);
320     bool HasPerspective() const;
321 
322     void Swap(Matrix& target);
323 
324 private:
325     std::shared_ptr<MatrixImpl> matrixImplPtr;
326 };
327 } // namespace Drawing
328 } // namespace Rosen
329 } // namespace OHOS
330 #endif
331