btModifiedGramSchmidt.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // btModifiedGramSchmidt.h
  3. // LinearMath
  4. //
  5. // Created by Xuchen Han on 4/4/20.
  6. //
  7. #ifndef btModifiedGramSchmidt_h
  8. #define btModifiedGramSchmidt_h
  9. #include "btReducedVector.h"
  10. #include "btAlignedObjectArray.h"
  11. #include <iostream>
  12. #include <cmath>
  13. template<class TV>
  14. class btModifiedGramSchmidt
  15. {
  16. public:
  17. btAlignedObjectArray<TV> m_in;
  18. btAlignedObjectArray<TV> m_out;
  19. btModifiedGramSchmidt(const btAlignedObjectArray<TV>& vecs): m_in(vecs)
  20. {
  21. m_out.resize(0);
  22. }
  23. void solve()
  24. {
  25. m_out.resize(m_in.size());
  26. for (int i = 0; i < m_in.size(); ++i)
  27. {
  28. // printf("========= starting %d ==========\n", i);
  29. TV v(m_in[i]);
  30. // v.print();
  31. for (int j = 0; j < i; ++j)
  32. {
  33. v = v - v.proj(m_out[j]);
  34. // v.print();
  35. }
  36. v.normalize();
  37. m_out[i] = v;
  38. // v.print();
  39. }
  40. }
  41. void test()
  42. {
  43. std::cout << SIMD_EPSILON << std::endl;
  44. printf("=======inputs=========\n");
  45. for (int i = 0; i < m_out.size(); ++i)
  46. {
  47. m_in[i].print();
  48. }
  49. printf("=======output=========\n");
  50. for (int i = 0; i < m_out.size(); ++i)
  51. {
  52. m_out[i].print();
  53. }
  54. btScalar eps = SIMD_EPSILON;
  55. for (int i = 0; i < m_out.size(); ++i)
  56. {
  57. for (int j = 0; j < m_out.size(); ++j)
  58. {
  59. if (i == j)
  60. {
  61. if (std::abs(1.0-m_out[i].dot(m_out[j])) > eps)// && std::abs(m_out[i].dot(m_out[j])) > eps)
  62. {
  63. printf("vec[%d] is not unit, norm squared = %f\n", i,m_out[i].dot(m_out[j]));
  64. }
  65. }
  66. else
  67. {
  68. if (std::abs(m_out[i].dot(m_out[j])) > eps)
  69. {
  70. printf("vec[%d] and vec[%d] is not orthogonal, dot product = %f\n", i, j, m_out[i].dot(m_out[j]));
  71. }
  72. }
  73. }
  74. }
  75. }
  76. };
  77. template class btModifiedGramSchmidt<btReducedVector>;
  78. #endif /* btModifiedGramSchmidt_h */