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_SOLVE_PROJECTED_GAUSS_SEIDEL_H 18 #define BT_SOLVE_PROJECTED_GAUSS_SEIDEL_H 19 20 21 #include "btMLCPSolverInterface.h" 22 23 ///This solver is mainly for debug/learning purposes: it is functionally equivalent to the btSequentialImpulseConstraintSolver solver, but much slower (it builds the full LCP matrix) 24 class btSolveProjectedGaussSeidel : public btMLCPSolverInterface 25 { 26 public: 27 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) 28 { 29 if (!A.rows()) 30 return true; 31 //the A matrix is sparse, so compute the non-zero elements 32 A.rowComputeNonZeroElements(); 33 34 //A is a m-n matrix, m rows, n columns 35 btAssert(A.rows() == b.rows()); 36 37 int i, j, numRows = A.rows(); 38 39 float delta; 40 41 for (int k = 0; k <numIterations; k++) 42 { 43 for (i = 0; i <numRows; i++) 44 { 45 delta = 0.0f; 46 if (useSparsity) 47 { 48 for (int h=0;h<A.m_rowNonZeroElements1[i].size();h++) 49 { 50 int j = A.m_rowNonZeroElements1[i][h]; 51 if (j != i)//skip main diagonal 52 { 53 delta += A(i,j) * x[j]; 54 } 55 } 56 } else 57 { 58 for (j = 0; j <i; j++) 59 delta += A(i,j) * x[j]; 60 for (j = i+1; j<numRows; j++) 61 delta += A(i,j) * x[j]; 62 } 63 64 float aDiag = A(i,i); 65 x [i] = (b [i] - delta) / aDiag; 66 float s = 1.f; 67 68 if (limitDependency[i]>=0) 69 { 70 s = x[limitDependency[i]]; 71 if (s<0) 72 s=1; 73 } 74 75 if (x[i]<lo[i]*s) 76 x[i]=lo[i]*s; 77 if (x[i]>hi[i]*s) 78 x[i]=hi[i]*s; 79 } 80 } 81 return true; 82 } 83 84 }; 85 86 #endif //BT_SOLVE_PROJECTED_GAUSS_SEIDEL_H 87