Bullet Collision Detection & Physics Library
btDeformableCorotatedForce.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_COROTATED_H
17 #define BT_COROTATED_H
18 
21 
22 static inline int PolarDecomposition(const btMatrix3x3& m, btMatrix3x3& q, btMatrix3x3& s)
23 {
24  static const btPolarDecomposition polar;
25  return polar.decompose(m, q, s);
26 }
27 
29 {
30 public:
34  {
35  }
36 
38  {
39  }
40 
41  virtual void addScaledForces(btScalar scale, TVStack& force)
42  {
43  addScaledElasticForce(scale, force);
44  }
45 
46  virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
47  {
48  addScaledElasticForce(scale, force);
49  }
50 
51  virtual void addScaledDampingForce(btScalar scale, TVStack& force)
52  {
53  }
54 
55  virtual void addScaledElasticForce(btScalar scale, TVStack& force)
56  {
57  int numNodes = getNumNodes();
58  btAssert(numNodes <= force.size());
59  btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
60  for (int i = 0; i < m_softBodies.size(); ++i)
61  {
62  btSoftBody* psb = m_softBodies[i];
63  for (int j = 0; j < psb->m_tetras.size(); ++j)
64  {
65  btSoftBody::Tetra& tetra = psb->m_tetras[j];
66  btMatrix3x3 P;
67  firstPiola(tetra.m_F, P);
68  btVector3 force_on_node0 = P * (tetra.m_Dm_inverse.transpose() * grad_N_hat_1st_col);
69  btMatrix3x3 force_on_node123 = P * tetra.m_Dm_inverse.transpose();
70 
71  btSoftBody::Node* node0 = tetra.m_n[0];
72  btSoftBody::Node* node1 = tetra.m_n[1];
73  btSoftBody::Node* node2 = tetra.m_n[2];
74  btSoftBody::Node* node3 = tetra.m_n[3];
75  size_t id0 = node0->index;
76  size_t id1 = node1->index;
77  size_t id2 = node2->index;
78  size_t id3 = node3->index;
79 
80  // elastic force
81  // explicit elastic force
82  btScalar scale1 = scale * tetra.m_element_measure;
83  force[id0] -= scale1 * force_on_node0;
84  force[id1] -= scale1 * force_on_node123.getColumn(0);
85  force[id2] -= scale1 * force_on_node123.getColumn(1);
86  force[id3] -= scale1 * force_on_node123.getColumn(2);
87  }
88  }
89  }
90 
91  void firstPiola(const btMatrix3x3& F, btMatrix3x3& P)
92  {
93  // btMatrix3x3 JFinvT = F.adjoint();
94  btScalar J = F.determinant();
95  P = F.adjoint().transpose() * (m_lambda * (J - 1));
96  if (m_mu > SIMD_EPSILON)
97  {
98  btMatrix3x3 R, S;
99  if (J < 1024 * SIMD_EPSILON)
100  R.setIdentity();
101  else
102  PolarDecomposition(F, R, S); // this QR is not robust, consider using implicit shift svd
103  /*https://fuchuyuan.github.io/research/svd/paper.pdf*/
104  P += (F - R) * 2 * m_mu;
105  }
106  }
107 
108  virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
109  {
110  }
111 
112  virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
113  {
114  }
115 
117 
119  {
120  return BT_COROTATED_FORCE;
121  }
122 };
123 
124 #endif /* btCorotated_h */
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:543
BT_COROTATED_FORCE
@ BT_COROTATED_FORCE
Definition: btDeformableLagrangianForce.h:27
btDeformableCorotatedForce::addScaledForces
virtual void addScaledForces(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:41
btSoftBody::m_tetras
tTetraArray m_tetras
Definition: btSoftBody.h:817
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btDeformableCorotatedForce::addScaledElasticForceDifferential
virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack &dx, TVStack &df)
Definition: btDeformableCorotatedForce.h:108
btDeformableCorotatedForce
Definition: btDeformableCorotatedForce.h:29
btSoftBody::Node
Definition: btSoftBody.h:268
btDeformableCorotatedForce::firstPiola
void firstPiola(const btMatrix3x3 &F, btMatrix3x3 &P)
Definition: btDeformableCorotatedForce.h:91
btSoftBody::Tetra::m_F
btMatrix3x3 m_F
Definition: btSoftBody.h:325
btDeformableCorotatedForce::m_mu
btScalar m_mu
Definition: btDeformableCorotatedForce.h:32
btSoftBody::Tetra
Definition: btSoftBody.h:317
btDeformableCorotatedForce::addScaledElasticForce
virtual void addScaledElasticForce(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:55
btAssert
#define btAssert(x)
Definition: btScalar.h:153
btSoftBody::Tetra::m_element_measure
btScalar m_element_measure
Definition: btSoftBody.h:326
btDeformableCorotatedForce::addScaledDampingForceDifferential
virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack &dv, TVStack &df)
Definition: btDeformableCorotatedForce.h:112
btDeformableCorotatedForce::btDeformableCorotatedForce
btDeformableCorotatedForce(btScalar mu, btScalar lambda)
Definition: btDeformableCorotatedForce.h:37
btDeformableLagrangianForce
Definition: btDeformableLagrangianForce.h:39
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:50
btMatrix3x3::determinant
btScalar determinant() const
Return the determinant of the matrix.
Definition: btMatrix3x3.h:1022
btMatrix3x3::transpose
btMatrix3x3 transpose() const
Return the transpose of the matrix.
Definition: btMatrix3x3.h:1049
btPolarDecomposition.h
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btDeformableCorotatedForce::getForceType
virtual btDeformableLagrangianForceType getForceType()
Definition: btDeformableCorotatedForce.h:118
btSoftBody::Node::index
int index
Definition: btSoftBody.h:280
btDeformableCorotatedForce::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition: btDeformableCorotatedForce.h:31
btPolarDecomposition
This class is used to compute the polar decomposition of a matrix.
Definition: btPolarDecomposition.h:15
btAlignedObjectArray< btVector3 >
PolarDecomposition
static int PolarDecomposition(const btMatrix3x3 &m, btMatrix3x3 &q, btMatrix3x3 &s)
Definition: btDeformableCorotatedForce.h:22
btDeformableCorotatedForce::btDeformableCorotatedForce
btDeformableCorotatedForce()
Definition: btDeformableCorotatedForce.h:33
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:75
btSoftBody::Tetra::m_n
Node * m_n[4]
Definition: btSoftBody.h:318
btMatrix3x3::adjoint
btMatrix3x3 adjoint() const
Return the adjoint of the matrix.
Definition: btMatrix3x3.h:1085
btMatrix3x3::setIdentity
void setIdentity()
Set the matrix to the identity.
Definition: btMatrix3x3.h:323
btDeformableLagrangianForce::m_softBodies
btAlignedObjectArray< btSoftBody * > m_softBodies
Definition: btDeformableLagrangianForce.h:42
btDeformableLagrangianForce::getNumNodes
virtual int getNumNodes()
Definition: btDeformableLagrangianForce.h:78
btDeformableCorotatedForce::addScaledExplicitForce
virtual void addScaledExplicitForce(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:46
btDeformableLagrangianForceType
btDeformableLagrangianForceType
Definition: btDeformableLagrangianForce.h:24
btDeformableCorotatedForce::addScaledDampingForce
virtual void addScaledDampingForce(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:51
btDeformableCorotatedForce::buildDampingForceDifferentialDiagonal
virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack &diagA)
Definition: btDeformableCorotatedForce.h:116
btDeformableLagrangianForce.h
btPolarDecomposition::decompose
unsigned int decompose(const btMatrix3x3 &a, btMatrix3x3 &u, btMatrix3x3 &h) const
Decomposes a matrix into orthogonal and symmetric, positive-definite parts.
Definition: btPolarDecomposition.cpp:38
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btDeformableCorotatedForce::m_lambda
btScalar m_lambda
Definition: btDeformableCorotatedForce.h:32
btSoftBody::Tetra::m_Dm_inverse
btMatrix3x3 m_Dm_inverse
Definition: btSoftBody.h:324