• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 geometry2d.h
28  *
29  * @brief Defines attributes of 2D geometries (including points, lines, rectangles, and polygons) of the lightweight
30  *        graphics system and provides functions for performing operations on the geometries.
31  *
32  * @since 1.0
33  * @version 1.0
34  */
35 
36 #ifndef GRAPHIC_LITE_GEOMETRY2D_H
37 #define GRAPHIC_LITE_GEOMETRY2D_H
38 
39 #include "gfx_utils/graphic_assert.h"
40 #include "gfx_utils/graphic_math.h"
41 #include "gfx_utils/graphic_types.h"
42 #include "gfx_utils/heap_base.h"
43 #include "gfx_utils/rect.h"
44 #include <string.h>
45 
46 namespace OHOS {
47 /**
48  * @brief Defines a line, which consists of the start and end points.
49  * @since 1.0
50  * @version 1.0
51  */
52 class Line : public HeapBase {
53 public:
54     /**
55      * @brief The default constructor used to create a <b>Line</b> instance.
56      * @since 1.0
57      * @version 1.0
58      */
Line()59     Line() {}
60 
61     /**
62      * @brief A constructor used to create a <b>Line</b> instance.
63      * @param a Indicates the start point of the line.
64      * @param b Indicates the end point of the line.
65      * @since 1.0
66      * @version 1.0
67      */
Line(const Vector2<int16_t> & a,const Vector2<int16_t> & b)68     Line(const Vector2<int16_t>& a, const Vector2<int16_t>& b)
69     {
70         vertex_[0] = a;
71         vertex_[1] = b;
72     }
73 
74     /**
75      * @brief A constructor used to create a <b>Line</b> instance.
76      * @param x1 Indicates the X coordinate of the line's start point.
77      * @param y1 Indicates the Y coordinate of the line's start point.
78      * @param x2 Indicates the X coordinate of the line's end point.
79      * @param y2 Indicates the Y coordinate of the line's end point.
80      * @since 1.0
81      * @version 1.0
82      */
Line(int16_t x1,int16_t y1,int16_t x2,int16_t y2)83     Line(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
84     {
85         vertex_[0].x_ = x1;
86         vertex_[0].y_ = y1;
87         vertex_[1].x_ = x2;
88         vertex_[1].y_ = y2;
89     }
90 
91     /**
92      * @brief A destructor used to delete the <b>Line</b> instance.
93      * @since 1.0
94      * @version 1.0
95      */
~Line()96     ~Line() {}
97 
98     /**
99      * @brief Obtains the start or end point of the line based on the value of <b>index</b>.
100      * @param index Indicates the start or end point to obtain. The value <b>0</b> indicates that the start point is to
101      *              be obtained, and <b>1</b> indicates that the end point is to be obtained.
102      * @return Returns the start or end point of the line.
103      * @since 1.0
104      * @version 1.0
105      */
106     Vector2<int16_t>& operator[](uint8_t index)
107     {
108         ASSERT(index < 2); // 2: number of vertex
109         return vertex_[index];
110     }
111 
112     /**
113      * @brief Obtains the start or end point of the line based on the value of <b>index</b>.
114      * @param index Indicates the start or end point to obtain. The value <b>0</b> indicates that the start point is to
115      *              be obtained, and <b>1</b> indicates that the end point is to be obtained.
116      * @return Returns the start or end point of the line.
117      * @since 1.0
118      * @version 1.0
119      */
120     const Vector2<int16_t> operator[](uint8_t index) const
121     {
122         ASSERT(index < 2); // 2: number of vertex
123         return vertex_[index];
124     }
125 
126 protected:
127     Vector2<int16_t> vertex_[2]; /* 2: two vertexes of the line */
128 };
129 
130 /**
131  * @brief Defines a polygon, including vertex coordinates and the maximum number of vertices
132  *        (defined by {@link MAX_VERTEX_NUM}).
133  *
134  * @since 1.0
135  * @version 1.0
136  */
137 class Polygon : public HeapBase {
138 public:
139     /**
140      * @brief The default constructor used to create a <b>Polygon</b> instance.
141      * @since 1.0
142      * @version 1.0
143      */
Polygon()144     Polygon() : vertexNum_(0) {}
145 
146     /**
147      * @brief A constructor used to construct a <b>Polygon</b> instance based on a rectangle.
148      *
149      * @param rect Indicates the rectangle used to construct the polygon.
150      * @since 1.0
151      * @version 1.0
152      */
Polygon(const Rect & rect)153     explicit Polygon(const Rect& rect)
154     {
155         vertexNum_ = 4; // 4: number of vertex
156         vertexes_[0].x_ = rect.GetLeft();
157         vertexes_[0].y_ = rect.GetTop();
158 
159         vertexes_[1].x_ = rect.GetRight();
160         vertexes_[1].y_ = rect.GetTop();
161 
162         vertexes_[2].x_ = rect.GetRight();
163         vertexes_[2].y_ = rect.GetBottom();
164 
165         vertexes_[3].x_ = rect.GetLeft();
166         vertexes_[3].y_ = rect.GetBottom();
167     }
168 
169     /**
170      * @brief A constructor used to create a <b>Polygon</b> instance based on the vertex coordinates and the number of
171      *        coordinates.
172      *
173      * The number of vertex coordinates cannot exceed the value of {@link MAX_VERTEX_NUM}.
174      *
175      * @param vertexes Indicates the pointer to the vertex coordinates.
176      * @param vertexNum Indicates the number of vertices.
177      * @since 1.0
178      * @version 1.0
179      */
180     Polygon(const Vector2<int16_t>* vertexes, const uint8_t vertexNum);
181 
182     /**
183      * @brief A destructor used to delete the <b>Polygon</b> instance.
184      * @since 1.0
185      * @version 1.0
186      */
~Polygon()187     ~Polygon() {}
188 
189     /**
190      * @brief Obtains the minimum rectangle that can contain the polygon. All vertices of the polygon are inside this
191      *        rectangle.
192      *
193      * @return Returns the minimum rectangle that contains the polygon.
194      * @since 1.0
195      * @version 1.0
196      */
197     Rect MakeAABB() const;
198 
199     /**
200      * @brief Obtains the number of vertices of the polygon.
201      *
202      * @return Returns the number of vertices.
203      * @since 1.0
204      * @version 1.0
205      */
GetVertexNum()206     uint8_t GetVertexNum() const
207     {
208         return vertexNum_;
209     }
210 
211     /**
212      * @brief Sets the number of vertices of a polygon.
213      * @param vertexNum Indicates the number of vertices.
214      * @since 1.0
215      * @version 1.0
216      */
SetVertexNum(uint8_t vertexNum)217     void SetVertexNum(uint8_t vertexNum)
218     {
219         vertexNum_ = vertexNum;
220     }
221 
222     const Vector2<int16_t> operator[](uint8_t index) const
223     {
224         return vertexes_[index];
225     }
226 
227     Vector2<int16_t>& operator[](uint8_t index)
228     {
229         ASSERT(index < MAX_VERTEX_NUM);
230         return vertexes_[index];
231     }
232 
233     Polygon& operator=(const Rect& rect)
234     {
235         /* clockwise */
236         vertexNum_ = 4; // 4: number of vertex
237         vertexes_[0].x_ = rect.GetLeft();
238         vertexes_[0].y_ = rect.GetTop();
239 
240         vertexes_[1].x_ = rect.GetRight();
241         vertexes_[1].y_ = rect.GetTop();
242 
243         vertexes_[2].x_ = rect.GetRight();
244         vertexes_[2].y_ = rect.GetBottom();
245 
246         vertexes_[3].x_ = rect.GetLeft();
247         vertexes_[3].y_ = rect.GetBottom();
248 
249         return *this;
250     }
251 
252     /** Maximum number of vertices in a polygon */
253     static const uint8_t MAX_VERTEX_NUM = 8;
254 
255 protected:
256     Vector2<int16_t> vertexes_[MAX_VERTEX_NUM]; /* the vertexes of polygon */
257     uint8_t vertexNum_;                         /* the vertex num of polygon */
258 };
259 
260 /**
261  * @brief Checks whether line segment a and line segment b intersect, and returns the intersection point (if available).
262  * @param a Indicates line segment a.
263  * @param b Indicates line segment b.
264  * @param out Indicates the intersection point.
265  * @return Returns <b>true</b> if the two line segments intersect; returns <b>false</b> otherwise.
266  * @since 1.0
267  * @version 1.0
268  */
269 bool Intersect(const Line& a, const Line& b, Vector2<int16_t>& out);
270 
271 /**
272  * @brief Chekcs whether line segment a and line segment b intersect.
273  * @param a Indicates line segment a.
274  * @param b Indicates line segment b.
275  * @return Returns <b>true</b> if the two line segments intersect; returns <b>false</b> otherwise.
276  * @since 1.0
277  * @version 1.0
278  */
279 bool IsIntersect(const Line& a, const Line& b);
280 
281 /**
282  * @brief Clips a polygon by using a line segment.
283  * @param poly Indicates the polygon to clip.
284  * @param line Indicates the line segment used for clipping.
285  * @since 1.0
286  * @version 1.0
287  */
288 void Clip(Polygon& poly, const Line& line);
289 
290 /**
291  * @brief Implements Sutherland-Hodgman, an algorithm used for clipping polygons.
292  * @param clipRect Indicates the rectangle used for clipping the polygon.
293  * @param polygon Indicates the polygon to clip.
294  * @return Returns the polygon after clipping.
295  * @since 1.0
296  * @version 1.0
297  */
298 Polygon SuthHodgClip(const Rect& clipRect, const Polygon& polygon);
299 
300 /**
301  * @brief Clips a polygon by using a line segment and obtains the intersections.
302  * @param line Indicates the line segment used for clipping.
303  * @param poly Indicates the polygon to clip.
304  * @param pOut Indicates the pointer to the intersections.
305  * @param pNum Indicates the pointer to the number of the intersections.
306  * @since 1.0
307  * @version 1.0
308  */
309 void Clip(const Line& line, const Polygon& poly, Vector2<int16_t>* pOut, uint8_t* pNum);
310 } // namespace OHOS
311 #endif // GRAPHIC_LITE_GEOMETRY2D_H
312