mat_to_quat.cpp 803 B

12345678910111213141516171819202122232425262728
  1. #include "mat_to_quat.h"
  2. #include <cmath>
  3. // This could be replaced by something fast
  4. template <typename Q_type>
  5. static inline Q_type ReciprocalSqrt( const Q_type x )
  6. {
  7. return 1.0/sqrt(x);
  8. }
  9. // Converts row major order matrix to quat
  10. // http://software.intel.com/sites/default/files/m/d/4/1/d/8/293748.pdf
  11. template <typename Q_type>
  12. IGL_INLINE void igl::mat4_to_quat(const Q_type * m, Q_type * q)
  13. {
  14. Q_type t = + m[0 * 4 + 0] + m[1 * 4 + 1] + m[2 * 4 + 2] + 1.0f;
  15. Q_type s = ReciprocalSqrt( t ) * 0.5f;
  16. q[3] = s * t;
  17. q[2] = ( m[0 * 4 + 1] - m[1 * 4 + 0] ) * s;
  18. q[1] = ( m[2 * 4 + 0] - m[0 * 4 + 2] ) * s;
  19. q[0] = ( m[1 * 4 + 2] - m[2 * 4 + 1] ) * s;
  20. }
  21. #ifndef IGL_HEADER_ONLY
  22. // Explicit template specialization
  23. template void igl::mat4_to_quat<double>(double const*, double*);
  24. #endif