quat_to_mat.h 1.3 KB

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