gfxQuaternion.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef GFX_QUATERNION_H
  6. #define GFX_QUATERNION_H
  7. #include "mozilla/gfx/BasePoint4D.h"
  8. #include "mozilla/gfx/Matrix.h"
  9. #include "nsAlgorithm.h"
  10. #include <algorithm>
  11. struct gfxQuaternion : public mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> {
  12. typedef mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> Super;
  13. gfxQuaternion() : Super() {}
  14. gfxQuaternion(gfxFloat aX, gfxFloat aY, gfxFloat aZ, gfxFloat aW) : Super(aX, aY, aZ, aW) {}
  15. explicit gfxQuaternion(const mozilla::gfx::Matrix4x4& aMatrix) {
  16. w = 0.5 * sqrt(std::max(1 + aMatrix[0][0] + aMatrix[1][1] + aMatrix[2][2], 0.0f));
  17. x = 0.5 * sqrt(std::max(1 + aMatrix[0][0] - aMatrix[1][1] - aMatrix[2][2], 0.0f));
  18. y = 0.5 * sqrt(std::max(1 - aMatrix[0][0] + aMatrix[1][1] - aMatrix[2][2], 0.0f));
  19. z = 0.5 * sqrt(std::max(1 - aMatrix[0][0] - aMatrix[1][1] + aMatrix[2][2], 0.0f));
  20. if(aMatrix[2][1] > aMatrix[1][2])
  21. x = -x;
  22. if(aMatrix[0][2] > aMatrix[2][0])
  23. y = -y;
  24. if(aMatrix[1][0] > aMatrix[0][1])
  25. z = -z;
  26. }
  27. // Convert from |direction axis, angle| pair to gfxQuaternion.
  28. //
  29. // Reference:
  30. // https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
  31. //
  32. // if the direction axis is (x, y, z) = xi + yj + zk,
  33. // and the angle is |theta|, this formula can be done using
  34. // an extension of Euler's formula:
  35. // q = cos(theta/2) + (xi + yj + zk)(sin(theta/2))
  36. // = cos(theta/2) +
  37. // x*sin(theta/2)i + y*sin(theta/2)j + z*sin(theta/2)k
  38. // Note: aDirection should be an unit vector and
  39. // the unit of aAngle should be Radian.
  40. gfxQuaternion(const mozilla::gfx::Point3D &aDirection, gfxFloat aAngle) {
  41. MOZ_ASSERT(mozilla::gfx::FuzzyEqual(aDirection.Length(), 1.0f),
  42. "aDirection should be an unit vector");
  43. x = aDirection.x * sin(aAngle/2.0);
  44. y = aDirection.y * sin(aAngle/2.0);
  45. z = aDirection.z * sin(aAngle/2.0);
  46. w = cos(aAngle/2.0);
  47. }
  48. gfxQuaternion Slerp(const gfxQuaternion &aOther, gfxFloat aCoeff) {
  49. gfxFloat dot = mozilla::clamped(DotProduct(aOther), -1.0, 1.0);
  50. if (dot == 1.0) {
  51. return *this;
  52. }
  53. gfxFloat theta = acos(dot);
  54. gfxFloat rsintheta = 1/sqrt(1 - dot*dot);
  55. gfxFloat rightWeight = sin(aCoeff*theta)*rsintheta;
  56. gfxQuaternion left = *this;
  57. gfxQuaternion right = aOther;
  58. left *= cos(aCoeff*theta) - dot*rightWeight;
  59. right *= rightWeight;
  60. return left + right;
  61. }
  62. mozilla::gfx::Matrix4x4 ToMatrix() {
  63. mozilla::gfx::Matrix4x4 temp;
  64. temp[0][0] = 1 - 2 * (y * y + z * z);
  65. temp[0][1] = 2 * (x * y + w * z);
  66. temp[0][2] = 2 * (x * z - w * y);
  67. temp[1][0] = 2 * (x * y - w * z);
  68. temp[1][1] = 1 - 2 * (x * x + z * z);
  69. temp[1][2] = 2 * (y * z + w * x);
  70. temp[2][0] = 2 * (x * z + w * y);
  71. temp[2][1] = 2 * (y * z - w * x);
  72. temp[2][2] = 1 - 2 * (x * x + y * y);
  73. return temp;
  74. }
  75. };
  76. #endif /* GFX_QUATERNION_H */