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_DANTZIG_SOLVER_H
18 #define BT_DANTZIG_SOLVER_H
19 
20 #include "btMLCPSolverInterface.h"
21 #include "btDantzigLCP.h"
22 
23 
24 class btDantzigSolver : public btMLCPSolverInterface
25 {
26 protected:
27 
28 	btScalar m_acceptableUpperLimitSolution;
29 
30 	btAlignedObjectArray<char>	m_tempBuffer;
31 
32 	btAlignedObjectArray<btScalar> m_A;
33 	btAlignedObjectArray<btScalar> m_b;
34 	btAlignedObjectArray<btScalar> m_x;
35 	btAlignedObjectArray<btScalar> m_lo;
36 	btAlignedObjectArray<btScalar> m_hi;
37 	btAlignedObjectArray<int>	m_dependencies;
38 	btDantzigScratchMemory m_scratchMemory;
39 public:
40 
btDantzigSolver()41 	btDantzigSolver()
42 		:m_acceptableUpperLimitSolution(btScalar(1000))
43 	{
44 	}
45 
46 	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)
47 	{
48 		bool result = true;
49 		int n = b.rows();
50 		if (n)
51 		{
52 			int nub = 0;
53 			btAlignedObjectArray<btScalar> ww;
54 			ww.resize(n);
55 
56 
57 			const btScalar* Aptr = A.getBufferPointer();
58 			m_A.resize(n*n);
59 			for (int i=0;i<n*n;i++)
60 			{
61 				m_A[i] = Aptr[i];
62 
63 			}
64 
65 			m_b.resize(n);
66 			m_x.resize(n);
67 			m_lo.resize(n);
68 			m_hi.resize(n);
69 			m_dependencies.resize(n);
70 			for (int i=0;i<n;i++)
71 			{
72 				m_lo[i] = lo[i];
73 				m_hi[i] = hi[i];
74 				m_b[i] = b[i];
75 				m_x[i] = x[i];
76 				m_dependencies[i] = limitDependency[i];
77 			}
78 
79 
80 			result = btSolveDantzigLCP (n,&m_A[0],&m_x[0],&m_b[0],&ww[0],nub,&m_lo[0],&m_hi[0],&m_dependencies[0],m_scratchMemory);
81 			if (!result)
82 				return result;
83 
84 //			printf("numAllocas = %d\n",numAllocas);
85 			for (int i=0;i<n;i++)
86 			{
87 				volatile btScalar xx = m_x[i];
88 				if (xx != m_x[i])
89 					return false;
90 				if (x[i] >= m_acceptableUpperLimitSolution)
91 				{
92 					return false;
93 				}
94 
95 				if (x[i] <= -m_acceptableUpperLimitSolution)
96 				{
97 					return false;
98 				}
99 			}
100 
101 			for (int i=0;i<n;i++)
102 			{
103 				x[i] = m_x[i];
104 			}
105 
106 		}
107 
108 		return result;
109 	}
110 };
111 
112 #endif //BT_DANTZIG_SOLVER_H
113