gfxQuaternion.h (3215B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef GFX_QUATERNION_H 7 #define GFX_QUATERNION_H 8 9 #include "gfxTypes.h" 10 #include "mozilla/gfx/BasePoint4D.h" 11 #include "mozilla/gfx/Matrix.h" 12 #include <algorithm> 13 14 struct gfxQuaternion 15 : public mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> { 16 typedef mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> Super; 17 18 gfxQuaternion() : Super() {} 19 gfxQuaternion(gfxFloat aX, gfxFloat aY, gfxFloat aZ, gfxFloat aW) 20 : Super(aX, aY, aZ, aW) {} 21 22 explicit gfxQuaternion(const mozilla::gfx::Matrix4x4& aMatrix) { 23 w = 0.5 * 24 sqrt(std::max(1 + aMatrix[0][0] + aMatrix[1][1] + aMatrix[2][2], 0.0f)); 25 x = 0.5 * 26 sqrt(std::max(1 + aMatrix[0][0] - aMatrix[1][1] - aMatrix[2][2], 0.0f)); 27 y = 0.5 * 28 sqrt(std::max(1 - aMatrix[0][0] + aMatrix[1][1] - aMatrix[2][2], 0.0f)); 29 z = 0.5 * 30 sqrt(std::max(1 - aMatrix[0][0] - aMatrix[1][1] + aMatrix[2][2], 0.0f)); 31 32 if (aMatrix[2][1] > aMatrix[1][2]) x = -x; 33 if (aMatrix[0][2] > aMatrix[2][0]) y = -y; 34 if (aMatrix[1][0] > aMatrix[0][1]) z = -z; 35 } 36 37 gfxQuaternion Slerp(const gfxQuaternion& aOther, gfxFloat aCoeff) const { 38 gfxFloat dot = std::clamp(DotProduct(aOther), -1.0, 1.0); 39 if (dot == 1.0) { 40 return *this; 41 } 42 43 gfxFloat theta = acos(dot); 44 gfxFloat rsintheta = 1 / sqrt(1 - dot * dot); 45 gfxFloat rightWeight = sin(aCoeff * theta) * rsintheta; 46 47 gfxQuaternion left = *this; 48 gfxQuaternion right = aOther; 49 50 left *= cos(aCoeff * theta) - dot * rightWeight; 51 right *= rightWeight; 52 53 return left + right; 54 } 55 56 using Super::operator*=; 57 58 // Quaternion multiplication 59 // Reference: 60 // https://en.wikipedia.org/wiki/Quaternion#Ordered_list_form 61 // 62 // (w1, x1, y1, z1)(w2, x2, y2, z2) = (w1w2 - x1x2 - y1y2 - z1z2, 63 // w1x2 + x1w2 + y1z2 - z1y2, 64 // w1y2 - x1z2 + y1w2 + z1x2, 65 // w1z2 + x1y2 - y1x2 + z1w2) 66 gfxQuaternion operator*(const gfxQuaternion& aOther) const { 67 return gfxQuaternion( 68 w * aOther.x + x * aOther.w + y * aOther.z - z * aOther.y, 69 w * aOther.y - x * aOther.z + y * aOther.w + z * aOther.x, 70 w * aOther.z + x * aOther.y - y * aOther.x + z * aOther.w, 71 w * aOther.w - x * aOther.x - y * aOther.y - z * aOther.z); 72 } 73 gfxQuaternion& operator*=(const gfxQuaternion& aOther) { 74 *this = *this * aOther; 75 return *this; 76 } 77 78 mozilla::gfx::Matrix4x4 ToMatrix() const { 79 mozilla::gfx::Matrix4x4 temp; 80 81 temp[0][0] = 1 - 2 * (y * y + z * z); 82 temp[0][1] = 2 * (x * y + w * z); 83 temp[0][2] = 2 * (x * z - w * y); 84 temp[1][0] = 2 * (x * y - w * z); 85 temp[1][1] = 1 - 2 * (x * x + z * z); 86 temp[1][2] = 2 * (y * z + w * x); 87 temp[2][0] = 2 * (x * z + w * y); 88 temp[2][1] = 2 * (y * z - w * x); 89 temp[2][2] = 1 - 2 * (x * x + y * y); 90 91 return temp; 92 } 93 }; 94 95 #endif /* GFX_QUATERNION_H */