Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bullet-featherstone: Support convex decomposition for meshes #606

Merged
merged 20 commits into from
Apr 22, 2024

Conversation

iche033
Copy link
Contributor

@iche033 iche033 commented Mar 14, 2024

🎉 New feature

Replaces #603

Depends on

Summary

Supports convex decomposition on meshes. Bullet-featherstone implementation will parse the new mesh simplification optimization attribute introduced in gazebosim/sdformat#1380, decompses the mesh into convex meshes, and builds btConvexHullShape collision shapes.

Compared to btGImpactMeshes (which is currently used for all meshes), the convex hulls seems to be more stable, do not have gaps between meshes (collision margins can be set to 1mm instead of 1cm), and some manual testing shows potentially faster performance (dependent on the number of submeshes generated)

Added test to verify that convex decomposition flag is parsed and valid collisions are generated.

Other changes:

  • the m_erp2 value is now only set when there are meshes in the scene. The param was reduced to improve stability in bullet-featherstone: Improve mesh collision stability #600. I think we do not need to do it globally when there are no meshes in the world as the instability seems to only come from meshes. The value is also bumped up a little too prevent to much penetration - This will likely need more tuning as we do more testing with bullet.

To Test

Run gz sim with bullet-featherstone plugin and a world that has a model (e.g. Cordless Drill Simplified) that uses mesh decomposition:

gz sim -v 4 your_test_world.sdf --physics-engine gz-physics-bullet-featherstone-plugin

Checklist

  • Signed all commits for DCO
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
Copy link

codecov bot commented Mar 21, 2024

Codecov Report

Attention: Patch coverage is 98.52941% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 78.91%. Comparing base (492b124) to head (1f2aaa7).
Report is 15 commits behind head on main.

❗ Current head 1f2aaa7 differs from pull request most recent head fec9fad. Consider uploading reports for the commit fec9fad to get more accurate results

Files Patch % Lines
bullet-featherstone/src/SDFFeatures.cc 98.48% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #606      +/-   ##
==========================================
+ Coverage   78.32%   78.91%   +0.59%     
==========================================
  Files         140      140              
  Lines        8069     8173     +104     
==========================================
+ Hits         6320     6450     +130     
+ Misses       1749     1723      -26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
Copy link
Member

@scpeters scpeters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tinkering with this by changing the //mesh/@optimization value in the test to both an empty string and convex_hull to for comparison's sake, and I realized from the console output that we are computing convex decompositions of all the submeshes and then adding them all to a btCompoundShape. For comparison, when using bullet collisions in dartsim (which stores mesh information with submeshes in an Assimp Scene), all the submeshes are merged together into a single mesh:

I think convex_decomposition and convex_hull operations should apply to the fusion of all submeshes, unless only a single submesh is specified, in which case it would act only on that submesh. In the unoptimized case, we could also consider merging to a single mesh shape instead of using btCompoundShape (this can happen in separate pull requests)

std::vector<common::SubMesh> decomposed =
std::move(meshManager.ConvexDecomposition(*s.get(), maxConvexHulls));

gzdbg << "Optimizing mesh using convex decomposition. " << std::endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may want to remove this, but it has been very helpful for testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shortened the debug msg to one line. 1f2aaa7

I can also remove this msg completely if preferred.

