1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Matt Woodrow <mwoodrow@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef GFX_QUATERNION_H
39 : #define GFX_QUATERNION_H
40 :
41 : #include "mozilla/gfx/BasePoint4D.h"
42 : #include "gfx3DMatrix.h"
43 : #include "nsAlgorithm.h"
44 :
45 : struct THEBES_API gfxQuaternion : public mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> {
46 : typedef mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> Super;
47 :
48 0 : gfxQuaternion() : Super() {}
49 0 : gfxQuaternion(gfxFloat aX, gfxFloat aY, gfxFloat aZ, gfxFloat aW) : Super(aX, aY, aZ, aW) {}
50 :
51 0 : gfxQuaternion(const gfx3DMatrix& aMatrix) {
52 0 : w = 0.5 * sqrt(NS_MAX(1 + aMatrix[0][0] + aMatrix[1][1] + aMatrix[2][2], 0.0f));
53 0 : x = 0.5 * sqrt(NS_MAX(1 + aMatrix[0][0] - aMatrix[1][1] - aMatrix[2][2], 0.0f));
54 0 : y = 0.5 * sqrt(NS_MAX(1 - aMatrix[0][0] + aMatrix[1][1] - aMatrix[2][2], 0.0f));
55 0 : z = 0.5 * sqrt(NS_MAX(1 - aMatrix[0][0] - aMatrix[1][1] + aMatrix[2][2], 0.0f));
56 :
57 0 : if(aMatrix[2][1] > aMatrix[1][2])
58 0 : x = -x;
59 0 : if(aMatrix[0][2] > aMatrix[2][0])
60 0 : y = -y;
61 0 : if(aMatrix[1][0] > aMatrix[0][1])
62 0 : z = -z;
63 0 : }
64 :
65 0 : gfxQuaternion Slerp(const gfxQuaternion &aOther, gfxFloat aCoeff) {
66 0 : gfxFloat dot = mozilla::clamped(DotProduct(aOther), -1.0, 1.0);
67 0 : if (dot == 1.0) {
68 0 : return *this;
69 : }
70 :
71 0 : gfxFloat theta = acos(dot);
72 0 : gfxFloat rsintheta = 1/sqrt(1 - dot*dot);
73 0 : gfxFloat w = sin(aCoeff*theta)*rsintheta;
74 :
75 0 : gfxQuaternion left = *this;
76 0 : gfxQuaternion right = aOther;
77 :
78 0 : left *= cos(aCoeff*theta) - dot*w;
79 0 : right *= w;
80 :
81 0 : return left + right;
82 : }
83 :
84 0 : gfx3DMatrix ToMatrix() {
85 0 : gfx3DMatrix temp;
86 :
87 0 : temp[0][0] = 1 - 2 * (y * y + z * z);
88 0 : temp[0][1] = 2 * (x * y + w * z);
89 0 : temp[0][2] = 2 * (x * z - w * y);
90 0 : temp[1][0] = 2 * (x * y - w * z);
91 0 : temp[1][1] = 1 - 2 * (x * x + z * z);
92 0 : temp[1][2] = 2 * (y * z + w * x);
93 0 : temp[2][0] = 2 * (x * z + w * y);
94 0 : temp[2][1] = 2 * (y * z - w * x);
95 0 : temp[2][2] = 1 - 2 * (x * x + y * y);
96 :
97 : return temp;
98 : }
99 :
100 : };
101 :
102 : #endif /* GFX_QUATERNION_H */
|