Blender  V3.3
btDeformableBackwardEulerObjective.cpp
Go to the documentation of this file.
1 /*
2  Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
3 
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.
14  */
15 
17 #include "btPreconditioner.h"
18 #include "LinearMath/btQuickprof.h"
19 
21  : m_softBodies(softBodies), m_projection(softBodies), m_backupVelocity(backup_v), m_implicit(false)
22 {
26 }
27 
29 {
30  delete m_KKTPreconditioner;
31  delete m_massPreconditioner;
32 }
33 
35 {
36  BT_PROFILE("reinitialize");
37  if (dt > 0)
38  {
39  setDt(dt);
40  }
41  if (nodeUpdated)
42  {
43  updateId();
44  }
45  for (int i = 0; i < m_lf.size(); ++i)
46  {
47  m_lf[i]->reinitialize(nodeUpdated);
48  }
49  btMatrix3x3 I;
50  I.setIdentity();
51  for (int i = 0; i < m_softBodies.size(); ++i)
52  {
53  btSoftBody* psb = m_softBodies[i];
54  for (int j = 0; j < psb->m_nodes.size(); ++j)
55  {
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);
58  }
59  }
60  m_projection.reinitialize(nodeUpdated);
61  // m_preconditioner->reinitialize(nodeUpdated);
62 }
63 
65 {
66  m_dt = dt;
67 }
68 
70 {
71  BT_PROFILE("multiply");
72  // add in the mass term
73  size_t counter = 0;
74  for (int i = 0; i < m_softBodies.size(); ++i)
75  {
76  btSoftBody* psb = m_softBodies[i];
77  for (int j = 0; j < psb->m_nodes.size(); ++j)
78  {
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;
81  ++counter;
82  }
83  }
84 
85  for (int i = 0; i < m_lf.size(); ++i)
86  {
87  // add damping matrix
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)
91  {
92  m_lf[i]->addScaledElasticForceDifferential(-m_dt * m_dt, x, b);
93  }
94  }
95  int offset = m_nodes.size();
96  for (int i = offset; i < b.size(); ++i)
97  {
98  b[i].setZero();
99  }
100  // add in the lagrange multiplier terms
101 
102  for (int c = 0; c < m_projection.m_lagrangeMultipliers.size(); ++c)
103  {
104  // C^T * lambda
106  for (int i = 0; i < lm.m_num_nodes; ++i)
107  {
108  for (int j = 0; j < lm.m_num_constraints; ++j)
109  {
110  b[lm.m_indices[i]] += x[offset + c][j] * lm.m_weights[i] * lm.m_dirs[j];
111  }
112  }
113  // C * x
114  for (int d = 0; d < lm.m_num_constraints; ++d)
115  {
116  for (int i = 0; i < lm.m_num_nodes; ++i)
117  {
118  b[offset + c][d] += lm.m_weights[i] * x[lm.m_indices[i]].dot(lm.m_dirs[d]);
119  }
120  }
121  }
122 }
123 
125 {
126  for (int i = 0; i < m_softBodies.size(); ++i)
127  {
128  btSoftBody* psb = m_softBodies[i];
129  for (int j = 0; j < psb->m_nodes.size(); ++j)
130  {
131  btSoftBody::Node& node = psb->m_nodes[j];
132  node.m_v = m_backupVelocity[node.index] + dv[node.index];
133  }
134  }
135 }
136 
138 {
139  size_t counter = 0;
140  for (int i = 0; i < m_softBodies.size(); ++i)
141  {
142  btSoftBody* psb = m_softBodies[i];
143  if (!psb->isActive())
144  {
145  counter += psb->m_nodes.size();
146  continue;
147  }
148  if (m_implicit)
149  {
150  for (int j = 0; j < psb->m_nodes.size(); ++j)
151  {
152  if (psb->m_nodes[j].m_im != 0)
153  {
154  psb->m_nodes[j].m_v += psb->m_nodes[j].m_effectiveMass_inv * force[counter++];
155  }
156  }
157  }
158  else
159  {
160  for (int j = 0; j < psb->m_nodes.size(); ++j)
161  {
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++];
164  }
165  }
166  }
167  if (setZero)
168  {
169  for (int i = 0; i < force.size(); ++i)
170  force[i].setZero();
171  }
172 }
173 
175 {
176  BT_PROFILE("computeResidual");
177  // add implicit force
178  for (int i = 0; i < m_lf.size(); ++i)
179  {
180  // Always integrate picking force implicitly for stability.
181  if (m_implicit || m_lf[i]->getForceType() == BT_MOUSE_PICKING_FORCE)
182  {
183  m_lf[i]->addScaledForces(dt, residual);
184  }
185  else
186  {
187  m_lf[i]->addScaledDampingForce(dt, residual);
188  }
189  }
190  // m_projection.project(residual);
191 }
192 
194 {
195  btScalar mag = 0;
196  for (int i = 0; i < residual.size(); ++i)
197  {
198  mag += residual[i].length2();
199  }
200  return std::sqrt(mag);
201 }
202 
204 {
205  btScalar e = 0;
206  for (int i = 0; i < m_lf.size(); ++i)
207  {
208  e += m_lf[i]->totalEnergy(dt);
209  }
210  return e;
211 }
212 
214 {
215  for (int i = 0; i < m_softBodies.size(); ++i)
216  {
217  m_softBodies[i]->advanceDeformation();
218  }
219  if (m_implicit)
220  {
221  // apply forces except gravity force
222  btVector3 gravity;
223  for (int i = 0; i < m_lf.size(); ++i)
224  {
225  if (m_lf[i]->getForceType() == BT_GRAVITY_FORCE)
226  {
227  gravity = static_cast<btDeformableGravityForce*>(m_lf[i])->m_gravity;
228  }
229  else
230  {
231  m_lf[i]->addScaledForces(m_dt, force);
232  }
233  }
234  for (int i = 0; i < m_lf.size(); ++i)
235  {
236  m_lf[i]->addScaledHessian(m_dt);
237  }
238  for (int i = 0; i < m_softBodies.size(); ++i)
239  {
240  btSoftBody* psb = m_softBodies[i];
241  if (psb->isActive())
242  {
243  for (int j = 0; j < psb->m_nodes.size(); ++j)
244  {
245  // add gravity explicitly
246  psb->m_nodes[j].m_v += m_dt * psb->m_gravityFactor * gravity;
247  }
248  }
249  }
250  }
251  else
252  {
253  for (int i = 0; i < m_lf.size(); ++i)
254  {
255  m_lf[i]->addScaledExplicitForce(m_dt, force);
256  }
257  }
258  // calculate inverse mass matrix for all nodes
259  for (int i = 0; i < m_softBodies.size(); ++i)
260  {
261  btSoftBody* psb = m_softBodies[i];
262  if (psb->isActive())
263  {
264  for (int j = 0; j < psb->m_nodes.size(); ++j)
265  {
266  psb->m_nodes[j].m_effectiveMass_inv = psb->m_nodes[j].m_effectiveMass.inverse();
267  }
268  }
269  }
270  applyForce(force, true);
271 }
272 
274 {
275  size_t counter = 0;
276  for (int i = 0; i < m_softBodies.size(); ++i)
277  {
278  btSoftBody* psb = m_softBodies[i];
279  for (int j = 0; j < psb->m_nodes.size(); ++j)
280  {
281  dv[counter] = psb->m_nodes[j].m_im * residual[counter];
282  ++counter;
283  }
284  }
285 }
286 
287 //set constraints as projections
289 {
291 }
292 
294 {
296 }
sqrt(x)+1/max(0
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
@ BT_MOUSE_PICKING_FORCE
btScalar m_gravity
void setZero()
Set the matrix to the identity.
Definition: btMatrix3x3.h:337
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:50
#define BT_PROFILE(name)
Definition: btQuickprof.h:198
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btSequentialImpulseConstraintSolverMt int btPersistentManifold int btTypedConstraint int const btContactSolverInfo & infoGlobal
btVector3
btVector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16-by...
Definition: btVector3.h:82
SIMD_FORCE_INLINE int size() const
return the number of elements in the array
btDeformableBackwardEulerObjective(btAlignedObjectArray< btSoftBody * > &softBodies, const TVStack &backup_v)
void setConstraints(const btContactSolverInfo &infoGlobal)
void computeResidual(btScalar dt, TVStack &residual)
btAlignedObjectArray< btDeformableLagrangianForce * > m_lf
btAlignedObjectArray< btSoftBody::Node * > m_nodes
btAlignedObjectArray< btSoftBody * > & m_softBodies
void initialGuess(TVStack &dv, const TVStack &residual)
btScalar computeNorm(const TVStack &residual) const
void multiply(const TVStack &x, TVStack &b) const
void reinitialize(bool nodeUpdated, btScalar dt)
btAlignedObjectArray< LagrangeMultiplier > m_lagrangeMultipliers
virtual void setConstraints(const btContactSolverInfo &infoGlobal)
virtual void reinitialize(bool nodeUpdated)
btScalar m_gravityFactor
Definition: btSoftBody.h:829
tNodeArray m_nodes
Definition: btSoftBody.h:799
OperationNode * node
ccl_gpu_kernel_postfix ccl_global int * counter
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
static unsigned c
Definition: RandGen.cpp:83
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
#define I