• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
23 namespace OHOS {
24 namespace Rosen {
25 template<typename T>
26 class RectT {
27 public:
28     union {
29         struct {
30             T left_;
31             T top_;
32             T width_;
33             T height_;
34         };
35         T data_[4]; // 4 is size of data or structure
36     };
37 
RectT()38     RectT()
39     {
40         data_[0] = 0;
41         data_[1] = 0;
42         data_[2] = 0;
43         data_[3] = 0;
44     }
RectT(T left,T top,T width,T height)45     RectT(T left, T top, T width, T height)
46     {
47         data_[0] = left;
48         data_[1] = top;
49         data_[2] = width;
50         data_[3] = height;
51     }
RectT(const T * v)52     explicit RectT(const T* v)
53     {
54         data_[0] = v[0];
55         data_[1] = v[1];
56         data_[2] = v[2];
57         data_[3] = v[3];
58     }
59     ~RectT() = default;
60 
61     inline bool operator==(const RectT<T>& rect) const
62     {
63         return (left_ == rect.left_) && (top_ == rect.top_) && (width_ == rect.width_) && (height_ == rect.height_);
64     }
65 
66     inline bool operator!=(const RectT<T>& rect) const
67     {
68         return (left_ != rect.left_) || (top_ != rect.top_) || (width_ != rect.width_) || (height_ != rect.height_);
69     }
70 
71     inline RectT& operator=(const RectT& other)
72     {
73         const T* oData = other.data_;
74         data_[0] = oData[0];
75         data_[1] = oData[1];
76         data_[2] = oData[2];
77         data_[3] = oData[3];
78         return *this;
79     }
SetAll(T left,T top,T width,T height)80     void SetAll(T left, T top, T width, T height)
81     {
82         data_[0] = left;
83         data_[1] = top;
84         data_[2] = width;
85         data_[3] = height;
86     }
GetRight()87     T GetRight() const
88     {
89         return left_ + width_;
90     }
GetBottom()91     T GetBottom() const
92     {
93         return top_ + height_;
94     }
SetRight(T right)95     void SetRight(T right)
96     {
97         width_ = right - left_;
98     }
SetBottom(T bottom)99     void SetBottom(T bottom)
100     {
101         height_ = bottom - top_;
102     }
Move(T x,T y)103     void Move(T x, T y)
104     {
105         left_ += x;
106         top_ += y;
107     }
Clear()108     void Clear()
109     {
110         left_ = 0;
111         top_ = 0;
112         width_ = 0;
113         height_ = 0;
114     }
IsEmpty()115     bool IsEmpty() const
116     {
117         return width_ <= 0 || height_ <= 0;
118     }
Intersect(T x,T y)119     bool Intersect(T x, T y) const
120     {
121         return (x >= left_) && (x < GetRight()) && (y >= top_) && (y < GetBottom());
122     }
IntersectRect(const RectT<T> & rect)123     RectT<T> IntersectRect(const RectT<T>& rect) const
124     {
125         T left = std::max(left_, rect.left_);
126         T top = std::max(top_, rect.top_);
127         T width = std::min(GetRight(), rect.GetRight()) - left;
128         T height = std::min(GetBottom(), rect.GetBottom()) - top;
129         return ((width <= 0) || (height <= 0)) ? RectT<T>() : RectT<T>(left, top, width, height);
130     }
JoinRect(const RectT<T> & rect)131     RectT<T> JoinRect(const RectT<T>& rect) const
132     {
133         T left = std::min(left_, rect.left_);
134         T top = std::min(top_, rect.top_);
135         T width = std::max(GetRight(), rect.GetRight()) - left;
136         T height = std::max(GetBottom(), rect.GetBottom()) - top;
137         return ((width <= 0) || (height <= 0)) ? RectT<T>() : RectT<T>(left, top, width, height);
138     }
139 };
140 
141 typedef RectT<int> RectI;
142 typedef RectT<float> RectF;
143 
144 template<typename T>
145 class RRectT {
146 public:
147     RectT<T> rect_ = RectT<T>();
148     Vector2f radius_[4] = { { 0, 0 } };
149 
RRectT()150     RRectT() {}
151     ~RRectT() = default;
152 
RRectT(RectT<T> rect,float rx,float ry)153     RRectT(RectT<T> rect, float rx, float ry)
154     {
155         rect_ = rect;
156         Vector2f vec = Vector2f(rx, ry);
157         radius_[0] = vec;
158         radius_[1] = vec;
159         radius_[2] = vec;
160         radius_[3] = vec;
161     }
RRectT(RectT<T> rect,const Vector2f * radius)162     RRectT(RectT<T> rect, const Vector2f* radius)
163     {
164         rect_ = rect;
165         radius_[0] = radius[0];
166         radius_[1] = radius[1];
167         radius_[2] = radius[2];
168         radius_[3] = radius[3];
169     }
170 };
171 typedef RRectT<float> RRect;
172 } // namespace Rosen
173 } // namespace OHOS
174 #endif
175