• 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 #ifndef BASE_GEOMETRY_MATRIX3_H
17 #define BASE_GEOMETRY_MATRIX3_H
18 #include <vector>
19 #include <string>
20 
21 namespace OHOS::uitest {
22 class Matrix3N;
23 class MatrixN3;
24 class Matrix3 {
25 public:
26     // Matrix dimension is 3X3.
27     static constexpr int32_t DIMENSION = 3;
28 
29     Matrix3() = default;
30     ~Matrix3() = default;
31 
32     // Gets the inverse of this matrix;
33     bool Invert(Matrix3& matrix) const;
34 
35     inline Matrix3& operator*(double num)
36     {
37         for (auto& vector : matrix3X3_) {
38             std::for_each(vector.begin(), vector.end(), [num](auto& item) { item = item * num; });
39         }
40         return *this;
41     }
42 
43     Matrix3N operator*(const Matrix3N& matrix) const;
44 
45     // Make sure that the value of index is less than 3.
46     inline std::vector<double>& operator[](int32_t index)
47     {
48         return matrix3X3_[index];
49     }
50 
51     // Make sure that the value of index is less than 3.
52     inline const std::vector<double>& operator[](int32_t index) const
53     {
54         return matrix3X3_[index];
55     }
56 
57     // Make sure that the value of row is less than 3 and col is less than 3.
operator()58     inline double operator()(int32_t row, int32_t col) const
59     {
60         return matrix3X3_[row][col];
61     }
62 
63     Matrix3 Transpose() const;
64 
65     // Make sure that the vector size is equal than column.
66     std::vector<double> ScaleMapping(const std::vector<double>& src) const;
67 
68     // Make sure that the vector size is equal than column.
69     bool ScaleMapping(const std::vector<double>& src, std::vector<double>& result) const;
70 
ToString()71     std::string ToString() const
72     {
73         std::string val;
74         for (auto& vector : matrix3X3_) {
75             std::for_each(vector.begin(), vector.end(),
76                 [&val](const auto& item) { val = val + "item: " + std::to_string(item) + " "; });
77         }
78         return val;
79     }
80 
81 private:
82     std::vector<std::vector<double>> matrix3X3_ = { DIMENSION, std::vector<double>(DIMENSION, 0.0) };
83 };
84 
85 class Matrix3N {
86 public:
87     // Matrix dimension is 3X3.
88     static constexpr int32_t DIMENSION = 3;
89 
90     explicit Matrix3N(int32_t columns);
91     ~Matrix3N() = default;
92 
GetColNum()93     inline int32_t GetColNum() const
94     {
95         return columns_;
96     }
97 
98     inline Matrix3N& operator*(double num)
99     {
100         for (auto& vector : Matrix3n_) {
101             std::for_each(vector.begin(), vector.end(), [num](auto& item) { item = item * num; });
102         }
103         return *this;
104     }
105 
106     // Make sure that the rows of MatrixN3 is equal than the columns of Matrix3N.
107     Matrix3 operator*(const MatrixN3& matrix) const;
108 
109     // Make sure that the value of index is less than 3.
110     inline std::vector<double>& operator[](int32_t index)
111     {
112         return Matrix3n_[index];
113     }
114 
115     // Make sure that the value of index is less than 3.
116     inline const std::vector<double>& operator[](int32_t index) const
117     {
118         return Matrix3n_[index];
119     }
120 
121     // Make sure that the value of row is less than 3 and col is less than columns.
operator()122     inline double operator()(int32_t row, int32_t col) const
123     {
124         return Matrix3n_[row][col];
125     }
126 
127     MatrixN3 Transpose() const;
128 
129     // Make sure that the vector size is equal than column.
130     std::vector<double> ScaleMapping(const std::vector<double>& src) const;
131 
132     // Make sure that the vector size is equal than column.
133     bool ScaleMapping(const std::vector<double>& src, std::vector<double>& result) const;
134 
ToString()135     std::string ToString() const
136     {
137         std::string val;
138         for (auto& vector : Matrix3n_) {
139             std::for_each(vector.begin(), vector.end(),
140                 [&val](const auto& item) { val = val + "item: " + std::to_string(item) + " "; });
141         }
142         return val;
143     }
144 
145 private:
146     std::vector<std::vector<double>> Matrix3n_;
147     int32_t columns_ = 0;
148 };
149 
150 class MatrixN3 {
151 public:
152     // Matrix dimension is 3XN.
153     static constexpr int32_t DIMENSION = 3;
154 
155     explicit MatrixN3(int32_t rows);
156     ~MatrixN3() = default;
157 
GetRowNum()158     inline int32_t GetRowNum() const
159     {
160         return rows_;
161     }
162 
163     inline MatrixN3& operator*(double num)
164     {
165         for (auto& vector : Matrixn3_) {
166             std::for_each(vector.begin(), vector.end(), [num](auto& item) { item = item * num; });
167         }
168         return *this;
169     }
170 
171     // Make sure that the value of index is less than rows.
172     inline std::vector<double>& operator[](int32_t index)
173     {
174         return Matrixn3_[index];
175     }
176 
177     // Make sure that the value of index is less than rows.
178     inline const std::vector<double>& operator[](int32_t index) const
179     {
180         return Matrixn3_[index];
181     }
182 
183     // Make sure that the value of row is less than rows and col is less than 3.
operator()184     inline double operator()(int32_t row, int32_t col) const
185     {
186         return Matrixn3_[row][col];
187     }
188 
189     Matrix3N Transpose() const;
190 
191     // Make sure that the vector size is equal than column.
192     std::vector<double> ScaleMapping(const std::vector<double>& src) const;
193 
ToString()194     std::string ToString() const
195     {
196         std::string val;
197         for (auto& vector : Matrixn3_) {
198             std::for_each(vector.begin(), vector.end(),
199                 [&val](const auto& item) { val = val + "item: " + std::to_string(item) + " "; });
200         }
201         return val;
202     }
203 
204 private:
205     std::vector<std::vector<double>> Matrixn3_;
206     int32_t rows_ = 0;
207 };
208 } // namespace OHOS::uitest
209 #endif // BASE_GEOMETRY_MATRIX3_H
210