2 Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
4 Bullet Continuous Collision Detection and Physics Library
5 Copyright (c) 2019 Google Inc. http://bulletphysics.org
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it freely,
10 subject to the following restrictions:
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.
16 #ifndef BT_PRECONDITIONER_H
17 #define BT_PRECONDITIONER_H
22 typedef btAlignedObjectArray<btVector3> TVStack;
23 virtual void operator()(const TVStack& x, TVStack& b) = 0;
24 virtual void reinitialize(bool nodeUpdated) = 0;
25 virtual ~Preconditioner() {}
28 class DefaultPreconditioner : public Preconditioner
31 virtual void operator()(const TVStack& x, TVStack& b)
33 btAssert(b.size() == x.size());
34 for (int i = 0; i < b.size(); ++i)
37 virtual void reinitialize(bool nodeUpdated)
41 virtual ~DefaultPreconditioner() {}
44 class MassPreconditioner : public Preconditioner
46 btAlignedObjectArray<btScalar> m_inv_mass;
47 const btAlignedObjectArray<btSoftBody*>& m_softBodies;
50 MassPreconditioner(const btAlignedObjectArray<btSoftBody*>& softBodies)
51 : m_softBodies(softBodies)
55 virtual void reinitialize(bool nodeUpdated)
60 for (int i = 0; i < m_softBodies.size(); ++i)
62 btSoftBody* psb = m_softBodies[i];
63 for (int j = 0; j < psb->m_nodes.size(); ++j)
64 m_inv_mass.push_back(psb->m_nodes[j].m_im);
69 virtual void operator()(const TVStack& x, TVStack& b)
71 btAssert(b.size() == x.size());
72 btAssert(m_inv_mass.size() <= x.size());
73 for (int i = 0; i < m_inv_mass.size(); ++i)
75 b[i] = x[i] * m_inv_mass[i];
77 for (int i = m_inv_mass.size(); i < b.size(); ++i)
84 class KKTPreconditioner : public Preconditioner
86 const btAlignedObjectArray<btSoftBody*>& m_softBodies;
87 const btDeformableContactProjection& m_projections;
88 const btAlignedObjectArray<btDeformableLagrangianForce*>& m_lf;
89 TVStack m_inv_A, m_inv_S;
91 const bool& m_implicit;
94 KKTPreconditioner(const btAlignedObjectArray<btSoftBody*>& softBodies, const btDeformableContactProjection& projections, const btAlignedObjectArray<btDeformableLagrangianForce*>& lf, const btScalar& dt, const bool& implicit)
95 : m_softBodies(softBodies), m_projections(projections), m_lf(lf), m_dt(dt), m_implicit(implicit)
99 virtual void reinitialize(bool nodeUpdated)
104 for (int i = 0; i < m_softBodies.size(); ++i)
106 btSoftBody* psb = m_softBodies[i];
107 num_nodes += psb->m_nodes.size();
109 m_inv_A.resize(num_nodes);
111 buildDiagonalA(m_inv_A);
112 for (int i = 0; i < m_inv_A.size(); ++i)
114 // printf("A[%d] = %f, %f, %f \n", i, m_inv_A[i][0], m_inv_A[i][1], m_inv_A[i][2]);
115 for (int d = 0; d < 3; ++d)
117 m_inv_A[i][d] = (m_inv_A[i][d] == 0) ? 0.0 : 1.0 / m_inv_A[i][d];
120 m_inv_S.resize(m_projections.m_lagrangeMultipliers.size());
121 // printf("S.size() = %d \n", m_inv_S.size());
122 buildDiagonalS(m_inv_A, m_inv_S);
123 for (int i = 0; i < m_inv_S.size(); ++i)
125 // printf("S[%d] = %f, %f, %f \n", i, m_inv_S[i][0], m_inv_S[i][1], m_inv_S[i][2]);
126 for (int d = 0; d < 3; ++d)
128 m_inv_S[i][d] = (m_inv_S[i][d] == 0) ? 0.0 : 1.0 / m_inv_S[i][d];
133 void buildDiagonalA(TVStack& diagA) const
136 for (int i = 0; i < m_softBodies.size(); ++i)
138 btSoftBody* psb = m_softBodies[i];
139 for (int j = 0; j < psb->m_nodes.size(); ++j)
141 const btSoftBody::Node& node = psb->m_nodes[j];
142 diagA[counter] = (node.m_im == 0) ? btVector3(0, 0, 0) : btVector3(1.0 / node.m_im, 1.0 / node.m_im, 1.0 / node.m_im);
148 printf("implicit not implemented\n");
151 for (int i = 0; i < m_lf.size(); ++i)
153 // add damping matrix
154 m_lf[i]->buildDampingForceDifferentialDiagonal(-m_dt, diagA);
158 void buildDiagonalS(const TVStack& inv_A, TVStack& diagS)
160 for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
162 // S[k,k] = e_k^T * C A_d^-1 C^T * e_k
163 const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
164 btVector3& t = diagS[c];
166 for (int j = 0; j < lm.m_num_constraints; ++j)
168 for (int i = 0; i < lm.m_num_nodes; ++i)
170 for (int d = 0; d < 3; ++d)
172 t[j] += inv_A[lm.m_indices[i]][d] * lm.m_dirs[j][d] * lm.m_dirs[j][d] * lm.m_weights[i] * lm.m_weights[i];
178 //#define USE_FULL_PRECONDITIONER
179 #ifndef USE_FULL_PRECONDITIONER
180 virtual void operator()(const TVStack& x, TVStack& b)
182 btAssert(b.size() == x.size());
183 for (int i = 0; i < m_inv_A.size(); ++i)
185 b[i] = x[i] * m_inv_A[i];
187 int offset = m_inv_A.size();
188 for (int i = 0; i < m_inv_S.size(); ++i)
190 b[i + offset] = x[i + offset] * m_inv_S[i];
194 virtual void operator()(const TVStack& x, TVStack& b)
196 btAssert(b.size() == x.size());
197 int offset = m_inv_A.size();
199 for (int i = 0; i < m_inv_A.size(); ++i)
201 b[i] = x[i] * m_inv_A[i];
204 for (int i = 0; i < m_inv_S.size(); ++i)
206 b[i + offset].setZero();
209 for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
211 const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
213 for (int d = 0; d < lm.m_num_constraints; ++d)
215 for (int i = 0; i < lm.m_num_nodes; ++i)
217 b[offset + c][d] += lm.m_weights[i] * b[lm.m_indices[i]].dot(lm.m_dirs[d]);
222 for (int i = 0; i < m_inv_S.size(); ++i)
224 b[i + offset] = b[i + offset] * m_inv_S[i];
227 for (int i = 0; i < m_inv_A.size(); ++i)
232 for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
235 const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
236 for (int i = 0; i < lm.m_num_nodes; ++i)
238 for (int j = 0; j < lm.m_num_constraints; ++j)
240 b[lm.m_indices[i]] += b[offset + c][j] * lm.m_weights[i] * lm.m_dirs[j];
245 for (int i = 0; i < m_inv_A.size(); ++i)
247 b[i] = (x[i] - b[i]) * m_inv_A[i];
252 for (int i = 0; i < m_inv_S.size(); ++i)
254 t[i + offset] = x[i + offset] * m_inv_S[i];
256 for (int i = 0; i < m_inv_A.size(); ++i)
260 for (int c = 0; c < m_projections.m_lagrangeMultipliers.size(); ++c)
263 const LagrangeMultiplier& lm = m_projections.m_lagrangeMultipliers[c];
264 for (int i = 0; i < lm.m_num_nodes; ++i)
266 for (int j = 0; j < lm.m_num_constraints; ++j)
268 t[lm.m_indices[i]] += t[offset + c][j] * lm.m_weights[i] * lm.m_dirs[j];
272 for (int i = 0; i < m_inv_A.size(); ++i)
274 b[i] += t[i] * m_inv_A[i];
277 for (int i = 0; i < m_inv_S.size(); ++i)
279 b[i + offset] -= x[i + offset] * m_inv_S[i];
285 #endif /* BT_PRECONDITIONER_H */