• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "matrix3.h"
17 #include "utils.h"
18 
19 namespace OHOS::uitest {
Invert(Matrix3 & matrix) const20 bool Matrix3::Invert(Matrix3& matrix) const
21 {
22     static const double diff = 1e-20;
23     double val1 = matrix3X3_[ZERO][ZERO] * matrix3X3_[ONE][ONE] * matrix3X3_[TWO][TWO];
24     double val2 = matrix3X3_[ZERO][ZERO] * matrix3X3_[ONE][TWO] * matrix3X3_[TWO][ONE];
25     double val3 = matrix3X3_[ONE][ZERO] * matrix3X3_[ZERO][ONE] * matrix3X3_[TWO][TWO];
26     double val4 = matrix3X3_[ONE][ZERO] * matrix3X3_[ZERO][TWO] * matrix3X3_[TWO][ONE];
27     double val5 = matrix3X3_[TWO][ZERO] * matrix3X3_[ZERO][ONE] * matrix3X3_[ONE][TWO];
28     double val6 = matrix3X3_[TWO][ZERO] * matrix3X3_[ZERO][TWO] * matrix3X3_[ONE][ONE];
29     double detA = val1 - val2 - val3 + val4 + val5 - val6;
30     if (NearZero(detA, diff)) {
31         return false;
32     }
33     detA = 1.0 / detA;
34     // a11a22 - a12a21
35     matrix[ZERO][ZERO] = matrix3X3_[ONE][ONE] * matrix3X3_[TWO][TWO] - \
36     matrix3X3_[ONE][TWO] * matrix3X3_[TWO][ONE];
37     // a20a21 - a01a22
38     matrix[ZERO][ONE] = matrix3X3_[ZERO][TWO] * matrix3X3_[TWO][ONE] - \
39     matrix3X3_[ZERO][ONE] * matrix3X3_[TWO][TWO];
40     // a01a12 - a02a11
41     matrix[ZERO][TWO] = matrix3X3_[ZERO][ONE] * matrix3X3_[ONE][TWO] - \
42     matrix3X3_[ZERO][TWO] * matrix3X3_[ONE][ONE];
43     // a12a20 - a10a22
44     matrix[ONE][ZERO] = matrix3X3_[ONE][TWO] * matrix3X3_[TWO][ZERO] - \
45     matrix3X3_[ONE][ZERO] * matrix3X3_[TWO][TWO];
46     // a00a22 - a02a20
47     matrix[ONE][ONE] = matrix3X3_[ZERO][ZERO] * matrix3X3_[TWO][TWO] - \
48     matrix3X3_[ZERO][TWO] * matrix3X3_[TWO][ZERO];
49     // a10a02 - a00a12
50     matrix[ONE][TWO] = matrix3X3_[ONE][ZERO] * matrix3X3_[ZERO][TWO] - \
51     matrix3X3_[ZERO][ZERO] * matrix3X3_[ONE][TWO];
52     // a10a21 - a11a20
53     matrix[TWO][ZERO] = matrix3X3_[ONE][ZERO] * matrix3X3_[TWO][ONE] - \
54     matrix3X3_[ONE][ONE] * matrix3X3_[TWO][ZERO];
55     // a01a20 - a00a21
56     matrix[TWO][ONE] = matrix3X3_[ZERO][ONE] * matrix3X3_[TWO][ZERO] - \
57     matrix3X3_[ZERO][ZERO] * matrix3X3_[TWO][ONE];
58     // a00a11 - a10a01
59     matrix[TWO][TWO] = matrix3X3_[ZERO][ZERO] * matrix3X3_[ONE][ONE] - \
60     matrix3X3_[ONE][ZERO] * matrix3X3_[ZERO][ONE];
61     // invert
62     matrix* detA;
63     return true;
64 }
65 
operator *(const Matrix3N & matrix) const66 Matrix3N Matrix3::operator*(const Matrix3N& matrix) const
67 {
68     int32_t columns = matrix.GetColNum();
69     Matrix3N Matrix3n { columns };
70     for (auto i = 0; i < DIMENSION; i++) {
71         for (auto j = 0; j < columns; j++) {
72             double value = 0.0;
73             for (auto k = 0; k < DIMENSION; k++) {
74                 value += matrix3X3_[i][k] * matrix[k][j];
75             }
76             Matrix3n[i][j] = value;
77         }
78     }
79     return Matrix3n;
80 }
81 
Transpose() const82 Matrix3 Matrix3::Transpose() const
83 {
84     Matrix3 matrix;
85     for (auto i = 0; i < DIMENSION; i++) {
86         for (auto j = 0; j < DIMENSION; j++) {
87             matrix[j][i] = matrix3X3_[i][j];
88         }
89     }
90     return matrix;
91 }
92 
ScaleMapping(const std::vector<double> & src) const93 std::vector<double> Matrix3::ScaleMapping(const std::vector<double>& src) const
94 {
95     std::vector<double> value { DIMENSION, 0 };
96     if (static_cast<int32_t>(src.size()) != DIMENSION) {
97         return value;
98     }
99     for (int32_t i = 0; i < DIMENSION; i++) {
100         double item = 0.0;
101         for (int32_t j = 0; j < DIMENSION; j++) {
102             item = item + matrix3X3_[i][j] * src[j];
103         }
104         value[i] = item;
105     }
106     return value;
107 }
108 
ScaleMapping(const std::vector<double> & src,std::vector<double> & result) const109 bool Matrix3::ScaleMapping(const std::vector<double>& src, std::vector<double>& result) const
110 {
111     if (static_cast<int32_t>(src.size()) != DIMENSION) {
112         return false;
113     }
114     result.resize(DIMENSION, 0);
115     for (int32_t i = 0; i < DIMENSION; i++) {
116         double item = 0.0;
117         for (int32_t j = 0; j < DIMENSION; j++) {
118             item = item + matrix3X3_[i][j] * src[j];
119         }
120         result[i] = item;
121     }
122     return true;
123 }
124 
Matrix3N(int32_t columns)125 Matrix3N::Matrix3N(int32_t columns) : columns_(columns)
126 {
127     Matrix3n_.resize(DIMENSION, std::vector<double>(columns_, 0));
128 }
129 
operator *(const MatrixN3 & matrix) const130 Matrix3 Matrix3N::operator*(const MatrixN3& matrix) const
131 {
132     Matrix3 Matrix3;
133     if (columns_ != matrix.GetRowNum()) {
134         return Matrix3;
135     }
136     for (auto i = 0; i < DIMENSION; i++) {
137         for (auto j = 0; j < DIMENSION; j++) {
138             double value = 0.0;
139             for (auto k = 0; k < columns_; k++) {
140                 value += Matrix3n_[i][k] * matrix[k][j];
141             }
142             Matrix3[i][j] = value;
143         }
144     }
145     return Matrix3;
146 }
147 
Transpose() const148 MatrixN3 Matrix3N::Transpose() const
149 {
150     MatrixN3 matrix { columns_ };
151     for (auto i = 0; i < DIMENSION; i++) {
152         for (auto j = 0; j < columns_; j++) {
153             matrix[j][i] = Matrix3n_[i][j];
154         }
155     }
156     return matrix;
157 }
158 
ScaleMapping(const std::vector<double> & src) const159 std::vector<double> Matrix3N::ScaleMapping(const std::vector<double>& src) const
160 {
161     std::vector<double> value { DIMENSION, 0 };
162     if (static_cast<int32_t>(src.size()) != columns_) {
163         return value;
164     }
165     for (int32_t i = 0; i < DIMENSION; i++) {
166         double item = 0.0;
167         for (int32_t j = 0; j < columns_; j++) {
168             item = item + Matrix3n_[i][j] * src[j];
169         }
170         value[i] = item;
171     }
172     return value;
173 }
174 
ScaleMapping(const std::vector<double> & src,std::vector<double> & result) const175 bool Matrix3N::ScaleMapping(const std::vector<double>& src, std::vector<double>& result) const
176 {
177     if (static_cast<int32_t>(src.size()) != columns_) {
178         LOG_E("failt to ScaleMapping, due to %{public}d, %{public}d", static_cast<int32_t>(src.size()), columns_);
179         return false;
180     }
181     result.resize(DIMENSION, 0);
182     for (int32_t i = 0; i < DIMENSION; i++) {
183         double item = 0.0;
184         for (int32_t j = 0; j < columns_; j++) {
185             item = item + Matrix3n_[i][j] * src[j];
186         }
187         result[i] = item;
188     }
189     return true;
190 }
191 
MatrixN3(int32_t rows)192 MatrixN3::MatrixN3(int32_t rows) : rows_(rows)
193 {
194     Matrixn3_.resize(rows, std::vector<double>(DIMENSION, 0));
195 }
196 
Transpose() const197 Matrix3N MatrixN3::Transpose() const
198 {
199     Matrix3N matrix { rows_ };
200     for (auto i = 0; i < DIMENSION; i++) {
201         for (auto j = 0; j < rows_; j++) {
202             matrix[i][j] = Matrixn3_[j][i];
203         }
204     }
205     return matrix;
206 }
207 
ScaleMapping(const std::vector<double> & src) const208 std::vector<double> MatrixN3::ScaleMapping(const std::vector<double>& src) const
209 {
210     std::vector<double> value { rows_, 0 };
211     if (static_cast<int32_t>(src.size()) != DIMENSION) {
212         return value;
213     }
214     for (int32_t i = 0; i < rows_; i++) {
215         double item = 0.0;
216         for (int32_t j = 0; j < DIMENSION; j++) {
217             item = item + Matrixn3_[i][j] * src[j];
218         }
219         value[i] = item;
220     }
221     return value;
222 }
223 } // namespace OHOS::uitest
224