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
22static 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{
30public:
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];
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
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 */
static int PolarDecomposition(const btMatrix3x3 &m, btMatrix3x3 &q, btMatrix3x3 &s)
btDeformableLagrangianForceType
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define SIMD_EPSILON
Definition: btScalar.h:543
#define btAssert(x)
Definition: btScalar.h:153
int size() const
return the number of elements in the array
virtual void addScaledElasticForce(btScalar scale, TVStack &force)
virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack &diagA)
virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack &dv, TVStack &df)
virtual btDeformableLagrangianForceType getForceType()
virtual void addScaledDampingForce(btScalar scale, TVStack &force)
btAlignedObjectArray< btVector3 > TVStack
virtual void addScaledForces(btScalar scale, TVStack &force)
void firstPiola(const btMatrix3x3 &F, btMatrix3x3 &P)
virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack &dx, TVStack &df)
virtual void addScaledExplicitForce(btScalar scale, TVStack &force)
btDeformableCorotatedForce(btScalar mu, btScalar lambda)
btAlignedObjectArray< btSoftBody * > m_softBodies
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:50
btMatrix3x3 adjoint() const
Return the adjoint of the matrix.
Definition: btMatrix3x3.h:1085
btMatrix3x3 transpose() const
Return the transpose of the matrix.
Definition: btMatrix3x3.h:1049
void setIdentity()
Set the matrix to the identity.
Definition: btMatrix3x3.h:323
btScalar determinant() const
Return the determinant of the matrix.
Definition: btMatrix3x3.h:1022
This class is used to compute the polar decomposition of a matrix.
unsigned int decompose(const btMatrix3x3 &a, btMatrix3x3 &u, btMatrix3x3 &h) const
Decomposes a matrix into orthogonal and symmetric, positive-definite parts.
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:75
tTetraArray m_tetras
Definition: btSoftBody.h:819
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btScalar m_element_measure
Definition: btSoftBody.h:326
btMatrix3x3 m_Dm_inverse
Definition: btSoftBody.h:324
btMatrix3x3 m_F
Definition: btSoftBody.h:325