btDeformableContactProjection.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. #include "btDeformableContactProjection.h"
  15. #include "btDeformableMultiBodyDynamicsWorld.h"
  16. #include <algorithm>
  17. #include <cmath>
  18. btScalar btDeformableContactProjection::update(btCollisionObject** deformableBodies, int numDeformableBodies, const btContactSolverInfo& infoGlobal)
  19. {
  20. btScalar residualSquare = 0;
  21. for (int i = 0; i < numDeformableBodies; ++i)
  22. {
  23. for (int j = 0; j < m_softBodies.size(); ++j)
  24. {
  25. btCollisionObject* psb = m_softBodies[j];
  26. if (psb != deformableBodies[i])
  27. {
  28. continue;
  29. }
  30. for (int k = 0; k < m_nodeRigidConstraints[j].size(); ++k)
  31. {
  32. btDeformableNodeRigidContactConstraint& constraint = m_nodeRigidConstraints[j][k];
  33. btScalar localResidualSquare = constraint.solveConstraint(infoGlobal);
  34. residualSquare = btMax(residualSquare, localResidualSquare);
  35. }
  36. for (int k = 0; k < m_nodeAnchorConstraints[j].size(); ++k)
  37. {
  38. btDeformableNodeAnchorConstraint& constraint = m_nodeAnchorConstraints[j][k];
  39. btScalar localResidualSquare = constraint.solveConstraint(infoGlobal);
  40. residualSquare = btMax(residualSquare, localResidualSquare);
  41. }
  42. for (int k = 0; k < m_faceRigidConstraints[j].size(); ++k)
  43. {
  44. btDeformableFaceRigidContactConstraint& constraint = m_faceRigidConstraints[j][k];
  45. btScalar localResidualSquare = constraint.solveConstraint(infoGlobal);
  46. residualSquare = btMax(residualSquare, localResidualSquare);
  47. }
  48. for (int k = 0; k < m_deformableConstraints[j].size(); ++k)
  49. {
  50. btDeformableFaceNodeContactConstraint& constraint = m_deformableConstraints[j][k];
  51. btScalar localResidualSquare = constraint.solveConstraint(infoGlobal);
  52. residualSquare = btMax(residualSquare, localResidualSquare);
  53. }
  54. }
  55. }
  56. return residualSquare;
  57. }
  58. btScalar btDeformableContactProjection::solveSplitImpulse(btCollisionObject** deformableBodies, int numDeformableBodies, const btContactSolverInfo& infoGlobal)
  59. {
  60. btScalar residualSquare = 0;
  61. for (int i = 0; i < numDeformableBodies; ++i)
  62. {
  63. for (int j = 0; j < m_softBodies.size(); ++j)
  64. {
  65. btCollisionObject* psb = m_softBodies[j];
  66. if (psb != deformableBodies[i])
  67. {
  68. continue;
  69. }
  70. for (int k = 0; k < m_nodeRigidConstraints[j].size(); ++k)
  71. {
  72. btDeformableNodeRigidContactConstraint& constraint = m_nodeRigidConstraints[j][k];
  73. btScalar localResidualSquare = constraint.solveSplitImpulse(infoGlobal);
  74. residualSquare = btMax(residualSquare, localResidualSquare);
  75. }
  76. for (int k = 0; k < m_faceRigidConstraints[j].size(); ++k)
  77. {
  78. btDeformableFaceRigidContactConstraint& constraint = m_faceRigidConstraints[j][k];
  79. btScalar localResidualSquare = constraint.solveSplitImpulse(infoGlobal);
  80. residualSquare = btMax(residualSquare, localResidualSquare);
  81. }
  82. }
  83. }
  84. return residualSquare;
  85. }
  86. void btDeformableContactProjection::setConstraints(const btContactSolverInfo& infoGlobal)
  87. {
  88. BT_PROFILE("setConstraints");
  89. for (int i = 0; i < m_softBodies.size(); ++i)
  90. {
  91. btSoftBody* psb = m_softBodies[i];
  92. if (!psb->isActive())
  93. {
  94. continue;
  95. }
  96. // set Dirichlet constraint
  97. for (int j = 0; j < psb->m_nodes.size(); ++j)
  98. {
  99. if (psb->m_nodes[j].m_im == 0)
  100. {
  101. btDeformableStaticConstraint static_constraint(&psb->m_nodes[j], infoGlobal);
  102. m_staticConstraints[i].push_back(static_constraint);
  103. }
  104. }
  105. // set up deformable anchors
  106. for (int j = 0; j < psb->m_deformableAnchors.size(); ++j)
  107. {
  108. btSoftBody::DeformableNodeRigidAnchor& anchor = psb->m_deformableAnchors[j];
  109. // skip fixed points
  110. if (anchor.m_node->m_im == 0)
  111. {
  112. continue;
  113. }
  114. anchor.m_c1 = anchor.m_cti.m_colObj->getWorldTransform().getBasis() * anchor.m_local;
  115. btDeformableNodeAnchorConstraint constraint(anchor, infoGlobal);
  116. m_nodeAnchorConstraints[i].push_back(constraint);
  117. }
  118. // set Deformable Node vs. Rigid constraint
  119. for (int j = 0; j < psb->m_nodeRigidContacts.size(); ++j)
  120. {
  121. const btSoftBody::DeformableNodeRigidContact& contact = psb->m_nodeRigidContacts[j];
  122. // skip fixed points
  123. if (contact.m_node->m_im == 0)
  124. {
  125. continue;
  126. }
  127. btDeformableNodeRigidContactConstraint constraint(contact, infoGlobal);
  128. m_nodeRigidConstraints[i].push_back(constraint);
  129. }
  130. // set Deformable Face vs. Rigid constraint
  131. for (int j = 0; j < psb->m_faceRigidContacts.size(); ++j)
  132. {
  133. const btSoftBody::DeformableFaceRigidContact& contact = psb->m_faceRigidContacts[j];
  134. // skip fixed faces
  135. if (contact.m_c2 == 0)
  136. {
  137. continue;
  138. }
  139. btDeformableFaceRigidContactConstraint constraint(contact, infoGlobal, m_useStrainLimiting);
  140. m_faceRigidConstraints[i].push_back(constraint);
  141. }
  142. }
  143. }
  144. void btDeformableContactProjection::project(TVStack& x)
  145. {
  146. #ifndef USE_MGS
  147. const int dim = 3;
  148. for (int index = 0; index < m_projectionsDict.size(); ++index)
  149. {
  150. btAlignedObjectArray<btVector3>& projectionDirs = *m_projectionsDict.getAtIndex(index);
  151. size_t i = m_projectionsDict.getKeyAtIndex(index).getUid1();
  152. if (projectionDirs.size() >= dim)
  153. {
  154. // static node
  155. x[i].setZero();
  156. continue;
  157. }
  158. else if (projectionDirs.size() == 2)
  159. {
  160. btVector3 dir0 = projectionDirs[0];
  161. btVector3 dir1 = projectionDirs[1];
  162. btVector3 free_dir = btCross(dir0, dir1);
  163. if (free_dir.safeNorm() < SIMD_EPSILON)
  164. {
  165. x[i] -= x[i].dot(dir0) * dir0;
  166. }
  167. else
  168. {
  169. free_dir.normalize();
  170. x[i] = x[i].dot(free_dir) * free_dir;
  171. }
  172. }
  173. else
  174. {
  175. btAssert(projectionDirs.size() == 1);
  176. btVector3 dir0 = projectionDirs[0];
  177. x[i] -= x[i].dot(dir0) * dir0;
  178. }
  179. }
  180. #else
  181. btReducedVector p(x.size());
  182. for (int i = 0; i < m_projections.size(); ++i)
  183. {
  184. p += (m_projections[i].dot(x) * m_projections[i]);
  185. }
  186. for (int i = 0; i < p.m_indices.size(); ++i)
  187. {
  188. x[p.m_indices[i]] -= p.m_vecs[i];
  189. }
  190. #endif
  191. }
  192. void btDeformableContactProjection::setProjection()
  193. {
  194. #ifndef USE_MGS
  195. BT_PROFILE("btDeformableContactProjection::setProjection");
  196. btAlignedObjectArray<btVector3> units;
  197. units.push_back(btVector3(1, 0, 0));
  198. units.push_back(btVector3(0, 1, 0));
  199. units.push_back(btVector3(0, 0, 1));
  200. for (int i = 0; i < m_softBodies.size(); ++i)
  201. {
  202. btSoftBody* psb = m_softBodies[i];
  203. if (!psb->isActive())
  204. {
  205. continue;
  206. }
  207. for (int j = 0; j < m_staticConstraints[i].size(); ++j)
  208. {
  209. int index = m_staticConstraints[i][j].m_node->index;
  210. m_staticConstraints[i][j].m_node->m_constrained = true;
  211. if (m_projectionsDict.find(index) == NULL)
  212. {
  213. m_projectionsDict.insert(index, units);
  214. }
  215. else
  216. {
  217. btAlignedObjectArray<btVector3>& projections = *m_projectionsDict[index];
  218. for (int k = 0; k < 3; ++k)
  219. {
  220. projections.push_back(units[k]);
  221. }
  222. }
  223. }
  224. for (int j = 0; j < m_nodeAnchorConstraints[i].size(); ++j)
  225. {
  226. int index = m_nodeAnchorConstraints[i][j].m_anchor->m_node->index;
  227. m_nodeAnchorConstraints[i][j].m_anchor->m_node->m_constrained = true;
  228. if (m_projectionsDict.find(index) == NULL)
  229. {
  230. m_projectionsDict.insert(index, units);
  231. }
  232. else
  233. {
  234. btAlignedObjectArray<btVector3>& projections = *m_projectionsDict[index];
  235. for (int k = 0; k < 3; ++k)
  236. {
  237. projections.push_back(units[k]);
  238. }
  239. }
  240. }
  241. for (int j = 0; j < m_nodeRigidConstraints[i].size(); ++j)
  242. {
  243. int index = m_nodeRigidConstraints[i][j].m_node->index;
  244. m_nodeRigidConstraints[i][j].m_node->m_constrained = true;
  245. if (m_nodeRigidConstraints[i][j].m_binding)
  246. {
  247. if (m_nodeRigidConstraints[i][j].m_static)
  248. {
  249. if (m_projectionsDict.find(index) == NULL)
  250. {
  251. m_projectionsDict.insert(index, units);
  252. }
  253. else
  254. {
  255. btAlignedObjectArray<btVector3>& projections = *m_projectionsDict[index];
  256. for (int k = 0; k < 3; ++k)
  257. {
  258. projections.push_back(units[k]);
  259. }
  260. }
  261. }
  262. else
  263. {
  264. if (m_projectionsDict.find(index) == NULL)
  265. {
  266. btAlignedObjectArray<btVector3> projections;
  267. projections.push_back(m_nodeRigidConstraints[i][j].m_normal);
  268. m_projectionsDict.insert(index, projections);
  269. }
  270. else
  271. {
  272. btAlignedObjectArray<btVector3>& projections = *m_projectionsDict[index];
  273. projections.push_back(m_nodeRigidConstraints[i][j].m_normal);
  274. }
  275. }
  276. }
  277. }
  278. for (int j = 0; j < m_faceRigidConstraints[i].size(); ++j)
  279. {
  280. const btSoftBody::Face* face = m_faceRigidConstraints[i][j].m_face;
  281. if (m_faceRigidConstraints[i][j].m_binding)
  282. {
  283. for (int k = 0; k < 3; ++k)
  284. {
  285. face->m_n[k]->m_constrained = true;
  286. }
  287. }
  288. for (int k = 0; k < 3; ++k)
  289. {
  290. btSoftBody::Node* node = face->m_n[k];
  291. int index = node->index;
  292. if (m_faceRigidConstraints[i][j].m_static)
  293. {
  294. if (m_projectionsDict.find(index) == NULL)
  295. {
  296. m_projectionsDict.insert(index, units);
  297. }
  298. else
  299. {
  300. btAlignedObjectArray<btVector3>& projections = *m_projectionsDict[index];
  301. for (int l = 0; l < 3; ++l)
  302. {
  303. projections.push_back(units[l]);
  304. }
  305. }
  306. }
  307. else
  308. {
  309. if (m_projectionsDict.find(index) == NULL)
  310. {
  311. btAlignedObjectArray<btVector3> projections;
  312. projections.push_back(m_faceRigidConstraints[i][j].m_normal);
  313. m_projectionsDict.insert(index, projections);
  314. }
  315. else
  316. {
  317. btAlignedObjectArray<btVector3>& projections = *m_projectionsDict[index];
  318. projections.push_back(m_faceRigidConstraints[i][j].m_normal);
  319. }
  320. }
  321. }
  322. }
  323. }
  324. #else
  325. int dof = 0;
  326. for (int i = 0; i < m_softBodies.size(); ++i)
  327. {
  328. dof += m_softBodies[i]->m_nodes.size();
  329. }
  330. for (int i = 0; i < m_softBodies.size(); ++i)
  331. {
  332. btSoftBody* psb = m_softBodies[i];
  333. if (!psb->isActive())
  334. {
  335. continue;
  336. }
  337. for (int j = 0; j < m_staticConstraints[i].size(); ++j)
  338. {
  339. int index = m_staticConstraints[i][j].m_node->index;
  340. m_staticConstraints[i][j].m_node->m_penetration = SIMD_INFINITY;
  341. btAlignedObjectArray<int> indices;
  342. btAlignedObjectArray<btVector3> vecs1, vecs2, vecs3;
  343. indices.push_back(index);
  344. vecs1.push_back(btVector3(1, 0, 0));
  345. vecs2.push_back(btVector3(0, 1, 0));
  346. vecs3.push_back(btVector3(0, 0, 1));
  347. m_projections.push_back(btReducedVector(dof, indices, vecs1));
  348. m_projections.push_back(btReducedVector(dof, indices, vecs2));
  349. m_projections.push_back(btReducedVector(dof, indices, vecs3));
  350. }
  351. for (int j = 0; j < m_nodeAnchorConstraints[i].size(); ++j)
  352. {
  353. int index = m_nodeAnchorConstraints[i][j].m_anchor->m_node->index;
  354. m_nodeAnchorConstraints[i][j].m_anchor->m_node->m_penetration = SIMD_INFINITY;
  355. btAlignedObjectArray<int> indices;
  356. btAlignedObjectArray<btVector3> vecs1, vecs2, vecs3;
  357. indices.push_back(index);
  358. vecs1.push_back(btVector3(1, 0, 0));
  359. vecs2.push_back(btVector3(0, 1, 0));
  360. vecs3.push_back(btVector3(0, 0, 1));
  361. m_projections.push_back(btReducedVector(dof, indices, vecs1));
  362. m_projections.push_back(btReducedVector(dof, indices, vecs2));
  363. m_projections.push_back(btReducedVector(dof, indices, vecs3));
  364. }
  365. for (int j = 0; j < m_nodeRigidConstraints[i].size(); ++j)
  366. {
  367. int index = m_nodeRigidConstraints[i][j].m_node->index;
  368. m_nodeRigidConstraints[i][j].m_node->m_penetration = -m_nodeRigidConstraints[i][j].getContact()->m_cti.m_offset;
  369. btAlignedObjectArray<int> indices;
  370. indices.push_back(index);
  371. btAlignedObjectArray<btVector3> vecs1, vecs2, vecs3;
  372. if (m_nodeRigidConstraints[i][j].m_static)
  373. {
  374. vecs1.push_back(btVector3(1, 0, 0));
  375. vecs2.push_back(btVector3(0, 1, 0));
  376. vecs3.push_back(btVector3(0, 0, 1));
  377. m_projections.push_back(btReducedVector(dof, indices, vecs1));
  378. m_projections.push_back(btReducedVector(dof, indices, vecs2));
  379. m_projections.push_back(btReducedVector(dof, indices, vecs3));
  380. }
  381. else
  382. {
  383. vecs1.push_back(m_nodeRigidConstraints[i][j].m_normal);
  384. m_projections.push_back(btReducedVector(dof, indices, vecs1));
  385. }
  386. }
  387. for (int j = 0; j < m_faceRigidConstraints[i].size(); ++j)
  388. {
  389. const btSoftBody::Face* face = m_faceRigidConstraints[i][j].m_face;
  390. btVector3 bary = m_faceRigidConstraints[i][j].getContact()->m_bary;
  391. btScalar penetration = -m_faceRigidConstraints[i][j].getContact()->m_cti.m_offset;
  392. for (int k = 0; k < 3; ++k)
  393. {
  394. face->m_n[k]->m_penetration = btMax(face->m_n[k]->m_penetration, penetration);
  395. }
  396. if (m_faceRigidConstraints[i][j].m_static)
  397. {
  398. for (int l = 0; l < 3; ++l)
  399. {
  400. btReducedVector rv(dof);
  401. for (int k = 0; k < 3; ++k)
  402. {
  403. rv.m_indices.push_back(face->m_n[k]->index);
  404. btVector3 v(0, 0, 0);
  405. v[l] = bary[k];
  406. rv.m_vecs.push_back(v);
  407. rv.sort();
  408. }
  409. m_projections.push_back(rv);
  410. }
  411. }
  412. else
  413. {
  414. btReducedVector rv(dof);
  415. for (int k = 0; k < 3; ++k)
  416. {
  417. rv.m_indices.push_back(face->m_n[k]->index);
  418. rv.m_vecs.push_back(bary[k] * m_faceRigidConstraints[i][j].m_normal);
  419. rv.sort();
  420. }
  421. m_projections.push_back(rv);
  422. }
  423. }
  424. }
  425. btModifiedGramSchmidt<btReducedVector> mgs(m_projections);
  426. mgs.solve();
  427. m_projections = mgs.m_out;
  428. #endif
  429. }
  430. void btDeformableContactProjection::checkConstraints(const TVStack& x)
  431. {
  432. for (int i = 0; i < m_lagrangeMultipliers.size(); ++i)
  433. {
  434. btVector3 d(0, 0, 0);
  435. const LagrangeMultiplier& lm = m_lagrangeMultipliers[i];
  436. for (int j = 0; j < lm.m_num_constraints; ++j)
  437. {
  438. for (int k = 0; k < lm.m_num_nodes; ++k)
  439. {
  440. d[j] += lm.m_weights[k] * x[lm.m_indices[k]].dot(lm.m_dirs[j]);
  441. }
  442. }
  443. // printf("d = %f, %f, %f\n", d[0], d[1], d[2]);
  444. // printf("val = %f, %f, %f\n", lm.m_vals[0], lm.m_vals[1], lm.m_vals[2]);
  445. }
  446. }
  447. void btDeformableContactProjection::setLagrangeMultiplier()
  448. {
  449. for (int i = 0; i < m_softBodies.size(); ++i)
  450. {
  451. btSoftBody* psb = m_softBodies[i];
  452. if (!psb->isActive())
  453. {
  454. continue;
  455. }
  456. for (int j = 0; j < m_staticConstraints[i].size(); ++j)
  457. {
  458. int index = m_staticConstraints[i][j].m_node->index;
  459. m_staticConstraints[i][j].m_node->m_constrained = true;
  460. LagrangeMultiplier lm;
  461. lm.m_num_nodes = 1;
  462. lm.m_indices[0] = index;
  463. lm.m_weights[0] = 1.0;
  464. lm.m_num_constraints = 3;
  465. lm.m_dirs[0] = btVector3(1, 0, 0);
  466. lm.m_dirs[1] = btVector3(0, 1, 0);
  467. lm.m_dirs[2] = btVector3(0, 0, 1);
  468. m_lagrangeMultipliers.push_back(lm);
  469. }
  470. for (int j = 0; j < m_nodeAnchorConstraints[i].size(); ++j)
  471. {
  472. int index = m_nodeAnchorConstraints[i][j].m_anchor->m_node->index;
  473. m_nodeAnchorConstraints[i][j].m_anchor->m_node->m_constrained = true;
  474. LagrangeMultiplier lm;
  475. lm.m_num_nodes = 1;
  476. lm.m_indices[0] = index;
  477. lm.m_weights[0] = 1.0;
  478. lm.m_num_constraints = 3;
  479. lm.m_dirs[0] = btVector3(1, 0, 0);
  480. lm.m_dirs[1] = btVector3(0, 1, 0);
  481. lm.m_dirs[2] = btVector3(0, 0, 1);
  482. m_lagrangeMultipliers.push_back(lm);
  483. }
  484. for (int j = 0; j < m_nodeRigidConstraints[i].size(); ++j)
  485. {
  486. if (!m_nodeRigidConstraints[i][j].m_binding)
  487. {
  488. continue;
  489. }
  490. int index = m_nodeRigidConstraints[i][j].m_node->index;
  491. m_nodeRigidConstraints[i][j].m_node->m_constrained = true;
  492. LagrangeMultiplier lm;
  493. lm.m_num_nodes = 1;
  494. lm.m_indices[0] = index;
  495. lm.m_weights[0] = 1.0;
  496. if (m_nodeRigidConstraints[i][j].m_static)
  497. {
  498. lm.m_num_constraints = 3;
  499. lm.m_dirs[0] = btVector3(1, 0, 0);
  500. lm.m_dirs[1] = btVector3(0, 1, 0);
  501. lm.m_dirs[2] = btVector3(0, 0, 1);
  502. }
  503. else
  504. {
  505. lm.m_num_constraints = 1;
  506. lm.m_dirs[0] = m_nodeRigidConstraints[i][j].m_normal;
  507. }
  508. m_lagrangeMultipliers.push_back(lm);
  509. }
  510. for (int j = 0; j < m_faceRigidConstraints[i].size(); ++j)
  511. {
  512. if (!m_faceRigidConstraints[i][j].m_binding)
  513. {
  514. continue;
  515. }
  516. btSoftBody::Face* face = m_faceRigidConstraints[i][j].m_face;
  517. btVector3 bary = m_faceRigidConstraints[i][j].getContact()->m_bary;
  518. LagrangeMultiplier lm;
  519. lm.m_num_nodes = 3;
  520. for (int k = 0; k < 3; ++k)
  521. {
  522. face->m_n[k]->m_constrained = true;
  523. lm.m_indices[k] = face->m_n[k]->index;
  524. lm.m_weights[k] = bary[k];
  525. }
  526. if (m_faceRigidConstraints[i][j].m_static)
  527. {
  528. face->m_pcontact[3] = 1;
  529. lm.m_num_constraints = 3;
  530. lm.m_dirs[0] = btVector3(1, 0, 0);
  531. lm.m_dirs[1] = btVector3(0, 1, 0);
  532. lm.m_dirs[2] = btVector3(0, 0, 1);
  533. }
  534. else
  535. {
  536. face->m_pcontact[3] = 0;
  537. lm.m_num_constraints = 1;
  538. lm.m_dirs[0] = m_faceRigidConstraints[i][j].m_normal;
  539. }
  540. m_lagrangeMultipliers.push_back(lm);
  541. }
  542. }
  543. }
  544. //
  545. void btDeformableContactProjection::applyDynamicFriction(TVStack& f)
  546. {
  547. for (int i = 0; i < m_softBodies.size(); ++i)
  548. {
  549. for (int j = 0; j < m_nodeRigidConstraints[i].size(); ++j)
  550. {
  551. const btDeformableNodeRigidContactConstraint& constraint = m_nodeRigidConstraints[i][j];
  552. const btSoftBody::Node* node = constraint.m_node;
  553. if (node->m_im != 0)
  554. {
  555. int index = node->index;
  556. f[index] += constraint.getDv(node) * (1. / node->m_im);
  557. }
  558. }
  559. for (int j = 0; j < m_faceRigidConstraints[i].size(); ++j)
  560. {
  561. const btDeformableFaceRigidContactConstraint& constraint = m_faceRigidConstraints[i][j];
  562. const btSoftBody::Face* face = constraint.getContact()->m_face;
  563. for (int k = 0; k < 3; ++k)
  564. {
  565. const btSoftBody::Node* node = face->m_n[k];
  566. if (node->m_im != 0)
  567. {
  568. int index = node->index;
  569. f[index] += constraint.getDv(node) * (1. / node->m_im);
  570. }
  571. }
  572. }
  573. for (int j = 0; j < m_deformableConstraints[i].size(); ++j)
  574. {
  575. const btDeformableFaceNodeContactConstraint& constraint = m_deformableConstraints[i][j];
  576. const btSoftBody::Face* face = constraint.getContact()->m_face;
  577. const btSoftBody::Node* node = constraint.getContact()->m_node;
  578. if (node->m_im != 0)
  579. {
  580. int index = node->index;
  581. f[index] += constraint.getDv(node) * (1. / node->m_im);
  582. }
  583. for (int k = 0; k < 3; ++k)
  584. {
  585. const btSoftBody::Node* node = face->m_n[k];
  586. if (node->m_im != 0)
  587. {
  588. int index = node->index;
  589. f[index] += constraint.getDv(node) * (1. / node->m_im);
  590. }
  591. }
  592. }
  593. }
  594. }
  595. void btDeformableContactProjection::reinitialize(bool nodeUpdated)
  596. {
  597. int N = m_softBodies.size();
  598. if (nodeUpdated)
  599. {
  600. m_staticConstraints.resize(N);
  601. m_nodeAnchorConstraints.resize(N);
  602. m_nodeRigidConstraints.resize(N);
  603. m_faceRigidConstraints.resize(N);
  604. m_deformableConstraints.resize(N);
  605. }
  606. for (int i = 0; i < N; ++i)
  607. {
  608. m_staticConstraints[i].clear();
  609. m_nodeAnchorConstraints[i].clear();
  610. m_nodeRigidConstraints[i].clear();
  611. m_faceRigidConstraints[i].clear();
  612. m_deformableConstraints[i].clear();
  613. }
  614. #ifndef USE_MGS
  615. m_projectionsDict.clear();
  616. #else
  617. m_projections.clear();
  618. #endif
  619. m_lagrangeMultipliers.clear();
  620. }