• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 RENDER_SERVICE_CLIENT_CORE_COMMON_RS_RECT_H
17 #define RENDER_SERVICE_CLIENT_CORE_COMMON_RS_RECT_H
18 #include <cmath>
19 
20 #include "common/rs_common_def.h"
21 #include "common/rs_vector2.h"
22 #include "common/rs_vector4.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 template<typename T>
27 class RectT {
28 public:
29     union {
30         struct {
31             T left_;
32             T top_;
33             T width_;
34             T height_;
35         };
36         T data_[4]; // 4 is size of data or structure
37     };
38 
RectT()39     RectT()
40     {
41         data_[0] = 0;
42         data_[1] = 0;
43         data_[2] = 0;
44         data_[3] = 0;
45     }
RectT(T left,T top,T width,T height)46     RectT(T left, T top, T width, T height)
47     {
48         data_[0] = left;
49         data_[1] = top;
50         data_[2] = width;
51         data_[3] = height;
52     }
RectT(const T * v)53     explicit RectT(const T* v)
54     {
55         data_[0] = v[0];
56         data_[1] = v[1];
57         data_[2] = v[2];
58         data_[3] = v[3];
59     }
60     ~RectT() = default;
61 
62     inline bool operator==(const RectT<T>& rect) const
63     {
64         return (left_ == rect.left_) && (top_ == rect.top_) && (width_ == rect.width_) && (height_ == rect.height_);
65     }
66 
67     inline bool operator!=(const RectT<T>& rect) const
68     {
69         return (left_ != rect.left_) || (top_ != rect.top_) || (width_ != rect.width_) || (height_ != rect.height_);
70     }
71 
72     inline RectT& operator=(const RectT& other)
73     {
74         const T* oData = other.data_;
75         data_[0] = oData[0];
76         data_[1] = oData[1];
77         data_[2] = oData[2];
78         data_[3] = oData[3];
79         return *this;
80     }
SetAll(T left,T top,T width,T height)81     void SetAll(T left, T top, T width, T height)
82     {
83         data_[0] = left;
84         data_[1] = top;
85         data_[2] = width;
86         data_[3] = height;
87     }
GetRight()88     T GetRight() const
89     {
90         return left_ + width_;
91     }
GetLeft()92     T GetLeft() const
93     {
94         return left_;
95     }
GetBottom()96     T GetBottom() const
97     {
98         return top_ + height_;
99     }
GetTop()100     T GetTop() const
101     {
102         return top_;
103     }
GetWidth()104     T GetWidth() const
105     {
106         return width_;
107     }
GetHeight()108     T GetHeight() const
109     {
110         return height_;
111     }
SetRight(T right)112     void SetRight(T right)
113     {
114         width_ = right - left_;
115     }
SetBottom(T bottom)116     void SetBottom(T bottom)
117     {
118         height_ = bottom - top_;
119     }
Move(T x,T y)120     void Move(T x, T y)
121     {
122         left_ += x;
123         top_ += y;
124     }
Clear()125     void Clear()
126     {
127         left_ = 0;
128         top_ = 0;
129         width_ = 0;
130         height_ = 0;
131     }
IsEmpty()132     bool IsEmpty() const
133     {
134         return width_ <= 0 || height_ <= 0;
135     }
Intersect(T x,T y)136     bool Intersect(T x, T y) const
137     {
138         return (x >= left_) && (x < GetRight()) && (y >= top_) && (y < GetBottom());
139     }
IsInsideOf(const RectT<T> & rect)140     bool IsInsideOf(const RectT<T>& rect) const
141     {
142         return (top_ >= rect.top_ && left_ >= rect.left_ &&
143             GetBottom() <= rect.GetBottom() && GetRight() <= rect.GetRight());
144     }
IntersectRect(const RectT<T> & rect)145     RectT<T> IntersectRect(const RectT<T>& rect) const
146     {
147         T left = std::max(left_, rect.left_);
148         T top = std::max(top_, rect.top_);
149         T width = std::min(GetRight(), rect.GetRight()) - left;
150         T height = std::min(GetBottom(), rect.GetBottom()) - top;
151         return ((width <= 0) || (height <= 0)) ? RectT<T>() : RectT<T>(left, top, width, height);
152     }
JoinRect(const RectT<T> & rect)153     RectT<T> JoinRect(const RectT<T>& rect) const
154     {
155         T left = std::min(left_, rect.left_);
156         T top = std::min(top_, rect.top_);
157         T width = std::max(GetRight(), rect.GetRight()) - left;
158         T height = std::max(GetBottom(), rect.GetBottom()) - top;
159         return ((width <= 0) || (height <= 0)) ? RectT<T>() : RectT<T>(left, top, width, height);
160     }
161     template<typename P>
ConvertTo()162     RectT<P> ConvertTo()
163     {
164         return RectT<P>(static_cast<P>(left_), static_cast<P>(top_), static_cast<P>(width_), static_cast<P>(height_));
165     }
ToString()166     std::string ToString() const
167     {
168         return std::string("(") + std::to_string(left_) + ", " + std::to_string(top_) + ", " +
169             std::to_string(width_) + ", " + std::to_string(height_) + ")";
170     }
171 };
172 
173 typedef RectT<int> RectI;
174 typedef RectT<float> RectF;
175 
176 template<typename T>
177 class RRectT {
178 public:
179     RectT<T> rect_ = RectT<T>();
180     Vector2f radius_[4] = { { 0, 0 } };
181 
RRectT()182     RRectT() {}
183     ~RRectT() = default;
184 
RRectT(RectT<T> rect,float rx,float ry)185     RRectT(RectT<T> rect, float rx, float ry)
186     {
187         rect_ = rect;
188         Vector2f vec = Vector2f(rx, ry);
189         radius_[0] = vec;
190         radius_[1] = vec;
191         radius_[2] = vec;
192         radius_[3] = vec;
193     }
RRectT(RectT<T> rect,const Vector2f * radius)194     RRectT(RectT<T> rect, const Vector2f* radius)
195     {
196         rect_ = rect;
197         radius_[0] = radius[0];
198         radius_[1] = radius[1];
199         radius_[2] = radius[2];
200         radius_[3] = radius[3];
201     }
RRectT(RectT<T> rect,const Vector4f & radius)202     RRectT(RectT<T> rect, const Vector4f& radius)
203     {
204         rect_ = rect;
205         radius_[0] = { radius[0], radius[0] };
206         radius_[1] = { radius[1], radius[1] };
207         radius_[2] = { radius[2], radius[2] };
208         radius_[3] = { radius[3], radius[3] };
209     }
210 };
211 typedef RRectT<float> RRect;
212 } // namespace Rosen
213 } // namespace OHOS
214 #endif
215