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 Point GetOrigPoint(const Point& point, const Rect& relativeRect); 193 194 /** 195 * @brief Rotates the rectangle. 196 * @param angle Indicates the angle to rotate. 197 * @param pivot Indicates the rotation pivot. 198 * @since 1.0 199 * @version 1.0 200 */ 201 void Rotate(int16_t angle, const Vector2<float>& pivot); 202 203 void Rotate(int16_t angle, const Vector3<float>& rotatePivotStart, const Vector3<float>& rotatePivotEnd); 204 205 /** 206 * @brief Scales the rectangle. 207 * 208 * @param scale Indicates the scaling factors of the x-axis and y-axis. 209 * @param pivot Indicates the pivot for scaling. 210 * @since 1.0 211 * @version 1.0 212 */ 213 void Scale(const Vector2<float>& scale, const Vector2<float>& pivot); 214 215 void Scale(const Vector3<float>& scale, const Vector3<float>& pivot); 216 217 void Translate(const Vector2<int16_t>& trans); 218 219 void Translate(const Vector3<int16_t>& trans); 220 221 void Translate(const Vector2<float>& trans); 222 223 void Translate(const Vector3<float>& trans); 224 225 void Shear(const Vector2<float>& shearX, const Vector2<float>& shearY, const Vector2<float>& shearZ); 226 227 bool operator==(const TransformMap& other) const; 228 229 void SetMatrix(const Matrix4<float>& matrix, bool isInternalMatrix = false); 230 231 void SetCameraDistance(int16_t distance); 232 233 void SetCameraPosition(const Vector2<float>& position); 234 235 Matrix3<float> invMatrix_; 236 237 bool Is3DTransform() const; 238 239 private: 240 void UpdateMap(); 241 void AddOp(uint8_t op); 242 243 enum : uint8_t { 244 ROTATE = 0, 245 SCALE, 246 SHEAR, 247 TRANSLATE, 248 TRANS_NUM, 249 }; 250 Rect rect_ = {0, 0, 0, 0}; /* orig rect */ 251 Polygon polygon_ = Polygon(Rect(0, 0, 0, 0)); /* transformed from rect and 'rotate_' 'translate_' 'scale_' */ 252 int16_t angle_ = 0; 253 int16_t cameraDistance_ = 1000; // 1000 : default distance 254 bool isInvalid_ = false; 255 bool isIdentity_ = false; 256 bool is3d_ = false; 257 bool isInternalMatrix_ = true; 258 Vector2<float> cameraPosition_ = {0, 0}; 259 Vector3<float> scaleCoeff_ = {1.0f, 1.0f, 1.0f}; 260 Vector3<float> scalePivot_ = {0, 0, 0}; 261 Vector3<float> rotatePivotStart_ = {0, 0, 0}; 262 Vector3<float> rotatePivotEnd_ = {0, 0, 0}; 263 Vector2<float> shearX_ = {0, 0}; 264 Vector2<float> shearY_ = {0, 0}; 265 Vector2<float> shearZ_ = {0, 0}; 266 267 Matrix4<float> scale_; 268 Matrix4<float> rotate_; 269 Matrix4<float> shear_; 270 Matrix4<float> translate_; 271 Matrix4<float>* trans_[TRANS_NUM]; 272 uint8_t opOrder_[TRANS_NUM]; 273 Matrix4<float> matrix_; 274 Matrix4<float> perspectiveMatrix_; 275 Matrix4<float> matrixOrig_; 276 }; 277 278 /** 279 * @brief Rotates a point around the pivot by a certain angle. 280 * @param point Indicates the point to rotate. 281 * @param angle Indicates the angle to rotate. 282 * @param pivot Indicates the rotation pivot. 283 * @param out Indicates the point generated after rotation. 284 * @since 1.0 285 * @version 1.0 286 */ 287 void Rotate(const Vector2<int16_t>& point, int16_t angle, const Vector2<int16_t>& pivot, Vector2<int16_t>& out); 288 289 /** 290 * @brief Rotates a line around the pivot by a certain angle. 291 * @param origLine Indicates the line segment to rotate. 292 * @param angle Indicates the angle to rotate. 293 * @param pivot Indicates the rotation pivot. 294 * @param out Indicates the line generated after rotation. 295 * @since 1.0 296 * @version 1.0 297 */ 298 void Rotate(const Line& origLine, int16_t angle, const Vector2<int16_t>& pivot, Line& out); 299 300 /** 301 * @brief Rotates a rectangle around the pivot by a certain angle. 302 * @param origRect Indicates the rectangle to rotate. 303 * @param angle Indicates the angle to rotate. 304 * @param pivot Indicates the rotation pivot. 305 * @param out Indicates the polygon generated after the rectangle is rotated. 306 * @since 1.0 307 * @version 1.0 308 */ 309 void Rotate(const Rect& origRect, int16_t angle, const Vector2<int16_t>& pivot, Polygon& out); 310 } // namespace OHOS 311 #endif // GRAPHIC_LITE_TRANSFORM_H 312