1 /* 2 * Copyright (c) 2020-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 /** 17 * @addtogroup UI_Utils 18 * @{ 19 * 20 * @brief Defines basic UI utils. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file transform.h 28 * 29 * @brief Provides functions to transform components, points, and line segments, including rotation and scaling. 30 * 31 * @since 1.0 32 * @version 1.0 33 */ 34 35 #ifndef GRAPHIC_LITE_TRANSFORM_H 36 #define GRAPHIC_LITE_TRANSFORM_H 37 38 #include "gfx_utils/geometry2d.h" 39 #include "gfx_utils/graphic_math.h" 40 namespace OHOS { 41 /** 42 * @brief Transforms a rectangle, including rotation and scaling. 43 * @since 1.0 44 * @version 1.0 45 */ 46 class TransformMap : public HeapBase { 47 public: 48 /** 49 * @brief The default constructor used to create a <b>TransformMap</b> instance. 50 * @since 1.0 51 * @version 1.0 52 */ 53 TransformMap(); 54 55 /** 56 * @brief A constructor used to create a <b>TransformMap</b> instance. 57 * 58 * @param rect Indicates the rectangle to transform. 59 * @since 1.0 60 * @version 1.0 61 */ 62 explicit TransformMap(const Rect& rect); 63 64 /** 65 * @brief A destructor used to delete the <b>TransformMap</b> instance. 66 * @since 1.0 67 * @version 1.0 68 */ ~TransformMap()69 virtual ~TransformMap() {} 70 71 /** 72 * @brief Checks whether the vertex coordinates of a polygon are clockwise. 73 * 74 * @return Returns <b>true</b> if the vertex coordinates are clockwise; returns <b>false</b> otherwise. 75 * @since 1.0 76 * @version 1.0 77 */ 78 bool GetClockWise() const; 79 80 /** 81 * @brief Sets a polygon after rectangle transformation. 82 * @param polygon Indicates the polygon to set. 83 * @since 1.0 84 * @version 1.0 85 */ SetPolygon(const Polygon & polygon)86 void SetPolygon(const Polygon& polygon) 87 { 88 polygon_ = polygon; 89 } 90 91 /** 92 * @brief Obtains the polygon after rectangle transformation. 93 * @return Returns the polygon. 94 * @since 1.0 95 * @version 1.0 96 */ GetPolygon()97 Polygon GetPolygon() const 98 { 99 return polygon_; 100 } 101 102 /** 103 * @brief Obtains the pivot for the rotation or scaling operation. 104 * @return Returns the pivot. 105 * @since 1.0 106 * @version 1.0 107 */ GetPivot()108 const Vector3<float>& GetPivot() const 109 { 110 return scalePivot_; 111 } 112 113 void SetTransMapRect(const Rect& rect); 114 GetTransMapRect()115 const Rect& GetTransMapRect() const 116 { 117 return rect_; 118 } 119 SetInvalid(bool state)120 void SetInvalid(bool state) 121 { 122 isInvalid_ = state; 123 } 124 125 /** 126 * @brief Checks whether the <b>TransformMap</b> instance is invalid. When the vertices are all 0, the 127 * <b>TransformMap</b> is invalid. 128 * @return Returns <b>true</b> if <b>TransformMap</b> is invalid; returns <b>false</b> otherwise. 129 * @since 1.0 130 * @version 1.0 131 */ 132 bool IsInvalid() const; 133 134 /** 135 * @brief Obtains the minimum rectangle that can contain a polygon. All vertices of the polygon are inside this 136 * rectangle. 137 * @return Returns the minimum rectangle that can contain the polygon. 138 * @since 1.0 139 * @version 1.0 140 */ GetBoxRect()141 Rect GetBoxRect() const 142 { 143 return polygon_.MakeAABB(); 144 } 145 146 /** 147 * @brief Obtains a three-dimensional homogeneous transformation matrix. 148 * @return Returns the three-dimensional homogeneous transformation matrix. 149 * @since 1.0 150 * @version 1.0 151 */ GetTransformMatrix()152 const Matrix4<float>& GetTransformMatrix() const 153 { 154 return matrix_; 155 } 156 GetOrigTransformMatrix()157 const Matrix4<float>& GetOrigTransformMatrix() const 158 { 159 return matrixOrig_; 160 } 161 GetRotateMatrix()162 const Matrix4<float>& GetRotateMatrix() const 163 { 164 return rotate_; 165 } 166 GetScaleMatrix()167 const Matrix4<float>& GetScaleMatrix() const 168 { 169 return scale_; 170 } 171 GetTranslateMatrix()172 const Matrix4<float>& GetTranslateMatrix() const 173 { 174 return translate_; 175 } 176 GetShearMatrix()177 const Matrix4<float>& GetShearMatrix() const 178 { 179 return shear_; 180 } 181 GetPerspectiveMatrix()182 const Matrix4<float>& GetPerspectiveMatrix() const 183 { 184 return perspectiveMatrix_; 185 } 186 GetRotateAngle()187 int16_t GetRotateAngle() const 188 { 189 return angle_; 190 } 191 192 /** 193 * @brief Rotates the rectangle. 194 * @param angle Indicates the angle to rotate. 195 * @param pivot Indicates the rotation pivot. 196 * @since 1.0 197 * @version 1.0 198 */ 199 void Rotate(int16_t angle, const Vector2<float>& pivot); 200 201 void Rotate(int16_t angle, const Vector3<float>& rotatePivotStart, const Vector3<float>& rotatePivotEnd); 202 203 /** 204 * @brief Scales the rectangle. 205 * 206 * @param scale Indicates the scaling factors of the x-axis and y-axis. 207 * @param pivot Indicates the pivot for scaling. 208 * @since 1.0 209 * @version 1.0 210 */ 211 void Scale(const Vector2<float>& scale, const Vector2<float>& pivot); 212 213 void Scale(const Vector3<float>& scale, const Vector3<float>& pivot); 214 215 void Translate(const Vector2<int16_t>& trans); 216 217 void Translate(const Vector3<int16_t>& trans); 218 219 void Shear(const Vector2<float>& shearX, const Vector2<float>& shearY, const Vector2<float>& shearZ); 220 221 bool operator==(const TransformMap& other) const; 222 223 void SetMatrix(const Matrix4<float>& matrix, bool isInternalMatrix = false); 224 225 void SetCameraDistance(int16_t distance); 226 227 void SetCameraPosition(const Vector2<float>& position); 228 229 Matrix3<float> invMatrix_; 230 231 bool Is3DTransform() const; 232 233 private: 234 void UpdateMap(); 235 void AddOp(uint8_t op); 236 237 enum : uint8_t { 238 ROTATE = 0, 239 SCALE, 240 SHEAR, 241 TRANSLATE, 242 TRANS_NUM, 243 }; 244 Rect rect_ = {0, 0, 0, 0}; /* orig rect */ 245 Polygon polygon_ = Polygon(Rect(0, 0, 0, 0)); /* transformed from rect and 'rotate_' 'translate_' 'scale_' */ 246 int16_t angle_ = 0; 247 int16_t cameraDistance_ = 1000; // 1000 : default distance 248 bool isInvalid_ = false; 249 bool isIdentity_ = false; 250 bool is3d_ = false; 251 bool isInternalMatrix_ = true; 252 Vector2<float> cameraPosition_ = {0, 0}; 253 Vector3<float> scaleCoeff_ = {1.0f, 1.0f, 1.0f}; 254 Vector3<float> scalePivot_ = {0, 0, 0}; 255 Vector3<float> rotatePivotStart_ = {0, 0, 0}; 256 Vector3<float> rotatePivotEnd_ = {0, 0, 0}; 257 Vector2<float> shearX_ = {0, 0}; 258 Vector2<float> shearY_ = {0, 0}; 259 Vector2<float> shearZ_ = {0, 0}; 260 261 Matrix4<float> scale_; 262 Matrix4<float> rotate_; 263 Matrix4<float> shear_; 264 Matrix4<float> translate_; 265 Matrix4<float>* trans_[TRANS_NUM]; 266 uint8_t opOrder_[TRANS_NUM]; 267 Matrix4<float> matrix_; 268 Matrix4<float> perspectiveMatrix_; 269 Matrix4<float> matrixOrig_; 270 }; 271 272 /** 273 * @brief Rotates a point around the pivot by a certain angle. 274 * @param point Indicates the point to rotate. 275 * @param angle Indicates the angle to rotate. 276 * @param pivot Indicates the rotation pivot. 277 * @param out Indicates the point generated after rotation. 278 * @since 1.0 279 * @version 1.0 280 */ 281 void Rotate(const Vector2<int16_t>& point, int16_t angle, const Vector2<int16_t>& pivot, Vector2<int16_t>& out); 282 283 /** 284 * @brief Rotates a line around the pivot by a certain angle. 285 * @param origLine Indicates the line segment to rotate. 286 * @param angle Indicates the angle to rotate. 287 * @param pivot Indicates the rotation pivot. 288 * @param out Indicates the line generated after rotation. 289 * @since 1.0 290 * @version 1.0 291 */ 292 void Rotate(const Line& origLine, int16_t angle, const Vector2<int16_t>& pivot, Line& out); 293 294 /** 295 * @brief Rotates a rectangle around the pivot by a certain angle. 296 * @param origRect Indicates the rectangle to rotate. 297 * @param angle Indicates the angle to rotate. 298 * @param pivot Indicates the rotation pivot. 299 * @param out Indicates the polygon generated after the rectangle is rotated. 300 * @since 1.0 301 * @version 1.0 302 */ 303 void Rotate(const Rect& origRect, int16_t angle, const Vector2<int16_t>& pivot, Polygon& out); 304 } // namespace OHOS 305 #endif // GRAPHIC_LITE_TRANSFORM_H 306