bullet-featherstone/src/SDFFeatures.cc Outdated Show resolved Hide resolved
test/common_test/collisions.cc Outdated Show resolved Hide resolved
this->triangleMeshes.back().get()));
this->meshesGImpact.back()->updateBound();
this->meshesGImpact.back()->setMargin(btScalar(0.01));
compoundShape->addChildShape(btTransform::getIdentity(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this currently uses btCompoundShape to join submeshes

Copy link
Contributor Author

@iche033 iche033 Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to merge all submeses before decomposition, which addresses the comment about fusing all submeshes first before doing optimization.

Note that the decomposed submeshes are then still joined together using btCompoundShape. Same for the unoptimized case. I can change this to using a single mesh in a follow-up PR.

@iche033
Copy link
Contributor Author

iche033 commented Mar 25, 2024

I think convex_decomposition and convex_hull operations should apply to the fusion of all submeshes, unless only a single submesh is specified, in which case it would act only on that submesh. In the unoptimized case, we could also consider merging to a single mesh shape instead of using btCompoundShape (this can happen in separate pull requests)

hmm ok to make this happen, we'll need to:

  • update sdf description - currently it explicitly states decomposition is for each submesh
  • add a new function to merge multiple common::SubMeshes into one (before calling gz::common::ConvexDecomposition with that one combined submesh)

I'll work on these changes

@iche033
Copy link
Contributor Author

iche033 commented Mar 26, 2024

hmm ok to make this happen, we'll need to:

  • update sdf description - currently it explicitly states decomposition is for each submesh
  • add a new function to merge multiple common::SubMeshes into one (before calling gz::common::ConvexDecomposition with that one combined submesh)

I'll work on these changes

gazebosim/sdformat#1386
gazebosim/gz-common#588

Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code LGTM! but I did see a weird behavior while testing.

bullet-featherstone/src/SDFFeatures.cc Outdated Show resolved Hide resolved
test/common_test/collisions.cc Outdated Show resolved Hide resolved
frameDataModelOptimizedBody.pose.translation().z(), tol);
EXPECT_NEAR(0.0, frameDataModelOptimizedBody.linearVelocity.z(), tol);

initialModelPose.Pos() += gz::math::Vector3d(0, 2, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I thought testing with a V or U shaped object would really show the difference in behavior between convex_hull and convex_decomposition, but
I'm getting some unexpected penetration with convex_decomposition. I've attached the .sdf files.
image

Maybe we can use this in the test?

test_conv_decomp.zip

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an integration test for this. In the test I set <max_convex_hulls> to 64 which prevents this penetration. 2099054

The generated convex hulls looked fine with either 16 or 64 max_convex_hulls so something else is not right. This is something to look into next.

@iche033
Copy link
Contributor Author

iche033 commented Apr 17, 2024

I'm getting some unexpected penetration with convex_decomposition. I've attached the .sdf files.

I found that by setting a small m_globalCfm value, it's able to prevent this penetration, e.g.

diff --git a/bullet-featherstone/src/Base.cc b/bullet-featherstone/src/Base.cc
index ac66f574..80a04ced 100644
--- a/bullet-featherstone/src/Base.cc
+++ b/bullet-featherstone/src/Base.cc
@@ -49,6 +49,7 @@ WorldInfo::WorldInfo(std::string name_)
   // the penentration impulse depends on the erp2 parameter so set to a small
   // value (default in bullet is 0.2).
   this->world->getSolverInfo().m_erp2 = btScalar(0.02);
+  this->world->getSolverInfo().m_globalCfm = btScalar(0.001);

by default m_globalCfm is 0

Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
@iche033 iche033 merged commit 9678446 into main Apr 22, 2024
6 checks passed
@iche033 iche033 deleted the bullet_convex_8 branch April 22, 2024 23:37
iche033 added a commit that referenced this pull request Apr 23, 2024
Supports convex decomposition on meshes. Bullet-featherstone implementation will parse the new mesh optimization attribute, decompose the mesh into convex meshes, and builds btConvexHullShape collision shapes.

Signed-off-by: Ian Chen <[email protected]>
iche033 added a commit that referenced this pull request Apr 24, 2024
Supports convex decomposition on meshes. Bullet-featherstone implementation will parse the new mesh optimization attribute, decompose the mesh into convex meshes, and builds btConvexHullShape collision shapes.

Signed-off-by: Ian Chen <[email protected]>
@iche033 iche033 added the Bullet Bullet engine label Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bullet Bullet engine 🏛️ ionic Gazebo Ionic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants