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 16 #ifndef MY_X_COMPONENT_QUATERNION_H 17 #define MY_X_COMPONENT_QUATERNION_H 18 19 #include <cmath> 20 #include "algorithm/Vector3.h" 21 class Quaternion { 22 public: Quaternion(float xData,float yData,float zData,float wData)23 Quaternion(float xData, float yData, float zData, float wData) 24 : dataX(xData), dataY(yData), dataZ(zData), dataW(wData) 25 { 26 } AngleAxis(float angleRadians,Vector3 axis)27 static Quaternion AngleAxis(float angleRadians, Vector3 axis) 28 { 29 double halfAngleRadians = static_cast<double>(angleRadians * 0.5); 30 float sinValue = static_cast<float>(sin(halfAngleRadians)); 31 Vector3 axisMutiplied = Vector3::Multiply(axis, sinValue); 32 return Quaternion(axisMutiplied.GetDataX(), axisMutiplied.GetDataY(), axisMutiplied.GetDataZ(), 33 (float)cos(halfAngleRadians)); 34 } Length(float xData,float yData,float zData,float wData)35 static float Length(float xData, float yData, float zData, float wData) 36 { 37 return sqrt(xData * xData + yData * yData + zData * zData + wData * wData); 38 } 39 Identity()40 static Quaternion Identity() { return Quaternion(0.0F, 0.0F, 0.0F, 1.0F); } Normalize(float xData,float yData,float zData,float wData)41 static Quaternion Normalize(float xData, float yData, float zData, float wData) 42 { 43 float oneOverLen = 1.0F / Length(xData, yData, zData, wData); 44 return isfinite(oneOverLen) 45 ? Quaternion(xData * oneOverLen, yData * oneOverLen, zData * oneOverLen, wData * oneOverLen) 46 : Identity(); 47 } 48 GetNormalized()49 Quaternion GetNormalized() { return Normalize(this->dataX, this->dataY, this->dataZ, this->dataW); } 50 51 float GetDataW(); 52 void SetDataW(float dataW); 53 float GetDataZ(); 54 void SetDataZ(float dataZ); 55 float GetDataY(); 56 void SetDataY(float dataY); 57 float GetDataX(); 58 void SetDataX(float dataX); 59 60 private: 61 float dataX; 62 float dataY; 63 float dataZ; 64 float dataW; 65 }; 66 #endif // MY_X_COMPONENT_QUATERNION_H 67