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