Blender  V3.3
btDeformableBackwardEulerObjective.h
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 
16 #ifndef BT_BACKWARD_EULER_OBJECTIVE_H
17 #define BT_BACKWARD_EULER_OBJECTIVE_H
18 //#include "btConjugateGradient.h"
27 #include "btPreconditioner.h"
29 #include "LinearMath/btQuickprof.h"
30 
32 {
33 public:
42  bool m_implicit;
45 
47 
49 
50  void initialize() {}
51 
52  // compute the rhs for CG solve, i.e, add the dt scaled implicit force to residual
53  void computeResidual(btScalar dt, TVStack& residual);
54 
55  // add explicit force to the velocity
56  void applyExplicitForce(TVStack& force);
57 
58  // apply force to velocity and optionally reset the force to zero
59  void applyForce(TVStack& force, bool setZero);
60 
61  // compute the norm of the residual
62  btScalar computeNorm(const TVStack& residual) const;
63 
64  // compute one step of the solve (there is only one solve if the system is linear)
65  void computeStep(TVStack& dv, const TVStack& residual, const btScalar& dt);
66 
67  // perform A*x = b
68  void multiply(const TVStack& x, TVStack& b) const;
69 
70  // set initial guess for CG solve
71  void initialGuess(TVStack& dv, const TVStack& residual);
72 
73  // reset data structure and reset dt
74  void reinitialize(bool nodeUpdated, btScalar dt);
75 
76  void setDt(btScalar dt);
77 
78  // add friction force to residual
80 
81  // add dv to velocity
82  void updateVelocity(const TVStack& dv);
83 
84  //set constraints as projections
86 
87  // update the projections and project the residual
88  void project(TVStack& r)
89  {
90  BT_PROFILE("project");
92  }
93 
94  // perform precondition M^(-1) x = b
95  void precondition(const TVStack& x, TVStack& b)
96  {
97  m_preconditioner->operator()(x, b);
98  }
99 
100  // reindex all the vertices
101  virtual void updateId()
102  {
103  size_t node_id = 0;
104  size_t face_id = 0;
105  m_nodes.clear();
106  for (int i = 0; i < m_softBodies.size(); ++i)
107  {
108  btSoftBody* psb = m_softBodies[i];
109  for (int j = 0; j < psb->m_nodes.size(); ++j)
110  {
111  psb->m_nodes[j].index = node_id;
112  m_nodes.push_back(&psb->m_nodes[j]);
113  ++node_id;
114  }
115  for (int j = 0; j < psb->m_faces.size(); ++j)
116  {
117  psb->m_faces[j].m_index = face_id;
118  ++face_id;
119  }
120  }
121  }
122 
124  {
125  return &m_nodes;
126  }
127 
128  void setImplicit(bool implicit)
129  {
130  m_implicit = implicit;
131  }
132 
133  // Calculate the total potential energy in the system
135 
136  void addLagrangeMultiplier(const TVStack& vec, TVStack& extended_vec)
137  {
138  extended_vec.resize(vec.size() + m_projection.m_lagrangeMultipliers.size());
139  for (int i = 0; i < vec.size(); ++i)
140  {
141  extended_vec[i] = vec[i];
142  }
143  int offset = vec.size();
144  for (int i = 0; i < m_projection.m_lagrangeMultipliers.size(); ++i)
145  {
146  extended_vec[offset + i].setZero();
147  }
148  }
149 
150  void addLagrangeMultiplierRHS(const TVStack& residual, const TVStack& m_dv, TVStack& extended_residual)
151  {
152  extended_residual.resize(residual.size() + m_projection.m_lagrangeMultipliers.size());
153  for (int i = 0; i < residual.size(); ++i)
154  {
155  extended_residual[i] = residual[i];
156  }
157  int offset = residual.size();
158  for (int i = 0; i < m_projection.m_lagrangeMultipliers.size(); ++i)
159  {
161  extended_residual[offset + i].setZero();
162  for (int d = 0; d < lm.m_num_constraints; ++d)
163  {
164  for (int n = 0; n < lm.m_num_nodes; ++n)
165  {
166  extended_residual[offset + i][d] += lm.m_weights[n] * m_dv[lm.m_indices[n]].dot(lm.m_dirs[d]);
167  }
168  }
169  }
170  }
171 
172  void calculateContactForce(const TVStack& dv, const TVStack& rhs, TVStack& f)
173  {
174  size_t counter = 0;
175  for (int i = 0; i < m_softBodies.size(); ++i)
176  {
177  btSoftBody* psb = m_softBodies[i];
178  for (int j = 0; j < psb->m_nodes.size(); ++j)
179  {
180  const btSoftBody::Node& node = psb->m_nodes[j];
181  f[counter] = (node.m_im == 0) ? btVector3(0, 0, 0) : dv[counter] / node.m_im;
182  ++counter;
183  }
184  }
185  for (int i = 0; i < m_lf.size(); ++i)
186  {
187  // add damping matrix
188  m_lf[i]->addScaledDampingForceDifferential(-m_dt, dv, f);
189  }
190  counter = 0;
191  for (; counter < f.size(); ++counter)
192  {
193  f[counter] = rhs[counter] - f[counter];
194  }
195  }
196 };
197 
198 #endif /* btBackwardEulerObjective_h */
_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
void setZero()
Set the matrix to the identity.
Definition: btMatrix3x3.h:337
#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 void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0),...
SIMD_FORCE_INLINE int size() const
return the number of elements in the array
SIMD_FORCE_INLINE void resize(int newsize, const T &fillData=T())
SIMD_FORCE_INLINE void push_back(const T &_Val)
btDeformableBackwardEulerObjective(btAlignedObjectArray< btSoftBody * > &softBodies, const TVStack &backup_v)
void calculateContactForce(const TVStack &dv, const TVStack &rhs, TVStack &f)
void precondition(const TVStack &x, TVStack &b)
void setConstraints(const btContactSolverInfo &infoGlobal)
void computeResidual(btScalar dt, TVStack &residual)
btAlignedObjectArray< btDeformableLagrangianForce * > m_lf
const btAlignedObjectArray< btSoftBody::Node * > * getIndices() const
btAlignedObjectArray< btSoftBody::Node * > m_nodes
void addLagrangeMultiplier(const TVStack &vec, TVStack &extended_vec)
btAlignedObjectArray< btSoftBody * > & m_softBodies
void computeStep(TVStack &dv, const TVStack &residual, const btScalar &dt)
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)
void addLagrangeMultiplierRHS(const TVStack &residual, const TVStack &m_dv, TVStack &extended_residual)
btAlignedObjectArray< LagrangeMultiplier > m_lagrangeMultipliers
tFaceArray m_faces
Definition: btSoftBody.h:802
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 const pxr::TfToken b("b", pxr::TfToken::Immortal)