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