1 /*
2  * Copyright (c) 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 GraphicGeometry
18  * @{
19  *
20  * @brief Defines GeometryVertexSequence VertexDist VertexDistCmd.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 #ifndef GRAPHIC_LTE_GEOMETRY_VERTEX_SEQUENCE_H
27 #define GRAPHIC_LTE_GEOMETRY_VERTEX_SEQUENCE_H
28 
29 #include "gfx_utils/diagram/common/common_basics.h"
30 #include "gfx_utils/diagram/common/common_math.h"
31 #include "gfx_utils/vector.h"
32 namespace OHOS {
33 struct VertexDist {
34     float vertexXCoord;
35     float vertexYCoord;
36     float vertexDistance;
37 
VertexDistVertexDist38     VertexDist() {}
39     /**
40      * @brief Construct vertexdist.
41      *
42      * @param x_,y_ Vertex coordinates.
43      * @since 1.0
44      * @version 1.0
45      */
VertexDistVertexDist46     VertexDist(float x, float y)
47         : vertexXCoord(x), vertexYCoord(y), vertexDistance(0.0f) {}
48     /**
49      * @brief Calculate whether the distance between the two points is very close.
50      *
51      * @param vertex.
52      * @return If the two points are close, false is returned.
53      * @since 1.0
54      * @version 1.0
55      */
operatorVertexDist56     bool operator()(const VertexDist& val)
57     {
58         vertexDistance = CalcDistance(vertexXCoord, vertexYCoord, val.vertexXCoord, val.vertexYCoord);
59         bool ret = vertexDistance > VERTEX_DIST_EPSILON;
60         if (!ret) {
61             vertexDistance = 1.0f / VERTEX_DIST_EPSILON;
62         }
63         return ret;
64     }
65 };
66 
67 struct VertexDistCmd : public VertexDist {
68     uint32_t cmd;
69 
VertexDistCmdVertexDistCmd70     VertexDistCmd() {}
71     /**
72      * @brief Construct vertexdist.
73      *
74      * @param x_,y_ Vertex coordinates, cmd_ Connection command.
75      * @since 1.0
76      * @version 1.0
77      */
VertexDistCmdVertexDistCmd78     VertexDistCmd(float x_, float y_, uint32_t cmd_) : VertexDist(x_, y_), cmd(cmd_) {}
79 };
80 
81 /**
82  * @file geometry_vertex_sequence.h
83  *
84  * @brief Defines Define the GeometryVertexSequence class.
85  *
86  * @since 1.0
87  * @version 1.0
88  */
89 class GeometryVertexSequence : public Graphic::Vector<VertexDist> {
90 public:
91     using BaseType = Graphic::Vector<VertexDist>;
92 
93     explicit GeometryVertexSequence(uint32_t size = BLOCK_SHIFT_SIZE) : BaseType(size) {}
94 
95     /**
96      * @brief Closed vertex source.
97      *
98      * @param removeFlag Is it closed.
99      * @since 1.0
100      * @version 1.0
101      */
Close(bool closed)102     void Close(bool closed)
103     {
104         while (BaseType::Size() > 1) {
105             if ((*this)[BaseType::Size() - TWO_STEP]((*this)[BaseType::Size() - 1])) {
106                 break;
107             }
108             VertexDist t = (*this)[BaseType::Size() - 1];
109             BaseType::PopBack();
110             ModifyLast(t);
111         }
112 
113         if (closed) {
114             while (BaseType::Size() > 1) {
115                 if ((*this)[BaseType::Size() - 1]((*this)[0])) { // Calculate the distance between two vertices
116                     break;
117                 }
118                 BaseType::PopBack();
119             }
120         }
121     }
122 
123     /**
124      * @brief Add a point.
125      *
126      * @param val vertex.
127      * @since 1.0
128      * @version 1.0
129      */
Add(const VertexDist & val)130     void Add(const VertexDist& val)
131     {
132         if (BaseType::Size() > 1) {
133             if (!(*this)[BaseType::Size() - TWO_STEP]((*this)[BaseType::Size() - 1])) {
134                 BaseType::PopBack();
135             }
136         }
137         BaseType::PushBack(val);
138     }
139 
140     /**
141      * @brief Modify the last vertex.
142      *
143      * @param t vertex.
144      * @since 1.0
145      * @version 1.0
146      */
ModifyLast(const VertexDist & val)147     void ModifyLast(const VertexDist& val)
148     {
149         BaseType::Clear();
150         Add(val);
151     }
152 
153     /**
154      * @brief Gets the next data block on the current index.
155      * @since 1.0
156      * @version 1.0
157      */
Next(uint32_t index)158     const VertexDist& Next(uint32_t index)
159     {
160         return (*this)[(index + 1) % BaseType::size_];
161     }
162 
163     /**
164      * @brief Gets the previous data block on the current index.
165      * @since 1.0
166      * @version 1.0
167      */
Prev(uint32_t index)168     const VertexDist& Prev(uint32_t index)
169     {
170         return (*this)[(index + BaseType::size_ - 1) % BaseType::size_];
171     }
172 
173     /**
174      * @brief Gets the data block on the current index.
175      * @since 1.0
176      * @version 1.0
177      */
Curr(uint32_t index)178     const VertexDist& Curr(uint32_t index)
179     {
180         return (*this)[index];
181     }
182 };
183 } // namespace OHOS
184 #endif
185