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

Bullet Collision Detection & Physics Library

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"
19 #include "btDeformableLagrangianForce.h"
20 #include "btDeformableMassSpringForce.h"
21 #include "btDeformableGravityForce.h"
22 #include "btDeformableCorotatedForce.h"
23 #include "btDeformableMousePickingForce.h"
24 #include "btDeformableLinearElasticityForce.h"
25 #include "btDeformableNeoHookeanForce.h"
26 #include "btDeformableContactProjection.h"
27 #include "btPreconditioner.h"
28 #include "btDeformableMultiBodyDynamicsWorld.h"
29 #include "LinearMath/btQuickprof.h"
30
31 class btDeformableBackwardEulerObjective
32 {
33 public:
34 typedef btAlignedObjectArray<btVector3> TVStack;
35 btScalar m_dt;
36 btAlignedObjectArray<btDeformableLagrangianForce*> m_lf;
37 btAlignedObjectArray<btSoftBody*>& m_softBodies;
38 Preconditioner* m_preconditioner;
39 btDeformableContactProjection m_projection;
40 const TVStack& m_backupVelocity;
41 btAlignedObjectArray<btSoftBody::Node*> m_nodes;
42 bool m_implicit;
43 MassPreconditioner* m_massPreconditioner;
44 KKTPreconditioner* m_KKTPreconditioner;
45
46 btDeformableBackwardEulerObjective(btAlignedObjectArray<btSoftBody*>& softBodies, const TVStack& backup_v);
47
48 virtual ~btDeformableBackwardEulerObjective();
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
79 void applyDynamicFriction(TVStack& r);
80
81 // add dv to velocity
82 void updateVelocity(const TVStack& dv);
83
84 //set constraints as projections
85 void setConstraints(const btContactSolverInfo& infoGlobal);
86
87 // update the projections and project the residual
88 void project(TVStack& r)
89 {
90 BT_PROFILE("project");
91 m_projection.project(r);
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
123 const btAlignedObjectArray<btSoftBody::Node*>* getIndices() const
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
134 btScalar totalEnergy(btScalar dt);
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 {
160 const LagrangeMultiplier& lm = m_projection.m_lagrangeMultipliers[i];
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 */
btDeformableContactProjection.h
btDeformableCorotatedForce.h
btDeformableGravityForce.h
btDeformableLagrangianForce.h
btDeformableLinearElasticityForce.h
btDeformableMassSpringForce.h
btDeformableMousePickingForce.h
btDeformableMultiBodyDynamicsWorld.h
btDeformableNeoHookeanForce.h
btPreconditioner.h
btQuickprof.h
BT_PROFILE
#define BT_PROFILE(name)
Definition: btQuickprof.h:198
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
KKTPreconditioner
Definition: btPreconditioner.h:85
MassPreconditioner
Definition: btPreconditioner.h:45
Preconditioner
Definition: btPreconditioner.h:20
btAlignedObjectArray< btVector3 >
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition: btAlignedObjectArray.h:203
btAlignedObjectArray::clear
void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0),...
Definition: btAlignedObjectArray.h:176
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:257
btDeformableBackwardEulerObjective
Definition: btDeformableBackwardEulerObjective.h:32
btDeformableBackwardEulerObjective::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition: btDeformableBackwardEulerObjective.h:34
btDeformableBackwardEulerObjective::btDeformableBackwardEulerObjective
btDeformableBackwardEulerObjective(btAlignedObjectArray< btSoftBody * > &softBodies, const TVStack &backup_v)
Definition: btDeformableBackwardEulerObjective.cpp:20
btDeformableBackwardEulerObjective::calculateContactForce
void calculateContactForce(const TVStack &dv, const TVStack &rhs, TVStack &f)
Definition: btDeformableBackwardEulerObjective.h:172
btDeformableBackwardEulerObjective::precondition
void precondition(const TVStack &x, TVStack &b)
Definition: btDeformableBackwardEulerObjective.h:95
btDeformableBackwardEulerObjective::applyExplicitForce
void applyExplicitForce(TVStack &force)
Definition: btDeformableBackwardEulerObjective.cpp:213
btDeformableBackwardEulerObjective::m_projection
btDeformableContactProjection m_projection
Definition: btDeformableBackwardEulerObjective.h:39
btDeformableBackwardEulerObjective::m_backupVelocity
const TVStack & m_backupVelocity
Definition: btDeformableBackwardEulerObjective.h:40
btDeformableBackwardEulerObjective::setConstraints
void setConstraints(const btContactSolverInfo &infoGlobal)
Definition: btDeformableBackwardEulerObjective.cpp:291
btDeformableBackwardEulerObjective::updateVelocity
void updateVelocity(const TVStack &dv)
Definition: btDeformableBackwardEulerObjective.cpp:124
btDeformableBackwardEulerObjective::applyForce
void applyForce(TVStack &force, bool setZero)
Definition: btDeformableBackwardEulerObjective.cpp:137
btDeformableBackwardEulerObjective::computeResidual
void computeResidual(btScalar dt, TVStack &residual)
Definition: btDeformableBackwardEulerObjective.cpp:174
btDeformableBackwardEulerObjective::setImplicit
void setImplicit(bool implicit)
Definition: btDeformableBackwardEulerObjective.h:128
btDeformableBackwardEulerObjective::m_lf
btAlignedObjectArray< btDeformableLagrangianForce * > m_lf
Definition: btDeformableBackwardEulerObjective.h:36
btDeformableBackwardEulerObjective::m_dt
btScalar m_dt
Definition: btDeformableBackwardEulerObjective.h:35
btDeformableBackwardEulerObjective::getIndices
const btAlignedObjectArray< btSoftBody::Node * > * getIndices() const
Definition: btDeformableBackwardEulerObjective.h:123
btDeformableBackwardEulerObjective::updateId
virtual void updateId()
Definition: btDeformableBackwardEulerObjective.h:101
btDeformableBackwardEulerObjective::m_nodes
btAlignedObjectArray< btSoftBody::Node * > m_nodes
Definition: btDeformableBackwardEulerObjective.h:41
btDeformableBackwardEulerObjective::totalEnergy
btScalar totalEnergy(btScalar dt)
Definition: btDeformableBackwardEulerObjective.cpp:203
btDeformableBackwardEulerObjective::applyDynamicFriction
void applyDynamicFriction(TVStack &r)
Definition: btDeformableBackwardEulerObjective.cpp:296
btDeformableBackwardEulerObjective::addLagrangeMultiplier
void addLagrangeMultiplier(const TVStack &vec, TVStack &extended_vec)
Definition: btDeformableBackwardEulerObjective.h:136
btDeformableBackwardEulerObjective::m_massPreconditioner
MassPreconditioner * m_massPreconditioner
Definition: btDeformableBackwardEulerObjective.h:43
btDeformableBackwardEulerObjective::m_softBodies
btAlignedObjectArray< btSoftBody * > & m_softBodies
Definition: btDeformableBackwardEulerObjective.h:37
btDeformableBackwardEulerObjective::computeStep
void computeStep(TVStack &dv, const TVStack &residual, const btScalar &dt)
btDeformableBackwardEulerObjective::initialGuess
void initialGuess(TVStack &dv, const TVStack &residual)
Definition: btDeformableBackwardEulerObjective.cpp:276
btDeformableBackwardEulerObjective::computeNorm
btScalar computeNorm(const TVStack &residual) const
Definition: btDeformableBackwardEulerObjective.cpp:193
btDeformableBackwardEulerObjective::project
void project(TVStack &r)
Definition: btDeformableBackwardEulerObjective.h:88
btDeformableBackwardEulerObjective::m_preconditioner
Preconditioner * m_preconditioner
Definition: btDeformableBackwardEulerObjective.h:38
btDeformableBackwardEulerObjective::multiply
void multiply(const TVStack &x, TVStack &b) const
Definition: btDeformableBackwardEulerObjective.cpp:69
btDeformableBackwardEulerObjective::~btDeformableBackwardEulerObjective
virtual ~btDeformableBackwardEulerObjective()
Definition: btDeformableBackwardEulerObjective.cpp:28
btDeformableBackwardEulerObjective::m_implicit
bool m_implicit
Definition: btDeformableBackwardEulerObjective.h:42
btDeformableBackwardEulerObjective::setDt
void setDt(btScalar dt)
Definition: btDeformableBackwardEulerObjective.cpp:64
btDeformableBackwardEulerObjective::reinitialize
void reinitialize(bool nodeUpdated, btScalar dt)
Definition: btDeformableBackwardEulerObjective.cpp:34
btDeformableBackwardEulerObjective::addLagrangeMultiplierRHS
void addLagrangeMultiplierRHS(const TVStack &residual, const TVStack &m_dv, TVStack &extended_residual)
Definition: btDeformableBackwardEulerObjective.h:150
btDeformableBackwardEulerObjective::m_KKTPreconditioner
KKTPreconditioner * m_KKTPreconditioner
Definition: btDeformableBackwardEulerObjective.h:44
btDeformableBackwardEulerObjective::initialize
void initialize()
Definition: btDeformableBackwardEulerObjective.h:50
btDeformableContactProjection
Definition: btDeformableContactProjection.h:38
btDeformableContactProjection::m_lagrangeMultipliers
btAlignedObjectArray< LagrangeMultiplier > m_lagrangeMultipliers
Definition: btDeformableContactProjection.h:52
btDeformableContactProjection::project
virtual void project(TVStack &x)
Definition: btDeformableContactProjection.cpp:153
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:75
btSoftBody::m_faces
tFaceArray m_faces
Definition: btSoftBody.h:815
btSoftBody::m_nodes
tNodeArray m_nodes
Definition: btSoftBody.h:812
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
LagrangeMultiplier
Definition: btDeformableContactProjection.h:29
LagrangeMultiplier::m_num_nodes
int m_num_nodes
Definition: btDeformableContactProjection.h:31
LagrangeMultiplier::m_indices
int m_indices[3]
Definition: btDeformableContactProjection.h:34
LagrangeMultiplier::m_num_constraints
int m_num_constraints
Definition: btDeformableContactProjection.h:30
LagrangeMultiplier::m_dirs
btVector3 m_dirs[3]
Definition: btDeformableContactProjection.h:33
LagrangeMultiplier::m_weights
btScalar m_weights[3]
Definition: btDeformableContactProjection.h:32
btContactSolverInfo
Definition: btContactSolverInfo.h:76
btSoftBody::Node
Definition: btSoftBody.h:268
btSoftBody::Node::m_im
btScalar m_im
Definition: btSoftBody.h:275