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.

101 lines
4.0KB

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