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

Support visualizing mesh collisions with convex decomposition #2331

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ msgs::Geometry gz::sim::convert(const sdf::Geometry &_in)
meshMsg->set_filename(asFullPath(meshSdf->Uri(), meshSdf->FilePath()));
meshMsg->set_submesh(meshSdf->Submesh());
meshMsg->set_center_submesh(meshSdf->CenterSubmesh());

if (!meshSdf->Simplification().empty())
{
auto header = out.mutable_header()->add_data();
header->set_key("simplification");
header->add_value(meshSdf->Simplification());
}
}
else if (_in.Type() == sdf::GeometryType::HEIGHTMAP && _in.HeightmapShape())
{
Expand Down Expand Up @@ -341,6 +348,14 @@ sdf::Geometry gz::sim::convert(const msgs::Geometry &_in)
meshShape.SetSubmesh(_in.mesh().submesh());
meshShape.SetCenterSubmesh(_in.mesh().center_submesh());

for (int i = 0; i < _in.header().data_size(); ++i)
{
auto data = _in.header().data(i);
if (data.key() == "simplification" && data.value_size() > 0)
{
meshShape.SetSimplification(data.value(0));
}
}
out.SetMeshShape(meshShape);
}
else if (_in.type() == msgs::Geometry::HEIGHTMAP && _in.has_heightmap())
Expand Down
5 changes: 5 additions & 0 deletions src/Conversions_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ TEST(Conversions, GeometryMesh)
meshShape.SetUri("file://watermelon");
meshShape.SetSubmesh("grape");
meshShape.SetCenterSubmesh(true);
meshShape.SetSimplification("convex_decomposition");
geometry.SetMeshShape(meshShape);

auto geometryMsg = convert<msgs::Geometry>(geometry);
Expand All @@ -473,6 +474,9 @@ TEST(Conversions, GeometryMesh)
EXPECT_EQ("file://watermelon", geometryMsg.mesh().filename());
EXPECT_EQ("grape", geometryMsg.mesh().submesh());
EXPECT_TRUE(geometryMsg.mesh().center_submesh());
auto header = geometryMsg.header().data(0);
EXPECT_EQ("simplification", header.key());
EXPECT_EQ("convex_decomposition", header.value(0));

auto newGeometry = convert<sdf::Geometry>(geometryMsg);
EXPECT_EQ(sdf::GeometryType::MESH, newGeometry.Type());
Expand All @@ -481,6 +485,7 @@ TEST(Conversions, GeometryMesh)
EXPECT_EQ("file://watermelon", newGeometry.MeshShape()->Uri());
EXPECT_EQ("grape", newGeometry.MeshShape()->Submesh());
EXPECT_TRUE(newGeometry.MeshShape()->CenterSubmesh());
EXPECT_EQ("convex_decomposition", newGeometry.MeshShape()->Simplification());
}

/////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <gz/common/MeshManager.hh>
#include <gz/common/Profiler.hh>
#include <gz/common/StringUtils.hh>
#include <gz/common/SubMesh.hh>
#include <gz/common/Uuid.hh>

#include <gz/gui/Application.hh>
Expand Down Expand Up @@ -1245,6 +1246,49 @@ rendering::GeometryPtr VisualizationCapabilitiesPrivate::CreateGeometry(
gz::common::MeshManager *meshManager =
gz::common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);

common::Mesh newMesh;
if (_geom.MeshShape()->Simplification() == "convex_decomposition")
{
gzdbg << "Simplifying mesh: " << descriptor.mesh->Name() << std::endl;
for (unsigned int submeshIdx = 0;
submeshIdx < descriptor.mesh->SubMeshCount();
++submeshIdx)
{
auto s = descriptor.mesh->SubMeshByIndex(submeshIdx).lock();

// check if a particular submesh is requested
if (!descriptor.subMeshName.empty() &&
s->Name() != descriptor.subMeshName)
{
continue;
}

// center the submesh if needed
if (descriptor.centerSubMesh)
s->Center(math::Vector3d::Zero);

// convex decomposition
auto decomposed = meshManager->ConvexDecomposition(s.get());
if (!decomposed.empty())
{
for (std::size_t n = 0; n < decomposed.size(); ++n)
Copy link
Contributor

Choose a reason for hiding this comment

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

include <cstddef>

{
newMesh.AddSubMesh(decomposed[n]);
}
}
}
if (newMesh.SubMeshCount() > 0u)
{
descriptor.mesh = &newMesh;
// if submesh is requested, we handled this above before mesh
// decomposition so we do not need need to pass these flags to
// gz-rendering
descriptor.subMeshName = "";
descriptor.centerSubMesh = false;
}
}

geom = this->scene->CreateMesh(descriptor);
scale = _geom.MeshShape()->Scale();
}
Expand Down
Loading