Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions graphics/include/gz/common/MeshManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ namespace gz
/// \param[in] _name the name of the mesh
public: bool HasMesh(const std::string &_name) const;

/// \brief Create an empty mesh and register it by name. If the mesh
/// already exists, returns nullptr.
/// \param[in] _name the name of the new mesh
/// \return a mutable pointer pointing to the newly allocated mesh, or
/// nullptr if the mesh name is already claimed.
public: common::Mesh *CreateMesh(const std::string &_name);

/// \brief Create a sphere mesh.
/// \param[in] _name the name of the mesh
/// \param[in] _radius radius of the sphere in meter
Expand Down
15 changes: 15 additions & 0 deletions graphics/src/MeshManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ void MeshManager::AddMesh(Mesh *_mesh)
this->dataPtr->meshes[_mesh->Name()] = _mesh;
}

//////////////////////////////////////////////////
Mesh *MeshManager::CreateMesh(const std::string &_name)
{
if (this->HasMesh(_name))
{
gzerr << "Mesh [" << _name << "] already exists." << std::endl;
return nullptr;
}

Mesh *newMesh = new Mesh();
newMesh->SetName(_name);
this->dataPtr->meshes[_name] = newMesh;
return newMesh;
}

//////////////////////////////////////////////////
const Mesh *MeshManager::MeshByName(const std::string &_name) const
{
Expand Down
33 changes: 33 additions & 0 deletions graphics/src/MeshManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,37 @@ TEST_F(MeshManager, MergeSubMeshes)
EXPECT_EQ(math::Vector2d(0, 0.6), mergedSubmesh->TexCoordBySet(5u, 2u));
}

/////////////////////////////////////////////////
TEST_F(MeshManager, CreateMesh)
{
auto *mgr = common::MeshManager::Instance();
std::string meshName = "test_create_mesh";

// Pre-condition
EXPECT_FALSE(mgr->HasMesh(meshName));
EXPECT_EQ(nullptr, mgr->MeshByName(meshName));

// Create the mesh
common::Mesh *mutableMesh = mgr->CreateMesh(meshName);
ASSERT_NE(nullptr, mutableMesh);
EXPECT_EQ(meshName, mutableMesh->Name());

// Fetch the newly created mesh
const common::Mesh *cachedMesh = mgr->MeshByName(meshName);
ASSERT_NE(nullptr, cachedMesh);
EXPECT_EQ(mutableMesh, cachedMesh);
EXPECT_TRUE(mgr->HasMesh(meshName));

// Attempt to create the same mesh again should return nullptr safely
common::Mesh *duplicateMesh = mgr->CreateMesh(meshName);
EXPECT_EQ(nullptr, duplicateMesh);

// Verify original mesh is intact
const common::Mesh *verifyMesh = mgr->MeshByName(meshName);
EXPECT_NE(nullptr, verifyMesh);
EXPECT_EQ(meshName, verifyMesh->Name());

mgr->RemoveAll();
}

#endif
Loading