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