• 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 #ifndef RENDER_SERVICE_CLIENT_CORE_COMMON_RS_OBJ_GEOMETRY_H
16 #define RENDER_SERVICE_CLIENT_CORE_COMMON_RS_OBJ_GEOMETRY_H
17 
18 #include <cmath>
19 
20 #include "common/rs_common_def.h"
21 #include "common/rs_vector4.h"
22 namespace OHOS {
23 namespace Rosen {
24 class Transform {
25 public:
Transform()26     Transform()
27         : pivotX_(0.5f), pivotY_(0.5f), scaleX_(1.f), scaleY_(1.f), rotation_(0.f), rotationX_(0.f), rotationY_(0.f),
28           translateX_(0), translateY_(0), translateZ_(0)
29     {}
~Transform()30     ~Transform() {}
31     float pivotX_;
32     float pivotY_;
33     float scaleX_;
34     float scaleY_;
35     float rotation_;
36     float rotationX_;
37     float rotationY_;
38     float translateX_;
39     float translateY_;
40     float translateZ_;
41     Quaternion quaternion_;
42 };
43 
44 class RSObjGeometry {
45 public:
RSObjGeometry()46     RSObjGeometry()
47         : x_(-INFINITY), y_(-INFINITY), z_(0), width_(-INFINITY), height_(-INFINITY), trans_(nullptr)
48     {}
49 
~RSObjGeometry()50     virtual ~RSObjGeometry() {}
51 
SetX(float x)52     void SetX(float x)
53     {
54         if (!ROSEN_EQ(x_, x)) {
55             x_ = x;
56         }
57     }
SetY(float y)58     void SetY(float y)
59     {
60         if (!ROSEN_EQ(y_, y)) {
61             y_ = y;
62         }
63     }
SetZ(float z)64     void SetZ(float z)
65     {
66         if (!ROSEN_EQ(z_, z)) {
67             z_ = z;
68         }
69     }
SetWidth(float w)70     void SetWidth(float w)
71     {
72         if (!ROSEN_EQ(width_, w)) {
73             width_ = w;
74         }
75     }
SetHeight(float h)76     void SetHeight(float h)
77     {
78         if (!ROSEN_EQ(height_, h)) {
79             height_ = h;
80         }
81     }
SetPosition(float x,float y)82     void SetPosition(float x, float y)
83     {
84         SetX(x);
85         SetY(y);
86     }
SetSize(float w,float h)87     void SetSize(float w, float h)
88     {
89         SetWidth(w);
90         SetHeight(h);
91     }
SetRect(float x,float y,float w,float h)92     void SetRect(float x, float y, float w, float h)
93     {
94         SetPosition(x, y);
95         SetSize(w, h);
96     }
SetPivotX(float x)97     void SetPivotX(float x)
98     {
99         if (trans_ == nullptr) {
100             trans_ = std::make_unique<Transform>();
101         }
102         if (!ROSEN_EQ(trans_->pivotX_, x)) {
103             trans_->pivotX_ = x;
104         }
105     }
SetPivotY(float y)106     void SetPivotY(float y)
107     {
108         if (trans_ == nullptr) {
109             trans_ = std::make_unique<Transform>();
110         }
111         if (!ROSEN_EQ(trans_->pivotY_, y)) {
112             trans_->pivotY_ = y;
113         }
114     }
SetPivot(float x,float y)115     void SetPivot(float x, float y)
116     {
117         SetPivotX(x);
118         SetPivotY(y);
119     }
SetScaleX(float x)120     void SetScaleX(float x)
121     {
122         if (trans_ == nullptr) {
123             trans_ = std::make_unique<Transform>();
124         }
125         if (!ROSEN_EQ(trans_->scaleX_, x)) {
126             trans_->scaleX_ = x;
127         }
128     }
SetScaleY(float y)129     void SetScaleY(float y)
130     {
131         if (trans_ == nullptr) {
132             trans_ = std::make_unique<Transform>();
133         }
134         if (!ROSEN_EQ(trans_->scaleY_, y)) {
135             trans_->scaleY_ = y;
136         }
137     }
SetScale(float x,float y)138     void SetScale(float x, float y)
139     {
140         SetScaleX(x);
141         SetScaleY(y);
142     }
SetRotation(float rotation)143     void SetRotation(float rotation)
144     {
145         if (trans_ == nullptr) {
146             trans_ = std::make_unique<Transform>();
147         }
148         if (!ROSEN_EQ(trans_->rotation_, rotation)) {
149             trans_->rotation_ = rotation;
150         }
151     }
SetRotationX(float rotationX)152     void SetRotationX(float rotationX)
153     {
154         if (trans_ == nullptr) {
155             trans_ = std::make_unique<Transform>();
156         }
157         if (!ROSEN_EQ(trans_->rotationX_, rotationX)) {
158             trans_->rotationX_ = rotationX;
159         }
160     }
SetRotationY(float rotationY)161     void SetRotationY(float rotationY)
162     {
163         if (trans_ == nullptr) {
164             trans_ = std::make_unique<Transform>();
165         }
166         if (!ROSEN_EQ(trans_->rotationY_, rotationY)) {
167             trans_->rotationY_ = rotationY;
168         }
169     }
SetTranslateX(float translateX)170     void SetTranslateX(float translateX)
171     {
172         if (trans_ == nullptr) {
173             trans_ = std::make_unique<Transform>();
174         }
175         if (!ROSEN_EQ(trans_->translateX_, translateX)) {
176             trans_->translateX_ = translateX;
177         }
178     }
SetTranslateY(float translateY)179     void SetTranslateY(float translateY)
180     {
181         if (trans_ == nullptr) {
182             trans_ = std::make_unique<Transform>();
183         }
184         if (!ROSEN_EQ(trans_->translateY_, translateY)) {
185             trans_->translateY_ = translateY;
186         }
187     }
SetTranslateZ(float translateZ)188     void SetTranslateZ(float translateZ)
189     {
190         if (trans_ == nullptr) {
191             trans_ = std::make_unique<Transform>();
192         }
193         if (!ROSEN_EQ(trans_->translateZ_, translateZ)) {
194             trans_->translateZ_ = translateZ;
195         }
196     }
SetQuaternion(const Quaternion & quaternion)197     void SetQuaternion(const Quaternion& quaternion)
198     {
199         if (trans_ == nullptr) {
200             trans_ = std::make_unique<Transform>();
201         }
202         if (trans_->quaternion_ != quaternion) {
203             trans_->quaternion_ = quaternion;
204         }
205     }
206 
GetX()207     float GetX() const
208     {
209         return x_;
210     }
GetWidth()211     float GetWidth() const
212     {
213         return width_;
214     }
GetY()215     float GetY() const
216     {
217         return y_;
218     }
GetHeight()219     float GetHeight() const
220     {
221         return height_;
222     }
GetZ()223     float GetZ() const
224     {
225         return z_;
226     }
GetPivotX()227     float GetPivotX() const
228     {
229         return trans_ ? trans_->pivotX_ : 0.5f;
230     }
GetPivotY()231     float GetPivotY() const
232     {
233         return trans_ ? trans_->pivotY_ : 0.5f;
234     }
GetScaleX()235     float GetScaleX() const
236     {
237         return trans_ ? trans_->scaleX_ : 1.f;
238     }
GetScaleY()239     float GetScaleY() const
240     {
241         return trans_ ? trans_->scaleY_ : 1.f;
242     }
GetRotation()243     float GetRotation() const
244     {
245         return trans_ ? trans_->rotation_ : 0.f;
246     }
GetRotationX()247     float GetRotationX() const
248     {
249         return trans_ ? trans_->rotationX_ : 0.f;
250     }
GetRotationY()251     float GetRotationY() const
252     {
253         return trans_ ? trans_->rotationY_ : 0.f;
254     }
GetTranslateX()255     float GetTranslateX() const
256     {
257         return trans_ ? trans_->translateX_ : 0.f;
258     }
GetTranslateY()259     float GetTranslateY() const
260     {
261         return trans_ ? trans_->translateY_ : 0.f;
262     }
GetTranslateZ()263     float GetTranslateZ() const
264     {
265         return trans_ ? trans_->translateZ_ : 0.f;
266     }
GetQuaternion()267     Quaternion GetQuaternion() const
268     {
269         return trans_ ? trans_->quaternion_ : Quaternion();
270     }
IsEmpty()271     bool IsEmpty() const
272     {
273         return width_ <= 0 && height_ <= 0;
274     }
275     RSObjGeometry& operator=(const RSObjGeometry& geo)
276     {
277         if (&geo != this) {
278             SetRect(geo.x_, geo.y_, geo.width_, geo.height_);
279             SetZ(geo.z_);
280             if (geo.trans_) {
281                 SetPivot(geo.trans_->pivotX_, geo.trans_->pivotY_);
282                 SetScale(geo.trans_->scaleX_, geo.trans_->scaleY_);
283                 SetRotation(geo.trans_->rotation_);
284                 SetRotationX(geo.trans_->rotationX_);
285                 SetRotationY(geo.trans_->rotationY_);
286                 SetTranslateX(geo.trans_->translateX_);
287                 SetTranslateY(geo.trans_->translateY_);
288                 SetTranslateZ(geo.trans_->translateZ_);
289                 SetQuaternion(geo.trans_->quaternion_);
290             } else if (trans_) {
291                 trans_ = nullptr;
292             }
293         }
294         return *this;
295     }
296 
297 protected:
298     float x_;
299     float y_;
300     float z_;
301     float width_;
302     float height_;
303     std::unique_ptr<Transform> trans_;
304 };
305 } // namespace Rosen
306 } // namespace OHOS
307 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_OBJ_GEOMETRY_H
308