btPolarDecomposition.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "btPolarDecomposition.h"
  2. #include "btMinMax.h"
  3. namespace
  4. {
  5. btScalar abs_column_sum(const btMatrix3x3& a, int i)
  6. {
  7. return btFabs(a[0][i]) + btFabs(a[1][i]) + btFabs(a[2][i]);
  8. }
  9. btScalar abs_row_sum(const btMatrix3x3& a, int i)
  10. {
  11. return btFabs(a[i][0]) + btFabs(a[i][1]) + btFabs(a[i][2]);
  12. }
  13. btScalar p1_norm(const btMatrix3x3& a)
  14. {
  15. const btScalar sum0 = abs_column_sum(a, 0);
  16. const btScalar sum1 = abs_column_sum(a, 1);
  17. const btScalar sum2 = abs_column_sum(a, 2);
  18. return btMax(btMax(sum0, sum1), sum2);
  19. }
  20. btScalar pinf_norm(const btMatrix3x3& a)
  21. {
  22. const btScalar sum0 = abs_row_sum(a, 0);
  23. const btScalar sum1 = abs_row_sum(a, 1);
  24. const btScalar sum2 = abs_row_sum(a, 2);
  25. return btMax(btMax(sum0, sum1), sum2);
  26. }
  27. } // namespace
  28. btPolarDecomposition::btPolarDecomposition(btScalar tolerance, unsigned int maxIterations)
  29. : m_tolerance(tolerance), m_maxIterations(maxIterations)
  30. {
  31. }
  32. unsigned int btPolarDecomposition::decompose(const btMatrix3x3& a, btMatrix3x3& u, btMatrix3x3& h) const
  33. {
  34. // Use the 'u' and 'h' matrices for intermediate calculations
  35. u = a;
  36. h = a.inverse();
  37. for (unsigned int i = 0; i < m_maxIterations; ++i)
  38. {
  39. const btScalar h_1 = p1_norm(h);
  40. const btScalar h_inf = pinf_norm(h);
  41. const btScalar u_1 = p1_norm(u);
  42. const btScalar u_inf = pinf_norm(u);
  43. const btScalar h_norm = h_1 * h_inf;
  44. const btScalar u_norm = u_1 * u_inf;
  45. // The matrix is effectively singular so we cannot invert it
  46. if (btFuzzyZero(h_norm) || btFuzzyZero(u_norm))
  47. break;
  48. const btScalar gamma = btPow(h_norm / u_norm, 0.25f);
  49. const btScalar inv_gamma = btScalar(1.0) / gamma;
  50. // Determine the delta to 'u'
  51. const btMatrix3x3 delta = (u * (gamma - btScalar(2.0)) + h.transpose() * inv_gamma) * btScalar(0.5);
  52. // Update the matrices
  53. u += delta;
  54. h = u.inverse();
  55. // Check for convergence
  56. if (p1_norm(delta) <= m_tolerance * u_1)
  57. {
  58. h = u.transpose() * a;
  59. h = (h + h.transpose()) * 0.5;
  60. return i;
  61. }
  62. }
  63. // The algorithm has failed to converge to the specified tolerance, but we
  64. // want to make sure that the matrices returned are in the right form.
  65. h = u.transpose() * a;
  66. h = (h + h.transpose()) * 0.5;
  67. return m_maxIterations;
  68. }
  69. unsigned int btPolarDecomposition::maxIterations() const
  70. {
  71. return m_maxIterations;
  72. }
  73. unsigned int polarDecompose(const btMatrix3x3& a, btMatrix3x3& u, btMatrix3x3& h)
  74. {
  75. static btPolarDecomposition polar;
  76. return polar.decompose(a, u, h);
  77. }