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