quaternion.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Quaternion arithmetic:
  3. * qadd(q, r) returns q+r
  4. * qsub(q, r) returns q-r
  5. * qneg(q) returns -q
  6. * qmul(q, r) returns q*r
  7. * qdiv(q, r) returns q/r, can divide check.
  8. * qinv(q) returns 1/q, can divide check.
  9. * double qlen(p) returns modulus of p
  10. * qunit(q) returns a unit quaternion parallel to q
  11. * The following only work on unit quaternions and rotation matrices:
  12. * slerp(q, r, a) returns q*(r*q^-1)^a
  13. * qmid(q, r) slerp(q, r, .5)
  14. * qsqrt(q) qmid(q, (Quaternion){1,0,0,0})
  15. * qtom(m, q) converts a unit quaternion q into a rotation matrix m
  16. * mtoq(m) returns a quaternion equivalent to a rotation matrix m
  17. */
  18. #include <u.h>
  19. #include <libc.h>
  20. #include <draw.h>
  21. #include <geometry.h>
  22. void qtom(Matrix m, Quaternion q){
  23. #ifndef new
  24. m[0][0]=1-2*(q.j*q.j+q.k*q.k);
  25. m[0][1]=2*(q.i*q.j+q.r*q.k);
  26. m[0][2]=2*(q.i*q.k-q.r*q.j);
  27. m[0][3]=0;
  28. m[1][0]=2*(q.i*q.j-q.r*q.k);
  29. m[1][1]=1-2*(q.i*q.i+q.k*q.k);
  30. m[1][2]=2*(q.j*q.k+q.r*q.i);
  31. m[1][3]=0;
  32. m[2][0]=2*(q.i*q.k+q.r*q.j);
  33. m[2][1]=2*(q.j*q.k-q.r*q.i);
  34. m[2][2]=1-2*(q.i*q.i+q.j*q.j);
  35. m[2][3]=0;
  36. m[3][0]=0;
  37. m[3][1]=0;
  38. m[3][2]=0;
  39. m[3][3]=1;
  40. #else
  41. /*
  42. * Transcribed from Ken Shoemake's new code -- not known to work
  43. */
  44. double Nq = q.r*q.r+q.i*q.i+q.j*q.j+q.k*q.k;
  45. double s = (Nq > 0.0) ? (2.0 / Nq) : 0.0;
  46. double xs = q.i*s, ys = q.j*s, zs = q.k*s;
  47. double wx = q.r*xs, wy = q.r*ys, wz = q.r*zs;
  48. double xx = q.i*xs, xy = q.i*ys, xz = q.i*zs;
  49. double yy = q.j*ys, yz = q.j*zs, zz = q.k*zs;
  50. m[0][0] = 1.0 - (yy + zz); m[1][0] = xy + wz; m[2][0] = xz - wy;
  51. m[0][1] = xy - wz; m[1][1] = 1.0 - (xx + zz); m[2][1] = yz + wx;
  52. m[0][2] = xz + wy; m[1][2] = yz - wx; m[2][2] = 1.0 - (xx + yy);
  53. m[0][3] = m[1][3] = m[2][3] = m[3][0] = m[3][1] = m[3][2] = 0.0;
  54. m[3][3] = 1.0;
  55. #endif
  56. }
  57. Quaternion mtoq(Matrix mat){
  58. #ifndef new
  59. #define EPS 1.387778780781445675529539585113525e-17 /* 2^-56 */
  60. double t;
  61. Quaternion q;
  62. q.r=0.;
  63. q.i=0.;
  64. q.j=0.;
  65. q.k=1.;
  66. if((t=.25*(1+mat[0][0]+mat[1][1]+mat[2][2]))>EPS){
  67. q.r=sqrt(t);
  68. t=4*q.r;
  69. q.i=(mat[1][2]-mat[2][1])/t;
  70. q.j=(mat[2][0]-mat[0][2])/t;
  71. q.k=(mat[0][1]-mat[1][0])/t;
  72. }
  73. else if((t=-.5*(mat[1][1]+mat[2][2]))>EPS){
  74. q.i=sqrt(t);
  75. t=2*q.i;
  76. q.j=mat[0][1]/t;
  77. q.k=mat[0][2]/t;
  78. }
  79. else if((t=.5*(1-mat[2][2]))>EPS){
  80. q.j=sqrt(t);
  81. q.k=mat[1][2]/(2*q.j);
  82. }
  83. return q;
  84. #else
  85. /*
  86. * Transcribed from Ken Shoemake's new code -- not known to work
  87. */
  88. /* This algorithm avoids near-zero divides by looking for a large
  89. * component -- first r, then i, j, or k. When the trace is greater than zero,
  90. * |r| is greater than 1/2, which is as small as a largest component can be.
  91. * Otherwise, the largest diagonal entry corresponds to the largest of |i|,
  92. * |j|, or |k|, one of which must be larger than |r|, and at least 1/2.
  93. */
  94. Quaternion qu;
  95. double tr, s;
  96. tr = mat[0][0] + mat[1][1] + mat[2][2];
  97. if (tr >= 0.0) {
  98. s = sqrt(tr + mat[3][3]);
  99. qu.r = s*0.5;
  100. s = 0.5 / s;
  101. qu.i = (mat[2][1] - mat[1][2]) * s;
  102. qu.j = (mat[0][2] - mat[2][0]) * s;
  103. qu.k = (mat[1][0] - mat[0][1]) * s;
  104. }
  105. else {
  106. int i = 0;
  107. if (mat[1][1] > mat[0][0]) i = 1;
  108. if (mat[2][2] > mat[i][i]) i = 2;
  109. switch(i){
  110. case 0:
  111. s = sqrt( (mat[0][0] - (mat[1][1]+mat[2][2])) + mat[3][3] );
  112. qu.i = s*0.5;
  113. s = 0.5 / s;
  114. qu.j = (mat[0][1] + mat[1][0]) * s;
  115. qu.k = (mat[2][0] + mat[0][2]) * s;
  116. qu.r = (mat[2][1] - mat[1][2]) * s;
  117. break;
  118. case 1:
  119. s = sqrt( (mat[1][1] - (mat[2][2]+mat[0][0])) + mat[3][3] );
  120. qu.j = s*0.5;
  121. s = 0.5 / s;
  122. qu.k = (mat[1][2] + mat[2][1]) * s;
  123. qu.i = (mat[0][1] + mat[1][0]) * s;
  124. qu.r = (mat[0][2] - mat[2][0]) * s;
  125. break;
  126. case 2:
  127. s = sqrt( (mat[2][2] - (mat[0][0]+mat[1][1])) + mat[3][3] );
  128. qu.k = s*0.5;
  129. s = 0.5 / s;
  130. qu.i = (mat[2][0] + mat[0][2]) * s;
  131. qu.j = (mat[1][2] + mat[2][1]) * s;
  132. qu.r = (mat[1][0] - mat[0][1]) * s;
  133. break;
  134. }
  135. }
  136. if (mat[3][3] != 1.0){
  137. s=1/sqrt(mat[3][3]);
  138. qu.r*=s;
  139. qu.i*=s;
  140. qu.j*=s;
  141. qu.k*=s;
  142. }
  143. return (qu);
  144. #endif
  145. }
  146. Quaternion qadd(Quaternion q, Quaternion r){
  147. q.r+=r.r;
  148. q.i+=r.i;
  149. q.j+=r.j;
  150. q.k+=r.k;
  151. return q;
  152. }
  153. Quaternion qsub(Quaternion q, Quaternion r){
  154. q.r-=r.r;
  155. q.i-=r.i;
  156. q.j-=r.j;
  157. q.k-=r.k;
  158. return q;
  159. }
  160. Quaternion qneg(Quaternion q){
  161. q.r=-q.r;
  162. q.i=-q.i;
  163. q.j=-q.j;
  164. q.k=-q.k;
  165. return q;
  166. }
  167. Quaternion qmul(Quaternion q, Quaternion r){
  168. Quaternion s;
  169. s.r=q.r*r.r-q.i*r.i-q.j*r.j-q.k*r.k;
  170. s.i=q.r*r.i+r.r*q.i+q.j*r.k-q.k*r.j;
  171. s.j=q.r*r.j+r.r*q.j+q.k*r.i-q.i*r.k;
  172. s.k=q.r*r.k+r.r*q.k+q.i*r.j-q.j*r.i;
  173. return s;
  174. }
  175. Quaternion qdiv(Quaternion q, Quaternion r){
  176. return qmul(q, qinv(r));
  177. }
  178. Quaternion qunit(Quaternion q){
  179. double l=qlen(q);
  180. q.r/=l;
  181. q.i/=l;
  182. q.j/=l;
  183. q.k/=l;
  184. return q;
  185. }
  186. /*
  187. * Bug?: takes no action on divide check
  188. */
  189. Quaternion qinv(Quaternion q){
  190. double l=q.r*q.r+q.i*q.i+q.j*q.j+q.k*q.k;
  191. q.r/=l;
  192. q.i=-q.i/l;
  193. q.j=-q.j/l;
  194. q.k=-q.k/l;
  195. return q;
  196. }
  197. double qlen(Quaternion p){
  198. return sqrt(p.r*p.r+p.i*p.i+p.j*p.j+p.k*p.k);
  199. }
  200. Quaternion slerp(Quaternion q, Quaternion r, double a){
  201. double u, v, ang, s;
  202. double dot=q.r*r.r+q.i*r.i+q.j*r.j+q.k*r.k;
  203. ang=dot<-1?PI:dot>1?0:acos(dot); /* acos gives NaN for dot slightly out of range */
  204. s=sin(ang);
  205. if(s==0) return ang<PI/2?q:r;
  206. u=sin((1-a)*ang)/s;
  207. v=sin(a*ang)/s;
  208. q.r=u*q.r+v*r.r;
  209. q.i=u*q.i+v*r.i;
  210. q.j=u*q.j+v*r.j;
  211. q.k=u*q.k+v*r.k;
  212. return q;
  213. }
  214. /*
  215. * Only works if qlen(q)==qlen(r)==1
  216. */
  217. Quaternion qmid(Quaternion q, Quaternion r){
  218. double l;
  219. q=qadd(q, r);
  220. l=qlen(q);
  221. if(l<1e-12){
  222. q.r=r.i;
  223. q.i=-r.r;
  224. q.j=r.k;
  225. q.k=-r.j;
  226. }
  227. else{
  228. q.r/=l;
  229. q.i/=l;
  230. q.j/=l;
  231. q.k/=l;
  232. }
  233. return q;
  234. }
  235. /*
  236. * Only works if qlen(q)==1
  237. */
  238. static Quaternion qident={1,0,0,0};
  239. Quaternion qsqrt(Quaternion q){
  240. return qmid(q, qident);
  241. }