Skip to content

Commit

Permalink
Support loading mesh by mesh name in <mesh><uri> (#2007)
Browse files Browse the repository at this point in the history
Allow users to specify name of mesh in SDF via <mesh><uri>. Addresses this use case: User has created common:Mesh object and added it to the MeshManager via AddMesh call. They now want to spawn a new model with this mesh using an SDF string that has <mesh><uri> set to the name of their common::Mesh.

---------

Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 authored Jul 21, 2023
1 parent 0fe4ed0 commit 50a99e6
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 21 deletions.
7 changes: 7 additions & 0 deletions include/gz/sim/Util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <unordered_set>
#include <vector>

#include <gz/common/Mesh.hh>
#include <gz/math/Pose3.hh>
#include <sdf/Mesh.hh>

#include "gz/sim/components/Environment.hh"
#include "gz/sim/config.hh"
Expand Down Expand Up @@ -309,6 +311,11 @@ namespace gz
const math::Vector3d& _worldPosition,
const std::shared_ptr<components::EnvironmentalData>& _gridField);

/// \brief Load a mesh from a Mesh SDF DOM
/// \param[in] _meshSdf Mesh SDF DOM
/// \return The loaded mesh or null if the mesh can not be loaded.
GZ_SIM_VISIBLE const common::Mesh *loadMesh(const sdf::Mesh &_meshSdf);

/// \brief Environment variable holding resource paths.
const std::string kResourcePathEnv{"GZ_SIM_RESOURCE_PATH"};

Expand Down
41 changes: 41 additions & 0 deletions src/Util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
#include <gz/msgs/entity.pb.h>

#include <gz/common/Filesystem.hh>
#include <gz/common/Mesh.hh>
#include <gz/common/MeshManager.hh>
#include <gz/common/StringUtils.hh>
#include <gz/common/URI.hh>
#include <gz/common/Util.hh>
#include <gz/math/Helpers.hh>
#include <gz/math/Pose3.hh>
Expand Down Expand Up @@ -837,6 +840,44 @@ std::string resolveSdfWorldFile(const std::string &_sdfFile,

return filePath;
}

const common::Mesh *loadMesh(const sdf::Mesh &_meshSdf)
{
const common::Mesh *mesh = nullptr;
auto &meshManager = *common::MeshManager::Instance();
if (common::URI(_meshSdf.Uri()).Scheme() == "name")
{
// if it has a name:// scheme, see if the mesh
// exists in the mesh manager and load it by name
const std::string basename = common::basename(_meshSdf.Uri());
mesh = meshManager.MeshByName(basename);
if (nullptr == mesh)
{
gzwarn << "Failed to load mesh by name [" << basename
<< "]." << std::endl;
return nullptr;
}
}
else if (meshManager.IsValidFilename(_meshSdf.Uri()))
{
// load mesh by file path
auto fullPath = asFullPath(_meshSdf.Uri(), _meshSdf.FilePath());
mesh = meshManager.Load(fullPath);
if (nullptr == mesh)
{
gzwarn << "Failed to load mesh from [" << fullPath
<< "]." << std::endl;
return nullptr;
}
}
else
{
gzwarn << "Failed to load mesh [" << _meshSdf.Uri()
<< "]." << std::endl;
return nullptr;
}
return mesh;
}
}
}
}
21 changes: 21 additions & 0 deletions src/Util_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "gz/sim/Util.hh"

#include "helpers/EnvTestFixture.hh"
#include "test_config.hh"

using namespace gz;
using namespace sim;
Expand Down Expand Up @@ -1003,3 +1004,23 @@ TEST_F(UtilTest, ResolveSdfWorldFile)
// A bad relative path should return an empty string
EXPECT_TRUE(resolveSdfWorldFile("../invalid/does_not_exist.sdf").empty());
}

/////////////////////////////////////////////////
TEST_F(UtilTest, LoadMesh)
{
sdf::Mesh meshSdf;
EXPECT_EQ(nullptr, loadMesh(meshSdf));

meshSdf.SetUri("invalid_uri");
meshSdf.SetFilePath("invalid_filepath");
EXPECT_EQ(nullptr, loadMesh(meshSdf));

meshSdf.SetUri("name://unit_box");
EXPECT_NE(nullptr, loadMesh(meshSdf));

meshSdf.SetUri("duck.dae");
std::string filePath = common::joinPaths(std::string(PROJECT_SOURCE_PATH),
"test", "media", "duck.dae");
meshSdf.SetFilePath(filePath);
EXPECT_NE(nullptr, loadMesh(meshSdf));
}
25 changes: 12 additions & 13 deletions src/rendering/SceneManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -702,23 +702,22 @@ rendering::GeometryPtr SceneManager::LoadGeometry(const sdf::Geometry &_geom,
}
else if (_geom.Type() == sdf::GeometryType::MESH)
{
auto fullPath = asFullPath(_geom.MeshShape()->Uri(),
_geom.MeshShape()->FilePath());
if (fullPath.empty())
{
gzerr << "Mesh geometry missing uri" << std::endl;
return geom;
}
rendering::MeshDescriptor descriptor;

// Assume absolute path to mesh file
descriptor.meshName = fullPath;
descriptor.mesh = loadMesh(*_geom.MeshShape());
if (!descriptor.mesh)
return geom;
std::string meshUri =
(common::URI(_geom.MeshShape()->Uri()).Scheme() == "name") ?
common::basename(_geom.MeshShape()->Uri()) :
asFullPath(_geom.MeshShape()->Uri(),
_geom.MeshShape()->FilePath());
auto a = asFullPath(_geom.MeshShape()->Uri(),
_geom.MeshShape()->FilePath());

descriptor.meshName = meshUri;
descriptor.subMeshName = _geom.MeshShape()->Submesh();
descriptor.centerSubMesh = _geom.MeshShape()->CenterSubmesh();

common::MeshManager *meshManager =
common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
geom = this->dataPtr->scene->CreateMesh(descriptor);
scale = _geom.MeshShape()->Scale();
}
Expand Down
10 changes: 2 additions & 8 deletions src/systems/physics/Physics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1406,15 +1406,9 @@ void PhysicsPrivate::CreateCollisionEntities(const EntityComponentManager &_ecm,
return true;
}

auto &meshManager = *common::MeshManager::Instance();
auto fullPath = asFullPath(meshSdf->Uri(), meshSdf->FilePath());
auto *mesh = meshManager.Load(fullPath);
if (nullptr == mesh)
{
gzwarn << "Failed to load mesh from [" << fullPath
<< "]." << std::endl;
const common::Mesh *mesh = loadMesh(*meshSdf);
if (!mesh)
return true;
}

auto linkMeshFeature =
this->entityLinkMap.EntityCast<MeshFeatureList>(_parent->Data());
Expand Down
1 change: 1 addition & 0 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ set(tests_needing_display
depth_camera.cc
distortion_camera.cc
gpu_lidar.cc
mesh_uri.cc
optical_tactile_plugin.cc
reset_sensors.cc
rgbd_camera.cc
Expand Down
Loading

0 comments on commit 50a99e6

Please sign in to comment.