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.

96 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. #include "juce_Vector3D.h"
  19. #include "juce_Matrix3D.h"
  20. //==============================================================================
  21. /**
  22. Holds a quaternion (a 3D vector and a scalar value).
  23. */
  24. template <typename Type>
  25. class Quaternion
  26. {
  27. public:
  28. Quaternion() noexcept : scalar() {}
  29. Quaternion (const Quaternion& other) noexcept : vector (other.vector), scalar (other.scalar) {}
  30. Quaternion (Vector3D<Type> vectorPart, Type scalarPart) noexcept : vector (vectorPart), scalar (scalarPart) {}
  31. Quaternion (Type x, Type y, Type z, Type w) noexcept : vector (x, y, z), scalar (w) {}
  32. /** Creates a quaternion from an angle and an axis. */
  33. static Quaternion fromAngle (Type angle, Vector3D<Type> axis) noexcept
  34. {
  35. return Quaternion (axis.normalised() * std::sin (angle / (Type) 2), std::cos (angle / (Type) 2));
  36. }
  37. Quaternion& operator= (Quaternion other) noexcept
  38. {
  39. vector = other.vector;
  40. scalar = other.scalar;
  41. return *this;
  42. }
  43. Quaternion& operator*= (Quaternion other) noexcept
  44. {
  45. const Type oldScalar (scalar);
  46. scalar = (scalar * other.scalar) - (vector * other.vector);
  47. vector = (other.vector * oldScalar) + (vector * other.scalar) + (vector ^ other.vector);
  48. return *this;
  49. }
  50. Type length() const noexcept { return std::sqrt (normal()); }
  51. Type normal() const noexcept { return scalar * scalar + vector.lengthSquared(); }
  52. Quaternion normalised() const noexcept
  53. {
  54. const Type len (length());
  55. jassert (len > 0);
  56. return Quaternion (vector / len, scalar / len);
  57. }
  58. /** Returns the matrix that will perform the rotation specified by this quaternion. */
  59. Matrix3D<Type> getRotationMatrix() const noexcept
  60. {
  61. const Type norm (normal());
  62. const Type s (norm > 0 ? ((Type) 2) / norm : 0);
  63. const Type xs (s * vector.x), ys (s * vector.y), zs (s * vector.z);
  64. const Type wx (xs * scalar), wy (ys * scalar), wz (zs * scalar);
  65. const Type xx (xs * vector.x), xy (ys * vector.x), xz (zs * vector.x);
  66. const Type yy (ys * vector.y), yz (zs * vector.y), zz (zs * vector.z);
  67. return Matrix3D<Type> (((Type) 1) - (yy + zz), xy - wz, xz + wy, 0,
  68. xy + wz, ((Type) 1) - (xx+ zz), yz - wx, 0,
  69. xz - wy, yz + wx, ((Type) 1) - (xx + yy), 0,
  70. 0, 0, 0, (Type) 1);
  71. }
  72. /** The vector part of the quaternion. */
  73. Vector3D<Type> vector;
  74. /** The scalar part of the quaternion. */
  75. Type scalar;
  76. };