mat_to_quat.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // https://bmgame.googlecode.com/svn/idlib/math/Simd_AltiVec.cpp
  22. template <typename Q_type>
  23. IGL_INLINE void igl::mat4_to_quat(const Q_type * mat, Q_type * q)
  24. {
  25. Q_type trace;
  26. Q_type s;
  27. Q_type t;
  28. int i;
  29. int j;
  30. int k;
  31. static int next[3] = { 1, 2, 0 };
  32. trace = mat[0 * 4 + 0] + mat[1 * 4 + 1] + mat[2 * 4 + 2];
  33. if ( trace > 0.0f ) {
  34. t = trace + 1.0f;
  35. s = ReciprocalSqrt( t ) * 0.5f;
  36. q[3] = s * t;
  37. q[0] = ( mat[1 * 4 + 2] - mat[2 * 4 + 1] ) * s;
  38. q[1] = ( mat[2 * 4 + 0] - mat[0 * 4 + 2] ) * s;
  39. q[2] = ( mat[0 * 4 + 1] - mat[1 * 4 + 0] ) * s;
  40. } else {
  41. i = 0;
  42. if ( mat[1 * 4 + 1] > mat[0 * 4 + 0] ) {
  43. i = 1;
  44. }
  45. if ( mat[2 * 4 + 2] > mat[i * 4 + i] ) {
  46. i = 2;
  47. }
  48. j = next[i];
  49. k = next[j];
  50. t = ( mat[i * 4 + i] - ( mat[j * 4 + j] + mat[k * 4 + k] ) ) + 1.0f;
  51. s = ReciprocalSqrt( t ) * 0.5f;
  52. q[i] = s * t;
  53. q[3] = ( mat[j * 4 + k] - mat[k * 4 + j] ) * s;
  54. q[j] = ( mat[i * 4 + j] + mat[j * 4 + i] ) * s;
  55. q[k] = ( mat[i * 4 + k] + mat[k * 4 + i] ) * s;
  56. }
  57. //// Unused translation
  58. //jq.t[0] = mat[0 * 4 + 3];
  59. //jq.t[1] = mat[1 * 4 + 3];
  60. //jq.t[2] = mat[2 * 4 + 3];
  61. }
  62. template <typename Q_type>
  63. IGL_INLINE void igl::mat3_to_quat(const Q_type * mat, Q_type * q)
  64. {
  65. Q_type trace;
  66. Q_type s;
  67. Q_type t;
  68. int i;
  69. int j;
  70. int k;
  71. static int next[3] = { 1, 2, 0 };
  72. trace = mat[0 * 3 + 0] + mat[1 * 3 + 1] + mat[2 * 3 + 2];
  73. if ( trace > 0.0f ) {
  74. t = trace + 1.0f;
  75. s = ReciprocalSqrt( t ) * 0.5f;
  76. q[3] = s * t;
  77. q[0] = ( mat[1 * 3 + 2] - mat[2 * 3 + 1] ) * s;
  78. q[1] = ( mat[2 * 3 + 0] - mat[0 * 3 + 2] ) * s;
  79. q[2] = ( mat[0 * 3 + 1] - mat[1 * 3 + 0] ) * s;
  80. } else {
  81. i = 0;
  82. if ( mat[1 * 3 + 1] > mat[0 * 3 + 0] ) {
  83. i = 1;
  84. }
  85. if ( mat[2 * 3 + 2] > mat[i * 3 + i] ) {
  86. i = 2;
  87. }
  88. j = next[i];
  89. k = next[j];
  90. t = ( mat[i * 3 + i] - ( mat[j * 3 + j] + mat[k * 3 + k] ) ) + 1.0f;
  91. s = ReciprocalSqrt( t ) * 0.5f;
  92. q[i] = s * t;
  93. q[3] = ( mat[j * 3 + k] - mat[k * 3 + j] ) * s;
  94. q[j] = ( mat[i * 3 + j] + mat[j * 3 + i] ) * s;
  95. q[k] = ( mat[i * 3 + k] + mat[k * 3 + i] ) * s;
  96. }
  97. //// Unused translation
  98. //jq.t[0] = mat[0 * 4 + 3];
  99. //jq.t[1] = mat[1 * 4 + 3];
  100. //jq.t[2] = mat[2 * 4 + 3];
  101. }
  102. #ifndef IGL_HEADER_ONLY
  103. // Explicit template specialization
  104. template void igl::mat4_to_quat<double>(double const*, double*);
  105. template void igl::mat4_to_quat<float>(float const*, float*);
  106. template void igl::mat3_to_quat<double>(double const*, double*);
  107. #endif