Bullet Collision Detection & Physics Library
btSolveProjectedGaussSeidel.h
Go to the documentation of this file.
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 */
16 
17 #ifndef BT_SOLVE_PROJECTED_GAUSS_SEIDEL_H
18 #define BT_SOLVE_PROJECTED_GAUSS_SEIDEL_H
19 
20 
21 #include "btMLCPSolverInterface.h"
22 
24 {
25 public:
26  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)
27  {
28  //A is a m-n matrix, m rows, n columns
29  btAssert(A.rows() == b.rows());
30 
31  int i, j, numRows = A.rows();
32 
33  float delta;
34 
35  for (int k = 0; k <numIterations; k++)
36  {
37  for (i = 0; i <numRows; i++)
38  {
39  delta = 0.0f;
40  if (useSparsity)
41  {
42  for (int h=0;h<A.m_rowNonZeroElements1[i].size();h++)
43  {
44  int j = A.m_rowNonZeroElements1[i][h];
45  if (j != i)//skip main diagonal
46  {
47  delta += A(i,j) * x[j];
48  }
49  }
50  } else
51  {
52  for (j = 0; j <i; j++)
53  delta += A(i,j) * x[j];
54  for (j = i+1; j<numRows; j++)
55  delta += A(i,j) * x[j];
56  }
57 
58  float aDiag = A(i,i);
59  x [i] = (b [i] - delta) / A(i,i);
60  float s = 1.f;
61 
62  if (limitDependency[i]>=0)
63  {
64  s = x[limitDependency[i]];
65  if (s<0)
66  s=1;
67  }
68 
69  if (x[i]<lo[i]*s)
70  x[i]=lo[i]*s;
71  if (x[i]>hi[i]*s)
72  x[i]=hi[i]*s;
73  }
74  }
75  return true;
76  }
77 
78 };
79 
80 #endif //BT_SOLVE_PROJECTED_GAUSS_SEIDEL_H
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)
#define btAssert(x)
Definition: btScalar.h:101
#define btMatrixXu
Definition: btMatrixX.h:499
original version written by Erwin Coumans, October 2013
#define btVectorXu
Definition: btMatrixX.h:498
original version written by Erwin Coumans, October 2013