1 /* 2 Bullet Continuous Collision Detection and Physics Library 3 Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org 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 ///original version written by Erwin Coumans, October 2013 16 17 18 #ifndef BT_PATH_SOLVER_H 19 #define BT_PATH_SOLVER_H 20 21 //#define BT_USE_PATH 22 #ifdef BT_USE_PATH 23 24 extern "C" { 25 #include "PATH/SimpleLCP.h" 26 #include "PATH/License.h" 27 #include "PATH/Error_Interface.h" 28 }; 29 void __stdcall MyError(Void *data, Char *msg) 30 { 31 printf("Path Error: %s\n",msg); 32 } 33 void __stdcall MyWarning(Void *data, Char *msg) 34 { 35 printf("Path Warning: %s\n",msg); 36 } 37 38 Error_Interface e; 39 40 41 42 #include "btMLCPSolverInterface.h" 43 #include "Dantzig/lcp.h" 44 45 class btPathSolver : public btMLCPSolverInterface 46 { 47 public: 48 49 btPathSolver() 50 { 51 License_SetString("2069810742&Courtesy_License&&&USR&2013&14_12_2011&1000&PATH&GEN&31_12_2013&0_0_0&0&0_0"); 52 e.error_data = 0; 53 e.warning = MyWarning; 54 e.error = MyError; 55 Error_SetInterface(&e); 56 } 57 58 59 virtual bool solveMLCP(const btMatrixXu & A, const btVectorXu & b, btVectorXu& x, const btVectorXu & lo,const btVectorXu & hi,const btAlignedObjectArray<int>& limitDependency, int numIterations, bool useSparsity = true) 60 { 61 MCP_Termination status; 62 63 64 int numVariables = b.rows(); 65 if (0==numVariables) 66 return true; 67 68 /* - variables - the number of variables in the problem 69 - m_nnz - the number of nonzeros in the M matrix 70 - m_i - a vector of size m_nnz containing the row indices for M 71 - m_j - a vector of size m_nnz containing the column indices for M 72 - m_ij - a vector of size m_nnz containing the data for M 73 - q - a vector of size variables 74 - lb - a vector of size variables containing the lower bounds on x 75 - ub - a vector of size variables containing the upper bounds on x 76 */ 77 btAlignedObjectArray<double> values; 78 btAlignedObjectArray<int> rowIndices; 79 btAlignedObjectArray<int> colIndices; 80 81 for (int i=0;i<A.rows();i++) 82 { 83 for (int j=0;j<A.cols();j++) 84 { 85 if (A(i,j)!=0.f) 86 { 87 //add 1, because Path starts at 1, instead of 0 88 rowIndices.push_back(i+1); 89 colIndices.push_back(j+1); 90 values.push_back(A(i,j)); 91 } 92 } 93 } 94 int numNonZero = rowIndices.size(); 95 btAlignedObjectArray<double> zResult; 96 zResult.resize(numVariables); 97 btAlignedObjectArray<double> rhs; 98 btAlignedObjectArray<double> upperBounds; 99 btAlignedObjectArray<double> lowerBounds; 100 for (int i=0;i<numVariables;i++) 101 { 102 upperBounds.push_back(hi[i]); 103 lowerBounds.push_back(lo[i]); 104 rhs.push_back(-b[i]); 105 } 106 107 108 SimpleLCP(numVariables,numNonZero,&rowIndices[0],&colIndices[0],&values[0],&rhs[0],&lowerBounds[0],&upperBounds[0], &status, &zResult[0]); 109 110 if (status != MCP_Solved) 111 { 112 static const char* gReturnMsgs[] = { 113 "Invalid return", 114 "MCP_Solved: The problem was solved", 115 "MCP_NoProgress: A stationary point was found", 116 "MCP_MajorIterationLimit: Major iteration limit met", 117 "MCP_MinorIterationLimit: Cumulative minor iteration limit met", 118 "MCP_TimeLimit: Ran out of time", 119 "MCP_UserInterrupt: Control-C, typically", 120 "MCP_BoundError: Problem has a bound error", 121 "MCP_DomainError: Could not find starting point", 122 "MCP_Infeasible: Problem has no solution", 123 "MCP_Error: An error occurred within the code", 124 "MCP_LicenseError: License could not be found", 125 "MCP_OK" 126 }; 127 128 printf("ERROR: The PATH MCP solver failed: %s\n", gReturnMsgs[(unsigned int)status]);// << std::endl; 129 printf("using Projected Gauss Seidel fallback\n"); 130 131 return false; 132 } else 133 { 134 for (int i=0;i<numVariables;i++) 135 { 136 x[i] = zResult[i]; 137 //check for #NAN 138 if (x[i] != zResult[i]) 139 return false; 140 } 141 return true; 142 143 } 144 145 } 146 }; 147 148 #endif //BT_USE_PATH 149 150 151 #endif //BT_PATH_SOLVER_H 152