Skip to content

Commit

Permalink
update code to merge meshes before decomp
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Apr 1, 2024
1 parent dd2c5ba commit 69851f4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
23 changes: 18 additions & 5 deletions src/Conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,18 @@ msgs::Geometry gz::sim::convert(const sdf::Geometry &_in)
meshMsg->set_submesh(meshSdf->Submesh());
meshMsg->set_center_submesh(meshSdf->CenterSubmesh());

if (!meshSdf->Simplification().empty())
if (!meshSdf->OptimizationStr().empty())
{
auto header = out.mutable_header()->add_data();
header->set_key("simplification");
header->add_value(meshSdf->Simplification());
header->set_key("optimization");
header->add_value(meshSdf->OptimizationStr());
}
if (meshSdf->ConvexDecomposition())
{
auto header = out.mutable_header()->add_data();
header->set_key("max_convex_hulls");
header->add_value(std::to_string(
meshSdf->ConvexDecomposition()->MaxConvexHulls()));
}
}
else if (_in.Type() == sdf::GeometryType::HEIGHTMAP && _in.HeightmapShape())
Expand Down Expand Up @@ -351,9 +358,15 @@ sdf::Geometry gz::sim::convert(const msgs::Geometry &_in)
for (int i = 0; i < _in.header().data_size(); ++i)
{
auto data = _in.header().data(i);
if (data.key() == "simplification" && data.value_size() > 0)
if (data.key() == "optimization" && data.value_size() > 0)
{
meshShape.SetOptimization(data.value(0));
}
if (data.key() == "max_convex_hulls" && data.value_size() > 0)
{
meshShape.SetSimplification(data.value(0));
sdf::ConvexDecomposition convexDecomp;
convexDecomp.SetMaxConvexHulls(std::stoul(data.value(0)));
meshShape.SetConvexDecomposition(convexDecomp);
}
}
out.SetMeshShape(meshShape);
Expand Down
6 changes: 3 additions & 3 deletions src/Conversions_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ TEST(Conversions, GeometryMesh)
meshShape.SetUri("file://watermelon");
meshShape.SetSubmesh("grape");
meshShape.SetCenterSubmesh(true);
meshShape.SetSimplification("convex_decomposition");
meshShape.SetOptimization("convex_decomposition");
geometry.SetMeshShape(meshShape);

auto geometryMsg = convert<msgs::Geometry>(geometry);
Expand All @@ -475,7 +475,7 @@ TEST(Conversions, GeometryMesh)
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("optimization", header.key());
EXPECT_EQ("convex_decomposition", header.value(0));

auto newGeometry = convert<sdf::Geometry>(geometryMsg);
Expand All @@ -485,7 +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());
EXPECT_EQ("convex_decomposition", newGeometry.MeshShape()->OptimizationStr());
}

/////////////////////////////////////////////////
Expand Down
2 changes: 0 additions & 2 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ Server::Server(const ServerConfig &_config)

sdf::Root sdfRoot;
sdf::ParserConfig sdfParserConfig = sdf::ParserConfig::GlobalConfig();
sdfParserConfig.SetStoreResolvedURIs(true);

sdfParserConfig.SetCalculateInertialConfiguration(
sdf::ConfigureResolveAutoInertials::SKIP_CALCULATION_IN_LOAD);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <gz/msgs/stringmsg.pb.h>

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <map>
#include <set>
Expand Down Expand Up @@ -1248,47 +1249,74 @@ rendering::GeometryPtr VisualizationCapabilitiesPrivate::CreateGeometry(
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)
if (_geom.MeshShape()->Optimization() ==
sdf::MeshOptimization::CONVEX_HULL ||
_geom.MeshShape()->Optimization() ==
sdf::MeshOptimization::CONVEX_DECOMPOSITION)
{
std::unique_ptr<common::Mesh> meshToDecompose =
std::make_unique<common::Mesh>();
// check if a particular submesh is requested
if (!descriptor.subMeshName.empty())
{
auto s = descriptor.mesh->SubMeshByIndex(submeshIdx).lock();

// check if a particular submesh is requested
if (!descriptor.subMeshName.empty() &&
s->Name() != descriptor.subMeshName)
for (unsigned int submeshIdx = 0;
submeshIdx < descriptor.mesh->SubMeshCount();
++submeshIdx)
{
continue;
auto submesh = descriptor.mesh->SubMeshByIndex(submeshIdx).lock();
if (submesh->Name() == descriptor.subMeshName)
{
if (descriptor.centerSubMesh)
submesh->Center(math::Vector3d::Zero);
meshToDecompose->AddSubMesh(*submesh.get());
break;
}
}

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

// convex decomposition
auto decomposed = meshManager->ConvexDecomposition(s.get());
}
else
{
meshToDecompose =
gz::common::MeshManager::MergeSubMeshes(*descriptor.mesh);
}
if (meshToDecompose && meshToDecompose->SubMeshCount() == 1u)
{
std::size_t maxConvexHulls = 16u;
if (_geom.MeshShape()->Optimization() ==
sdf::MeshOptimization::CONVEX_HULL)
{
/// create 1 convex hull for the whole submesh
maxConvexHulls = 1u;
}
else if (_geom.MeshShape()->ConvexDecomposition())
{
// limit max number of convex hulls to generate
maxConvexHulls =
_geom.MeshShape()->ConvexDecomposition()->MaxConvexHulls();
}
auto submesh = meshToDecompose->SubMeshByIndex(0u).lock();
std::vector<common::SubMesh> decomposed =
gz::common::MeshManager::ConvexDecomposition(
*submesh.get(), maxConvexHulls);
gzdbg << "Optimizing mesh (" << _geom.MeshShape()->OptimizationStr()
<< "): " << descriptor.mesh->Name() << std::endl;
if (!decomposed.empty())
{
for (std::size_t n = 0; n < decomposed.size(); ++n)
{
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;
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

0 comments on commit 69851f4

Please sign in to comment.