mat_to_quat.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "mat_to_quat.h"
  9. #include <cmath>
  10. // This could be replaced by something fast
  11. template <typename Q_type>
  12. static inline Q_type ReciprocalSqrt( const Q_type x )
  13. {
  14. return 1.0/sqrt(x);
  15. }
  16. //// Converts row major order matrix to quat
  17. //// http://software.intel.com/sites/default/files/m/d/4/1/d/8/293748.pdf
  18. //template <typename Q_type>
  19. //IGL_INLINE void igl::mat4_to_quat(const Q_type * m, Q_type * q)
  20. //{
  21. // Q_type t = + m[0 * 4 + 0] + m[1 * 4 + 1] + m[2 * 4 + 2] + 1.0f;
  22. // Q_type s = ReciprocalSqrt( t ) * 0.5f;
  23. // q[3] = s * t;
  24. // q[2] = ( m[0 * 4 + 1] - m[1 * 4 + 0] ) * s;
  25. // q[1] = ( m[2 * 4 + 0] - m[0 * 4 + 2] ) * s;
  26. // q[0] = ( m[1 * 4 + 2] - m[2 * 4 + 1] ) * s;
  27. //}
  28. // https://bmgame.googlecode.com/svn/idlib/math/Simd_AltiVec.cpp
  29. template <typename Q_type>
  30. IGL_INLINE void igl::mat4_to_quat(const Q_type * mat, Q_type * q)
  31. {
  32. Q_type trace;
  33. Q_type s;
  34. Q_type t;
  35. int i;
  36. int j;
  37. int k;
  38. static int next[3] = { 1, 2, 0 };
  39. trace = mat[0 * 4 + 0] + mat[1 * 4 + 1] + mat[2 * 4 + 2];
  40. if ( trace > 0.0f ) {
  41. t = trace + 1.0f;
  42. s = ReciprocalSqrt( t ) * 0.5f;
  43. q[3] = s * t;
  44. q[0] = ( mat[1 * 4 + 2] - mat[2 * 4 + 1] ) * s;
  45. q[1] = ( mat[2 * 4 + 0] - mat[0 * 4 + 2] ) * s;
  46. q[2] = ( mat[0 * 4 + 1] - mat[1 * 4 + 0] ) * s;
  47. } else {
  48. i = 0;
  49. if ( mat[1 * 4 + 1] > mat[0 * 4 + 0] ) {
  50. i = 1;
  51. }
  52. if ( mat[2 * 4 + 2] > mat[i * 4 + i] ) {
  53. i = 2;
  54. }
  55. j = next[i];
  56. k = next[j];
  57. t = ( mat[i * 4 + i] - ( mat[j * 4 + j] + mat[k * 4 + k] ) ) + 1.0f;
  58. s = ReciprocalSqrt( t ) * 0.5f;
  59. q[i] = s * t;
  60. q[3] = ( mat[j * 4 + k] - mat[k * 4 + j] ) * s;
  61. q[j] = ( mat[i * 4 + j] + mat[j * 4 + i] ) * s;
  62. q[k] = ( mat[i * 4 + k] + mat[k * 4 + i] ) * s;
  63. }
  64. //// Unused translation
  65. //jq.t[0] = mat[0 * 4 + 3];
  66. //jq.t[1] = mat[1 * 4 + 3];
  67. //jq.t[2] = mat[2 * 4 + 3];
  68. }
  69. template <typename Q_type>
  70. IGL_INLINE void igl::mat3_to_quat(const Q_type * mat, Q_type * q)
  71. {
  72. Q_type trace;
  73. Q_type s;
  74. Q_type t;
  75. int i;
  76. int j;
  77. int k;
  78. static int next[3] = { 1, 2, 0 };
  79. trace = mat[0 * 3 + 0] + mat[1 * 3 + 1] + mat[2 * 3 + 2];
  80. if ( trace > 0.0f ) {
  81. t = trace + 1.0f;
  82. s = ReciprocalSqrt( t ) * 0.5f;
  83. q[3] = s * t;
  84. q[0] = ( mat[1 * 3 + 2] - mat[2 * 3 + 1] ) * s;
  85. q[1] = ( mat[2 * 3 + 0] - mat[0 * 3 + 2] ) * s;
  86. q[2] = ( mat[0 * 3 + 1] - mat[1 * 3 + 0] ) * s;
  87. } else {
  88. i = 0;
  89. if ( mat[1 * 3 + 1] > mat[0 * 3 + 0] ) {
  90. i = 1;
  91. }
  92. if ( mat[2 * 3 + 2] > mat[i * 3 + i] ) {
  93. i = 2;
  94. }
  95. j = next[i];
  96. k = next[j];
  97. t = ( mat[i * 3 + i] - ( mat[j * 3 + j] + mat[k * 3 + k] ) ) + 1.0f;
  98. s = ReciprocalSqrt( t ) * 0.5f;
  99. q[i] = s * t;
  100. q[3] = ( mat[j * 3 + k] - mat[k * 3 + j] ) * s;
  101. q[j] = ( mat[i * 3 + j] + mat[j * 3 + i] ) * s;
  102. q[k] = ( mat[i * 3 + k] + mat[k * 3 + i] ) * s;
  103. }
  104. //// Unused translation
  105. //jq.t[0] = mat[0 * 4 + 3];
  106. //jq.t[1] = mat[1 * 4 + 3];
  107. //jq.t[2] = mat[2 * 4 + 3];
  108. }
  109. #ifdef IGL_STATIC_LIBRARY
  110. // Explicit template specialization
  111. template void igl::mat4_to_quat<double>(double const*, double*);
  112. template void igl::mat4_to_quat<float>(float const*, float*);
  113. template void igl::mat3_to_quat<double>(double const*, double*);
  114. #endif