1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3 
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10 
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef VPOINT_H
23 #define VPOINT_H
24 
25 #include "vglobal.h"
26 
27 V_BEGIN_NAMESPACE
28 
29 class VPointF {
30 public:
31     VPointF() = default;
VPointF(float x,float y)32     constexpr inline VPointF(float x, float y) noexcept : mx(x), my(y) {}
x()33     constexpr inline float x() const noexcept { return mx; }
y()34     constexpr inline float y() const noexcept { return my; }
rx()35     inline float &         rx() noexcept { return mx; }
ry()36     inline float &         ry() noexcept { return my; }
setX(float x)37     inline void            setX(float x) { mx = x; }
setY(float y)38     inline void            setY(float y) { my = y; }
39     inline VPointF         operator-() noexcept { return {-mx, -my}; }
40     inline VPointF &       operator+=(const VPointF &p) noexcept;
41     inline VPointF &       operator-=(const VPointF &p) noexcept;
42     friend const VPointF   operator+(const VPointF &p1, const VPointF &p2)
43     {
44         return VPointF(p1.mx + p2.mx, p1.my + p2.my);
45     }
46     inline friend bool fuzzyCompare(const VPointF &p1, const VPointF &p2);
47     inline friend VDebug &   operator<<(VDebug &os, const VPointF &o);
48 
49     friend inline VPointF       operator-(const VPointF &p1, const VPointF &p2);
50     friend inline const VPointF operator*(const VPointF &, float);
51     friend inline const VPointF operator*(float, const VPointF &);
52     friend inline const VPointF operator/(const VPointF &, float);
53     friend inline const VPointF operator/(float, const VPointF &);
54 
55 private:
56     float mx{0};
57     float my{0};
58 };
59 
fuzzyCompare(const VPointF & p1,const VPointF & p2)60 inline bool fuzzyCompare(const VPointF &p1, const VPointF &p2)
61 {
62     return (vCompare(p1.mx, p2.mx) && vCompare(p1.my, p2.my));
63 }
64 
65 inline VPointF operator-(const VPointF &p1, const VPointF &p2)
66 {
67     return {p1.mx - p2.mx, p1.my - p2.my};
68 }
69 
70 inline const VPointF operator*(const VPointF &p, float c)
71 {
72     return VPointF(p.mx * c, p.my * c);
73 }
74 
75 inline const VPointF operator*(float c, const VPointF &p)
76 {
77     return VPointF(p.mx * c, p.my * c);
78 }
79 
80 inline const VPointF operator/(const VPointF &p, float c)
81 {
82     return VPointF(p.mx / c, p.my / c);
83 }
84 
85 inline const VPointF operator/(float c, const VPointF &p)
86 {
87     return VPointF(p.mx / c, p.my / c);
88 }
89 
90 inline VDebug &operator<<(VDebug &os, const VPointF &o)
91 {
92     os << "{P " << o.x() << "," << o.y() << "}";
93     return os;
94 }
95 
96 inline VPointF &VPointF::operator+=(const VPointF &p) noexcept
97 {
98     mx += p.mx;
99     my += p.my;
100     return *this;
101 }
102 
103 inline VPointF &VPointF::operator-=(const VPointF &p) noexcept
104 {
105     mx -= p.mx;
106     my -= p.my;
107     return *this;
108 }
109 
110 class VPoint {
111 public:
112     VPoint() = default;
VPoint(int x,int y)113     constexpr inline VPoint(int x, int y) noexcept : mx(x), my(y) {}
x()114     constexpr inline int  x() const noexcept { return mx; }
y()115     constexpr inline int  y() const noexcept { return my; }
setX(int x)116     inline void           setX(int x) { mx = x; }
setY(int y)117     inline void           setY(int y) { my = y; }
118     inline VPoint &       operator+=(const VPoint &p) noexcept;
119     inline VPoint &       operator-=(const VPoint &p) noexcept;
120     constexpr inline bool operator==(const VPoint &o) const;
121     constexpr inline bool operator!=(const VPoint &o) const
122     {
123         return !(operator==(o));
124     }
125     friend inline VPoint  operator-(const VPoint &p1, const VPoint &p2);
126     inline friend VDebug &operator<<(VDebug &os, const VPoint &o);
127 
128 private:
129     int mx{0};
130     int my{0};
131 };
132 inline VDebug &operator<<(VDebug &os, const VPoint &o)
133 {
134     os << "{P " << o.x() << "," << o.y() << "}";
135     return os;
136 }
137 
138 inline VPoint operator-(const VPoint &p1, const VPoint &p2)
139 {
140     return {p1.mx - p2.mx, p1.my - p2.my};
141 }
142 
143 constexpr inline bool VPoint::operator==(const VPoint &o) const
144 {
145     return (mx == o.x() && my == o.y());
146 }
147 
148 inline VPoint &VPoint::operator+=(const VPoint &p) noexcept
149 {
150     mx += p.mx;
151     my += p.my;
152     return *this;
153 }
154 
155 inline VPoint &VPoint::operator-=(const VPoint &p) noexcept
156 {
157     mx -= p.mx;
158     my -= p.my;
159     return *this;
160 }
161 
162 class VSize {
163 public:
164     VSize() = default;
VSize(int w,int h)165     constexpr inline VSize(int w, int h) noexcept : mw(w), mh(h) {}
empty()166     bool empty() const {return (mw <= 0 || mh <= 0);}
width()167     constexpr inline int  width() const noexcept { return mw; }
height()168     constexpr inline int  height() const noexcept { return mh; }
setWidth(int w)169     inline void           setWidth(int w) { mw = w; }
setHeight(int h)170     inline void           setHeight(int h) { mh = h; }
171     inline VSize &        operator+=(const VSize &p) noexcept;
172     inline VSize &        operator-=(const VSize &p) noexcept;
173     constexpr inline bool operator==(const VSize &o) const;
174     constexpr inline bool operator!=(const VSize &o) const
175     {
176         return !(operator==(o));
177     }
178     inline friend VDebug &operator<<(VDebug &os, const VSize &o);
179 
180 private:
181     int mw{0};
182     int mh{0};
183 };
184 inline VDebug &operator<<(VDebug &os, const VSize &o)
185 {
186     os << "{P " << o.width() << "," << o.height() << "}";
187     return os;
188 }
189 constexpr inline bool VSize::operator==(const VSize &o) const
190 {
191     return (mw == o.width() && mh == o.height());
192 }
193 
194 inline VSize &VSize::operator+=(const VSize &p) noexcept
195 {
196     mw += p.mw;
197     mh += p.mh;
198     return *this;
199 }
200 
201 inline VSize &VSize::operator-=(const VSize &p) noexcept
202 {
203     mw -= p.mw;
204     mh -= p.mh;
205     return *this;
206 }
207 
208 V_END_NAMESPACE
209 
210 #endif  // VPOINT_H
211