2
0

aiQuaternion.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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 aiQuaternion.h
  34. * @brief Quaternion structure, including operators when compiling in C++
  35. */
  36. #ifndef AI_QUATERNION_H_INC
  37. #define AI_QUATERNION_H_INC
  38. #include <math.h>
  39. #include "aiTypes.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. // ---------------------------------------------------------------------------
  44. /** Represents a quaternion in a 4D vector. */
  45. struct aiQuaternion
  46. {
  47. #ifdef __cplusplus
  48. aiQuaternion() : w(0.0f), x(0.0f), y(0.0f), z(0.0f) {}
  49. aiQuaternion(float _w, float _x, float _y, float _z) : w(_w), x(_x), y(_y), z(_z) {}
  50. /** Construct from rotation matrix. Result is undefined if the matrix is not orthonormal. */
  51. aiQuaternion( const aiMatrix3x3& pRotMatrix);
  52. /** Construct from euler angles */
  53. aiQuaternion( float rotx, float roty, float rotz);
  54. /** Construct from an axis-angle pair */
  55. aiQuaternion( aiVector3D axis, float angle);
  56. /** Construct from a normalized quaternion stored in a vec3 */
  57. aiQuaternion( aiVector3D normalized);
  58. /** Returns a matrix representation of the quaternion */
  59. aiMatrix3x3 GetMatrix() const;
  60. bool operator== (const aiQuaternion& o) const
  61. {return x == o.x && y == o.y && z == o.z && w == o.w;}
  62. bool operator!= (const aiQuaternion& o) const
  63. {return !(*this == o);}
  64. /** Normalize the quaternion */
  65. aiQuaternion& Normalize();
  66. /** Compute quaternion conjugate */
  67. aiQuaternion& Conjugate ();
  68. /** Rotate a point by this quaternion */
  69. aiVector3D Rotate (const aiVector3D& in);
  70. /** Multiply two quaternions */
  71. aiQuaternion operator* (const aiQuaternion& two) const;
  72. /** Performs a spherical interpolation between two quaternions and writes the result into the third.
  73. * @param pOut Target object to received the interpolated rotation.
  74. * @param pStart Start rotation of the interpolation at factor == 0.
  75. * @param pEnd End rotation, factor == 1.
  76. * @param pFactor Interpolation factor between 0 and 1. Values outside of this range yield undefined results.
  77. */
  78. static void Interpolate( aiQuaternion& pOut, const aiQuaternion& pStart, const aiQuaternion& pEnd, float pFactor);
  79. #endif // __cplusplus
  80. //! w,x,y,z components of the quaternion
  81. float w, x, y, z;
  82. } ;
  83. #ifdef __cplusplus
  84. // ---------------------------------------------------------------------------
  85. // Constructs a quaternion from a rotation matrix
  86. inline aiQuaternion::aiQuaternion( const aiMatrix3x3 &pRotMatrix)
  87. {
  88. float t = 1 + pRotMatrix.a1 + pRotMatrix.b2 + pRotMatrix.c3;
  89. // large enough
  90. if( t > 0.001f)
  91. {
  92. float s = sqrt( t) * 2.0f;
  93. x = (pRotMatrix.c2 - pRotMatrix.b3) / s;
  94. y = (pRotMatrix.a3 - pRotMatrix.c1) / s;
  95. z = (pRotMatrix.b1 - pRotMatrix.a2) / s;
  96. w = 0.25f * s;
  97. } // else we have to check several cases
  98. else if( pRotMatrix.a1 > pRotMatrix.b2 && pRotMatrix.a1 > pRotMatrix.c3 )
  99. {
  100. // Column 0:
  101. float s = sqrt( 1.0f + pRotMatrix.a1 - pRotMatrix.b2 - pRotMatrix.c3) * 2.0f;
  102. x = 0.25f * s;
  103. y = (pRotMatrix.b1 + pRotMatrix.a2) / s;
  104. z = (pRotMatrix.a3 + pRotMatrix.c1) / s;
  105. w = (pRotMatrix.c2 - pRotMatrix.b3) / s;
  106. }
  107. else if( pRotMatrix.b2 > pRotMatrix.c3)
  108. {
  109. // Column 1:
  110. float s = sqrt( 1.0f + pRotMatrix.b2 - pRotMatrix.a1 - pRotMatrix.c3) * 2.0f;
  111. x = (pRotMatrix.b1 + pRotMatrix.a2) / s;
  112. y = 0.25f * s;
  113. z = (pRotMatrix.c2 + pRotMatrix.b3) / s;
  114. w = (pRotMatrix.a3 - pRotMatrix.c1) / s;
  115. } else
  116. {
  117. // Column 2:
  118. float s = sqrt( 1.0f + pRotMatrix.c3 - pRotMatrix.a1 - pRotMatrix.b2) * 2.0f;
  119. x = (pRotMatrix.a3 + pRotMatrix.c1) / s;
  120. y = (pRotMatrix.c2 + pRotMatrix.b3) / s;
  121. z = 0.25f * s;
  122. w = (pRotMatrix.b1 - pRotMatrix.a2) / s;
  123. }
  124. }
  125. // ---------------------------------------------------------------------------
  126. // Construction from euler angles
  127. inline aiQuaternion::aiQuaternion( float fPitch, float fYaw, float fRoll )
  128. {
  129. const float fSinPitch(sin(fPitch*0.5F));
  130. const float fCosPitch(cos(fPitch*0.5F));
  131. const float fSinYaw(sin(fYaw*0.5F));
  132. const float fCosYaw(cos(fYaw*0.5F));
  133. const float fSinRoll(sin(fRoll*0.5F));
  134. const float fCosRoll(cos(fRoll*0.5F));
  135. const float fCosPitchCosYaw(fCosPitch*fCosYaw);
  136. const float fSinPitchSinYaw(fSinPitch*fSinYaw);
  137. x = fSinRoll * fCosPitchCosYaw - fCosRoll * fSinPitchSinYaw;
  138. y = fCosRoll * fSinPitch * fCosYaw + fSinRoll * fCosPitch * fSinYaw;
  139. z = fCosRoll * fCosPitch * fSinYaw - fSinRoll * fSinPitch * fCosYaw;
  140. w = fCosRoll * fCosPitchCosYaw + fSinRoll * fSinPitchSinYaw;
  141. }
  142. // ---------------------------------------------------------------------------
  143. // Returns a matrix representation of the quaternion
  144. inline aiMatrix3x3 aiQuaternion::GetMatrix() const
  145. {
  146. aiMatrix3x3 resMatrix;
  147. resMatrix.a1 = 1.0f - 2.0f * (y * y + z * z);
  148. resMatrix.a2 = 2.0f * (x * y - z * w);
  149. resMatrix.a3 = 2.0f * (x * z + y * w);
  150. resMatrix.b1 = 2.0f * (x * y + z * w);
  151. resMatrix.b2 = 1.0f - 2.0f * (x * x + z * z);
  152. resMatrix.b3 = 2.0f * (y * z - x * w);
  153. resMatrix.c1 = 2.0f * (x * z - y * w);
  154. resMatrix.c2 = 2.0f * (y * z + x * w);
  155. resMatrix.c3 = 1.0f - 2.0f * (x * x + y * y);
  156. return resMatrix;
  157. }
  158. // ---------------------------------------------------------------------------
  159. // Construction from an axis-angle pair
  160. inline aiQuaternion::aiQuaternion( aiVector3D axis, float angle)
  161. {
  162. axis.Normalize();
  163. const float sin_a = sin( angle / 2 );
  164. const float cos_a = cos( angle / 2 );
  165. x = axis.x * sin_a;
  166. y = axis.y * sin_a;
  167. z = axis.z * sin_a;
  168. w = cos_a;
  169. }
  170. // ---------------------------------------------------------------------------
  171. // Construction from am existing, normalized quaternion
  172. inline aiQuaternion::aiQuaternion( aiVector3D normalized)
  173. {
  174. x = normalized.x;
  175. y = normalized.y;
  176. z = normalized.z;
  177. const float t = 1.0f - (x*x) - (y*y) - (z*z);
  178. if (t < 0.0f)
  179. w = 0.0f;
  180. else w = sqrt (t);
  181. }
  182. // ---------------------------------------------------------------------------
  183. // Performs a spherical interpolation between two quaternions
  184. // Implementation adopted from the gmtl project. All others I found on the net fail in some cases.
  185. // Congrats, gmtl!
  186. inline void aiQuaternion::Interpolate( aiQuaternion& pOut, const aiQuaternion& pStart, const aiQuaternion& pEnd, float pFactor)
  187. {
  188. // calc cosine theta
  189. float cosom = pStart.x * pEnd.x + pStart.y * pEnd.y + pStart.z * pEnd.z + pStart.w * pEnd.w;
  190. // adjust signs (if necessary)
  191. aiQuaternion end = pEnd;
  192. if( cosom < 0.0f)
  193. {
  194. cosom = -cosom;
  195. end.x = -end.x; // Reverse all signs
  196. end.y = -end.y;
  197. end.z = -end.z;
  198. end.w = -end.w;
  199. }
  200. // Calculate coefficients
  201. float sclp, sclq;
  202. if( (1.0f - cosom) > 0.0001f) // 0.0001 -> some epsillon
  203. {
  204. // Standard case (slerp)
  205. float omega, sinom;
  206. omega = acos( cosom); // extract theta from dot product's cos theta
  207. sinom = sin( omega);
  208. sclp = sin( (1.0f - pFactor) * omega) / sinom;
  209. sclq = sin( pFactor * omega) / sinom;
  210. } else
  211. {
  212. // Very close, do linear interp (because it's faster)
  213. sclp = 1.0f - pFactor;
  214. sclq = pFactor;
  215. }
  216. pOut.x = sclp * pStart.x + sclq * end.x;
  217. pOut.y = sclp * pStart.y + sclq * end.y;
  218. pOut.z = sclp * pStart.z + sclq * end.z;
  219. pOut.w = sclp * pStart.w + sclq * end.w;
  220. }
  221. // ---------------------------------------------------------------------------
  222. inline aiQuaternion& aiQuaternion::Normalize()
  223. {
  224. // compute the magnitude and divide through it
  225. const float mag = sqrt(x*x + y*y + z*z + w*w);
  226. if (mag)
  227. {
  228. const float invMag = 1.0f/mag;
  229. x *= invMag;
  230. y *= invMag;
  231. z *= invMag;
  232. w *= invMag;
  233. }
  234. return *this;
  235. }
  236. // ---------------------------------------------------------------------------
  237. inline aiQuaternion aiQuaternion::operator* (const aiQuaternion& t) const
  238. {
  239. return aiQuaternion(w*t.w - x*t.x - y*t.y - z*t.z,
  240. w*t.x + x*t.w + y*t.z - z*t.y,
  241. w*t.y + y*t.w + z*t.x - x*t.z,
  242. w*t.z + z*t.w + x*t.y - y*t.x);
  243. }
  244. // ---------------------------------------------------------------------------
  245. inline aiQuaternion& aiQuaternion::Conjugate ()
  246. {
  247. x = -x;
  248. y = -y;
  249. z = -z;
  250. return *this;
  251. }
  252. // ---------------------------------------------------------------------------
  253. inline aiVector3D aiQuaternion::Rotate (const aiVector3D& v)
  254. {
  255. aiQuaternion q2(0.f,v.x,v.y,v.z), q = *this, qinv = q;
  256. q.Conjugate();
  257. q = q*q2*qinv;
  258. return aiVector3D(q.x,q.y,q.z);
  259. }
  260. } // end extern "C"
  261. #endif // __cplusplus
  262. #endif // AI_QUATERNION_H_INC