Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.3KB

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