quat_to_mat.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Name history:
  2. // quat2mat until 16 Sept 2011
  3. namespace igl
  4. {
  5. // Convert a quaternion to a 4x4 matrix
  6. // A Quaternion, q, is defined here as an arrays of four scalars (x,y,z,w),
  7. // such that q = x*i + y*j + z*k + w
  8. // Input:
  9. // quat pointer to four elements of quaternion (x,y,z,w)
  10. // Output:
  11. // mat pointer to 16 elements of matrix
  12. template <typename Q_type>
  13. void quat_to_mat(const Q_type * quat, Q_type * mat);
  14. }
  15. // Implementation
  16. template <typename Q_type>
  17. void igl::quat_to_mat(const Q_type * quat, Q_type * mat)
  18. {
  19. Q_type yy2 = 2.0f * quat[1] * quat[1];
  20. Q_type xy2 = 2.0f * quat[0] * quat[1];
  21. Q_type xz2 = 2.0f * quat[0] * quat[2];
  22. Q_type yz2 = 2.0f * quat[1] * quat[2];
  23. Q_type zz2 = 2.0f * quat[2] * quat[2];
  24. Q_type wz2 = 2.0f * quat[3] * quat[2];
  25. Q_type wy2 = 2.0f * quat[3] * quat[1];
  26. Q_type wx2 = 2.0f * quat[3] * quat[0];
  27. Q_type xx2 = 2.0f * quat[0] * quat[0];
  28. mat[0*4+0] = - yy2 - zz2 + 1.0f;
  29. mat[0*4+1] = xy2 + wz2;
  30. mat[0*4+2] = xz2 - wy2;
  31. mat[0*4+3] = 0;
  32. mat[1*4+0] = xy2 - wz2;
  33. mat[1*4+1] = - xx2 - zz2 + 1.0f;
  34. mat[1*4+2] = yz2 + wx2;
  35. mat[1*4+3] = 0;
  36. mat[2*4+0] = xz2 + wy2;
  37. mat[2*4+1] = yz2 - wx2;
  38. mat[2*4+2] = - xx2 - yy2 + 1.0f;
  39. mat[2*4+3] = 0;
  40. mat[3*4+0] = mat[3*4+1] = mat[3*4+2] = 0;
  41. mat[3*4+3] = 1;
  42. }