[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletDynamics / MLCPSolvers / btPATHSolver.h
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 #ifndef BT_PATH_SOLVER_H
18 #define BT_PATH_SOLVER_H
19
20 //#define BT_USE_PATH
21 #ifdef BT_USE_PATH
22
23 extern "C"
24 {
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 #include "btMLCPSolverInterface.h"
41 #include "Dantzig/lcp.h"
42
43 class btPathSolver : public btMLCPSolverInterface
44 {
45 public:
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                         */
72                 btAlignedObjectArray<double> values;
73                 btAlignedObjectArray<int> rowIndices;
74                 btAlignedObjectArray<int> colIndices;
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();
90                 btAlignedObjectArray<double> zResult;
91                 zResult.resize(numVariables);
92                 btAlignedObjectArray<double> rhs;
93                 btAlignedObjectArray<double> upperBounds;
94                 btAlignedObjectArray<double> lowerBounds;
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