Skip to content

Commit

Permalink
Added method to remove meshes from the MeshManager (#222)
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Louise Poubel <louise@openrobotics.org>
Co-authored-by: Michael Carroll <michael@openrobotics.org>
Co-authored-by: Nate Koenig <nate@openrobotics.org>
Co-authored-by: Nate Koenig <nkoenig@users.noreply.github.com>
  • Loading branch information
5 people authored Nov 9, 2021
1 parent 8ba9b71 commit b337f4e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions graphics/include/ignition/common/MeshManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ namespace ignition
/// \param[in] the mesh to add.
public: void AddMesh(Mesh *_mesh);

/// \brief Remove a mesh based on a name.
/// \param[in] _name Name of the mesh to remove.
/// \return True if the mesh was removed, false if the mesh with the
/// provided name could not be found.
public: bool RemoveMesh(const std::string &_name);

/// \brief Remove all meshes.
public: void RemoveAll();

/// \brief Get a mesh by name.
/// \param[in] _name the name of the mesh to look for
/// \return the mesh or nullptr if not found
Expand Down
27 changes: 27 additions & 0 deletions graphics/src/MeshManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,33 @@ const Mesh *MeshManager::MeshByName(const std::string &_name) const
return nullptr;
}

//////////////////////////////////////////////////
void MeshManager::RemoveAll()
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
for (auto m : this->dataPtr->meshes)
{
delete m.second;
}
this->dataPtr->meshes.clear();
}

//////////////////////////////////////////////////
bool MeshManager::RemoveMesh(const std::string &_name)
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);

auto iter = this->dataPtr->meshes.find(_name);
if (iter != this->dataPtr->meshes.end())
{
delete iter->second;
this->dataPtr->meshes.erase(iter);
return true;
}

return false;
}

//////////////////////////////////////////////////
bool MeshManager::HasMesh(const std::string &_name) const
{
Expand Down
22 changes: 22 additions & 0 deletions graphics/src/MeshManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,28 @@ TEST_F(MeshManager, CreateExtrudedPolylineInvalid)
EXPECT_TRUE(!common::MeshManager::Instance()->HasMesh(meshName));
}

/////////////////////////////////////////////////
TEST_F(MeshManager, Remove)
{
auto mgr = common::MeshManager::Instance();

EXPECT_FALSE(mgr->HasMesh("box"));
mgr->CreateBox("box",
ignition::math::Vector3d(1, 1, 1),
ignition::math::Vector2d(0, 0));
EXPECT_TRUE(mgr->HasMesh("box"));

mgr->CreateSphere("sphere", 1.0, 1, 1);
EXPECT_TRUE(mgr->HasMesh("sphere"));

EXPECT_TRUE(mgr->RemoveMesh("box"));
EXPECT_FALSE(mgr->HasMesh("box"));
EXPECT_TRUE(mgr->HasMesh("sphere"));

mgr->RemoveAll();
EXPECT_FALSE(mgr->HasMesh("sphere"));
}

/////////////////////////////////////////////////
int main(int argc, char **argv)
{
Expand Down

0 comments on commit b337f4e

Please sign in to comment.