btPolarDecomposition.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }
  28. const btScalar btPolarDecomposition::DEFAULT_TOLERANCE = btScalar(0.0001);
  29. const unsigned int btPolarDecomposition::DEFAULT_MAX_ITERATIONS = 16;
  30. btPolarDecomposition::btPolarDecomposition(btScalar tolerance, unsigned int maxIterations)
  31. : m_tolerance(tolerance)
  32. , m_maxIterations(maxIterations)
  33. {
  34. }
  35. unsigned int btPolarDecomposition::decompose(const btMatrix3x3& a, btMatrix3x3& u, btMatrix3x3& h) const
  36. {
  37. // Use the 'u' and 'h' matrices for intermediate calculations
  38. u = a;
  39. h = a.inverse();
  40. for (unsigned int i = 0; i < m_maxIterations; ++i)
  41. {
  42. const btScalar h_1 = p1_norm(h);
  43. const btScalar h_inf = pinf_norm(h);
  44. const btScalar u_1 = p1_norm(u);
  45. const btScalar u_inf = pinf_norm(u);
  46. const btScalar h_norm = h_1 * h_inf;
  47. const btScalar u_norm = u_1 * u_inf;
  48. // The matrix is effectively singular so we cannot invert it
  49. if (btFuzzyZero(h_norm) || btFuzzyZero(u_norm))
  50. break;
  51. const btScalar gamma = btPow(h_norm / u_norm, 0.25f);
  52. const btScalar inv_gamma = btScalar(1.0) / gamma;
  53. // Determine the delta to 'u'
  54. const btMatrix3x3 delta = (u * (gamma - btScalar(2.0)) + h.transpose() * inv_gamma) * btScalar(0.5);
  55. // Update the matrices
  56. u += delta;
  57. h = u.inverse();
  58. // Check for convergence
  59. if (p1_norm(delta) <= m_tolerance * u_1)
  60. {
  61. h = u.transpose() * a;
  62. h = (h + h.transpose()) * 0.5;
  63. return i;
  64. }
  65. }
  66. // The algorithm has failed to converge to the specified tolerance, but we
  67. // want to make sure that the matrices returned are in the right form.
  68. h = u.transpose() * a;
  69. h = (h + h.transpose()) * 0.5;
  70. return m_maxIterations;
  71. }
  72. unsigned int btPolarDecomposition::maxIterations() const
  73. {
  74. return m_maxIterations;
  75. }
  76. unsigned int polarDecompose(const btMatrix3x3& a, btMatrix3x3& u, btMatrix3x3& h)
  77. {
  78. static btPolarDecomposition polar;
  79. return polar.decompose(a, u, h);
  80. }