btLemkeAlgorithm.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (C) 2004-2013 MBSim Development Team
  2. Code was converted for the Bullet Continuous Collision Detection and Physics Library
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. //The original version is here
  13. //https://code.google.com/p/mbsim-env/source/browse/trunk/kernel/mbsim/numerics/linear_complementarity_problem/lemke_algorithm.cc
  14. //This file is re-distributed under the ZLib license, with permission of the original author (Kilian Grundl)
  15. //Math library was replaced from fmatvec to a the file src/LinearMath/btMatrixX.h
  16. //STL/std::vector replaced by btAlignedObjectArray
  17. #ifndef BT_NUMERICS_LEMKE_ALGORITHM_H_
  18. #define BT_NUMERICS_LEMKE_ALGORITHM_H_
  19. #include "LinearMath/btMatrixX.h"
  20. #include <vector> //todo: replace by btAlignedObjectArray
  21. class btLemkeAlgorithm
  22. {
  23. public:
  24. btLemkeAlgorithm(const btMatrixXu& M_, const btVectorXu& q_, const int& DEBUGLEVEL_ = 0) : DEBUGLEVEL(DEBUGLEVEL_)
  25. {
  26. setSystem(M_, q_);
  27. }
  28. /* GETTER / SETTER */
  29. /**
  30. * \brief return info of solution process
  31. */
  32. int getInfo()
  33. {
  34. return info;
  35. }
  36. /**
  37. * \brief get the number of steps until the solution was found
  38. */
  39. int getSteps(void)
  40. {
  41. return steps;
  42. }
  43. /**
  44. * \brief set system with Matrix M and vector q
  45. */
  46. void setSystem(const btMatrixXu& M_, const btVectorXu& q_)
  47. {
  48. m_M = M_;
  49. m_q = q_;
  50. }
  51. /***************************************************/
  52. /**
  53. * \brief solve algorithm adapted from : Fast Implementation of Lemke’s Algorithm for Rigid Body Contact Simulation (John E. Lloyd)
  54. */
  55. btVectorXu solve(unsigned int maxloops = 0);
  56. virtual ~btLemkeAlgorithm()
  57. {
  58. }
  59. protected:
  60. int findLexicographicMinimum(const btMatrixXu& A, const int& pivotColIndex);
  61. bool LexicographicPositive(const btVectorXu& v);
  62. void GaussJordanEliminationStep(btMatrixXu& A, int pivotRowIndex, int pivotColumnIndex, const btAlignedObjectArray<int>& basis);
  63. bool greaterZero(const btVectorXu& vector);
  64. bool validBasis(const btAlignedObjectArray<int>& basis);
  65. btMatrixXu m_M;
  66. btVectorXu m_q;
  67. /**
  68. * \brief number of steps until the Lemke algorithm found a solution
  69. */
  70. unsigned int steps;
  71. /**
  72. * \brief define level of debug output
  73. */
  74. int DEBUGLEVEL;
  75. /**
  76. * \brief did the algorithm find a solution
  77. *
  78. * -1 : not successful
  79. * 0 : successful
  80. */
  81. int info;
  82. };
  83. #endif /* BT_NUMERICS_LEMKE_ALGORITHM_H_ */