lottie: modernize using clang-tidy 'modernize-use-default-member-init'
[platform/core/uifw/lottie-player.git] / src / vector / vrect.h
1 #ifndef VRECT_H
2 #define VRECT_H
3 #include "vglobal.h"
4 #include "vpoint.h"
5
6 V_BEGIN_NAMESPACE
7
8 class VRect {
9 public:
10     VRect() = default;
11     V_CONSTEXPR             VRect(int left, int top, int width, int height);
12     V_CONSTEXPR inline bool isEmpty() const;
13     V_CONSTEXPR inline bool isNull() const;
14
15     V_CONSTEXPR inline int left() const;
16     V_CONSTEXPR inline int top() const;
17     V_CONSTEXPR inline int right() const;
18     V_CONSTEXPR inline int bottom() const;
19     V_CONSTEXPR inline int width() const;
20     V_CONSTEXPR inline int height() const;
21     V_CONSTEXPR inline int x() const;
22     V_CONSTEXPR inline int y() const;
23     inline void            setLeft(int l) { x1 = l; }
24     inline void            setTop(int t) { y1 = t; }
25     inline void            setRight(int r) { x2 = r; }
26     inline void            setBottom(int b) { y2 = b; }
27     inline void            setWidth(int w) { x2 = x1 + w; }
28     inline void            setHeight(int h) { y2 = y1 + h; }
29     inline VRect           translated(int dx, int dy) const;
30     inline void            translate(int dx, int dy);
31     inline bool            contains(const VRect &r, bool proper = false) const;
32     inline bool            intersects(const VRect &r);
33     friend V_CONSTEXPR inline bool operator==(const VRect &,
34                                               const VRect &) noexcept;
35     friend V_CONSTEXPR inline bool operator!=(const VRect &,
36                                               const VRect &) noexcept;
37     friend VDebug &                operator<<(VDebug &os, const VRect &o);
38
39 private:
40     int x1{0};
41     int y1{0};
42     int x2{-1};
43     int y2{-1};
44 };
45
46 inline bool VRect::intersects(const VRect &r)
47 {
48     return (right() > r.left() && left() < r.right() && bottom() > r.top() &&
49             top() < r.bottom());
50 }
51
52 inline VDebug &operator<<(VDebug &os, const VRect &o)
53 {
54     os << "{R " << o.x() << "," << o.y() << "," << o.width() << ","
55        << o.height() << "}";
56     return os;
57 }
58 V_CONSTEXPR inline bool operator==(const VRect &r1, const VRect &r2) noexcept
59 {
60     return r1.x1 == r2.x1 && r1.x2 == r2.x2 && r1.y1 == r2.y1 && r1.y2 == r2.y2;
61 }
62
63 V_CONSTEXPR inline bool operator!=(const VRect &r1, const VRect &r2) noexcept
64 {
65     return r1.x1 != r2.x1 || r1.x2 != r2.x2 || r1.y1 != r2.y1 || r1.y2 != r2.y2;
66 }
67
68 V_CONSTEXPR inline bool VRect::isEmpty() const
69 {
70     return x1 > x2 || y1 > y2;
71 }
72
73 V_CONSTEXPR inline bool VRect::isNull() const
74 {
75     return (((x2 - x1) == 0) || ((y2 - y1) == 0));
76 }
77
78 V_CONSTEXPR inline int VRect::x() const
79 {
80     return x1;
81 }
82
83 V_CONSTEXPR inline int VRect::y() const
84 {
85     return y1;
86 }
87
88 V_CONSTEXPR inline int VRect::left() const
89 {
90     return x1;
91 }
92
93 V_CONSTEXPR inline int VRect::top() const
94 {
95     return y1;
96 }
97
98 V_CONSTEXPR inline int VRect::right() const
99 {
100     return x2;
101 }
102
103 V_CONSTEXPR inline int VRect::bottom() const
104 {
105     return y2;
106 }
107 V_CONSTEXPR inline int VRect::width() const
108 {
109     return x2 - x1;
110 }
111 V_CONSTEXPR inline int VRect::height() const
112 {
113     return y2 - y1;
114 }
115
116 inline VRect VRect::translated(int dx, int dy) const
117 {
118     return {x1 + dx, y1 + dy, x2 - x1, y2 - y1};
119 }
120
121 inline void VRect::translate(int dx, int dy)
122 {
123     x1 += dx;
124     y1 += dy;
125     x2 += dx;
126     y2 += dy;
127 }
128 inline bool VRect::contains(const VRect &r, bool proper) const
129 {
130     if (!proper) {
131         if ((x1 <= r.x1) && (x2 >= r.x2) && (y1 <= r.y1) && (y2 >= r.y2))
132             return true;
133         return false;
134     } else {
135         if ((x1 < r.x1) && (x2 > r.x2) && (y1 < r.y1) && (y2 > r.y2))
136             return true;
137         return false;
138     }
139 }
140 V_CONSTEXPR inline VRect::VRect(int left, int top, int width, int height)
141     : x1(left), y1(top), x2(width + left), y2(height + top)
142 {
143 }
144
145 class VRectF {
146 public:
147     VRectF() = default;
148     VRectF(float left, float top, float width, float height)
149     {
150         x1 = left;
151         y1 = top;
152         x2 = x1 + width;
153         y2 = y1 + height;
154     }
155
156     V_CONSTEXPR inline bool    isEmpty() const;
157     V_CONSTEXPR inline bool    isNull() const;
158     V_CONSTEXPR inline float   left() const;
159     V_CONSTEXPR inline float   top() const;
160     V_CONSTEXPR inline float   right() const;
161     V_CONSTEXPR inline float   bottom() const;
162     V_CONSTEXPR inline float   width() const;
163     V_CONSTEXPR inline float   height() const;
164     V_CONSTEXPR inline float   x() const;
165     V_CONSTEXPR inline float   y() const;
166     V_CONSTEXPR inline VPointF center() const
167     {
168         return {x1 + (x2 - x1) / 2.f, y1 + (y2 - y1) / 2.f};
169     }
170     inline void setLeft(float l) { x1 = l; }
171     inline void setTop(float t) { y1 = t; }
172     inline void setRight(float r) { x2 = r; }
173     inline void setBottom(float b) { y2 = b; }
174     inline void setWidth(float w) { x2 = x1 + w; }
175     inline void setHeight(float h) { y2 = y1 + h; }
176     inline void translate(float dx, float dy)
177     {
178         x1 += dx;
179         y1 += dy;
180         x2 += dx;
181         y2 += dy;
182     }
183
184 private:
185     float x1{0};
186     float y1{0};
187     float x2{-1};
188     float y2{-1};
189 };
190
191 V_CONSTEXPR inline bool VRectF::isEmpty() const
192 {
193     return x1 > x2 || y1 > y2;
194 }
195
196 V_CONSTEXPR inline bool VRectF::isNull() const
197 {
198     return (((x2 - x1) == 0) || ((y2 - y1) == 0));
199 }
200
201 V_CONSTEXPR inline float VRectF::x() const
202 {
203     return x1;
204 }
205
206 V_CONSTEXPR inline float VRectF::y() const
207 {
208     return y1;
209 }
210
211 V_CONSTEXPR inline float VRectF::left() const
212 {
213     return x1;
214 }
215
216 V_CONSTEXPR inline float VRectF::top() const
217 {
218     return y1;
219 }
220
221 V_CONSTEXPR inline float VRectF::right() const
222 {
223     return x2;
224 }
225
226 V_CONSTEXPR inline float VRectF::bottom() const
227 {
228     return y2;
229 }
230 V_CONSTEXPR inline float VRectF::width() const
231 {
232     return x2 - x1;
233 }
234 V_CONSTEXPR inline float VRectF::height() const
235 {
236     return y2 - y1;
237 }
238
239 V_END_NAMESPACE
240
241 #endif  // VRECT_H