Quaternion.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. Quaternion& Quaternion::operator*=(Flt f)
  6. {
  7. x*=f;
  8. y*=f;
  9. z*=f;
  10. w*=f;
  11. return T;
  12. }
  13. /******************************************************************************/
  14. void Quaternion::mul(C Quaternion &q, Quaternion &dest)C
  15. {
  16. dest.set(y*q.z - z*q.y + w*q.x + x*q.w,
  17. z*q.x - x*q.z + w*q.y + y*q.w,
  18. x*q.y - y*q.x + w*q.z + z*q.w,
  19. w*q.w - x*q.x - y*q.y - z*q.z);
  20. }
  21. void Quaternion::inverse(Quaternion &dest)C
  22. {
  23. dest.set(-x, -y, -z, w);
  24. dest/=length2();
  25. }
  26. /******************************************************************************/
  27. Quaternion& Quaternion::setRotateX(Flt angle ) {CosSin(w, x, angle*-0.5f); y=z=0; return T;}
  28. Quaternion& Quaternion::setRotateY(Flt angle ) {CosSin(w, y, angle*-0.5f); x=z=0; return T;}
  29. Quaternion& Quaternion::setRotateZ(Flt angle ) {CosSin(w, z, angle*-0.5f); x=y=0; return T;}
  30. Quaternion& Quaternion::setRotate (Flt x, Flt y, Flt z)
  31. {
  32. Flt cos_x_2, sin_x_2; CosSin(cos_x_2, sin_x_2, x*-0.5f);
  33. Flt cos_y_2, sin_y_2; CosSin(cos_y_2, sin_y_2, y*-0.5f);
  34. Flt cos_z_2, sin_z_2; CosSin(cos_z_2, sin_z_2, z*-0.5f);
  35. T.x = cos_z_2*cos_y_2*sin_x_2 - sin_z_2*sin_y_2*cos_x_2;
  36. T.y = cos_z_2*sin_y_2*cos_x_2 + sin_z_2*cos_y_2*sin_x_2;
  37. T.z = sin_z_2*cos_y_2*cos_x_2 - cos_z_2*sin_y_2*sin_x_2;
  38. T.w = cos_z_2*cos_y_2*cos_x_2 + sin_z_2*sin_y_2*sin_x_2;
  39. return T;
  40. }
  41. Quaternion& Quaternion::setRotate(C Vec &axis, Flt angle)
  42. {
  43. Flt sin; CosSin(w, sin, angle*-0.5f);
  44. xyz=axis*sin;
  45. return T;
  46. }
  47. /******************************************************************************/
  48. Flt Quaternion::angle()C
  49. {
  50. return Acos(w)*2;
  51. }
  52. Vec Quaternion::axis()C
  53. {
  54. Vec O=-xyz; O.normalize(); return O;
  55. }
  56. /******************************************************************************/
  57. Orient::Orient(C Quaternion &q)
  58. {
  59. Flt xx=q.x*q.x,
  60. xy=q.x*q.y,
  61. xz=q.x*q.z,
  62. xw=q.x*q.w,
  63. yy=q.y*q.y,
  64. yz=q.y*q.z,
  65. yw=q.y*q.w,
  66. zz=q.z*q.z,
  67. zw=q.z*q.w;
  68. perp.x= 2*(xy+zw);
  69. perp.y=1-2*(xx+zz);
  70. perp.z= 2*(yz-xw);
  71. dir.x= 2*(xz-yw);
  72. dir.y= 2*(yz+xw);
  73. dir.z=1-2*(xx+yy);
  74. perp.normalize();
  75. dir .normalize();
  76. }
  77. Matrix3::Matrix3(C Quaternion &q)
  78. {
  79. Flt xx=q.x*q.x,
  80. xy=q.x*q.y,
  81. xz=q.x*q.z,
  82. xw=q.x*q.w,
  83. yy=q.y*q.y,
  84. yz=q.y*q.z,
  85. yw=q.y*q.w,
  86. zz=q.z*q.z,
  87. zw=q.z*q.w;
  88. T.x.x=1-2*(yy+zz);
  89. T.x.y= 2*(xy-zw);
  90. T.x.z= 2*(xz+yw);
  91. T.y.x= 2*(xy+zw);
  92. T.y.y=1-2*(xx+zz);
  93. T.y.z= 2*(yz-xw);
  94. T.z.x= 2*(xz-yw);
  95. T.z.y= 2*(yz+xw);
  96. T.z.z=1-2*(xx+yy);
  97. normalize();
  98. }
  99. /******************************************************************************/
  100. Quaternion::Quaternion(C Matrix3 &m)
  101. {
  102. Flt f=m.x.x+m.y.y+m.z.z+1;
  103. if( f>0.4f)
  104. {
  105. w=0.5f *Sqrt(f);
  106. f=0.25f/w;
  107. x=(m.z.y-m.y.z)*f;
  108. y=(m.x.z-m.z.x)*f;
  109. z=(m.y.x-m.x.y)*f;
  110. }else
  111. if(m.x.x>m.y.y && m.x.x>m.z.z)
  112. {
  113. x=-0.5f *Sqrt(1+m.x.x-m.y.y-m.z.z);
  114. f= 0.25f/x;
  115. y=(m.x.y+m.y.x)*f;
  116. z=(m.x.z+m.z.x)*f;
  117. w=(m.z.y-m.y.z)*f;
  118. }else
  119. if(m.y.y>m.z.z)
  120. {
  121. y=-0.5f *Sqrt(1+m.y.y-m.x.x-m.z.z);
  122. f= 0.25f/y;
  123. x=(m.x.y+m.y.x)*f;
  124. z=(m.y.z+m.z.y)*f;
  125. w=(m.x.z-m.z.x)*f;
  126. }else
  127. {
  128. z=-0.5f *Sqrt(1+m.z.z-m.x.x-m.y.y);
  129. f= 0.25f/z;
  130. x=(m.x.z+m.z.x)*f;
  131. y=(m.y.z+m.z.y)*f;
  132. w=(m.y.x-m.x.y)*f;
  133. }
  134. }
  135. /******************************************************************************/
  136. static Quaternion Log(C Quaternion &q)
  137. {
  138. Flt length=q.xyz.length();
  139. if( length<=EPS)return Vec4(q.xyz , 0);
  140. else return Vec4(q.xyz*(Acos(q.w)/length), 0);
  141. }
  142. static Quaternion Exp(C Quaternion &q)
  143. {
  144. Flt length=q.xyz.length();
  145. if( length<=EPS)return Vec4(q.xyz , Cos(length));
  146. else return Vec4(q.xyz*(Sin(length)/length), Cos(length));
  147. }
  148. Quaternion GetTangent(C Quaternion &prev, C Quaternion &cur, C Quaternion &next)
  149. {
  150. Quaternion a=cur; a.xyz.chs();
  151. Quaternion b=a ;
  152. a=Log(a*=prev)
  153. +Log(b*=next);
  154. a*=-0.25f;
  155. Quaternion O; cur.mul(Exp(a), O); return O;
  156. }
  157. /******************************************************************************/
  158. static Quaternion SlerpNoInv(C Quaternion &a, C Quaternion &b, Flt step)
  159. {
  160. Quaternion O;
  161. Flt dot=Dot(a, b);
  162. if(Abs(dot)>=0.99f)O=Lerp(a, b, step);else // if angle is small then use linear interpolation
  163. {
  164. Flt angle=Acos(dot ),
  165. sin =Sin (angle);
  166. O=a*(Sin(angle*(1-step))/sin) + b*(Sin(angle*step)/sin);
  167. }
  168. O.normalize();
  169. return O;
  170. }
  171. /******************************************************************************/
  172. Quaternion Slerp(C Quaternion &a, C Quaternion &b, Flt step)
  173. {
  174. Quaternion O, temp=b;
  175. Flt dot=Dot(a, b);
  176. if( dot<0) // other side
  177. {
  178. CHS(dot);
  179. temp.chs();
  180. }
  181. if(dot>=0.99f)O=Lerp(a, temp, step);else // if angle is small then use linear interpolation
  182. {
  183. Flt angle=Acos(dot ),
  184. sin =Sin (angle);
  185. O=a*(Sin(angle*(1-step))/sin) + temp*(Sin(angle*step)/sin);
  186. }
  187. O.normalize();
  188. return O;
  189. }
  190. /******************************************************************************/
  191. Quaternion Squad(C Quaternion &from, C Quaternion &to, C Quaternion &tan0, C Quaternion &tan1, Flt step)
  192. {
  193. return SlerpNoInv(SlerpNoInv(from, to , step),
  194. SlerpNoInv(tan0, tan1, step), 2*step*(1-step));
  195. }
  196. /******************************************************************************/
  197. }
  198. /******************************************************************************/