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  }
37 
39  {
40  }
41 
42  virtual void addScaledForces(btScalar scale, TVStack& force)
43  {
44  addScaledElasticForce(scale, force);
45  }
46 
47  virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
48  {
49  addScaledElasticForce(scale, force);
50  }
51 
52  virtual void addScaledDampingForce(btScalar scale, TVStack& force)
53  {
54  }
55 
56  virtual void addScaledElasticForce(btScalar scale, TVStack& force)
57  {
58  int numNodes = getNumNodes();
59  btAssert(numNodes <= force.size());
60  btVector3 grad_N_hat_1st_col = btVector3(-1,-1,-1);
61  for (int i = 0; i < m_softBodies.size(); ++i)
62  {
63  btSoftBody* psb = m_softBodies[i];
64  for (int j = 0; j < psb->m_tetras.size(); ++j)
65  {
66  btSoftBody::Tetra& tetra = psb->m_tetras[j];
67  btMatrix3x3 P;
68  firstPiola(tetra.m_F,P);
69  btVector3 force_on_node0 = P * (tetra.m_Dm_inverse.transpose()*grad_N_hat_1st_col);
70  btMatrix3x3 force_on_node123 = P * tetra.m_Dm_inverse.transpose();
71 
72  btSoftBody::Node* node0 = tetra.m_n[0];
73  btSoftBody::Node* node1 = tetra.m_n[1];
74  btSoftBody::Node* node2 = tetra.m_n[2];
75  btSoftBody::Node* node3 = tetra.m_n[3];
76  size_t id0 = node0->index;
77  size_t id1 = node1->index;
78  size_t id2 = node2->index;
79  size_t id3 = node3->index;
80 
81  // elastic force
82  // explicit elastic force
83  btScalar scale1 = scale * tetra.m_element_measure;
84  force[id0] -= scale1 * force_on_node0;
85  force[id1] -= scale1 * force_on_node123.getColumn(0);
86  force[id2] -= scale1 * force_on_node123.getColumn(1);
87  force[id3] -= scale1 * force_on_node123.getColumn(2);
88  }
89  }
90  }
91 
92  void firstPiola(const btMatrix3x3& F, btMatrix3x3& P)
93  {
94  // btMatrix3x3 JFinvT = F.adjoint();
95  btScalar J = F.determinant();
96  P = F.adjoint().transpose() * (m_lambda * (J-1));
97  if (m_mu > SIMD_EPSILON)
98  {
99  btMatrix3x3 R,S;
100  if (J < 1024 * SIMD_EPSILON)
101  R.setIdentity();
102  else
103  PolarDecomposition(F, R, S); // this QR is not robust, consider using implicit shift svd
104  /*https://fuchuyuan.github.io/research/svd/paper.pdf*/
105  P += (F-R) * 2 * m_mu;
106  }
107  }
108 
109  virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
110  {
111  }
112 
113  virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
114  {
115  }
116 
118  {
119  return BT_COROTATED_FORCE;
120  }
121 
122 };
123 
124 
125 #endif /* btCorotated_h */
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:543
BT_COROTATED_FORCE
Definition: btDeformableLagrangianForce.h:27
btDeformableCorotatedForce::addScaledForces
virtual void addScaledForces(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:42
btSoftBody::m_tetras
tTetraArray m_tetras
Definition: btSoftBody.h:782
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:109
btDeformableCorotatedForce
Definition: btDeformableCorotatedForce.h:28
btSoftBody::Node
Definition: btSoftBody.h:255
btDeformableCorotatedForce::firstPiola
void firstPiola(const btMatrix3x3 &F, btMatrix3x3 &P)
Definition: btDeformableCorotatedForce.h:92
btSoftBody::Tetra::m_F
btMatrix3x3 m_F
Definition: btSoftBody.h:304
btDeformableCorotatedForce::m_mu
btScalar m_mu
Definition: btDeformableCorotatedForce.h:32
btSoftBody::Tetra
Definition: btSoftBody.h:295
btDeformableCorotatedForce::addScaledElasticForce
virtual void addScaledElasticForce(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:56
btAssert
#define btAssert(x)
Definition: btScalar.h:153
btSoftBody::Tetra::m_element_measure
btScalar m_element_measure
Definition: btSoftBody.h:305
btDeformableLagrangianForce::m_softBodies
btAlignedObjectArray< btSoftBody * > m_softBodies
Definition: btDeformableLagrangianForce.h:41
btDeformableCorotatedForce::addScaledDampingForceDifferential
virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack &dv, TVStack &df)
Definition: btDeformableCorotatedForce.h:113
btDeformableCorotatedForce::btDeformableCorotatedForce
btDeformableCorotatedForce(btScalar mu, btScalar lambda)
Definition: btDeformableCorotatedForce.h:38
btDeformableLagrangianForce
Definition: btDeformableLagrangianForce.h:37
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:46
btMatrix3x3::determinant
btScalar determinant() const
Return the determinant of the matrix.
Definition: btMatrix3x3.h:1006
btMatrix3x3::transpose
btMatrix3x3 transpose() const
Return the transpose of the matrix.
Definition: btMatrix3x3.h:1033
btPolarDecomposition.h
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:80
btDeformableCorotatedForce::getForceType
virtual btDeformableLagrangianForceType getForceType()
Definition: btDeformableCorotatedForce.h:117
btSoftBody::Node::index
int index
Definition: btSoftBody.h:268
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:14
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:72
btSoftBody::Tetra::m_n
Node * m_n[4]
Definition: btSoftBody.h:297
btMatrix3x3::adjoint
btMatrix3x3 adjoint() const
Return the adjoint of the matrix.
Definition: btMatrix3x3.h:1069
btMatrix3x3::setIdentity
void setIdentity()
Set the matrix to the identity.
Definition: btMatrix3x3.h:321
btDeformableLagrangianForce::getNumNodes
virtual int getNumNodes()
Definition: btDeformableLagrangianForce.h:72
btDeformableCorotatedForce::addScaledExplicitForce
virtual void addScaledExplicitForce(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:47
btDeformableLagrangianForceType
btDeformableLagrangianForceType
Definition: btDeformableLagrangianForce.h:23
btDeformableCorotatedForce::addScaledDampingForce
virtual void addScaledDampingForce(btScalar scale, TVStack &force)
Definition: btDeformableCorotatedForce.h:52
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:303