[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletSoftBody / btDeformableCorotatedForce.h
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
19 #include "btDeformableLagrangianForce.h"
20 #include "LinearMath/btPolarDecomposition.h"
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
28 class btDeformableCorotatedForce : public btDeformableLagrangianForce
29 {
30 public:
31         typedef btAlignedObjectArray<btVector3> TVStack;
32         btScalar m_mu, m_lambda;
33         btDeformableCorotatedForce() : m_mu(1), m_lambda(1)
34         {
35         }
36
37         btDeformableCorotatedForce(btScalar mu, btScalar lambda) : m_mu(mu), m_lambda(lambda)
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
116         virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
117
118         virtual btDeformableLagrangianForceType getForceType()
119         {
120                 return BT_COROTATED_FORCE;
121         }
122 };
123
124 #endif /* btCorotated_h */