Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFX_QUATERNION_H |
michael@0 | 7 | #define GFX_QUATERNION_H |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/gfx/BasePoint4D.h" |
michael@0 | 10 | #include "gfx3DMatrix.h" |
michael@0 | 11 | #include "nsAlgorithm.h" |
michael@0 | 12 | #include <algorithm> |
michael@0 | 13 | |
michael@0 | 14 | struct gfxQuaternion : public mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> { |
michael@0 | 15 | typedef mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> Super; |
michael@0 | 16 | |
michael@0 | 17 | gfxQuaternion() : Super() {} |
michael@0 | 18 | gfxQuaternion(gfxFloat aX, gfxFloat aY, gfxFloat aZ, gfxFloat aW) : Super(aX, aY, aZ, aW) {} |
michael@0 | 19 | |
michael@0 | 20 | gfxQuaternion(const gfx3DMatrix& aMatrix) { |
michael@0 | 21 | w = 0.5 * sqrt(std::max(1 + aMatrix[0][0] + aMatrix[1][1] + aMatrix[2][2], 0.0f)); |
michael@0 | 22 | x = 0.5 * sqrt(std::max(1 + aMatrix[0][0] - aMatrix[1][1] - aMatrix[2][2], 0.0f)); |
michael@0 | 23 | y = 0.5 * sqrt(std::max(1 - aMatrix[0][0] + aMatrix[1][1] - aMatrix[2][2], 0.0f)); |
michael@0 | 24 | z = 0.5 * sqrt(std::max(1 - aMatrix[0][0] - aMatrix[1][1] + aMatrix[2][2], 0.0f)); |
michael@0 | 25 | |
michael@0 | 26 | if(aMatrix[2][1] > aMatrix[1][2]) |
michael@0 | 27 | x = -x; |
michael@0 | 28 | if(aMatrix[0][2] > aMatrix[2][0]) |
michael@0 | 29 | y = -y; |
michael@0 | 30 | if(aMatrix[1][0] > aMatrix[0][1]) |
michael@0 | 31 | z = -z; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | gfxQuaternion Slerp(const gfxQuaternion &aOther, gfxFloat aCoeff) { |
michael@0 | 35 | gfxFloat dot = mozilla::clamped(DotProduct(aOther), -1.0, 1.0); |
michael@0 | 36 | if (dot == 1.0) { |
michael@0 | 37 | return *this; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | gfxFloat theta = acos(dot); |
michael@0 | 41 | gfxFloat rsintheta = 1/sqrt(1 - dot*dot); |
michael@0 | 42 | gfxFloat rightWeight = sin(aCoeff*theta)*rsintheta; |
michael@0 | 43 | |
michael@0 | 44 | gfxQuaternion left = *this; |
michael@0 | 45 | gfxQuaternion right = aOther; |
michael@0 | 46 | |
michael@0 | 47 | left *= cos(aCoeff*theta) - dot*rightWeight; |
michael@0 | 48 | right *= rightWeight; |
michael@0 | 49 | |
michael@0 | 50 | return left + right; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | gfx3DMatrix ToMatrix() { |
michael@0 | 54 | gfx3DMatrix temp; |
michael@0 | 55 | |
michael@0 | 56 | temp[0][0] = 1 - 2 * (y * y + z * z); |
michael@0 | 57 | temp[0][1] = 2 * (x * y + w * z); |
michael@0 | 58 | temp[0][2] = 2 * (x * z - w * y); |
michael@0 | 59 | temp[1][0] = 2 * (x * y - w * z); |
michael@0 | 60 | temp[1][1] = 1 - 2 * (x * x + z * z); |
michael@0 | 61 | temp[1][2] = 2 * (y * z + w * x); |
michael@0 | 62 | temp[2][0] = 2 * (x * z + w * y); |
michael@0 | 63 | temp[2][1] = 2 * (y * z - w * x); |
michael@0 | 64 | temp[2][2] = 1 - 2 * (x * x + y * y); |
michael@0 | 65 | |
michael@0 | 66 | return temp; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | }; |
michael@0 | 70 | |
michael@0 | 71 | #endif /* GFX_QUATERNION_H */ |