Skip to content

Commit

Permalink
Consolidate entity creation. (#2452)
Browse files Browse the repository at this point in the history
* Resolve failing tests

Signed-off-by: Nate Koenig <natekoenig@gmail.com>

* Cleanup

Signed-off-by: Nate Koenig <natekoenig@gmail.com>

* Update src/EntityComponentManager.cc

Co-authored-by: Ian Chen <ichen@openrobotics.org>
Signed-off-by: Nate Koenig <natekoenig@gmail.com>

* Incorrect else

Signed-off-by: Nate Koenig <natekoenig@gmail.com>

---------

Signed-off-by: Nate Koenig <natekoenig@gmail.com>
Co-authored-by: Ian Chen <ichen@openrobotics.org>
  • Loading branch information
nkoenig and iche033 authored Jul 3, 2024
1 parent a02e891 commit 517a73a
Show file tree
Hide file tree
Showing 16 changed files with 483 additions and 407 deletions.
8 changes: 8 additions & 0 deletions include/gz/sim/EntityComponentManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,14 @@ namespace gz
/// \return True if there are components marked for removal.
public: bool HasRemovedComponents() const;

/// \brief Get an Entity based on a name component that is associated
/// with the entity.
/// \param[in] _name Name associated with the Entity
/// \return The Entity, if an Entity with the given name exists,
/// otherwise return std::nullopt.
public: std::optional<Entity> EntityByName(
const std::string &_name) const;

/// \brief Clear the list of newly added entities so that a call to
/// EachAdded after this will have no entities to iterate. This function
/// is protected to facilitate testing.
Expand Down
10 changes: 10 additions & 0 deletions include/gz/sim/SdfEntityCreator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ namespace gz
/// \return World entity.
public: Entity CreateEntities(const sdf::World *_world);

/// \brief Create all entities that exist in the sdf::World object and
/// load their plugins.
/// \param[in] _world SDF world object.
/// \param[in] _worldEntity The world entity object.
public: void CreateEntities(const sdf::World *_world,
Entity _worldEntity);

/// \brief Create all entities that exist in the sdf::Model object and
/// load their plugins. Also loads plugins of child sensors.
/// \param[in] _model SDF model object.
Expand Down Expand Up @@ -186,6 +193,9 @@ namespace gz
private: Entity CreateEntities(const sdf::Model *_model,
bool _staticParent);

/// \brief Load plugins for all models
private: void LoadModelPlugins();

/// \brief Pointer to private data.
private: std::unique_ptr<SdfEntityCreatorPrivate> dataPtr;
};
Expand Down
6 changes: 6 additions & 0 deletions include/gz/sim/components/Performer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef GZ_SIM_COMPONENTS_PERFORMER_HH_
#define GZ_SIM_COMPONENTS_PERFORMER_HH_

#include <string>
#include <gz/sim/config.hh>
#include <gz/sim/Export.hh>

Expand All @@ -34,6 +35,11 @@ namespace components
/// \brief This component identifies an entity as being a performer.
using Performer = Component<NoData, class PerformerTag>;
GZ_SIM_REGISTER_COMPONENT("gz_sim_components.Performer", Performer)

/// \brief This component contains the performer reference name.
using PerformerRef = Component<std::string, class PerformerRefTag,
serializers::StringSerializer>;
GZ_SIM_REGISTER_COMPONENT("gz_sim_components.PerformerRef", PerformerRef)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/EntityComponentManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2314,3 +2314,15 @@ void EntityComponentManager::ResetTo(const EntityComponentManager &_other)
tmpCopy.ApplyEntityDiff(*this, ecmDiff);
this->CopyFrom(tmpCopy);
}

/////////////////////////////////////////////////
std::optional<Entity> EntityComponentManager::EntityByName(
const std::string &_name) const
{
std::optional<Entity> entity;
Entity entByName = EntityByComponents(components::Name(_name));
if (entByName != kNullEntity)
entity = entByName;

return entity;
}
17 changes: 17 additions & 0 deletions src/EntityComponentManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3421,6 +3421,23 @@ TEST_P(EntityComponentManagerFixture,
EXPECT_EQ(321, comp->Data());
}

//////////////////////////////////////////////////
TEST_P(EntityComponentManagerFixture, EntityByName)
{
// Create an entity, and give it a name
Entity entity = manager.CreateEntity();
manager.CreateComponent(entity, components::Name("entity_name_a"));

// Try to get an entity that doesn't exist
std::optional<Entity> entityByName = manager.EntityByName("a_bad_name");
EXPECT_FALSE(entityByName);

entityByName = manager.EntityByName("entity_name_a");
EXPECT_TRUE(entityByName);
CompareEntityComponents<components::Name>(manager, entity,
*entityByName, true);
}

// Run multiple times. We want to make sure that static globals don't cause
// problems.
INSTANTIATE_TEST_SUITE_P(EntityComponentManagerRepeat,
Expand Down
Loading

0 comments on commit 517a73a

Please sign in to comment.