Bug 1876318 - set shipping-product for push-bundle tasks. r=bhearsum,releng-reviewers
[gecko.git] / gfx / thebes / gfxQuaternion.h
blobec6f94cad93757a6a4e179660c266533f6821825
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/. */
6 #ifndef GFX_QUATERNION_H
7 #define GFX_QUATERNION_H
9 #include "mozilla/gfx/BasePoint4D.h"
10 #include "mozilla/gfx/Matrix.h"
11 #include "nsAlgorithm.h"
12 #include <algorithm>
14 struct gfxQuaternion
15 : public mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> {
16 typedef mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> Super;
18 gfxQuaternion() : Super() {}
19 gfxQuaternion(gfxFloat aX, gfxFloat aY, gfxFloat aZ, gfxFloat aW)
20 : Super(aX, aY, aZ, aW) {}
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));
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;
37 gfxQuaternion Slerp(const gfxQuaternion& aOther, gfxFloat aCoeff) const {
38 gfxFloat dot = mozilla::clamped(DotProduct(aOther), -1.0, 1.0);
39 if (dot == 1.0) {
40 return *this;
43 gfxFloat theta = acos(dot);
44 gfxFloat rsintheta = 1 / sqrt(1 - dot * dot);
45 gfxFloat rightWeight = sin(aCoeff * theta) * rsintheta;
47 gfxQuaternion left = *this;
48 gfxQuaternion right = aOther;
50 left *= cos(aCoeff * theta) - dot * rightWeight;
51 right *= rightWeight;
53 return left + right;
56 using Super::operator*=;
58 // Quaternion multiplication
59 // Reference:
60 // https://en.wikipedia.org/wiki/Quaternion#Ordered_list_form
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);
73 gfxQuaternion& operator*=(const gfxQuaternion& aOther) {
74 *this = *this * aOther;
75 return *this;
78 mozilla::gfx::Matrix4x4 ToMatrix() const {
79 mozilla::gfx::Matrix4x4 temp;
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);
91 return temp;
95 #endif /* GFX_QUATERNION_H */