Put samsung copyright header to all our own source code.
[platform/core/uifw/lottie-player.git] / src / vector / vrect.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef VRECT_H
18 #define VRECT_H
19 #include "vglobal.h"
20 #include "vpoint.h"
21
22 V_BEGIN_NAMESPACE
23 class VRectF;
24
25 class VRect {
26 public:
27     VRect() = default;
28     VRect(int x, int y, int w, int h):x1(x),y1(y),x2(x+w),y2(y+h){}
29     VRect(const VRectF &r);
30     V_CONSTEXPR bool empty() const {return x1 >= x2 || y1 >= y2;}
31     V_CONSTEXPR int left() const {return x1;}
32     V_CONSTEXPR int top() const {return y1;}
33     V_CONSTEXPR int right() const {return x2;}
34     V_CONSTEXPR int bottom() const {return y2;}
35     V_CONSTEXPR int width() const {return x2-x1;}
36     V_CONSTEXPR int height() const {return y2-y1;}
37     V_CONSTEXPR int x() const {return x1;}
38     V_CONSTEXPR int y() const {return y1;}
39     void            setLeft(int l) { x1 = l; }
40     void            setTop(int t) { y1 = t; }
41     void            setRight(int r) { x2 = r; }
42     void            setBottom(int b) { y2 = b; }
43     void            setWidth(int w) { x2 = x1 + w; }
44     void            setHeight(int h) { y2 = y1 + h; }
45     VRect    translated(int dx, int dy) const;
46     void     translate(int dx, int dy);
47     bool     contains(const VRect &r, bool proper = false) const;
48     bool     intersects(const VRect &r);
49     friend V_CONSTEXPR inline bool operator==(const VRect &,
50                                               const VRect &) noexcept;
51     friend V_CONSTEXPR inline bool operator!=(const VRect &,
52                                               const VRect &) noexcept;
53     friend VDebug &                operator<<(VDebug &os, const VRect &o);
54
55 private:
56     int x1{0};
57     int y1{0};
58     int x2{0};
59     int y2{0};
60 };
61
62 inline bool VRect::intersects(const VRect &r)
63 {
64     return (right() > r.left() && left() < r.right() && bottom() > r.top() &&
65             top() < r.bottom());
66 }
67
68 inline VDebug &operator<<(VDebug &os, const VRect &o)
69 {
70     os << "{R " << o.x() << "," << o.y() << "," << o.width() << ","
71        << o.height() << "}";
72     return os;
73 }
74 V_CONSTEXPR inline bool operator==(const VRect &r1, const VRect &r2) noexcept
75 {
76     return r1.x1 == r2.x1 && r1.x2 == r2.x2 && r1.y1 == r2.y1 && r1.y2 == r2.y2;
77 }
78
79 V_CONSTEXPR inline bool operator!=(const VRect &r1, const VRect &r2) noexcept
80 {
81     return r1.x1 != r2.x1 || r1.x2 != r2.x2 || r1.y1 != r2.y1 || r1.y2 != r2.y2;
82 }
83
84 inline VRect VRect::translated(int dx, int dy) const
85 {
86     return {x1 + dx, y1 + dy, x2 - x1, y2 - y1};
87 }
88
89 inline void VRect::translate(int dx, int dy)
90 {
91     x1 += dx;
92     y1 += dy;
93     x2 += dx;
94     y2 += dy;
95 }
96
97 inline bool VRect::contains(const VRect &r, bool proper) const
98 {
99     if (!proper) {
100         if ((x1 <= r.x1) && (x2 >= r.x2) && (y1 <= r.y1) && (y2 >= r.y2))
101             return true;
102         return false;
103     } else {
104         if ((x1 < r.x1) && (x2 > r.x2) && (y1 < r.y1) && (y2 > r.y2))
105             return true;
106         return false;
107     }
108 }
109
110 class VRectF {
111 public:
112     VRectF() = default;
113     VRectF(float x, float y, float w, float h):x1(x),y1(y),x2(x+w),y2(y+h){}
114     VRectF(const VRect &r):x1(r.left()),y1(r.top()),
115                            x2(r.right()),y2(r.bottom()){}
116
117     V_CONSTEXPR bool  empty() const {return x1 >= x2 || y1 >= y2;}
118     V_CONSTEXPR float left() const {return x1;}
119     V_CONSTEXPR float top() const {return y1;}
120     V_CONSTEXPR float right() const {return x2;}
121     V_CONSTEXPR float bottom() const {return y2;}
122     V_CONSTEXPR float width() const {return x2-x1;}
123     V_CONSTEXPR float height() const {return y2-y1;}
124     V_CONSTEXPR float x() const {return x1;}
125     V_CONSTEXPR float y() const {return y1;}
126     V_CONSTEXPR inline VPointF center() const
127     {
128         return {x1 + (x2 - x1) / 2.f, y1 + (y2 - y1) / 2.f};
129     }
130     void setLeft(float l) { x1 = l; }
131     void setTop(float t) { y1 = t; }
132     void setRight(float r) { x2 = r; }
133     void setBottom(float b) { y2 = b; }
134     void setWidth(float w) { x2 = x1 + w; }
135     void setHeight(float h) { y2 = y1 + h; }
136     void translate(float dx, float dy)
137     {
138         x1 += dx;
139         y1 += dy;
140         x2 += dx;
141         y2 += dy;
142     }
143
144 private:
145     float x1{0};
146     float y1{0};
147     float x2{0};
148     float y2{0};
149 };
150
151 inline VRect::VRect(const VRectF &r):x1(r.left()),y1(r.top()),
152                                      x2(r.right()),y2(r.bottom()){}
153 V_END_NAMESPACE
154
155 #endif  // VRECT_H