quaternion.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file quaternion.h
  34. * @brief Quaternion structure, including operators when compiling in C++
  35. */
  36. #pragma once
  37. #ifndef AI_QUATERNION_H_INC
  38. #define AI_QUATERNION_H_INC
  39. #include <assimp/defs.h>
  40. #ifdef __cplusplus
  41. #ifdef __GNUC__
  42. # pragma GCC system_header
  43. #endif
  44. // Forward declarations
  45. template <typename TReal> class aiVector3t;
  46. template <typename TReal> class aiMatrix3x3t;
  47. template <typename TReal> class aiMatrix4x4t;
  48. // ---------------------------------------------------------------------------
  49. /**
  50. * @brief This class represents a quaternion as a 4D vector.
  51. */
  52. template <typename TReal>
  53. class aiQuaterniont {
  54. public:
  55. aiQuaterniont() AI_NO_EXCEPT : w(1.0), x(), y(), z() {}
  56. aiQuaterniont(TReal pw, TReal px, TReal py, TReal pz)
  57. : w(pw), x(px), y(py), z(pz) {}
  58. /**
  59. * @brief Construct from rotation matrix. Result is undefined if the matrix is not orthonormal.
  60. */
  61. explicit aiQuaterniont( const aiMatrix3x3t<TReal>& pRotMatrix);
  62. /** Construct from euler angles */
  63. aiQuaterniont( TReal roty, TReal rotz, TReal rotx);
  64. /** Construct from an axis-angle pair */
  65. aiQuaterniont( aiVector3t<TReal> axis, TReal angle);
  66. /** Construct from a normalized quaternion stored in a vec3 */
  67. explicit aiQuaterniont( aiVector3t<TReal> normalized);
  68. /** Returns a matrix representation of the quaternion */
  69. aiMatrix3x3t<TReal> GetMatrix() const;
  70. bool operator== (const aiQuaterniont& o) const;
  71. bool operator!= (const aiQuaterniont& o) const;
  72. // transform vector by matrix
  73. aiQuaterniont& operator *= (const aiMatrix4x4t<TReal>& mat);
  74. bool Equal(const aiQuaterniont &o, TReal epsilon = ai_epsilon) const;
  75. /**
  76. * @brief Will normalize the quaternion representation.
  77. */
  78. aiQuaterniont& Normalize();
  79. /**
  80. * @brief Will compute the quaternion conjugate. The result will be stored in the instance.
  81. */
  82. aiQuaterniont& Conjugate();
  83. /**
  84. * @brief Rotate a point by this quaternion
  85. */
  86. aiVector3t<TReal> Rotate(const aiVector3t<TReal>& in) const;
  87. /**
  88. * @brief Multiply two quaternions
  89. * @param two The other quaternion.
  90. * @return The result of the multiplication.
  91. */
  92. aiQuaterniont operator * (const aiQuaterniont& two) const;
  93. /**
  94. * @brief Performs a spherical interpolation between two quaternions and writes the result into the third.
  95. * @param pOut Target object to received the interpolated rotation.
  96. * @param pStart Start rotation of the interpolation at factor == 0.
  97. * @param pEnd End rotation, factor == 1.
  98. * @param pFactor Interpolation factor between 0 and 1. Values outside of this range yield undefined results.
  99. */
  100. static void Interpolate( aiQuaterniont& pOut, const aiQuaterniont& pStart,
  101. const aiQuaterniont& pEnd, TReal pFactor);
  102. //! w,x,y,z components of the quaternion
  103. TReal w, x, y, z;
  104. } ;
  105. using aiQuaternion = aiQuaterniont<ai_real>;
  106. #else
  107. struct aiQuaternion {
  108. ai_real w, x, y, z;
  109. };
  110. #endif
  111. #endif // AI_QUATERNION_H_INC