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