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.

92 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Holds a quaternion (a 3D vector and a scalar value).
  18. @tags{OpenGL}
  19. */
  20. template <typename Type>
  21. class Quaternion
  22. {
  23. public:
  24. Quaternion() noexcept : scalar() {}
  25. Quaternion (const Quaternion& other) noexcept : vector (other.vector), scalar (other.scalar) {}
  26. Quaternion (Vector3D<Type> vectorPart, Type scalarPart) noexcept : vector (vectorPart), scalar (scalarPart) {}
  27. Quaternion (Type x, Type y, Type z, Type w) noexcept : vector (x, y, z), scalar (w) {}
  28. /** Creates a quaternion from an angle and an axis. */
  29. static Quaternion fromAngle (Type angle, Vector3D<Type> axis) noexcept
  30. {
  31. return Quaternion (axis.normalised() * std::sin (angle / (Type) 2), std::cos (angle / (Type) 2));
  32. }
  33. Quaternion& operator= (Quaternion other) noexcept
  34. {
  35. vector = other.vector;
  36. scalar = other.scalar;
  37. return *this;
  38. }
  39. Quaternion& operator*= (Quaternion other) noexcept
  40. {
  41. const Type oldScalar (scalar);
  42. scalar = (scalar * other.scalar) - (vector * other.vector);
  43. vector = (other.vector * oldScalar) + (vector * other.scalar) + (vector ^ other.vector);
  44. return *this;
  45. }
  46. Type length() const noexcept { return std::sqrt (normal()); }
  47. Type normal() const noexcept { return scalar * scalar + vector.lengthSquared(); }
  48. Quaternion normalised() const noexcept
  49. {
  50. const Type len (length());
  51. jassert (len > 0);
  52. return Quaternion (vector / len, scalar / len);
  53. }
  54. /** Returns the matrix that will perform the rotation specified by this quaternion. */
  55. Matrix3D<Type> getRotationMatrix() const noexcept
  56. {
  57. const Type norm (normal());
  58. const Type s (norm > 0 ? ((Type) 2) / norm : 0);
  59. const Type xs (s * vector.x), ys (s * vector.y), zs (s * vector.z);
  60. const Type wx (xs * scalar), wy (ys * scalar), wz (zs * scalar);
  61. const Type xx (xs * vector.x), xy (ys * vector.x), xz (zs * vector.x);
  62. const Type yy (ys * vector.y), yz (zs * vector.y), zz (zs * vector.z);
  63. return Matrix3D<Type> (((Type) 1) - (yy + zz), xy - wz, xz + wy, 0,
  64. xy + wz, ((Type) 1) - (xx+ zz), yz - wx, 0,
  65. xz - wy, yz + wx, ((Type) 1) - (xx + yy), 0,
  66. 0, 0, 0, (Type) 1);
  67. }
  68. /** The vector part of the quaternion. */
  69. Vector3D<Type> vector;
  70. /** The scalar part of the quaternion. */
  71. Type scalar;
  72. };
  73. } // namespace juce