Bullet Collision Detection & Physics Library: btDeformableBodySolver.h Source File

Bullet Collision Detection & Physics Library

btDeformableBodySolver.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_DEFORMABLE_BODY_SOLVERS_H
17 #define BT_DEFORMABLE_BODY_SOLVERS_H
18
19
20 #include "btSoftBodySolvers.h"
21 #include "btDeformableBackwardEulerObjective.h"
22 #include "btDeformableMultiBodyDynamicsWorld.h"
23 #include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
24 #include "BulletDynamics/Featherstone/btMultiBodyConstraint.h"
25
26 struct btCollisionObjectWrapper;
27 class btDeformableBackwardEulerObjective;
28 class btDeformableMultiBodyDynamicsWorld;
29
30 class btDeformableBodySolver : public btSoftBodySolver
31 {
32 typedef btAlignedObjectArray<btVector3> TVStack;
33 protected:
34 int m_numNodes; // total number of deformable body nodes
35 TVStack m_dv; // v_{n+1} - v_n
36 TVStack m_backup_dv; // backed up dv
37 TVStack m_ddv; // incremental dv
38 TVStack m_residual; // rhs of the linear solve
39 btAlignedObjectArray<btSoftBody *> m_softBodies; // all deformable bodies
40 TVStack m_backupVelocity; // backed up v, equals v_n for implicit, equals v_{n+1}^* for explicit
41 btScalar m_dt; // dt
42 btConjugateGradient<btDeformableBackwardEulerObjective> m_cg; // CG solver
43 bool m_implicit; // use implicit scheme if true, explicit scheme if false
44 int m_maxNewtonIterations; // max number of newton iterations
45 btScalar m_newtonTolerance; // stop newton iterations if f(x) < m_newtonTolerance
46 bool m_lineSearch; // If true, use newton's method with line search under implicit scheme
47
48 public:
49 // handles data related to objective function
50 btDeformableBackwardEulerObjective* m_objective;
51
52 btDeformableBodySolver();
53
54 virtual ~btDeformableBodySolver();
55
56 virtual SolverTypes getSolverType() const
57 {
58 return DEFORMABLE_SOLVER;
59 }
60
61 // update soft body normals
62 virtual void updateSoftBodies();
63
64 // solve the momentum equation
65 virtual void solveDeformableConstraints(btScalar solverdt);
66
67 // solve the contact between deformable and rigid as well as among deformables
68 btScalar solveContactConstraints(btCollisionObject** deformableBodies,int numDeformableBodies);
69
70 // solve the position error between deformable and rigid as well as among deformables;
71 btScalar solveSplitImpulse(const btContactSolverInfo& infoGlobal);
72
73 // set up the position error in split impulse
74 void splitImpulseSetup(const btContactSolverInfo& infoGlobal);
75
76 // resize/clear data structures
77 void reinitialize(const btAlignedObjectArray<btSoftBody *>& softBodies, btScalar dt);
78
79 // set up contact constraints
80 void setConstraints();
81
82 // add in elastic forces and gravity to obtain v_{n+1}^* and calls predictDeformableMotion
83 virtual void predictMotion(btScalar solverdt);
84
85 // move to temporary position x_{n+1}^* = x_n + dt * v_{n+1}^*
86 // x_{n+1}^* is stored in m_q
87 void predictDeformableMotion(btSoftBody* psb, btScalar dt);
88
89 // save the current velocity to m_backupVelocity
90 void backupVelocity();
91
92 // set m_dv and m_backupVelocity to desired value to prepare for momentum solve
93 void setupDeformableSolve(bool implicit);
94
95 // set the current velocity to that backed up in m_backupVelocity
96 void revertVelocity();
97
98 // set velocity to m_dv + m_backupVelocity
99 void updateVelocity();
100
101 // update the node count
102 bool updateNodes();
103
104 // calculate the change in dv resulting from the momentum solve
105 void computeStep(TVStack& ddv, const TVStack& residual);
106
107 // calculate the change in dv resulting from the momentum solve when line search is turned on
108 btScalar computeDescentStep(TVStack& ddv, const TVStack& residual, bool verbose=false);
109
110 virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer) {}
111
112 // process collision between deformable and rigid
113 virtual void processCollision(btSoftBody * softBody, const btCollisionObjectWrapper * collisionObjectWrap)
114 {
115 softBody->defaultCollisionHandler(collisionObjectWrap);
116 }
117
118 // process collision between deformable and deformable
119 virtual void processCollision(btSoftBody * softBody, btSoftBody * otherSoftBody) {
120 softBody->defaultCollisionHandler(otherSoftBody);
121 }
122
123 // If true, implicit time stepping scheme is used.
124 // Otherwise, explicit time stepping scheme is used
125 void setImplicit(bool implicit);
126
127 // If true, newton's method with line search is used when implicit time stepping scheme is turned on
128 void setLineSearch(bool lineSearch);
129
130 // set temporary position x^* = x_n + dt * v
131 // update the deformation gradient at position x^*
132 void updateState();
133
134 // set dv = dv + scale * ddv
135 void updateDv(btScalar scale = 1);
136
137 // set temporary position x^* = x_n + dt * v^*
138 void updateTempPosition();
139
140 // save the current dv to m_backup_dv;
141 void backupDv();
142
143 // set dv to the backed-up value
144 void revertDv();
145
146 // set dv = dv + scale * ddv
147 // set v^* = v_n + dv
148 // set temporary position x^* = x_n + dt * v^*
149 // update the deformation gradient at position x^*
150 void updateEnergy(btScalar scale);
151
152 // calculates the appropriately scaled kinetic energy in the system, which is
153 // 1/2 * dv^T * M * dv
154 // used in line search
155 btScalar kineticEnergy();
156
157 // unused functions
158 virtual void optimize(btAlignedObjectArray<btSoftBody *> &softBodies, bool forceUpdate = false){}
159 virtual void solveConstraints(btScalar dt){}
160 virtual bool checkInitialized(){return true;}
161 virtual void copyBackToSoftBodies(bool bMove = true) {}
162 };
163
164 #endif /* btDeformableBodySolver_h */
btDeformableBodySolver::solveContactConstraints
btScalar solveContactConstraints(btCollisionObject **deformableBodies, int numDeformableBodies)
Definition: btDeformableBodySolver.cpp:237
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:50
btDeformableBodySolver::setupDeformableSolve
void setupDeformableSolve(bool implicit)
Definition: btDeformableBodySolver.cpp:314
btDeformableBodySolver::m_ddv
TVStack m_ddv
Definition: btDeformableBodySolver.h:37
btDeformableBodySolver::updateSoftBodies
virtual void updateSoftBodies()
Perform necessary per-step updates of soft bodies such as recomputing normals and bounding boxes.
Definition: btDeformableBodySolver.cpp:463
btDeformableBodySolver::computeDescentStep
btScalar computeDescentStep(TVStack &ddv, const TVStack &residual, bool verbose=false)
Definition: btDeformableBodySolver.cpp:153
btContactSolverInfo
Definition: btContactSolverInfo.h:73
btSoftBodySolvers.h
btDeformableBodySolver::m_backupVelocity
TVStack m_backupVelocity
Definition: btDeformableBodySolver.h:40
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btDeformableBodySolver::kineticEnergy
btScalar kineticEnergy()
Definition: btDeformableBodySolver.cpp:108
btSoftBody::defaultCollisionHandler
void defaultCollisionHandler(const btCollisionObjectWrapper *pcoWrap)
Definition: btSoftBody.cpp:3613
btCollisionObjectWrapper
Definition: btCollisionObjectWrapper.h:18
btDeformableBodySolver::revertDv
void revertDv()
Definition: btDeformableBodySolver.cpp:135
btDeformableBodySolver::processCollision
virtual void processCollision(btSoftBody *softBody, btSoftBody *otherSoftBody)
Process a collision between two soft bodies.
Definition: btDeformableBodySolver.h:119
btDeformableBodySolver::updateEnergy
void updateEnergy(btScalar scale)
Definition: btDeformableBodySolver.cpp:143
btDeformableBodySolver::reinitialize
void reinitialize(const btAlignedObjectArray< btSoftBody * > &softBodies, btScalar dt)
Definition: btDeformableBodySolver.cpp:206
btConjugateGradient< btDeformableBackwardEulerObjective >
btDeformableBodySolver::updateDv
void updateDv(btScalar scale=1)
Definition: btDeformableBodySolver.cpp:193
btDeformableBodySolver::m_numNodes
int m_numNodes
Definition: btDeformableBodySolver.h:34
btDeformableBodySolver::btDeformableBodySolver
btDeformableBodySolver()
Definition: btDeformableBodySolver.cpp:22
btDeformableBodySolver::copyBackToSoftBodies
virtual void copyBackToSoftBodies(bool bMove=true)
Copy necessary data back to the original soft body source objects.
Definition: btDeformableBodySolver.h:161
btDeformableBackwardEulerObjective
Definition: btDeformableBackwardEulerObjective.h:31
btDeformableBodySolver::optimize
virtual void optimize(btAlignedObjectArray< btSoftBody * > &softBodies, bool forceUpdate=false)
Optimize soft bodies in this solver.
Definition: btDeformableBodySolver.h:158
btDeformableBodySolver::setLineSearch
void setLineSearch(bool lineSearch)
Definition: btDeformableBodySolver.cpp:482
btDeformableBodySolver::m_backup_dv
TVStack m_backup_dv
Definition: btDeformableBodySolver.h:36
btSoftBodySolver::DEFORMABLE_SOLVER
@ DEFORMABLE_SOLVER
Definition: btSoftBodySolvers.h:39
btDeformableBodySolver::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition: btDeformableBodySolver.h:32
btDeformableBodySolver::solveConstraints
virtual void solveConstraints(btScalar dt)
Solve constraints for a set of soft bodies.
Definition: btDeformableBodySolver.h:159
btDeformableBodySolver::copySoftBodyToVertexBuffer
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
Definition: btDeformableBodySolver.h:110
btDeformableBodySolver::setConstraints
void setConstraints()
Definition: btDeformableBodySolver.cpp:231
btMultiBodyLinkCollider.h
btSoftBodySolver::SolverTypes
SolverTypes
Definition: btSoftBodySolvers.h:32
btDeformableBodySolver::backupVelocity
void backupVelocity()
Definition: btDeformableBodySolver.cpp:301
btDeformableMultiBodyDynamicsWorld.h
btDeformableBodySolver::m_newtonTolerance
btScalar m_newtonTolerance
Definition: btDeformableBodySolver.h:45
btDeformableBodySolver::computeStep
void computeStep(TVStack &ddv, const TVStack &residual)
Definition: btDeformableBodySolver.cpp:201
btVertexBufferDescriptor
Definition: btSoftBodySolverVertexBuffer.h:20
btDeformableBodySolver::m_implicit
bool m_implicit
Definition: btDeformableBodySolver.h:43
btDeformableBodySolver::m_cg
btConjugateGradient< btDeformableBackwardEulerObjective > m_cg
Definition: btDeformableBodySolver.h:42
btDeformableBodySolver::m_lineSearch
bool m_lineSearch
Definition: btDeformableBodySolver.h:46
btDeformableBodySolver::m_maxNewtonIterations
int m_maxNewtonIterations
Definition: btDeformableBodySolver.h:44
btDeformableBodySolver::m_dv
TVStack m_dv
Definition: btDeformableBodySolver.h:35
btDeformableBodySolver::m_residual
TVStack m_residual
Definition: btDeformableBodySolver.h:38
btDeformableBodySolver::updateNodes
bool updateNodes()
Definition: btDeformableBodySolver.cpp:356
btAlignedObjectArray< btVector3 >
btDeformableBodySolver::m_dt
btScalar m_dt
Definition: btDeformableBodySolver.h:41
btDeformableBodySolver::updateVelocity
void updateVelocity()
Definition: btDeformableBodySolver.cpp:255
btDeformableBodySolver::checkInitialized
virtual bool checkInitialized()
Ensure that this solver is initialized.
Definition: btDeformableBodySolver.h:160
btDeformableBodySolver::updateTempPosition
void updateTempPosition()
Definition: btDeformableBodySolver.cpp:281
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:73
btDeformableBodySolver::predictMotion
virtual void predictMotion(btScalar solverdt)
Predict motion of soft bodies into next timestep.
Definition: btDeformableBodySolver.cpp:370
btDeformableBodySolver::solveSplitImpulse
btScalar solveSplitImpulse(const btContactSolverInfo &infoGlobal)
Definition: btDeformableBodySolver.cpp:244
btSoftBodySolver
Definition: btSoftBodySolvers.h:29
btDeformableBodySolver
Definition: btDeformableBodySolver.h:31
btMultiBodyConstraint.h
btDeformableBodySolver::revertVelocity
void revertVelocity()
Definition: btDeformableBodySolver.cpp:343
btDeformableBodySolver::updateState
void updateState()
Definition: btDeformableBodySolver.cpp:187
btDeformableBodySolver::predictDeformableMotion
void predictDeformableMotion(btSoftBody *psb, btScalar dt)
Definition: btDeformableBodySolver.cpp:386
btDeformableBodySolver::m_objective
btDeformableBackwardEulerObjective * m_objective
Definition: btDeformableBodySolver.h:50
btDeformableBodySolver::m_softBodies
btAlignedObjectArray< btSoftBody * > m_softBodies
Definition: btDeformableBodySolver.h:39
btDeformableBodySolver::backupDv
void backupDv()
Definition: btDeformableBodySolver.cpp:126
btDeformableBodySolver::solveDeformableConstraints
virtual void solveDeformableConstraints(btScalar solverdt)
Definition: btDeformableBodySolver.cpp:37
btDeformableBodySolver::splitImpulseSetup
void splitImpulseSetup(const btContactSolverInfo &infoGlobal)
Definition: btDeformableBodySolver.cpp:250
btDeformableBodySolver::setImplicit
void setImplicit(bool implicit)
Definition: btDeformableBodySolver.cpp:476
btDeformableBodySolver::~btDeformableBodySolver
virtual ~btDeformableBodySolver()
Definition: btDeformableBodySolver.cpp:32
btDeformableBackwardEulerObjective.h
btDeformableBodySolver::getSolverType
virtual SolverTypes getSolverType() const
Return the type of the solver.
Definition: btDeformableBodySolver.h:56
btDeformableBodySolver::processCollision
virtual void processCollision(btSoftBody *softBody, const btCollisionObjectWrapper *collisionObjectWrap)
Definition: btDeformableBodySolver.h:113
btDeformableMultiBodyDynamicsWorld
Definition: btDeformableMultiBodyDynamicsWorld.h:38