Bullet Collision Detection & Physics Library
btPATHSolver.h
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
16
17#ifndef BT_PATH_SOLVER_H
18#define BT_PATH_SOLVER_H
19
20//#define BT_USE_PATH
21#ifdef BT_USE_PATH
22
23extern "C"
24{
25#include "PATH/SimpleLCP.h"
26#include "PATH/License.h"
27#include "PATH/Error_Interface.h"
28};
29void __stdcall MyError(Void *data, Char *msg)
30{
31 printf("Path Error: %s\n", msg);
32}
33void __stdcall MyWarning(Void *data, Char *msg)
34{
35 printf("Path Warning: %s\n", msg);
36}
37
38Error_Interface e;
39
41#include "Dantzig/lcp.h"
42
43class btPathSolver : public btMLCPSolverInterface
44{
45public:
46 btPathSolver()
47 {
48 License_SetString("2069810742&Courtesy_License&&&USR&2013&14_12_2011&1000&PATH&GEN&31_12_2013&0_0_0&0&0_0");
49 e.error_data = 0;
50 e.warning = MyWarning;
51 e.error = MyError;
52 Error_SetInterface(&e);
53 }
54
55 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)
56 {
57 MCP_Termination status;
58
59 int numVariables = b.rows();
60 if (0 == numVariables)
61 return true;
62
63 /* - variables - the number of variables in the problem
64 - m_nnz - the number of nonzeros in the M matrix
65 - m_i - a vector of size m_nnz containing the row indices for M
66 - m_j - a vector of size m_nnz containing the column indices for M
67 - m_ij - a vector of size m_nnz containing the data for M
68 - q - a vector of size variables
69 - lb - a vector of size variables containing the lower bounds on x
70 - ub - a vector of size variables containing the upper bounds on x
71 */
75
76 for (int i = 0; i < A.rows(); i++)
77 {
78 for (int j = 0; j < A.cols(); j++)
79 {
80 if (A(i, j) != 0.f)
81 {
82 //add 1, because Path starts at 1, instead of 0
83 rowIndices.push_back(i + 1);
84 colIndices.push_back(j + 1);
85 values.push_back(A(i, j));
86 }
87 }
88 }
89 int numNonZero = rowIndices.size();
91 zResult.resize(numVariables);
95 for (int i = 0; i < numVariables; i++)
96 {
97 upperBounds.push_back(hi[i]);
98 lowerBounds.push_back(lo[i]);
99 rhs.push_back(-b[i]);
100 }
101
102 SimpleLCP(numVariables, numNonZero, &rowIndices[0], &colIndices[0], &values[0], &rhs[0], &lowerBounds[0], &upperBounds[0], &status, &zResult[0]);
103
104 if (status != MCP_Solved)
105 {
106 static const char *gReturnMsgs[] = {
107 "Invalid return",
108 "MCP_Solved: The problem was solved",
109 "MCP_NoProgress: A stationary point was found",
110 "MCP_MajorIterationLimit: Major iteration limit met",
111 "MCP_MinorIterationLimit: Cumulative minor iteration limit met",
112 "MCP_TimeLimit: Ran out of time",
113 "MCP_UserInterrupt: Control-C, typically",
114 "MCP_BoundError: Problem has a bound error",
115 "MCP_DomainError: Could not find starting point",
116 "MCP_Infeasible: Problem has no solution",
117 "MCP_Error: An error occurred within the code",
118 "MCP_LicenseError: License could not be found",
119 "MCP_OK"};
120
121 printf("ERROR: The PATH MCP solver failed: %s\n", gReturnMsgs[(unsigned int)status]); // << std::endl;
122 printf("using Projected Gauss Seidel fallback\n");
123
124 return false;
125 }
126 else
127 {
128 for (int i = 0; i < numVariables; i++)
129 {
130 x[i] = zResult[i];
131 //check for #NAN
132 if (x[i] != zResult[i])
133 return false;
134 }
135 return true;
136 }
137 }
138};
139
140#endif //BT_USE_PATH
141
142#endif //BT_PATH_SOLVER_H
#define btMatrixXu
Definition: btMatrixX.h:529
#define btVectorXu
Definition: btMatrixX.h:528
int size() const
return the number of elements in the array
void resize(int newsize, const T &fillData=T())
void push_back(const T &_Val)
original version written by Erwin Coumans, October 2013
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)=0