1 /*
2 * Copyright (c) 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 #ifndef POINT_H
17 #define POINT_H
18
19 #include <cfloat>
20 #include <string>
21 #include "utils/drawing_macros.h"
22 #include "utils/scalar.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class PointF;
28
29 typedef PointF Point;
30
31 class DRAWING_API PointF {
32 public:
33 inline PointF() noexcept;
34 inline PointF(const PointF& p) noexcept;
35 inline PointF(scalar x, scalar y) noexcept;
36
~PointF()37 inline ~PointF() {}
38
39 inline scalar GetX() const;
40 inline scalar GetY() const;
41
42 inline void SetX(scalar x);
43 inline void SetY(scalar y);
44 inline void Set(scalar x, scalar y);
45
46 inline bool IsZero() const;
47
48 inline void Offset(scalar x, scalar y);
49
50 inline PointF& operator+=(const PointF& p);
51 inline PointF& operator-=(const PointF& p);
52 inline PointF& operator*=(scalar scale);
53 inline PointF& operator/=(scalar divisor);
54
55 friend inline const PointF operator+(const PointF& p1, const PointF& p2);
56 friend inline const PointF operator-(const PointF& p1, const PointF& p2);
57 friend inline const PointF operator*(scalar scale, const PointF& p);
58 friend inline const PointF operator*(const PointF& p, scalar scale);
59 friend inline const PointF operator/(const PointF& p, scalar divisor);
60 friend inline const PointF operator+(const PointF& p);
61 friend inline const PointF operator-(const PointF& p);
62 friend inline bool operator==(const PointF& p1, const PointF& p2);
63 friend inline bool operator!=(const PointF& p1, const PointF& p2);
64
65 inline void Dump(std::string& out) const;
66
67 private:
68 scalar x_;
69 scalar y_;
70 };
71
PointF()72 inline PointF::PointF() noexcept : x_(0.0), y_(0.0) {}
73
PointF(const PointF & p)74 inline PointF::PointF(const PointF& p) noexcept
75 {
76 // Tell the compiler there is no alias and to select wider load/store instructions.
77 scalar x = p.GetX();
78 scalar y = p.GetY();
79 x_ = x;
80 y_ = y;
81 }
82
PointF(scalar x,scalar y)83 inline PointF::PointF(scalar x, scalar y) noexcept : x_(x), y_(y) {}
84
GetX()85 inline scalar PointF::GetX() const
86 {
87 return x_;
88 }
89
GetY()90 inline scalar PointF::GetY() const
91 {
92 return y_;
93 }
94
SetX(scalar x)95 inline void PointF::SetX(scalar x)
96 {
97 x_ = x;
98 }
99
SetY(scalar y)100 inline void PointF::SetY(scalar y)
101 {
102 y_ = y;
103 }
104
Set(scalar x,scalar y)105 inline void PointF::Set(scalar x, scalar y)
106 {
107 x_ = x;
108 y_ = y;
109 }
110
IsZero()111 inline bool PointF::IsZero() const
112 {
113 return (0 == x_) && (0 == y_);
114 }
115
Offset(scalar x,scalar y)116 inline void PointF::Offset(scalar x, scalar y)
117 {
118 x_ += x;
119 y_ += y;
120 }
121
122 inline PointF& PointF::operator+=(const PointF& p)
123 {
124 x_ += p.x_;
125 y_ += p.y_;
126 return *this;
127 }
128
129 inline PointF& PointF::operator-=(const PointF& p)
130 {
131 x_ -= p.x_;
132 y_ -= p.y_;
133 return *this;
134 }
135
136 inline PointF& PointF::operator*=(scalar scale)
137 {
138 x_ *= scale;
139 y_ *= scale;
140 return *this;
141 }
142
143 inline PointF& PointF::operator/=(scalar divisor)
144 {
145 if (fabs(divisor) < FLT_EPSILON) {
146 return *this;
147 }
148 x_ /= divisor;
149 y_ /= divisor;
150 return *this;
151 }
152
Dump(std::string & out)153 inline void PointF::Dump(std::string& out) const
154 {
155 out += "[";
156 out += "x:" + std::to_string(x_);
157 out += " y:" + std::to_string(y_);
158 out += "]";
159 }
160
161 inline const PointF operator+(const PointF& p1, const PointF& p2)
162 {
163 return PointF(p1.x_ + p2.x_, p1.y_ + p2.y_);
164 }
165
166 inline const PointF operator-(const PointF& p1, const PointF& p2)
167 {
168 return PointF(p1.x_ - p2.x_, p1.y_ - p2.y_);
169 }
170
171 inline const PointF operator*(scalar scale, const PointF& p)
172 {
173 return PointF(scale * p.x_, scale * p.y_);
174 }
175
176 inline const PointF operator*(const PointF& p, scalar scale)
177 {
178 return PointF(p.x_ * scale, p.y_ * scale);
179 }
180
181 inline const PointF operator/(const PointF& p, scalar divisor)
182 {
183 if (fabs(divisor) < FLT_EPSILON) {
184 return PointF(p.x_, p.y_);
185 }
186 return PointF(p.x_ / divisor, p.y_ / divisor);
187 }
188
189 inline const PointF operator+(const PointF& p)
190 {
191 return PointF(p.x_, p.y_);
192 }
193
194 inline const PointF operator-(const PointF& p)
195 {
196 return PointF(-p.x_, -p.y_);
197 }
198
199 inline bool operator==(const PointF& p1, const PointF& p2)
200 {
201 return IsScalarAlmostEqual(p1.x_, p2.x_) && IsScalarAlmostEqual(p1.y_, p2.y_);
202 }
203
204 inline bool operator!=(const PointF& p1, const PointF& p2)
205 {
206 return !IsScalarAlmostEqual(p1.x_, p2.x_) || !IsScalarAlmostEqual(p1.y_, p2.y_);
207 }
208
209 class DRAWING_API PointI {
210 public:
211 inline PointI() noexcept;
212 inline PointI(const PointI& p) noexcept;
213 inline PointI(int x, int y) noexcept;
214
~PointI()215 inline ~PointI() {}
216
217 inline int GetX() const;
218 inline int GetY() const;
219
220 inline void SetX(int x);
221 inline void SetY(int y);
222 inline void Set(int x, int y);
223
224 inline PointI& operator+=(const PointI& p);
225 inline PointI& operator-=(const PointI& p);
226 inline PointI& operator*=(scalar scale);
227 inline PointI& operator/=(scalar divisor);
228
229 friend inline const PointI operator+(const PointI& p1, const PointI& p2);
230 friend inline const PointI operator-(const PointI& p1, const PointI& p2);
231 friend inline const PointI operator*(scalar scale, const PointI& p);
232 friend inline const PointI operator*(const PointI& p, scalar scale);
233 friend inline const PointI operator/(const PointI& p, scalar divisor);
234 friend inline const PointI operator+(const PointI& p);
235 friend inline const PointI operator-(const PointI& p);
236 friend inline bool operator==(const PointI& p1, const PointI& p2);
237 friend inline bool operator!=(const PointI& p1, const PointI& p2);
238
239 private:
240 int x_;
241 int y_;
242 };
243
PointI()244 inline PointI::PointI() noexcept : x_(0), y_(0) {}
245
PointI(const PointI & p)246 inline PointI::PointI(const PointI& p) noexcept : x_(p.GetX()), y_(p.GetY()) {}
247
PointI(int x,int y)248 inline PointI::PointI(int x, int y) noexcept : x_(x), y_(y) {}
249
GetX()250 inline int PointI::GetX() const
251 {
252 return x_;
253 }
254
GetY()255 inline int PointI::GetY() const
256 {
257 return y_;
258 }
259
SetX(int x)260 inline void PointI::SetX(int x)
261 {
262 x_ = x;
263 }
264
SetY(int y)265 inline void PointI::SetY(int y)
266 {
267 y_ = y;
268 }
269
Set(int x,int y)270 inline void PointI::Set(int x, int y)
271 {
272 x_ = x;
273 y_ = y;
274 }
275
276 inline PointI& PointI::operator+=(const PointI& p)
277 {
278 x_ += p.x_;
279 y_ += p.y_;
280 return *this;
281 }
282
283 inline PointI& PointI::operator-=(const PointI& p)
284 {
285 x_ -= p.x_;
286 y_ -= p.y_;
287 return *this;
288 }
289
290 inline PointI& PointI::operator*=(scalar scale)
291 {
292 x_ = static_cast<int64_t>(x_ * scale);
293 y_ = static_cast<int64_t>(y_ * scale);
294 return *this;
295 }
296
297 inline PointI& PointI::operator/=(scalar divisor)
298 {
299 if (divisor == 0) {
300 return *this;
301 }
302 x_ = static_cast<int>(x_ / divisor);
303 y_ = static_cast<int>(y_ / divisor);
304 return *this;
305 }
306
307 inline const PointI operator+(const PointI& p1, const PointI& p2)
308 {
309 return PointI(p1.x_ + p2.x_, p1.y_ + p2.y_);
310 }
311
312 inline const PointI operator-(const PointI& p1, const PointI& p2)
313 {
314 return PointI(p1.x_ - p2.x_, p1.y_ - p2.y_);
315 }
316
317 inline const PointI operator*(scalar scale, const PointI& p)
318 {
319 return PointI(static_cast<int64_t>(scale * p.x_), static_cast<int64_t>(scale * p.y_));
320 }
321
322 inline const PointI operator*(const PointI& p, scalar scale)
323 {
324 return PointI(static_cast<int64_t>(p.x_ * scale), static_cast<int64_t>(p.y_ * scale));
325 }
326
327 inline const PointI operator/(const PointI& p, scalar divisor)
328 {
329 if (divisor == 0) {
330 return PointI(p.x_, p.y_);
331 }
332 return PointI(static_cast<int>(p.x_ / divisor), static_cast<int>(p.y_ / divisor));
333 }
334
335 inline const PointI operator+(const PointI& p)
336 {
337 return PointI(p.x_, p.y_);
338 }
339
340 inline const PointI operator-(const PointI& p)
341 {
342 return PointI(-p.x_, -p.y_);
343 }
344
345 inline bool operator==(const PointI& p1, const PointI& p2)
346 {
347 return p1.x_ == p2.x_ && p1.y_ == p2.y_;
348 }
349
350 inline bool operator!=(const PointI& p1, const PointI& p2)
351 {
352 return p1.x_ != p2.x_ || p1.y_ != p2.y_;
353 }
354 } // namespace Drawing
355 } // namespace Rosen
356 } // namespace OHOS
357 #endif
358