1 /* 2 * Copyright (c) 2024 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 #ifndef MY_X_COMPONENT_MATRIX4X4_H 16 #define MY_X_COMPONENT_MATRIX4X4_H 17 18 #include "Quaternion.h" 19 #include "Vector3.h" 20 #include "util/types.h" 21 class Matrix4x4 { 22 public: Matrix4x4()23 Matrix4x4() {} Identity()24 static Matrix4x4 Identity() 25 { 26 Matrix4x4 mat; 27 for (int i = 0; i < matrixComponentCount; i++) { 28 mat.values[i] = identityValues[i]; 29 } 30 return mat; 31 } Set(float dataIn[],int offset)32 void Set(float dataIn[], int offset) 33 { 34 for (int i = 0; i < matrixComponentCount; i++) { 35 values[i] = dataIn[offset + i]; 36 } 37 } Multiply(Matrix4x4 lhs,Matrix4x4 rhs,Matrix4x4 & product)38 static void Multiply(Matrix4x4 lhs, Matrix4x4 rhs, Matrix4x4 &product) 39 { 40 float outArray[16] = {0}; 41 for (int row = 0; row < kFour; ++row) { 42 int index = 0; 43 for (int column = 0; column < kFour; ++column) { 44 for (int column2 = 0; column2 < kFour; ++column2) { 45 outArray[index] += lhs.values[column2 + row * kFour] * rhs.values[row + column * kFour]; 46 ++index; 47 } 48 } 49 } 50 product.Set(outArray, 0); 51 } 52 GetValues()53 float *GetValues() { return values; } 54 MakeTranslation(Vector3 translation)55 void MakeTranslation(Vector3 translation) 56 { 57 Identity(); 58 values[12] = translation.GetDataX(); // M12 59 values[13] = translation.GetDataY(); // M13 60 values[14] = translation.GetDataZ(); // M14 61 } 62 MakeScale(Vector3 scale)63 void MakeScale(Vector3 scale) 64 { 65 Identity(); 66 values[0] = scale.GetDataX(); 67 values[kFive] = scale.GetDataY(); 68 values[kTen] = scale.GetDataZ(); 69 } 70 MakeRoate(float angle,Vector3 axis)71 void MakeRoate(float angle, Vector3 axis) 72 { 73 Identity(); 74 Quaternion rotation = Quaternion::AngleAxis(angle, axis); 75 Quaternion rotationNorm = rotation.GetNormalized(); 76 float yy = rotationNorm.GetDataY() * rotationNorm.GetDataY(); 77 float zz = rotationNorm.GetDataZ() * rotationNorm.GetDataZ(); 78 this->values[0] = 1.0F - 2.0F * (yy + zz); 79 float xy = rotationNorm.GetDataX() * rotationNorm.GetDataY(); 80 float zw = rotationNorm.GetDataZ() * rotationNorm.GetDataW(); 81 this->values[kFour] = 2.0F * (xy - zw); 82 float xz = rotationNorm.GetDataX() * rotationNorm.GetDataZ(); 83 float yw = rotationNorm.GetDataY() * rotationNorm.GetDataW(); 84 this->values[kEight] = 2.0F * (xz + yw); 85 this->values[1] = 2.0F * (xy + zw); 86 float xx = rotationNorm.GetDataX() * rotationNorm.GetDataX(); 87 this->values[kFive] = 1.0F - 2.0F * (xx + zz); 88 float yz = rotationNorm.GetDataY() * rotationNorm.GetDataZ(); 89 float xw = rotationNorm.GetDataX() * rotationNorm.GetDataW(); 90 this->values[kNine] = 2.0F * (yz - xw); 91 this->values[kTwo] = 2.0F * (xz - yw); 92 this->values[kSix] = 2.0F * (yz + xw); 93 this->values[kTen] = 1.0F - 2.0F * (xx + yy); 94 } 95 SetValue(int index,float value)96 void SetValue(int index, float value) { values[index] = value; } 97 GetValue(int index)98 float GetValue(int index) { return values[index]; } 99 100 private: 101 float values[16] = {0}; 102 static constexpr int matrixComponentCount = 16; 103 static constexpr float identityValues[16] = {1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 104 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F}; 105 }; 106 107 #endif // MY_X_COMPONENT_MATRIX4X4_H 108