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 #include "btDeformableBackwardEulerObjective.h"
17 #include "btPreconditioner.h"
18 #include "LinearMath/btQuickprof.h"
20 btDeformableBackwardEulerObjective::btDeformableBackwardEulerObjective(btAlignedObjectArray<btSoftBody*>& softBodies, const TVStack& backup_v)
21 : m_softBodies(softBodies), m_projection(softBodies), m_backupVelocity(backup_v), m_implicit(false)
23 m_massPreconditioner = new MassPreconditioner(m_softBodies);
24 m_KKTPreconditioner = new KKTPreconditioner(m_softBodies, m_projection, m_lf, m_dt, m_implicit);
25 m_preconditioner = m_KKTPreconditioner;
28 btDeformableBackwardEulerObjective::~btDeformableBackwardEulerObjective()
30 delete m_KKTPreconditioner;
31 delete m_massPreconditioner;
34 void btDeformableBackwardEulerObjective::reinitialize(bool nodeUpdated, btScalar dt)
36 BT_PROFILE("reinitialize");
45 for (int i = 0; i < m_lf.size(); ++i)
47 m_lf[i]->reinitialize(nodeUpdated);
51 for (int i = 0; i < m_softBodies.size(); ++i)
53 btSoftBody* psb = m_softBodies[i];
54 for (int j = 0; j < psb->m_nodes.size(); ++j)
56 if (psb->m_nodes[j].m_im > 0)
57 psb->m_nodes[j].m_effectiveMass = I * (1.0 / psb->m_nodes[j].m_im);
60 m_projection.reinitialize(nodeUpdated);
61 // m_preconditioner->reinitialize(nodeUpdated);
64 void btDeformableBackwardEulerObjective::setDt(btScalar dt)
69 void btDeformableBackwardEulerObjective::multiply(const TVStack& x, TVStack& b) const
71 BT_PROFILE("multiply");
72 // add in the mass term
74 for (int i = 0; i < m_softBodies.size(); ++i)
76 btSoftBody* psb = m_softBodies[i];
77 for (int j = 0; j < psb->m_nodes.size(); ++j)
79 const btSoftBody::Node& node = psb->m_nodes[j];
80 b[counter] = (node.m_im == 0) ? btVector3(0, 0, 0) : x[counter] / node.m_im;
85 for (int i = 0; i < m_lf.size(); ++i)
88 m_lf[i]->addScaledDampingForceDifferential(-m_dt, x, b);
89 // Always integrate picking force implicitly for stability.
90 if (m_implicit || m_lf[i]->getForceType() == BT_MOUSE_PICKING_FORCE)
92 m_lf[i]->addScaledElasticForceDifferential(-m_dt * m_dt, x, b);
95 int offset = m_nodes.size();
96 for (int i = offset; i < b.size(); ++i)
100 // add in the lagrange multiplier terms
102 for (int c = 0; c < m_projection.m_lagrangeMultipliers.size(); ++c)
105 const LagrangeMultiplier& lm = m_projection.m_lagrangeMultipliers[c];
106 for (int i = 0; i < lm.m_num_nodes; ++i)
108 for (int j = 0; j < lm.m_num_constraints; ++j)
110 b[lm.m_indices[i]] += x[offset + c][j] * lm.m_weights[i] * lm.m_dirs[j];
114 for (int d = 0; d < lm.m_num_constraints; ++d)
116 for (int i = 0; i < lm.m_num_nodes; ++i)
118 b[offset + c][d] += lm.m_weights[i] * x[lm.m_indices[i]].dot(lm.m_dirs[d]);
124 void btDeformableBackwardEulerObjective::updateVelocity(const TVStack& dv)
126 for (int i = 0; i < m_softBodies.size(); ++i)
128 btSoftBody* psb = m_softBodies[i];
129 for (int j = 0; j < psb->m_nodes.size(); ++j)
131 btSoftBody::Node& node = psb->m_nodes[j];
132 node.m_v = m_backupVelocity[node.index] + dv[node.index];
137 void btDeformableBackwardEulerObjective::applyForce(TVStack& force, bool setZero)
140 for (int i = 0; i < m_softBodies.size(); ++i)
142 btSoftBody* psb = m_softBodies[i];
143 if (!psb->isActive())
145 counter += psb->m_nodes.size();
150 for (int j = 0; j < psb->m_nodes.size(); ++j)
152 if (psb->m_nodes[j].m_im != 0)
154 psb->m_nodes[j].m_v += psb->m_nodes[j].m_effectiveMass_inv * force[counter++];
160 for (int j = 0; j < psb->m_nodes.size(); ++j)
162 btScalar one_over_mass = (psb->m_nodes[j].m_im == 0) ? 0 : psb->m_nodes[j].m_im;
163 psb->m_nodes[j].m_v += one_over_mass * force[counter++];
169 for (int i = 0; i < force.size(); ++i)
174 void btDeformableBackwardEulerObjective::computeResidual(btScalar dt, TVStack& residual)
176 BT_PROFILE("computeResidual");
177 // add implicit force
178 for (int i = 0; i < m_lf.size(); ++i)
180 // Always integrate picking force implicitly for stability.
181 if (m_implicit || m_lf[i]->getForceType() == BT_MOUSE_PICKING_FORCE)
183 m_lf[i]->addScaledForces(dt, residual);
187 m_lf[i]->addScaledDampingForce(dt, residual);
190 // m_projection.project(residual);
193 btScalar btDeformableBackwardEulerObjective::computeNorm(const TVStack& residual) const
196 for (int i = 0; i < residual.size(); ++i)
198 mag += residual[i].length2();
200 return std::sqrt(mag);
203 btScalar btDeformableBackwardEulerObjective::totalEnergy(btScalar dt)
206 for (int i = 0; i < m_lf.size(); ++i)
208 e += m_lf[i]->totalEnergy(dt);
213 void btDeformableBackwardEulerObjective::applyExplicitForce(TVStack& force)
215 for (int i = 0; i < m_softBodies.size(); ++i)
217 m_softBodies[i]->advanceDeformation();
221 // apply forces except gravity force
223 for (int i = 0; i < m_lf.size(); ++i)
225 if (m_lf[i]->getForceType() == BT_GRAVITY_FORCE)
227 gravity = static_cast<btDeformableGravityForce*>(m_lf[i])->m_gravity;
231 m_lf[i]->addScaledForces(m_dt, force);
234 for (int i = 0; i < m_lf.size(); ++i)
236 m_lf[i]->addScaledHessian(m_dt);
238 for (int i = 0; i < m_softBodies.size(); ++i)
240 btSoftBody* psb = m_softBodies[i];
243 for (int j = 0; j < psb->m_nodes.size(); ++j)
245 // add gravity explicitly
246 psb->m_nodes[j].m_v += m_dt * psb->m_gravityFactor * gravity;
253 for (int i = 0; i < m_lf.size(); ++i)
255 m_lf[i]->addScaledExplicitForce(m_dt, force);
258 // calculate inverse mass matrix for all nodes
259 for (int i = 0; i < m_softBodies.size(); ++i)
261 btSoftBody* psb = m_softBodies[i];
264 for (int j = 0; j < psb->m_nodes.size(); ++j)
266 if (psb->m_nodes[j].m_im > 0)
268 psb->m_nodes[j].m_effectiveMass_inv = psb->m_nodes[j].m_effectiveMass.inverse();
273 applyForce(force, true);
276 void btDeformableBackwardEulerObjective::initialGuess(TVStack& dv, const TVStack& residual)
279 for (int i = 0; i < m_softBodies.size(); ++i)
281 btSoftBody* psb = m_softBodies[i];
282 for (int j = 0; j < psb->m_nodes.size(); ++j)
284 dv[counter] = psb->m_nodes[j].m_im * residual[counter];
290 //set constraints as projections
291 void btDeformableBackwardEulerObjective::setConstraints(const btContactSolverInfo& infoGlobal)
293 m_projection.setConstraints(infoGlobal);
296 void btDeformableBackwardEulerObjective::applyDynamicFriction(TVStack& r)
298 m_projection.applyDynamicFriction(r);