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