quat_to_mat.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. void quat_to_mat(const float * quat, float * mat);
  13. }
  14. // Implementation
  15. void igl::quat_to_mat(const float * quat, float * mat)
  16. {
  17. float yy2 = 2.0f * quat[1] * quat[1];
  18. float xy2 = 2.0f * quat[0] * quat[1];
  19. float xz2 = 2.0f * quat[0] * quat[2];
  20. float yz2 = 2.0f * quat[1] * quat[2];
  21. float zz2 = 2.0f * quat[2] * quat[2];
  22. float wz2 = 2.0f * quat[3] * quat[2];
  23. float wy2 = 2.0f * quat[3] * quat[1];
  24. float wx2 = 2.0f * quat[3] * quat[0];
  25. float xx2 = 2.0f * quat[0] * quat[0];
  26. mat[0*4+0] = - yy2 - zz2 + 1.0f;
  27. mat[0*4+1] = xy2 + wz2;
  28. mat[0*4+2] = xz2 - wy2;
  29. mat[0*4+3] = 0;
  30. mat[1*4+0] = xy2 - wz2;
  31. mat[1*4+1] = - xx2 - zz2 + 1.0f;
  32. mat[1*4+2] = yz2 + wx2;
  33. mat[1*4+3] = 0;
  34. mat[2*4+0] = xz2 + wy2;
  35. mat[2*4+1] = yz2 - wx2;
  36. mat[2*4+2] = - xx2 - yy2 + 1.0f;
  37. mat[2*4+3] = 0;
  38. mat[3*4+0] = mat[3*4+1] = mat[3*4+2] = 0;
  39. mat[3*4+3] = 1;
  40. }