btDeformableNeoHookeanForce.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
  3. Bullet Continuous Collision Detection and Physics Library
  4. Copyright (c) 2019 Google Inc. http://bulletphysics.org
  5. This software is provided 'as-is', without any express or implied warranty.
  6. In no event will the authors be held liable for any damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it freely,
  9. subject to the following restrictions:
  10. 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.
  11. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  12. 3. This notice may not be removed or altered from any source distribution.
  13. */
  14. #ifndef BT_NEOHOOKEAN_H
  15. #define BT_NEOHOOKEAN_H
  16. #include "btDeformableLagrangianForce.h"
  17. #include "LinearMath/btQuickprof.h"
  18. #include "LinearMath/btImplicitQRSVD.h"
  19. // This energy is as described in https://graphics.pixar.com/library/StableElasticity/paper.pdf
  20. class btDeformableNeoHookeanForce : public btDeformableLagrangianForce
  21. {
  22. public:
  23. typedef btAlignedObjectArray<btVector3> TVStack;
  24. btScalar m_mu, m_lambda; // Lame Parameters
  25. btScalar m_E, m_nu; // Young's modulus and Poisson ratio
  26. btScalar m_mu_damp, m_lambda_damp;
  27. btDeformableNeoHookeanForce() : m_mu(1), m_lambda(1)
  28. {
  29. btScalar damping = 0.05;
  30. m_mu_damp = damping * m_mu;
  31. m_lambda_damp = damping * m_lambda;
  32. updateYoungsModulusAndPoissonRatio();
  33. }
  34. btDeformableNeoHookeanForce(btScalar mu, btScalar lambda, btScalar damping = 0.05) : m_mu(mu), m_lambda(lambda)
  35. {
  36. m_mu_damp = damping * m_mu;
  37. m_lambda_damp = damping * m_lambda;
  38. updateYoungsModulusAndPoissonRatio();
  39. }
  40. void updateYoungsModulusAndPoissonRatio()
  41. {
  42. // conversion from Lame Parameters to Young's modulus and Poisson ratio
  43. // https://en.wikipedia.org/wiki/Lam%C3%A9_parameters
  44. m_E = m_mu * (3 * m_lambda + 2 * m_mu) / (m_lambda + m_mu);
  45. m_nu = m_lambda * 0.5 / (m_mu + m_lambda);
  46. }
  47. void updateLameParameters()
  48. {
  49. // conversion from Young's modulus and Poisson ratio to Lame Parameters
  50. // https://en.wikipedia.org/wiki/Lam%C3%A9_parameters
  51. m_mu = m_E * 0.5 / (1 + m_nu);
  52. m_lambda = m_E * m_nu / ((1 + m_nu) * (1 - 2 * m_nu));
  53. }
  54. void setYoungsModulus(btScalar E)
  55. {
  56. m_E = E;
  57. updateLameParameters();
  58. }
  59. void setPoissonRatio(btScalar nu)
  60. {
  61. m_nu = nu;
  62. updateLameParameters();
  63. }
  64. void setDamping(btScalar damping)
  65. {
  66. m_mu_damp = damping * m_mu;
  67. m_lambda_damp = damping * m_lambda;
  68. }
  69. void setLameParameters(btScalar mu, btScalar lambda)
  70. {
  71. m_mu = mu;
  72. m_lambda = lambda;
  73. updateYoungsModulusAndPoissonRatio();
  74. }
  75. virtual void addScaledForces(btScalar scale, TVStack& force)
  76. {
  77. addScaledDampingForce(scale, force);
  78. addScaledElasticForce(scale, force);
  79. }
  80. virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
  81. {
  82. addScaledElasticForce(scale, force);
  83. }
  84. // The damping matrix is calculated using the time n state as described in https://www.math.ucla.edu/~jteran/papers/GSSJT15.pdf to allow line search
  85. virtual void addScaledDampingForce(btScalar scale, TVStack& force)
  86. {
  87. if (m_mu_damp == 0 && m_lambda_damp == 0)
  88. return;
  89. int numNodes = getNumNodes();
  90. btAssert(numNodes <= force.size());
  91. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  92. for (int i = 0; i < m_softBodies.size(); ++i)
  93. {
  94. btSoftBody* psb = m_softBodies[i];
  95. if (!psb->isActive())
  96. {
  97. continue;
  98. }
  99. for (int j = 0; j < psb->m_tetras.size(); ++j)
  100. {
  101. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  102. btSoftBody::Node* node0 = tetra.m_n[0];
  103. btSoftBody::Node* node1 = tetra.m_n[1];
  104. btSoftBody::Node* node2 = tetra.m_n[2];
  105. btSoftBody::Node* node3 = tetra.m_n[3];
  106. size_t id0 = node0->index;
  107. size_t id1 = node1->index;
  108. size_t id2 = node2->index;
  109. size_t id3 = node3->index;
  110. btMatrix3x3 dF = DsFromVelocity(node0, node1, node2, node3) * tetra.m_Dm_inverse;
  111. btMatrix3x3 I;
  112. I.setIdentity();
  113. btMatrix3x3 dP = (dF + dF.transpose()) * m_mu_damp + I * (dF[0][0] + dF[1][1] + dF[2][2]) * m_lambda_damp;
  114. // firstPiolaDampingDifferential(psb->m_tetraScratchesTn[j], dF, dP);
  115. btVector3 df_on_node0 = dP * (tetra.m_Dm_inverse.transpose() * grad_N_hat_1st_col);
  116. btMatrix3x3 df_on_node123 = dP * tetra.m_Dm_inverse.transpose();
  117. // damping force differential
  118. btScalar scale1 = scale * tetra.m_element_measure;
  119. force[id0] -= scale1 * df_on_node0;
  120. force[id1] -= scale1 * df_on_node123.getColumn(0);
  121. force[id2] -= scale1 * df_on_node123.getColumn(1);
  122. force[id3] -= scale1 * df_on_node123.getColumn(2);
  123. }
  124. }
  125. }
  126. virtual double totalElasticEnergy(btScalar dt)
  127. {
  128. double energy = 0;
  129. for (int i = 0; i < m_softBodies.size(); ++i)
  130. {
  131. btSoftBody* psb = m_softBodies[i];
  132. if (!psb->isActive())
  133. {
  134. continue;
  135. }
  136. for (int j = 0; j < psb->m_tetraScratches.size(); ++j)
  137. {
  138. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  139. btSoftBody::TetraScratch& s = psb->m_tetraScratches[j];
  140. energy += tetra.m_element_measure * elasticEnergyDensity(s);
  141. }
  142. }
  143. return energy;
  144. }
  145. // The damping energy is formulated as in https://www.math.ucla.edu/~jteran/papers/GSSJT15.pdf to allow line search
  146. virtual double totalDampingEnergy(btScalar dt)
  147. {
  148. double energy = 0;
  149. int sz = 0;
  150. for (int i = 0; i < m_softBodies.size(); ++i)
  151. {
  152. btSoftBody* psb = m_softBodies[i];
  153. if (!psb->isActive())
  154. {
  155. continue;
  156. }
  157. for (int j = 0; j < psb->m_nodes.size(); ++j)
  158. {
  159. sz = btMax(sz, psb->m_nodes[j].index);
  160. }
  161. }
  162. TVStack dampingForce;
  163. dampingForce.resize(sz + 1);
  164. for (int i = 0; i < dampingForce.size(); ++i)
  165. dampingForce[i].setZero();
  166. addScaledDampingForce(0.5, dampingForce);
  167. for (int i = 0; i < m_softBodies.size(); ++i)
  168. {
  169. btSoftBody* psb = m_softBodies[i];
  170. for (int j = 0; j < psb->m_nodes.size(); ++j)
  171. {
  172. const btSoftBody::Node& node = psb->m_nodes[j];
  173. energy -= dampingForce[node.index].dot(node.m_v) / dt;
  174. }
  175. }
  176. return energy;
  177. }
  178. double elasticEnergyDensity(const btSoftBody::TetraScratch& s)
  179. {
  180. double density = 0;
  181. density += m_mu * 0.5 * (s.m_trace - 3.);
  182. density += m_lambda * 0.5 * (s.m_J - 1. - 0.75 * m_mu / m_lambda) * (s.m_J - 1. - 0.75 * m_mu / m_lambda);
  183. density -= m_mu * 0.5 * log(s.m_trace + 1);
  184. return density;
  185. }
  186. virtual void addScaledElasticForce(btScalar scale, TVStack& force)
  187. {
  188. int numNodes = getNumNodes();
  189. btAssert(numNodes <= force.size());
  190. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  191. for (int i = 0; i < m_softBodies.size(); ++i)
  192. {
  193. btSoftBody* psb = m_softBodies[i];
  194. if (!psb->isActive())
  195. {
  196. continue;
  197. }
  198. btScalar max_p = psb->m_cfg.m_maxStress;
  199. for (int j = 0; j < psb->m_tetras.size(); ++j)
  200. {
  201. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  202. btMatrix3x3 P;
  203. firstPiola(psb->m_tetraScratches[j], P);
  204. #ifdef USE_SVD
  205. if (max_p > 0)
  206. {
  207. // since we want to clamp the principal stress to max_p, we only need to
  208. // calculate SVD when sigma_0^2 + sigma_1^2 + sigma_2^2 > max_p * max_p
  209. btScalar trPTP = (P[0].length2() + P[1].length2() + P[2].length2());
  210. if (trPTP > max_p * max_p)
  211. {
  212. btMatrix3x3 U, V;
  213. btVector3 sigma;
  214. singularValueDecomposition(P, U, sigma, V);
  215. sigma[0] = btMin(sigma[0], max_p);
  216. sigma[1] = btMin(sigma[1], max_p);
  217. sigma[2] = btMin(sigma[2], max_p);
  218. sigma[0] = btMax(sigma[0], -max_p);
  219. sigma[1] = btMax(sigma[1], -max_p);
  220. sigma[2] = btMax(sigma[2], -max_p);
  221. btMatrix3x3 Sigma;
  222. Sigma.setIdentity();
  223. Sigma[0][0] = sigma[0];
  224. Sigma[1][1] = sigma[1];
  225. Sigma[2][2] = sigma[2];
  226. P = U * Sigma * V.transpose();
  227. }
  228. }
  229. #endif
  230. // btVector3 force_on_node0 = P * (tetra.m_Dm_inverse.transpose()*grad_N_hat_1st_col);
  231. btMatrix3x3 force_on_node123 = P * tetra.m_Dm_inverse.transpose();
  232. btVector3 force_on_node0 = force_on_node123 * grad_N_hat_1st_col;
  233. btSoftBody::Node* node0 = tetra.m_n[0];
  234. btSoftBody::Node* node1 = tetra.m_n[1];
  235. btSoftBody::Node* node2 = tetra.m_n[2];
  236. btSoftBody::Node* node3 = tetra.m_n[3];
  237. size_t id0 = node0->index;
  238. size_t id1 = node1->index;
  239. size_t id2 = node2->index;
  240. size_t id3 = node3->index;
  241. // elastic force
  242. btScalar scale1 = scale * tetra.m_element_measure;
  243. force[id0] -= scale1 * force_on_node0;
  244. force[id1] -= scale1 * force_on_node123.getColumn(0);
  245. force[id2] -= scale1 * force_on_node123.getColumn(1);
  246. force[id3] -= scale1 * force_on_node123.getColumn(2);
  247. }
  248. }
  249. }
  250. // The damping matrix is calculated using the time n state as described in https://www.math.ucla.edu/~jteran/papers/GSSJT15.pdf to allow line search
  251. virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
  252. {
  253. if (m_mu_damp == 0 && m_lambda_damp == 0)
  254. return;
  255. int numNodes = getNumNodes();
  256. btAssert(numNodes <= df.size());
  257. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  258. for (int i = 0; i < m_softBodies.size(); ++i)
  259. {
  260. btSoftBody* psb = m_softBodies[i];
  261. if (!psb->isActive())
  262. {
  263. continue;
  264. }
  265. for (int j = 0; j < psb->m_tetras.size(); ++j)
  266. {
  267. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  268. btSoftBody::Node* node0 = tetra.m_n[0];
  269. btSoftBody::Node* node1 = tetra.m_n[1];
  270. btSoftBody::Node* node2 = tetra.m_n[2];
  271. btSoftBody::Node* node3 = tetra.m_n[3];
  272. size_t id0 = node0->index;
  273. size_t id1 = node1->index;
  274. size_t id2 = node2->index;
  275. size_t id3 = node3->index;
  276. btMatrix3x3 dF = Ds(id0, id1, id2, id3, dv) * tetra.m_Dm_inverse;
  277. btMatrix3x3 I;
  278. I.setIdentity();
  279. btMatrix3x3 dP = (dF + dF.transpose()) * m_mu_damp + I * (dF[0][0] + dF[1][1] + dF[2][2]) * m_lambda_damp;
  280. // firstPiolaDampingDifferential(psb->m_tetraScratchesTn[j], dF, dP);
  281. // btVector3 df_on_node0 = dP * (tetra.m_Dm_inverse.transpose()*grad_N_hat_1st_col);
  282. btMatrix3x3 df_on_node123 = dP * tetra.m_Dm_inverse.transpose();
  283. btVector3 df_on_node0 = df_on_node123 * grad_N_hat_1st_col;
  284. // damping force differential
  285. btScalar scale1 = scale * tetra.m_element_measure;
  286. df[id0] -= scale1 * df_on_node0;
  287. df[id1] -= scale1 * df_on_node123.getColumn(0);
  288. df[id2] -= scale1 * df_on_node123.getColumn(1);
  289. df[id3] -= scale1 * df_on_node123.getColumn(2);
  290. }
  291. }
  292. }
  293. virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
  294. virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
  295. {
  296. int numNodes = getNumNodes();
  297. btAssert(numNodes <= df.size());
  298. btVector3 grad_N_hat_1st_col = btVector3(-1, -1, -1);
  299. for (int i = 0; i < m_softBodies.size(); ++i)
  300. {
  301. btSoftBody* psb = m_softBodies[i];
  302. if (!psb->isActive())
  303. {
  304. continue;
  305. }
  306. for (int j = 0; j < psb->m_tetras.size(); ++j)
  307. {
  308. btSoftBody::Tetra& tetra = psb->m_tetras[j];
  309. btSoftBody::Node* node0 = tetra.m_n[0];
  310. btSoftBody::Node* node1 = tetra.m_n[1];
  311. btSoftBody::Node* node2 = tetra.m_n[2];
  312. btSoftBody::Node* node3 = tetra.m_n[3];
  313. size_t id0 = node0->index;
  314. size_t id1 = node1->index;
  315. size_t id2 = node2->index;
  316. size_t id3 = node3->index;
  317. btMatrix3x3 dF = Ds(id0, id1, id2, id3, dx) * tetra.m_Dm_inverse;
  318. btMatrix3x3 dP;
  319. firstPiolaDifferential(psb->m_tetraScratches[j], dF, dP);
  320. // btVector3 df_on_node0 = dP * (tetra.m_Dm_inverse.transpose()*grad_N_hat_1st_col);
  321. btMatrix3x3 df_on_node123 = dP * tetra.m_Dm_inverse.transpose();
  322. btVector3 df_on_node0 = df_on_node123 * grad_N_hat_1st_col;
  323. // elastic force differential
  324. btScalar scale1 = scale * tetra.m_element_measure;
  325. df[id0] -= scale1 * df_on_node0;
  326. df[id1] -= scale1 * df_on_node123.getColumn(0);
  327. df[id2] -= scale1 * df_on_node123.getColumn(1);
  328. df[id3] -= scale1 * df_on_node123.getColumn(2);
  329. }
  330. }
  331. }
  332. void firstPiola(const btSoftBody::TetraScratch& s, btMatrix3x3& P)
  333. {
  334. btScalar c1 = (m_mu * (1. - 1. / (s.m_trace + 1.)));
  335. btScalar c2 = (m_lambda * (s.m_J - 1.) - 0.75 * m_mu);
  336. P = s.m_F * c1 + s.m_cofF * c2;
  337. }
  338. // Let P be the first piola stress.
  339. // This function calculates the dP = dP/dF * dF
  340. void firstPiolaDifferential(const btSoftBody::TetraScratch& s, const btMatrix3x3& dF, btMatrix3x3& dP)
  341. {
  342. btScalar c1 = m_mu * (1. - 1. / (s.m_trace + 1.));
  343. btScalar c2 = (2. * m_mu) * DotProduct(s.m_F, dF) * (1. / ((1. + s.m_trace) * (1. + s.m_trace)));
  344. btScalar c3 = (m_lambda * DotProduct(s.m_cofF, dF));
  345. dP = dF * c1 + s.m_F * c2;
  346. addScaledCofactorMatrixDifferential(s.m_F, dF, m_lambda * (s.m_J - 1.) - 0.75 * m_mu, dP);
  347. dP += s.m_cofF * c3;
  348. }
  349. // Let Q be the damping stress.
  350. // This function calculates the dP = dQ/dF * dF
  351. void firstPiolaDampingDifferential(const btSoftBody::TetraScratch& s, const btMatrix3x3& dF, btMatrix3x3& dP)
  352. {
  353. btScalar c1 = (m_mu_damp * (1. - 1. / (s.m_trace + 1.)));
  354. btScalar c2 = ((2. * m_mu_damp) * DotProduct(s.m_F, dF) * (1. / ((1. + s.m_trace) * (1. + s.m_trace))));
  355. btScalar c3 = (m_lambda_damp * DotProduct(s.m_cofF, dF));
  356. dP = dF * c1 + s.m_F * c2;
  357. addScaledCofactorMatrixDifferential(s.m_F, dF, m_lambda_damp * (s.m_J - 1.) - 0.75 * m_mu_damp, dP);
  358. dP += s.m_cofF * c3;
  359. }
  360. btScalar DotProduct(const btMatrix3x3& A, const btMatrix3x3& B)
  361. {
  362. btScalar ans = 0;
  363. for (int i = 0; i < 3; ++i)
  364. {
  365. ans += A[i].dot(B[i]);
  366. }
  367. return ans;
  368. }
  369. // Let C(A) be the cofactor of the matrix A
  370. // Let H = the derivative of C(A) with respect to A evaluated at F = A
  371. // This function calculates H*dF
  372. void addScaledCofactorMatrixDifferential(const btMatrix3x3& F, const btMatrix3x3& dF, btScalar scale, btMatrix3x3& M)
  373. {
  374. M[0][0] += scale * (dF[1][1] * F[2][2] + F[1][1] * dF[2][2] - dF[2][1] * F[1][2] - F[2][1] * dF[1][2]);
  375. M[1][0] += scale * (dF[2][1] * F[0][2] + F[2][1] * dF[0][2] - dF[0][1] * F[2][2] - F[0][1] * dF[2][2]);
  376. M[2][0] += scale * (dF[0][1] * F[1][2] + F[0][1] * dF[1][2] - dF[1][1] * F[0][2] - F[1][1] * dF[0][2]);
  377. M[0][1] += scale * (dF[2][0] * F[1][2] + F[2][0] * dF[1][2] - dF[1][0] * F[2][2] - F[1][0] * dF[2][2]);
  378. M[1][1] += scale * (dF[0][0] * F[2][2] + F[0][0] * dF[2][2] - dF[2][0] * F[0][2] - F[2][0] * dF[0][2]);
  379. M[2][1] += scale * (dF[1][0] * F[0][2] + F[1][0] * dF[0][2] - dF[0][0] * F[1][2] - F[0][0] * dF[1][2]);
  380. M[0][2] += scale * (dF[1][0] * F[2][1] + F[1][0] * dF[2][1] - dF[2][0] * F[1][1] - F[2][0] * dF[1][1]);
  381. M[1][2] += scale * (dF[2][0] * F[0][1] + F[2][0] * dF[0][1] - dF[0][0] * F[2][1] - F[0][0] * dF[2][1]);
  382. M[2][2] += scale * (dF[0][0] * F[1][1] + F[0][0] * dF[1][1] - dF[1][0] * F[0][1] - F[1][0] * dF[0][1]);
  383. }
  384. virtual btDeformableLagrangianForceType getForceType()
  385. {
  386. return BT_NEOHOOKEAN_FORCE;
  387. }
  388. };
  389. #endif /* BT_NEOHOOKEAN_H */