• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 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 /**
17  * @addtogroup UI_Utils
18  * @{
19  *
20  * @brief Defines basic UI utils.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file transform.h
28  *
29  * @brief Provides functions to transform components, points, and line segments, including rotation and scaling.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_TRANSFORM_H
36 #define GRAPHIC_LITE_TRANSFORM_H
37 
38 #include "gfx_utils/geometry2d.h"
39 #include "gfx_utils/graphic_math.h"
40 namespace OHOS {
41 /**
42  * @brief Transforms a rectangle, including rotation and scaling.
43  * @since 1.0
44  * @version 1.0
45  */
46 class TransformMap : public HeapBase {
47 public:
48     /**
49      * @brief The default constructor used to create a <b>TransformMap</b> instance.
50      * @since 1.0
51      * @version 1.0
52      */
53     TransformMap();
54 
55     /**
56      * @brief A constructor used to create a <b>TransformMap</b> instance.
57      *
58      * @param rect Indicates the rectangle to transform.
59      * @since 1.0
60      * @version 1.0
61      */
62     explicit TransformMap(const Rect& rect);
63 
64     /**
65      * @brief A destructor used to delete the <b>TransformMap</b> instance.
66      * @since 1.0
67      * @version 1.0
68      */
~TransformMap()69     virtual ~TransformMap() {}
70 
71     /**
72      * @brief Checks whether the vertex coordinates of a polygon are clockwise.
73      *
74      * @return Returns <b>true</b> if the vertex coordinates are clockwise; returns <b>false</b> otherwise.
75      * @since 1.0
76      * @version 1.0
77      */
78     bool GetClockWise() const;
79 
80     /**
81      * @brief Sets a polygon after rectangle transformation.
82      * @param polygon Indicates the polygon to set.
83      * @since 1.0
84      * @version 1.0
85      */
SetPolygon(const Polygon & polygon)86     void SetPolygon(const Polygon& polygon)
87     {
88         polygon_ = polygon;
89     }
90 
91     /**
92      * @brief Obtains the polygon after rectangle transformation.
93      * @return Returns the polygon.
94      * @since 1.0
95      * @version 1.0
96      */
GetPolygon()97     Polygon GetPolygon() const
98     {
99         return polygon_;
100     }
101 
102     /**
103      * @brief Obtains the pivot for the rotation or scaling operation.
104      * @return Returns the pivot.
105      * @since 1.0
106      * @version 1.0
107      */
GetPivot()108     const Vector3<float>& GetPivot() const
109     {
110         return scalePivot_;
111     }
112 
113     void SetTransMapRect(const Rect& rect);
114 
GetTransMapRect()115     const Rect& GetTransMapRect() const
116     {
117         return rect_;
118     }
119 
SetInvalid(bool state)120     void SetInvalid(bool state)
121     {
122         isInvalid_ = state;
123     }
124 
125     /**
126      * @brief Checks whether the <b>TransformMap</b> instance is invalid. When the vertices are all 0, the
127      *        <b>TransformMap</b> is invalid.
128      * @return Returns <b>true</b> if <b>TransformMap</b> is invalid; returns <b>false</b> otherwise.
129      * @since 1.0
130      * @version 1.0
131      */
132     bool IsInvalid() const;
133 
134     /**
135      * @brief Obtains the minimum rectangle that can contain a polygon. All vertices of the polygon are inside this
136      *        rectangle.
137      * @return Returns the minimum rectangle that can contain the polygon.
138      * @since 1.0
139      * @version 1.0
140      */
GetBoxRect()141     Rect GetBoxRect() const
142     {
143         return polygon_.MakeAABB();
144     }
145 
146     /**
147      * @brief Obtains a three-dimensional homogeneous transformation matrix.
148      * @return Returns the three-dimensional homogeneous transformation matrix.
149      * @since 1.0
150      * @version 1.0
151      */
GetTransformMatrix()152     const Matrix4<float>& GetTransformMatrix() const
153     {
154         return matrix_;
155     }
156 
GetOrigTransformMatrix()157     const Matrix4<float>& GetOrigTransformMatrix() const
158     {
159         return matrixOrig_;
160     }
161 
GetRotateMatrix()162     const Matrix4<float>& GetRotateMatrix() const
163     {
164         return rotate_;
165     }
166 
GetScaleMatrix()167     const Matrix4<float>& GetScaleMatrix() const
168     {
169         return scale_;
170     }
171 
GetTranslateMatrix()172     const Matrix4<float>& GetTranslateMatrix() const
173     {
174         return translate_;
175     }
176 
GetShearMatrix()177     const Matrix4<float>& GetShearMatrix() const
178     {
179         return shear_;
180     }
181 
GetPerspectiveMatrix()182     const Matrix4<float>& GetPerspectiveMatrix() const
183     {
184         return perspectiveMatrix_;
185     }
186 
GetRotateAngle()187     int16_t GetRotateAngle() const
188     {
189         return angle_;
190     }
191 
192     Point GetOrigPoint(const Point& point, const Rect& relativeRect);
193 
194     /**
195      * @brief Rotates the rectangle.
196      * @param angle Indicates the angle to rotate.
197      * @param pivot Indicates the rotation pivot.
198      * @since 1.0
199      * @version 1.0
200      */
201     void Rotate(int16_t angle, const Vector2<float>& pivot);
202 
203     void Rotate(int16_t angle, const Vector3<float>& rotatePivotStart, const Vector3<float>& rotatePivotEnd);
204 
205     /**
206      * @brief Scales the rectangle.
207      *
208      * @param scale Indicates the scaling factors of the x-axis and y-axis.
209      * @param pivot Indicates the pivot for scaling.
210      * @since 1.0
211      * @version 1.0
212      */
213     void Scale(const Vector2<float>& scale, const Vector2<float>& pivot);
214 
215     void Scale(const Vector3<float>& scale, const Vector3<float>& pivot);
216 
217     void Translate(const Vector2<int16_t>& trans);
218 
219     void Translate(const Vector3<int16_t>& trans);
220 
221     void Shear(const Vector2<float>& shearX, const Vector2<float>& shearY, const Vector2<float>& shearZ);
222 
223     bool operator==(const TransformMap& other) const;
224 
225     void SetMatrix(const Matrix4<float>& matrix, bool isInternalMatrix = false);
226 
227     void SetCameraDistance(int16_t distance);
228 
229     void SetCameraPosition(const Vector2<float>& position);
230 
231     Matrix3<float> invMatrix_;
232 
233     bool Is3DTransform() const;
234 
235 private:
236     void UpdateMap();
237     void AddOp(uint8_t op);
238 
239     enum : uint8_t {
240         ROTATE = 0,
241         SCALE,
242         SHEAR,
243         TRANSLATE,
244         TRANS_NUM,
245     };
246     Rect rect_ = {0, 0, 0, 0};       /* orig rect */
247     Polygon polygon_ = Polygon(Rect(0, 0, 0, 0)); /* transformed from rect and 'rotate_' 'translate_' 'scale_' */
248     int16_t angle_ = 0;
249     int16_t cameraDistance_ = 1000;  // 1000 : default distance
250     bool isInvalid_ = false;
251     bool isIdentity_ = false;
252     bool is3d_ = false;
253     bool isInternalMatrix_ = true;
254     Vector2<float> cameraPosition_ = {0, 0};
255     Vector3<float> scaleCoeff_ = {1.0f, 1.0f, 1.0f};
256     Vector3<float> scalePivot_ = {0, 0, 0};
257     Vector3<float> rotatePivotStart_ = {0, 0, 0};
258     Vector3<float> rotatePivotEnd_ = {0, 0, 0};
259     Vector2<float> shearX_ = {0, 0};
260     Vector2<float> shearY_ = {0, 0};
261     Vector2<float> shearZ_ = {0, 0};
262 
263     Matrix4<float> scale_;
264     Matrix4<float> rotate_;
265     Matrix4<float> shear_;
266     Matrix4<float> translate_;
267     Matrix4<float>* trans_[TRANS_NUM];
268     uint8_t opOrder_[TRANS_NUM];
269     Matrix4<float> matrix_;
270     Matrix4<float> perspectiveMatrix_;
271     Matrix4<float> matrixOrig_;
272 };
273 
274 /**
275  * @brief Rotates a point around the pivot by a certain angle.
276  * @param point Indicates the point to rotate.
277  * @param angle Indicates the angle to rotate.
278  * @param pivot Indicates the rotation pivot.
279  * @param out Indicates the point generated after rotation.
280  * @since 1.0
281  * @version 1.0
282  */
283 void Rotate(const Vector2<int16_t>& point, int16_t angle, const Vector2<int16_t>& pivot, Vector2<int16_t>& out);
284 
285 /**
286  * @brief Rotates a line around the pivot by a certain angle.
287  * @param origLine Indicates the line segment to rotate.
288  * @param angle Indicates the angle to rotate.
289  * @param pivot Indicates the rotation pivot.
290  * @param out Indicates the line generated after rotation.
291  * @since 1.0
292  * @version 1.0
293  */
294 void Rotate(const Line& origLine, int16_t angle, const Vector2<int16_t>& pivot, Line& out);
295 
296 /**
297  * @brief Rotates a rectangle around the pivot by a certain angle.
298  * @param origRect Indicates the rectangle to rotate.
299  * @param angle Indicates the angle to rotate.
300  * @param pivot Indicates the rotation pivot.
301  * @param out Indicates the polygon generated after the rectangle is rotated.
302  * @since 1.0
303  * @version 1.0
304  */
305 void Rotate(const Rect& origRect, int16_t angle, const Vector2<int16_t>& pivot, Polygon& out);
306 } // namespace OHOS
307 #endif // GRAPHIC_LITE_TRANSFORM_H
308