1 /*
2 * Copyright (c) 2020-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
16 #include "gfx_utils/transform.h"
17
18 #include "gfx_utils/graphic_math.h"
19 namespace OHOS {
20 constexpr uint8_t VERTEX_NUM_MIN = 3;
21
TransformMap()22 TransformMap::TransformMap()
23 {
24 scale_ = Matrix4<float>::Scale(Vector3<float>(1.0f, 1.0f, 1.0f), Vector3<float>(0, 0, 0));
25 rotate_ = Matrix4<float>::Rotate(0, Vector3<float>(0, 0, 0), Vector3<float>(0, 0, 0));
26 shear_ = Matrix4<float>::Shear(Vector2<float>(0, 0), Vector2<float>(0, 0), Vector2<float>(0, 0));
27 translate_ = Matrix4<float>::Translate(Vector3<float>(0, 0, 0));
28 trans_[ROTATE] = &rotate_;
29 trans_[SCALE] = &scale_;
30 trans_[SHEAR] = &shear_;
31 trans_[TRANSLATE] = &translate_;
32 opOrder_[ROTATE] = ROTATE;
33 opOrder_[SCALE] = SCALE;
34 opOrder_[SHEAR] = SHEAR;
35 opOrder_[TRANSLATE] = TRANSLATE;
36
37 UpdateMap();
38 }
39
TransformMap(const Rect & rect)40 TransformMap::TransformMap(const Rect& rect)
41 {
42 rect_ = rect;
43 polygon_ = rect;
44 scale_ = Matrix4<float>::Scale(Vector3<float>(1.0f, 1.0f, 1.0f), Vector3<float>(0, 0, 0));
45 rotate_ = Matrix4<float>::Rotate(0, Vector3<float>(0, 0, 0), Vector3<float>(0, 0, 0));
46 shear_ = Matrix4<float>::Shear(Vector2<float>(0, 0), Vector2<float>(0, 0), Vector2<float>(0, 0));
47 translate_ = Matrix4<float>::Translate(Vector3<float>(0, 0, 0));
48 trans_[ROTATE] = &rotate_;
49 trans_[SCALE] = &scale_;
50 trans_[SHEAR] = &shear_;
51 trans_[TRANSLATE] = &translate_;
52 opOrder_[ROTATE] = ROTATE;
53 opOrder_[SCALE] = SCALE;
54 opOrder_[SHEAR] = SHEAR;
55 opOrder_[TRANSLATE] = TRANSLATE;
56
57 UpdateMap();
58 }
59
GetClockWise() const60 bool TransformMap::GetClockWise() const
61 {
62 int16_t count = 0;
63
64 uint8_t vertexNum = polygon_.GetVertexNum();
65 if (vertexNum < VERTEX_NUM_MIN) {
66 return false;
67 }
68
69 for (uint8_t i = 0; i < vertexNum; i++) {
70 uint8_t j = (i + 1) % vertexNum; // 1: the next vertex
71 uint8_t k = (i + 2) % vertexNum; // 2: the after next vertex
72 int32_t c = (static_cast<int32_t>(polygon_[j].x_ - polygon_[i].x_) * (polygon_[k].y_ - polygon_[j].y_)) -
73 (static_cast<int32_t>(polygon_[j].y_ - polygon_[i].y_) * (polygon_[k].x_ - polygon_[j].x_));
74 if (c < 0) {
75 count--;
76 } else if (c > 0) {
77 count++;
78 }
79 }
80 if (count > 0) {
81 return true;
82 }
83 return false;
84 }
85
SetTransMapRect(const Rect & rect)86 void TransformMap::SetTransMapRect(const Rect& rect)
87 {
88 rect_ = rect;
89 polygon_ = rect;
90 if (isInternalMatrix_) {
91 UpdateMap();
92 } else {
93 SetMatrix(matrixOrig_);
94 }
95 }
96
Scale(const Vector2<float> & scale,const Vector2<float> & pivot)97 void TransformMap::Scale(const Vector2<float>& scale, const Vector2<float>& pivot)
98 {
99 Scale(Vector3<float>(scale.x_, scale.y_, 1.0f), Vector3<float>(pivot.x_, pivot.y_, 0));
100 }
101
Scale(const Vector3<float> & scale,const Vector3<float> & pivot)102 void TransformMap::Scale(const Vector3<float>& scale, const Vector3<float>& pivot)
103 {
104 scaleCoeff_ = scale;
105 scalePivot_ = pivot;
106 AddOp(SCALE);
107 UpdateMap();
108 }
109
IsInvalid() const110 bool TransformMap::IsInvalid() const
111 {
112 if (isInvalid_ || isIdentity_) {
113 return true;
114 }
115
116 for (uint8_t i = 0; i < polygon_.GetVertexNum(); i++) {
117 if (polygon_[i].x_ != 0 || polygon_[i].y_ != 0) {
118 return false;
119 }
120 }
121 return true;
122 }
123
Rotate(int16_t angle,const Vector2<float> & pivot)124 void TransformMap::Rotate(int16_t angle, const Vector2<float>& pivot)
125 {
126 Rotate(angle, Vector3<float>(pivot.x_, pivot.y_, 0), Vector3<float>(pivot.x_, pivot.y_, 1.0f));
127 }
128
Rotate(int16_t angle,const Vector3<float> & rotatePivotStart,const Vector3<float> & rotatePivotEnd)129 void TransformMap::Rotate(int16_t angle, const Vector3<float>& rotatePivotStart, const Vector3<float>& rotatePivotEnd)
130 {
131 angle_ = angle;
132 rotatePivotStart_ = rotatePivotStart;
133 rotatePivotEnd_ = rotatePivotEnd;
134 AddOp(ROTATE);
135 UpdateMap();
136 }
137
Translate(const Vector2<int16_t> & trans)138 void TransformMap::Translate(const Vector2<int16_t>& trans)
139 {
140 Translate(Vector3<int16_t>(trans.x_, trans.y_, 0));
141 }
142
Translate(const Vector3<int16_t> & trans)143 void TransformMap::Translate(const Vector3<int16_t>& trans)
144 {
145 translate_ = Matrix4<float>::Translate(Vector3<float>(trans.x_, trans.y_, trans.z_));
146 AddOp(TRANSLATE);
147 UpdateMap();
148 }
149
Shear(const Vector2<float> & shearX,const Vector2<float> & shearY,const Vector2<float> & shearZ)150 void TransformMap::Shear(const Vector2<float>& shearX, const Vector2<float>& shearY, const Vector2<float>& shearZ)
151 {
152 shearX_ = shearX;
153 shearY_ = shearY;
154 shearZ_ = shearZ;
155 AddOp(SHEAR);
156 UpdateMap();
157 }
158
operator ==(const TransformMap & other) const159 bool TransformMap::operator==(const TransformMap& other) const
160 {
161 if (rotate_ == other.rotate_ && translate_ == other.translate_ && scale_ == other.scale_ &&
162 rect_ == other.rect_ && matrix_ == other.matrix_) {
163 return true;
164 }
165 return false;
166 }
167
SetCameraDistance(int16_t distance)168 void TransformMap::SetCameraDistance(int16_t distance)
169 {
170 cameraDistance_ = distance;
171 UpdateMap();
172 }
173
SetCameraPosition(const Vector2<float> & position)174 void TransformMap::SetCameraPosition(const Vector2<float>& position)
175 {
176 cameraPosition_ = position;
177 UpdateMap();
178 }
179
Is3DTransform() const180 bool TransformMap::Is3DTransform() const
181 {
182 return is3d_;
183 }
184
UpdateMap()185 void TransformMap::UpdateMap()
186 {
187 trans_[ROTATE] = &rotate_;
188 trans_[SCALE] = &scale_;
189 trans_[SHEAR] = &shear_;
190 trans_[TRANSLATE] = &translate_;
191 rotate_ =
192 Matrix4<float>::Rotate(angle_,
193 Vector3<float>(rotatePivotStart_.x_ + rect_.GetX(), rotatePivotStart_.y_ + rect_.GetY(),
194 rotatePivotStart_.z_),
195 Vector3<float>(rotatePivotEnd_.x_ + rect_.GetX(), rotatePivotEnd_.y_ + rect_.GetY(), rotatePivotEnd_.z_));
196
197 scale_ = Matrix4<float>::Scale(scaleCoeff_,
198 Vector3<float>(scalePivot_.x_ + rect_.GetX(), scalePivot_.y_ + rect_.GetY(), scalePivot_.z_));
199
200 shear_ = Matrix4<float>::Shear(shearX_, shearY_, shearZ_);
201 shear_ = shear_ * Matrix4<float>::Translate(Vector3<float>(-rect_.GetX(), -rect_.GetY(), 0));
202 shear_ = Matrix4<float>::Translate(Vector3<float>(rect_.GetX(), rect_.GetY(), 0)) * shear_;
203
204 matrix_ = (*trans_[opOrder_[TRANSLATE]]) * (*trans_[opOrder_[SHEAR]]) * (*trans_[opOrder_[SCALE]]) *
205 (*trans_[opOrder_[ROTATE]]);
206
207 float x = rect_.GetX() + cameraPosition_.x_;
208 float y = rect_.GetY() + cameraPosition_.y_;
209 float z = 0;
210 Matrix4<float> translateFromCamera = Matrix4<float>::Translate(Vector3<float>(-x, -y, -z));
211 Matrix4<float> translateToCamera = Matrix4<float>::Translate(Vector3<float>(x, y, z));
212 Matrix4<float> perspectiveMatrix;
213 perspectiveMatrix[2][2] = 0; // 2 : index
214 if (!FloatEqual(cameraDistance_, 0)) {
215 perspectiveMatrix[2][3] = -1.0f / cameraDistance_; // 2 3 : index
216 }
217 perspectiveMatrix_ = translateToCamera * (perspectiveMatrix * translateFromCamera);
218 matrix_ = perspectiveMatrix_ * matrix_;
219 SetMatrix(matrix_, true);
220 }
221
SetMatrix(const Matrix4<float> & matrix,bool isInternalMatrix)222 void TransformMap::SetMatrix(const Matrix4<float>& matrix, bool isInternalMatrix)
223 {
224 isInternalMatrix_ = isInternalMatrix;
225 polygon_ = rect_;
226 matrixOrig_ = matrix;
227 uint8_t vertexNum = polygon_.GetVertexNum();
228 Vector4<float> imgPoint4;
229 is3d_ = false;
230 for (uint8_t i = 0; i < vertexNum; i++) {
231 Vector4<float> point(polygon_[i].x_, polygon_[i].y_, 0, 1.0f);
232 imgPoint4 = matrix * point;
233 if (!FloatEqual(imgPoint4.w_, 1)) {
234 is3d_ = true;
235 }
236 if (!FloatEqual(imgPoint4.w_, 0)) {
237 imgPoint4.x_ /= imgPoint4.w_;
238 imgPoint4.y_ /= imgPoint4.w_;
239 }
240 if (imgPoint4.x_ < COORD_MIN) {
241 polygon_[i].x_ = COORD_MIN;
242 } else if (imgPoint4.x_ > COORD_MAX) {
243 polygon_[i].x_ = COORD_MAX;
244 } else {
245 polygon_[i].x_ = MATH_ROUND(imgPoint4.x_);
246 }
247
248 if (imgPoint4.y_ < COORD_MIN) {
249 polygon_[i].y_ = COORD_MIN;
250 } else if (imgPoint4.y_ > COORD_MAX) {
251 polygon_[i].y_ = COORD_MAX;
252 } else {
253 polygon_[i].y_ = MATH_ROUND(imgPoint4.y_);
254 }
255 }
256 isIdentity_ = IsIdentity(const_cast<Matrix4<float>&>(matrix));
257 Matrix4<float> translate = Matrix4<float>::Translate(Vector3<float>(rect_.GetX(), rect_.GetY(), 0));
258 matrix_ = matrix * translate;
259 /* 0 1 2 3 : index of matrix */
260 Matrix3<float> matrix3(matrix_[0][0], matrix_[0][1], matrix_[0][3],
261 matrix_[1][0], matrix_[1][1], matrix_[1][3],
262 matrix_[3][0], matrix_[3][1], matrix_[3][3]);
263 invMatrix_ = matrix3.Inverse();
264 }
265
AddOp(uint8_t op)266 void TransformMap::AddOp(uint8_t op)
267 {
268 uint8_t index = 0;
269 for (; index < TRANS_NUM; index++) {
270 if (opOrder_[index] == op) {
271 break;
272 }
273 }
274 for (; index < TRANSLATE; index++) {
275 opOrder_[index] = opOrder_[index + 1];
276 }
277 opOrder_[TRANSLATE] = op;
278 }
279
Rotate(const Vector2<int16_t> & point,int16_t angle,const Vector2<int16_t> & pivot,Vector2<int16_t> & out)280 void Rotate(const Vector2<int16_t>& point, int16_t angle, const Vector2<int16_t>& pivot, Vector2<int16_t>& out)
281 {
282 float sinma = Sin(angle);
283 float cosma = Sin(angle + 90); // 90: cos
284
285 int16_t xt = point.x_ - pivot.x_;
286 int16_t yt = point.y_ - pivot.y_;
287
288 /* 0.5: round up */
289 float temp = cosma * xt - sinma * yt;
290 out.x_ = static_cast<int16_t>((temp > 0) ? (temp + 0.5f) : (temp - 0.5f)) + pivot.x_;
291 temp = sinma * xt + cosma * yt;
292 out.y_ = static_cast<int16_t>((temp > 0) ? (temp + 0.5f) : (temp - 0.5f)) + pivot.y_;
293 }
294
Rotate(const Line & origLine,int16_t angle,const Vector2<int16_t> & pivot,Line & out)295 void Rotate(const Line& origLine, int16_t angle, const Vector2<int16_t>& pivot, Line& out)
296 {
297 Vector2<int16_t> pt1 = origLine[0];
298 Vector2<int16_t> pt2 = origLine[1];
299
300 Rotate(pt1, angle, pivot, out[1]); // 1: the first point of line
301 Rotate(pt2, angle, pivot, out[2]); // 2: the second point of line
302 }
303
Rotate(const Rect & origRect,int16_t angle,const Vector2<int16_t> & pivot,Polygon & out)304 void Rotate(const Rect& origRect, int16_t angle, const Vector2<int16_t>& pivot, Polygon& out)
305 {
306 Vector2<int16_t> pt1 = {origRect.GetLeft(), origRect.GetTop()};
307 Vector2<int16_t> pt2 = {origRect.GetRight(), origRect.GetTop()};
308 Vector2<int16_t> pt3 = {origRect.GetRight(), origRect.GetBottom()};
309 Vector2<int16_t> pt4 = {origRect.GetLeft(), origRect.GetBottom()};
310
311 Rotate(pt1, angle, pivot, out[1]); // 1: the first point
312 Rotate(pt2, angle, pivot, out[2]); // 2: the second point
313 Rotate(pt3, angle, pivot, out[3]); // 3: the third point
314 Rotate(pt4, angle, pivot, out[4]); // 4: the fourth point
315
316 out.SetVertexNum(4); // 4: number of vertex
317 }
318 } // namespace OHOS
319