1 /* 2 * Copyright (c) 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 #ifndef UTILS_MATRIX_EXPORT_MATRIX_H 17 #define UTILS_MATRIX_EXPORT_MATRIX_H 18 19 #include <math.h> 20 #include <memory> 21 #include <ostream> 22 23 template<class T = float> 24 class Matrix { 25 public: 26 static inline constexpr int32_t rowcol = 4; 27 static inline constexpr int32_t units = rowcol * rowcol; 28 UnitMatrix()29 static Matrix<T> UnitMatrix() 30 { 31 Matrix<T> retval; 32 for (int32_t i = 0; i < units; i++) { 33 retval.ptr[i] = 0; 34 } 35 retval.ele(0x0, 0x0) = 0x1; 36 retval.ele(0x1, 0x1) = 0x1; 37 retval.ele(0x2, 0x2) = 0x1; 38 retval.ele(0x3, 0x3) = 0x1; 39 return retval; 40 } 41 42 static Matrix<T> TranslateMatrix(T x, T y, T z = 0) 43 { 44 Matrix<T> retval = UnitMatrix(); 45 retval.ele(0x0, 0x3) = x; 46 retval.ele(0x1, 0x3) = y; 47 retval.ele(0x2, 0x3) = z; 48 return retval; 49 } 50 51 static Matrix<T> ScaleMatrix(T x, T y, T z = 1) 52 { 53 Matrix<T> retval = UnitMatrix(); 54 retval.ele(0x0, 0x0) *= x; 55 retval.ele(0x1, 0x1) *= y; 56 retval.ele(0x2, 0x2) *= z; 57 return retval; 58 } 59 RotateMatrixZ(double angle)60 static Matrix<T> RotateMatrixZ(double angle) 61 { 62 Matrix<T> retval = UnitMatrix(); 63 double rad = angle * acos(-1) / 180.0; 64 retval.ele(0x0, 0x0) = cos(rad); 65 retval.ele(0x0, 0x1) = -sin(rad); 66 retval.ele(0x1, 0x0) = sin(rad); 67 retval.ele(0x1, 0x1) = cos(rad); 68 return retval; 69 } 70 71 Matrix() = default; 72 Matrix(const Matrix<T> & m)73 Matrix(const Matrix<T> &m) 74 { 75 for (int32_t i = 0; i < units; i++) { 76 ptr[i] = m.ptr[i]; 77 } 78 } 79 Matrix(Matrix<T> && m)80 Matrix(Matrix<T> &&m) 81 { 82 ptr = std::move(m.ptr); 83 } 84 85 Matrix<T> &operator =(const Matrix<T> &m) 86 { 87 for (int32_t i = 0; i < units; i++) { 88 ptr[i] = m.ptr[i]; 89 } 90 return *this; 91 } 92 93 Matrix<T> operator *(const Matrix<T> other) 94 { 95 Matrix<T> retval; 96 for (int i = 0; i < rowcol; i++) { 97 for (int j = 0; j < rowcol; j++) { 98 retval.ele(i, j) = 0; 99 for (int k = 0; k < rowcol; k++) { 100 retval.ele(i, j) += ele(i, k) * other.ele(k, j); 101 } 102 } 103 } 104 return retval; 105 } 106 107 operator T*() const 108 { 109 return ptr.get(); 110 } 111 112 friend std::ostream &operator <<(std::ostream &os, const Matrix<T> &m) 113 { 114 os << std::endl; 115 os << "Matrix {" << std::endl; 116 os << " [" << m.ele(0x0, 0x0) << "] [" 117 << m.ele(0x0, 0x1) << "] [" 118 << m.ele(0x0, 0x2) << "] [" 119 << m.ele(0x0, 0x3) << "]" 120 << std::endl; 121 os << " [" << m.ele(0x1, 0x0) << "] [" 122 << m.ele(0x1, 0x1) << "] [" 123 << m.ele(0x1, 0x2) << "] [" 124 << m.ele(0x1, 0x3) << "]" 125 << std::endl; 126 os << " [" << m.ele(0x2, 0x0) << "] [" 127 << m.ele(0x2, 0x1) << "] [" 128 << m.ele(0x2, 0x2) << "] [" 129 << m.ele(0x2, 0x3) << "]" 130 << std::endl; 131 os << " [" << m.ele(0x3, 0x0) << "] [" 132 << m.ele(0x3, 0x1) << "] [" 133 << m.ele(0x3, 0x2) << "] [" 134 << m.ele(0x3, 0x3) << "]" 135 << std::endl; 136 os << "}" << std::endl; 137 return os; 138 } 139 140 private: ele(int i,int j)141 inline T &ele(int i, int j) const 142 { 143 return ptr[i + j * rowcol]; 144 } 145 146 std::unique_ptr<T[]> ptr = std::make_unique<T[]>(units); 147 }; 148 149 #endif // UTILS_MATRIX_EXPORT_MATRIX_H 150