Skip to content

Commit

Permalink
Fix topLevelModel method (#600)
Browse files Browse the repository at this point in the history
Signed-off-by: Ashton Larkin <[email protected]>
  • Loading branch information
adlarkin authored Feb 1, 2021
1 parent 4c87e1d commit d8b9d0c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/ignition/gazebo/Util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ namespace ignition
/// \brief Get the top level model of an entity
/// \param[in] _entity Input entity
/// \param[in] _ecm Constant reference to ECM.
/// \return Entity of top level model
/// \return Entity of top level model. If _entity has no top level model,
/// kNullEntity is returned.
ignition::gazebo::Entity IGNITION_GAZEBO_VISIBLE topLevelModel(
const Entity &_entity,
const EntityComponentManager &_ecm);
Expand Down
27 changes: 14 additions & 13 deletions src/Util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,23 @@ ignition::gazebo::Entity topLevelModel(const Entity &_entity,
{
auto entity = _entity;

// check if parent is a model
auto parentComp = _ecm.Component<components::ParentEntity>(entity);
while (parentComp)
{
// check if parent is a model
auto parentEntity = parentComp->Data();
auto modelComp = _ecm.Component<components::Model>(
parentEntity);
if (!modelComp)
// search up the entity tree and find the model with no parent models
// (there is the possibility of nested models)
Entity modelEntity = kNullEntity;
while (entity)
{
if (_ecm.Component<components::Model>(entity))
modelEntity = entity;

// stop searching if we are at the root of the tree
auto parentComp = _ecm.Component<components::ParentEntity>(entity);
if (!parentComp)
break;

// set current model entity
entity = parentEntity;
parentComp = _ecm.Component<components::ParentEntity>(entity);
entity = parentComp->Data();
}
return entity;

return modelEntity;
}

//////////////////////////////////////////////////
Expand Down
16 changes: 14 additions & 2 deletions src/Util_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ TEST_F(UtilTest, TopLevelModel)
// - linkA
// - modelB
// - linkB
// - visualB
// - modelC

// World
Expand Down Expand Up @@ -459,20 +460,31 @@ TEST_F(UtilTest, TopLevelModel)
ecm.CreateComponent(linkBEntity, components::Name("linkB_name"));
ecm.CreateComponent(linkBEntity, components::ParentEntity(modelBEntity));

// Visual B - child of Link B
auto visualBEntity = ecm.CreateEntity();
ecm.CreateComponent(visualBEntity, components::Visual());
ecm.CreateComponent(visualBEntity, components::Name("visualB_name"));
ecm.CreateComponent(visualBEntity, components::ParentEntity(linkBEntity));

// Model C
auto modelCEntity = ecm.CreateEntity();
ecm.CreateComponent(modelCEntity, components::Model());
ecm.CreateComponent(modelCEntity, components::Name("modelC_name"));
ecm.CreateComponent(modelCEntity, components::ParentEntity(worldEntity));

// model A, link A, model B and link B should have model A as top level entity
// model A, link A, model B, link B and visual B should have
// model A as the top level model
EXPECT_EQ(modelAEntity, topLevelModel(modelAEntity, ecm));
EXPECT_EQ(modelAEntity, topLevelModel(linkAEntity, ecm));
EXPECT_EQ(modelAEntity, topLevelModel(modelBEntity, ecm));
EXPECT_EQ(modelAEntity, topLevelModel(linkBEntity, ecm));
EXPECT_EQ(modelAEntity, topLevelModel(visualBEntity, ecm));

// model C should have itself as the top level entity
// model C should have itself as the top level model
EXPECT_EQ(modelCEntity, topLevelModel(modelCEntity, ecm));

// the world should have no top level model
EXPECT_EQ(kNullEntity, topLevelModel(worldEntity, ecm));
}

/////////////////////////////////////////////////
Expand Down

0 comments on commit d8b9d0c

Please sign in to comment.