matrix_decompose.inl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /// @ref gtx_matrix_decompose
  2. #include "../gtc/constants.hpp"
  3. #include "../gtc/epsilon.hpp"
  4. namespace glm{
  5. namespace detail
  6. {
  7. /// Make a linear combination of two vectors and return the result.
  8. // result = (a * ascl) + (b * bscl)
  9. template<typename T, qualifier Q>
  10. GLM_FUNC_QUALIFIER vec<3, T, Q> combine(
  11. vec<3, T, Q> const& a,
  12. vec<3, T, Q> const& b,
  13. T ascl, T bscl)
  14. {
  15. return (a * ascl) + (b * bscl);
  16. }
  17. template<typename T, qualifier Q>
  18. GLM_FUNC_QUALIFIER vec<3, T, Q> scale(vec<3, T, Q> const& v, T desiredLength)
  19. {
  20. return v * desiredLength / length(v);
  21. }
  22. }//namespace detail
  23. // Matrix decompose
  24. // http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
  25. // Decomposes the mode matrix to translations,rotation scale components
  26. template<typename T, qualifier Q>
  27. GLM_FUNC_QUALIFIER bool decompose(mat<4, 4, T, Q> const& ModelMatrix, vec<3, T, Q> & Scale, qua<T, Q> & Orientation, vec<3, T, Q> & Translation, vec<3, T, Q> & Skew, vec<4, T, Q> & Perspective)
  28. {
  29. mat<4, 4, T, Q> LocalMatrix(ModelMatrix);
  30. // Normalize the matrix.
  31. if(epsilonEqual(LocalMatrix[3][3], static_cast<T>(0), epsilon<T>()))
  32. return false;
  33. for(length_t i = 0; i < 4; ++i)
  34. for(length_t j = 0; j < 4; ++j)
  35. LocalMatrix[i][j] /= LocalMatrix[3][3];
  36. // perspectiveMatrix is used to solve for perspective, but it also provides
  37. // an easy way to test for singularity of the upper 3x3 component.
  38. mat<4, 4, T, Q> PerspectiveMatrix(LocalMatrix);
  39. for(length_t i = 0; i < 3; i++)
  40. PerspectiveMatrix[i][3] = static_cast<T>(0);
  41. PerspectiveMatrix[3][3] = static_cast<T>(1);
  42. /// TODO: Fixme!
  43. if(epsilonEqual(determinant(PerspectiveMatrix), static_cast<T>(0), epsilon<T>()))
  44. return false;
  45. // First, isolate perspective. This is the messiest.
  46. if(
  47. epsilonNotEqual(LocalMatrix[0][3], static_cast<T>(0), epsilon<T>()) ||
  48. epsilonNotEqual(LocalMatrix[1][3], static_cast<T>(0), epsilon<T>()) ||
  49. epsilonNotEqual(LocalMatrix[2][3], static_cast<T>(0), epsilon<T>()))
  50. {
  51. // rightHandSide is the right hand side of the equation.
  52. vec<4, T, Q> RightHandSide;
  53. RightHandSide[0] = LocalMatrix[0][3];
  54. RightHandSide[1] = LocalMatrix[1][3];
  55. RightHandSide[2] = LocalMatrix[2][3];
  56. RightHandSide[3] = LocalMatrix[3][3];
  57. // Solve the equation by inverting PerspectiveMatrix and multiplying
  58. // rightHandSide by the inverse. (This is the easiest way, not
  59. // necessarily the best.)
  60. mat<4, 4, T, Q> InversePerspectiveMatrix = glm::inverse(PerspectiveMatrix);// inverse(PerspectiveMatrix, inversePerspectiveMatrix);
  61. mat<4, 4, T, Q> TransposedInversePerspectiveMatrix = glm::transpose(InversePerspectiveMatrix);// transposeMatrix4(inversePerspectiveMatrix, transposedInversePerspectiveMatrix);
  62. Perspective = TransposedInversePerspectiveMatrix * RightHandSide;
  63. // v4MulPointByMatrix(rightHandSide, transposedInversePerspectiveMatrix, perspectivePoint);
  64. // Clear the perspective partition
  65. LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] = static_cast<T>(0);
  66. LocalMatrix[3][3] = static_cast<T>(1);
  67. }
  68. else
  69. {
  70. // No perspective.
  71. Perspective = vec<4, T, Q>(0, 0, 0, 1);
  72. }
  73. // Next take care of translation (easy).
  74. Translation = vec<3, T, Q>(LocalMatrix[3]);
  75. LocalMatrix[3] = vec<4, T, Q>(0, 0, 0, LocalMatrix[3].w);
  76. vec<3, T, Q> Row[3], Pdum3;
  77. // Now get scale and shear.
  78. for(length_t i = 0; i < 3; ++i)
  79. for(length_t j = 0; j < 3; ++j)
  80. Row[i][j] = LocalMatrix[i][j];
  81. // Compute X scale factor and normalize first row.
  82. Scale.x = length(Row[0]);// v3Length(Row[0]);
  83. Row[0] = detail::scale(Row[0], static_cast<T>(1));
  84. // Compute XY shear factor and make 2nd row orthogonal to 1st.
  85. Skew.z = dot(Row[0], Row[1]);
  86. Row[1] = detail::combine(Row[1], Row[0], static_cast<T>(1), -Skew.z);
  87. // Now, compute Y scale and normalize 2nd row.
  88. Scale.y = length(Row[1]);
  89. Row[1] = detail::scale(Row[1], static_cast<T>(1));
  90. Skew.z /= Scale.y;
  91. // Compute XZ and YZ shears, orthogonalize 3rd row.
  92. Skew.y = glm::dot(Row[0], Row[2]);
  93. Row[2] = detail::combine(Row[2], Row[0], static_cast<T>(1), -Skew.y);
  94. Skew.x = glm::dot(Row[1], Row[2]);
  95. Row[2] = detail::combine(Row[2], Row[1], static_cast<T>(1), -Skew.x);
  96. // Next, get Z scale and normalize 3rd row.
  97. Scale.z = length(Row[2]);
  98. Row[2] = detail::scale(Row[2], static_cast<T>(1));
  99. Skew.y /= Scale.z;
  100. Skew.x /= Scale.z;
  101. // At this point, the matrix (in rows[]) is orthonormal.
  102. // Check for a coordinate system flip. If the determinant
  103. // is -1, then negate the matrix and the scaling factors.
  104. Pdum3 = cross(Row[1], Row[2]); // v3Cross(row[1], row[2], Pdum3);
  105. if(dot(Row[0], Pdum3) < 0)
  106. {
  107. for(length_t i = 0; i < 3; i++)
  108. {
  109. Scale[i] *= static_cast<T>(-1);
  110. Row[i] *= static_cast<T>(-1);
  111. }
  112. }
  113. // Now, get the rotations out, as described in the gem.
  114. // FIXME - Add the ability to return either quaternions (which are
  115. // easier to recompose with) or Euler angles (rx, ry, rz), which
  116. // are easier for authors to deal with. The latter will only be useful
  117. // when we fix https://bugs.webkit.org/show_bug.cgi?id=23799, so I
  118. // will leave the Euler angle code here for now.
  119. // ret.rotateY = asin(-Row[0][2]);
  120. // if (cos(ret.rotateY) != 0) {
  121. // ret.rotateX = atan2(Row[1][2], Row[2][2]);
  122. // ret.rotateZ = atan2(Row[0][1], Row[0][0]);
  123. // } else {
  124. // ret.rotateX = atan2(-Row[2][0], Row[1][1]);
  125. // ret.rotateZ = 0;
  126. // }
  127. int i, j, k = 0;
  128. T root, trace = Row[0].x + Row[1].y + Row[2].z;
  129. if(trace > static_cast<T>(0))
  130. {
  131. root = sqrt(trace + static_cast<T>(1.0));
  132. Orientation.w = static_cast<T>(0.5) * root;
  133. root = static_cast<T>(0.5) / root;
  134. Orientation.x = root * (Row[1].z - Row[2].y);
  135. Orientation.y = root * (Row[2].x - Row[0].z);
  136. Orientation.z = root * (Row[0].y - Row[1].x);
  137. } // End if > 0
  138. else
  139. {
  140. static int Next[3] = {1, 2, 0};
  141. i = 0;
  142. if(Row[1].y > Row[0].x) i = 1;
  143. if(Row[2].z > Row[i][i]) i = 2;
  144. j = Next[i];
  145. k = Next[j];
  146. root = sqrt(Row[i][i] - Row[j][j] - Row[k][k] + static_cast<T>(1.0));
  147. Orientation[i] = static_cast<T>(0.5) * root;
  148. root = static_cast<T>(0.5) / root;
  149. Orientation[j] = root * (Row[i][j] + Row[j][i]);
  150. Orientation[k] = root * (Row[i][k] + Row[k][i]);
  151. Orientation.w = root * (Row[j][k] - Row[k][j]);
  152. } // End if <= 0
  153. return true;
  154. }
  155. }//namespace glm