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.

99 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Holds a quaternion (a 3D vector and a scalar value).
  23. @tags{OpenGL}
  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 (Vector3D<Type> vectorPart, Type scalarPart) noexcept : vector (vectorPart), scalar (scalarPart) {}
  32. Quaternion (Type x, Type y, Type z, Type w) noexcept : vector (x, y, z), scalar (w) {}
  33. /** Creates a quaternion from an angle and an axis. */
  34. static Quaternion fromAngle (Type angle, Vector3D<Type> axis) noexcept
  35. {
  36. return Quaternion (axis.normalised() * std::sin (angle / (Type) 2), std::cos (angle / (Type) 2));
  37. }
  38. Quaternion& operator= (Quaternion other) noexcept
  39. {
  40. vector = other.vector;
  41. scalar = other.scalar;
  42. return *this;
  43. }
  44. Quaternion& operator*= (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. } // namespace juce