updated licenses info.
[platform/core/uifw/lottie-player.git] / src / vector / vrect.h
1 /* 
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #ifndef VRECT_H
20 #define VRECT_H
21 #include "vglobal.h"
22 #include "vpoint.h"
23
24 V_BEGIN_NAMESPACE
25 class VRectF;
26
27 class VRect {
28 public:
29     VRect() = default;
30     VRect(int x, int y, int w, int h):x1(x),y1(y),x2(x+w),y2(y+h){}
31     VRect(const VRectF &r);
32     V_CONSTEXPR bool empty() const {return x1 >= x2 || y1 >= y2;}
33     V_CONSTEXPR int left() const {return x1;}
34     V_CONSTEXPR int top() const {return y1;}
35     V_CONSTEXPR int right() const {return x2;}
36     V_CONSTEXPR int bottom() const {return y2;}
37     V_CONSTEXPR int width() const {return x2-x1;}
38     V_CONSTEXPR int height() const {return y2-y1;}
39     V_CONSTEXPR int x() const {return x1;}
40     V_CONSTEXPR int y() const {return y1;}
41     void            setLeft(int l) { x1 = l; }
42     void            setTop(int t) { y1 = t; }
43     void            setRight(int r) { x2 = r; }
44     void            setBottom(int b) { y2 = b; }
45     void            setWidth(int w) { x2 = x1 + w; }
46     void            setHeight(int h) { y2 = y1 + h; }
47     VRect    translated(int dx, int dy) const;
48     void     translate(int dx, int dy);
49     bool     contains(const VRect &r, bool proper = false) const;
50     bool     intersects(const VRect &r);
51     friend V_CONSTEXPR inline bool operator==(const VRect &,
52                                               const VRect &) noexcept;
53     friend V_CONSTEXPR inline bool operator!=(const VRect &,
54                                               const VRect &) noexcept;
55     friend VDebug &                operator<<(VDebug &os, const VRect &o);
56
57 private:
58     int x1{0};
59     int y1{0};
60     int x2{0};
61     int y2{0};
62 };
63
64 inline bool VRect::intersects(const VRect &r)
65 {
66     return (right() > r.left() && left() < r.right() && bottom() > r.top() &&
67             top() < r.bottom());
68 }
69
70 inline VDebug &operator<<(VDebug &os, const VRect &o)
71 {
72     os << "{R " << o.x() << "," << o.y() << "," << o.width() << ","
73        << o.height() << "}";
74     return os;
75 }
76 V_CONSTEXPR inline bool operator==(const VRect &r1, const VRect &r2) noexcept
77 {
78     return r1.x1 == r2.x1 && r1.x2 == r2.x2 && r1.y1 == r2.y1 && r1.y2 == r2.y2;
79 }
80
81 V_CONSTEXPR inline bool operator!=(const VRect &r1, const VRect &r2) noexcept
82 {
83     return r1.x1 != r2.x1 || r1.x2 != r2.x2 || r1.y1 != r2.y1 || r1.y2 != r2.y2;
84 }
85
86 inline VRect VRect::translated(int dx, int dy) const
87 {
88     return {x1 + dx, y1 + dy, x2 - x1, y2 - y1};
89 }
90
91 inline void VRect::translate(int dx, int dy)
92 {
93     x1 += dx;
94     y1 += dy;
95     x2 += dx;
96     y2 += dy;
97 }
98
99 inline bool VRect::contains(const VRect &r, bool proper) const
100 {
101     if (!proper) {
102         if ((x1 <= r.x1) && (x2 >= r.x2) && (y1 <= r.y1) && (y2 >= r.y2))
103             return true;
104         return false;
105     } else {
106         if ((x1 < r.x1) && (x2 > r.x2) && (y1 < r.y1) && (y2 > r.y2))
107             return true;
108         return false;
109     }
110 }
111
112 class VRectF {
113 public:
114     VRectF() = default;
115     VRectF(float x, float y, float w, float h):x1(x),y1(y),x2(x+w),y2(y+h){}
116     VRectF(const VRect &r):x1(r.left()),y1(r.top()),
117                            x2(r.right()),y2(r.bottom()){}
118
119     V_CONSTEXPR bool  empty() const {return x1 >= x2 || y1 >= y2;}
120     V_CONSTEXPR float left() const {return x1;}
121     V_CONSTEXPR float top() const {return y1;}
122     V_CONSTEXPR float right() const {return x2;}
123     V_CONSTEXPR float bottom() const {return y2;}
124     V_CONSTEXPR float width() const {return x2-x1;}
125     V_CONSTEXPR float height() const {return y2-y1;}
126     V_CONSTEXPR float x() const {return x1;}
127     V_CONSTEXPR float y() const {return y1;}
128     V_CONSTEXPR inline VPointF center() const
129     {
130         return {x1 + (x2 - x1) / 2.f, y1 + (y2 - y1) / 2.f};
131     }
132     void setLeft(float l) { x1 = l; }
133     void setTop(float t) { y1 = t; }
134     void setRight(float r) { x2 = r; }
135     void setBottom(float b) { y2 = b; }
136     void setWidth(float w) { x2 = x1 + w; }
137     void setHeight(float h) { y2 = y1 + h; }
138     void translate(float dx, float dy)
139     {
140         x1 += dx;
141         y1 += dy;
142         x2 += dx;
143         y2 += dy;
144     }
145
146 private:
147     float x1{0};
148     float y1{0};
149     float x2{0};
150     float y2{0};
151 };
152
153 inline VRect::VRect(const VRectF &r):x1(r.left()),y1(r.top()),
154                                      x2(r.right()),y2(r.bottom()){}
155 V_END_NAMESPACE
156
157 #endif  // VRECT_H