btDeformableMultiBodyConstraintSolver.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Written by Xuchen Han <[email protected]>
  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 "btDeformableMultiBodyConstraintSolver.h"
  15. #include "BulletReducedDeformableBody/btReducedDeformableBodySolver.h"
  16. #include <iostream>
  17. // override the iterations method to include deformable/multibody contact
  18. btScalar btDeformableMultiBodyConstraintSolver::solveDeformableGroupIterations(btCollisionObject** bodies, int numBodies, btCollisionObject** deformableBodies, int numDeformableBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
  19. {
  20. {
  21. // pair deformable body with solver body
  22. pairDeformableAndSolverBody(bodies, numBodies, numDeformableBodies, infoGlobal);
  23. ///this is a special step to resolve penetrations (just for contacts)
  24. solveGroupCacheFriendlySplitImpulseIterations(bodies, numBodies, deformableBodies, numDeformableBodies, manifoldPtr, numManifolds, constraints, numConstraints, infoGlobal, debugDrawer);
  25. int maxIterations = m_maxOverrideNumSolverIterations > infoGlobal.m_numIterations ? m_maxOverrideNumSolverIterations : infoGlobal.m_numIterations;
  26. for (int iteration = 0; iteration < maxIterations; iteration++)
  27. {
  28. // rigid bodies are solved using solver body velocity, but rigid/deformable contact directly uses the velocity of the actual rigid body. So we have to do the following: Solve one iteration of the rigid/rigid contact, get the updated velocity in the solver body and update the velocity of the underlying rigid body. Then solve the rigid/deformable contact. Finally, grab the (once again) updated rigid velocity and update the velocity of the wrapping solver body
  29. // solve rigid/rigid in solver body
  30. m_leastSquaresResidual = solveSingleIteration(iteration, bodies, numBodies, manifoldPtr, numManifolds, constraints, numConstraints, infoGlobal, debugDrawer);
  31. // solver body velocity -> rigid body velocity
  32. solverBodyWriteBack(infoGlobal);
  33. btScalar deformableResidual = m_deformableSolver->solveContactConstraints(deformableBodies, numDeformableBodies, infoGlobal);
  34. // update rigid body velocity in rigid/deformable contact
  35. m_leastSquaresResidual = btMax(m_leastSquaresResidual, deformableResidual);
  36. // solver body velocity <- rigid body velocity
  37. writeToSolverBody(bodies, numBodies, infoGlobal);
  38. // std::cout << "------------Iteration " << iteration << "------------\n";
  39. // std::cout << "m_leastSquaresResidual: " << m_leastSquaresResidual << "\n";
  40. if (m_leastSquaresResidual <= infoGlobal.m_leastSquaresResidualThreshold || (iteration >= (maxIterations - 1)))
  41. {
  42. #ifdef VERBOSE_RESIDUAL_PRINTF
  43. if (iteration >= (maxIterations - 1))
  44. printf("residual = %f at iteration #%d\n", m_leastSquaresResidual, iteration);
  45. #endif
  46. m_analyticsData.m_numSolverCalls++;
  47. m_analyticsData.m_numIterationsUsed = iteration + 1;
  48. m_analyticsData.m_islandId = -2;
  49. if (numBodies > 0)
  50. m_analyticsData.m_islandId = bodies[0]->getCompanionId();
  51. m_analyticsData.m_numBodies = numBodies;
  52. m_analyticsData.m_numContactManifolds = numManifolds;
  53. m_analyticsData.m_remainingLeastSquaresResidual = m_leastSquaresResidual;
  54. m_deformableSolver->deformableBodyInternalWriteBack();
  55. // std::cout << "[===================Next Step===================]\n";
  56. break;
  57. }
  58. }
  59. }
  60. return 0.f;
  61. }
  62. void btDeformableMultiBodyConstraintSolver::solveDeformableBodyGroup(btCollisionObject** bodies, int numBodies, btCollisionObject** deformableBodies, int numDeformableBodies, btPersistentManifold** manifold, int numManifolds, btTypedConstraint** constraints, int numConstraints, btMultiBodyConstraint** multiBodyConstraints, int numMultiBodyConstraints, const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btDispatcher* dispatcher)
  63. {
  64. m_tmpMultiBodyConstraints = multiBodyConstraints;
  65. m_tmpNumMultiBodyConstraints = numMultiBodyConstraints;
  66. // inherited from MultiBodyConstraintSolver
  67. solveGroupCacheFriendlySetup(bodies, numBodies, manifold, numManifolds, constraints, numConstraints, info, debugDrawer);
  68. // overriden
  69. solveDeformableGroupIterations(bodies, numBodies, deformableBodies, numDeformableBodies, manifold, numManifolds, constraints, numConstraints, info, debugDrawer);
  70. // inherited from MultiBodyConstraintSolver
  71. solveGroupCacheFriendlyFinish(bodies, numBodies, info);
  72. m_tmpMultiBodyConstraints = 0;
  73. m_tmpNumMultiBodyConstraints = 0;
  74. }
  75. void btDeformableMultiBodyConstraintSolver::writeToSolverBody(btCollisionObject** bodies, int numBodies, const btContactSolverInfo& infoGlobal)
  76. {
  77. // reduced soft body solver directly modifies the solver body
  78. if (m_deformableSolver->isReducedSolver())
  79. {
  80. return;
  81. }
  82. for (int i = 0; i < numBodies; i++)
  83. {
  84. int bodyId = getOrInitSolverBody(*bodies[i], infoGlobal.m_timeStep);
  85. btRigidBody* body = btRigidBody::upcast(bodies[i]);
  86. if (body && body->getInvMass())
  87. {
  88. btSolverBody& solverBody = m_tmpSolverBodyPool[bodyId];
  89. solverBody.m_linearVelocity = body->getLinearVelocity() - solverBody.m_deltaLinearVelocity;
  90. solverBody.m_angularVelocity = body->getAngularVelocity() - solverBody.m_deltaAngularVelocity;
  91. }
  92. }
  93. }
  94. void btDeformableMultiBodyConstraintSolver::solverBodyWriteBack(const btContactSolverInfo& infoGlobal)
  95. {
  96. // reduced soft body solver directly modifies the solver body
  97. if (m_deformableSolver->isReducedSolver())
  98. {
  99. return;
  100. }
  101. for (int i = 0; i < m_tmpSolverBodyPool.size(); i++)
  102. {
  103. btRigidBody* body = m_tmpSolverBodyPool[i].m_originalBody;
  104. if (body)
  105. {
  106. m_tmpSolverBodyPool[i].m_originalBody->setLinearVelocity(m_tmpSolverBodyPool[i].m_linearVelocity + m_tmpSolverBodyPool[i].m_deltaLinearVelocity);
  107. m_tmpSolverBodyPool[i].m_originalBody->setAngularVelocity(m_tmpSolverBodyPool[i].m_angularVelocity + m_tmpSolverBodyPool[i].m_deltaAngularVelocity);
  108. }
  109. }
  110. }
  111. void btDeformableMultiBodyConstraintSolver::pairDeformableAndSolverBody(btCollisionObject** bodies, int numBodies, int numDeformableBodies, const btContactSolverInfo& infoGlobal)
  112. {
  113. if (!m_deformableSolver->isReducedSolver())
  114. {
  115. return;
  116. }
  117. btReducedDeformableBodySolver* solver = static_cast<btReducedDeformableBodySolver*>(m_deformableSolver);
  118. for (int i = 0; i < numDeformableBodies; ++i)
  119. {
  120. for (int k = 0; k < solver->m_nodeRigidConstraints[i].size(); ++k)
  121. {
  122. btReducedDeformableNodeRigidContactConstraint& constraint = solver->m_nodeRigidConstraints[i][k];
  123. if (!constraint.m_contact->m_cti.m_colObj->isStaticObject())
  124. {
  125. btCollisionObject& col_obj = const_cast<btCollisionObject&>(*constraint.m_contact->m_cti.m_colObj);
  126. // object index in the solver body pool
  127. int bodyId = getOrInitSolverBody(col_obj, infoGlobal.m_timeStep);
  128. const btRigidBody* body = btRigidBody::upcast(bodies[bodyId]);
  129. if (body && body->getInvMass())
  130. {
  131. // std::cout << "Node: " << constraint.m_node->index << ", body: " << bodyId << "\n";
  132. btSolverBody& solverBody = m_tmpSolverBodyPool[bodyId];
  133. constraint.setSolverBody(bodyId, solverBody);
  134. }
  135. }
  136. }
  137. // for (int j = 0; j < numBodies; j++)
  138. // {
  139. // int bodyId = getOrInitSolverBody(*bodies[j], infoGlobal.m_timeStep);
  140. // btRigidBody* body = btRigidBody::upcast(bodies[j]);
  141. // if (body && body->getInvMass())
  142. // {
  143. // btSolverBody& solverBody = m_tmpSolverBodyPool[bodyId];
  144. // m_deformableSolver->pairConstraintWithSolverBody(i, bodyId, solverBody);
  145. // }
  146. // }
  147. }
  148. }
  149. void btDeformableMultiBodyConstraintSolver::solveGroupCacheFriendlySplitImpulseIterations(btCollisionObject** bodies, int numBodies, btCollisionObject** deformableBodies, int numDeformableBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
  150. {
  151. BT_PROFILE("solveGroupCacheFriendlySplitImpulseIterations");
  152. int iteration;
  153. if (infoGlobal.m_splitImpulse)
  154. {
  155. {
  156. for (iteration = 0; iteration < infoGlobal.m_numIterations; iteration++)
  157. {
  158. btScalar leastSquaresResidual = 0.f;
  159. {
  160. int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
  161. int j;
  162. for (j = 0; j < numPoolConstraints; j++)
  163. {
  164. const btSolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[m_orderTmpConstraintPool[j]];
  165. btScalar residual = resolveSplitPenetrationImpulse(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA], m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB], solveManifold);
  166. leastSquaresResidual = btMax(leastSquaresResidual, residual * residual);
  167. }
  168. // solve the position correction between deformable and rigid/multibody
  169. // btScalar residual = m_deformableSolver->solveSplitImpulse(infoGlobal);
  170. btScalar residual = m_deformableSolver->m_objective->m_projection.solveSplitImpulse(deformableBodies, numDeformableBodies, infoGlobal);
  171. leastSquaresResidual = btMax(leastSquaresResidual, residual * residual);
  172. }
  173. if (leastSquaresResidual <= infoGlobal.m_leastSquaresResidualThreshold || iteration >= (infoGlobal.m_numIterations - 1))
  174. {
  175. #ifdef VERBOSE_RESIDUAL_PRINTF
  176. if (iteration >= (infoGlobal.m_numIterations - 1))
  177. printf("split impulse residual = %f at iteration #%d\n", leastSquaresResidual, iteration);
  178. #endif
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. }