The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_QUATERNION_H_INCLUDED
  18. #define JUCE_QUATERNION_H_INCLUDED
  19. #include "juce_Matrix3D.h"
  20. #include "juce_Vector3D.h"
  21. //==============================================================================
  22. /**
  23. Holds a quaternion (a 3D vector and a scalar value).
  24. */
  25. template <typename Type>
  26. class Quaternion
  27. {
  28. public:
  29. Quaternion() noexcept : scalar() {}
  30. Quaternion (const Quaternion& other) noexcept : vector (other.vector), scalar (other.scalar) {}
  31. Quaternion (const Vector3D<Type>& vectorPart, const Type& scalarPart) noexcept : vector (vectorPart), scalar (scalarPart) {}
  32. Quaternion (const Type& x, const Type& y, const Type& z, const Type& w) noexcept : vector (x, y, z), scalar (w) {}
  33. /** Creates a quaternion from an angle and an axis. */
  34. static Quaternion fromAngle (const Type& angle, const Vector3D<Type>& axis) noexcept
  35. {
  36. return Quaternion (axis.normalised() * std::sin (angle / (Type) 2), std::cos (angle / (Type) 2));
  37. }
  38. Quaternion& operator= (const Quaternion& other) noexcept
  39. {
  40. vector = other.vector;
  41. scalar = other.scalar;
  42. return *this;
  43. }
  44. Quaternion& operator*= (const Quaternion& other) noexcept
  45. {
  46. const Type oldScalar (scalar);
  47. scalar = (scalar * other.scalar) - (vector * other.vector);
  48. vector = (other.vector * oldScalar) + (vector * other.scalar) + (vector ^ other.vector);
  49. return *this;
  50. }
  51. Type length() const noexcept { return std::sqrt (normal()); }
  52. Type normal() const noexcept { return scalar * scalar + vector.lengthSquared(); }
  53. Quaternion normalised() const noexcept
  54. {
  55. const Type len (length());
  56. jassert (len > 0);
  57. return Quaternion (vector / len, scalar / len);
  58. }
  59. /** Returns the matrix that will perform the rotation specified by this quaternion. */
  60. Matrix3D<Type> getRotationMatrix() const noexcept
  61. {
  62. const Type norm (normal());
  63. const Type s (norm > 0 ? ((Type) 2) / norm : 0);
  64. const Type xs (s * vector.x), ys (s * vector.y), zs (s * vector.z);
  65. const Type wx (xs * scalar), wy (ys * scalar), wz (zs * scalar);
  66. const Type xx (xs * vector.x), xy (ys * vector.x), xz (zs * vector.x);
  67. const Type yy (ys * vector.y), yz (zs * vector.y), zz (zs * vector.z);
  68. return Matrix3D<Type> (((Type) 1) - (yy + zz), xy - wz, xz + wy, 0,
  69. xy + wz, ((Type) 1) - (xx+ zz), yz - wx, 0,
  70. xz - wy, yz + wx, ((Type) 1) - (xx + yy), 0,
  71. 0, 0, 0, (Type) 1);
  72. }
  73. /** The vector part of the quaternion. */
  74. Vector3D<Type> vector;
  75. /** The scalar part of the quaternion. */
  76. Type scalar;
  77. };
  78. #endif // JUCE_QUATERNION_H_INCLUDED