gfx/thebes/gfxQuaternion.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/thebes/gfxQuaternion.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef GFX_QUATERNION_H
    1.10 +#define GFX_QUATERNION_H
    1.11 +
    1.12 +#include "mozilla/gfx/BasePoint4D.h"
    1.13 +#include "gfx3DMatrix.h"
    1.14 +#include "nsAlgorithm.h"
    1.15 +#include <algorithm>
    1.16 +
    1.17 +struct gfxQuaternion : public mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> {
    1.18 +    typedef mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> Super;
    1.19 +
    1.20 +    gfxQuaternion() : Super() {}
    1.21 +    gfxQuaternion(gfxFloat aX, gfxFloat aY, gfxFloat aZ, gfxFloat aW) : Super(aX, aY, aZ, aW) {}
    1.22 +
    1.23 +    gfxQuaternion(const gfx3DMatrix& aMatrix) {
    1.24 +        w = 0.5 * sqrt(std::max(1 + aMatrix[0][0] + aMatrix[1][1] + aMatrix[2][2], 0.0f));
    1.25 +        x = 0.5 * sqrt(std::max(1 + aMatrix[0][0] - aMatrix[1][1] - aMatrix[2][2], 0.0f));
    1.26 +        y = 0.5 * sqrt(std::max(1 - aMatrix[0][0] + aMatrix[1][1] - aMatrix[2][2], 0.0f));
    1.27 +        z = 0.5 * sqrt(std::max(1 - aMatrix[0][0] - aMatrix[1][1] + aMatrix[2][2], 0.0f));
    1.28 +
    1.29 +        if(aMatrix[2][1] > aMatrix[1][2])
    1.30 +            x = -x;
    1.31 +        if(aMatrix[0][2] > aMatrix[2][0])
    1.32 +            y = -y;
    1.33 +        if(aMatrix[1][0] > aMatrix[0][1])
    1.34 +            z = -z;
    1.35 +    }
    1.36 +
    1.37 +    gfxQuaternion Slerp(const gfxQuaternion &aOther, gfxFloat aCoeff) {
    1.38 +        gfxFloat dot = mozilla::clamped(DotProduct(aOther), -1.0, 1.0);
    1.39 +        if (dot == 1.0) {
    1.40 +            return *this;
    1.41 +        }
    1.42 +
    1.43 +        gfxFloat theta = acos(dot);
    1.44 +        gfxFloat rsintheta = 1/sqrt(1 - dot*dot);
    1.45 +        gfxFloat rightWeight = sin(aCoeff*theta)*rsintheta;
    1.46 +
    1.47 +        gfxQuaternion left = *this;
    1.48 +        gfxQuaternion right = aOther;
    1.49 +
    1.50 +        left *= cos(aCoeff*theta) - dot*rightWeight;
    1.51 +        right *= rightWeight;
    1.52 +
    1.53 +        return left + right;
    1.54 +    }
    1.55 +
    1.56 +    gfx3DMatrix ToMatrix() {
    1.57 +        gfx3DMatrix temp;
    1.58 +
    1.59 +        temp[0][0] = 1 - 2 * (y * y + z * z);
    1.60 +        temp[0][1] = 2 * (x * y + w * z);
    1.61 +        temp[0][2] = 2 * (x * z - w * y);
    1.62 +        temp[1][0] = 2 * (x * y - w * z);
    1.63 +        temp[1][1] = 1 - 2 * (x * x + z * z);
    1.64 +        temp[1][2] = 2 * (y * z + w * x);
    1.65 +        temp[2][0] = 2 * (x * z + w * y);
    1.66 +        temp[2][1] = 2 * (y * z - w * x);
    1.67 +        temp[2][2] = 1 - 2 * (x * x + y * y);
    1.68 +
    1.69 +        return temp;
    1.70 +    }
    1.71 +
    1.72 +};
    1.73 +
    1.74 +#endif /* GFX_QUATERNION_H */

mercurial