Skip to content

Commit d3ee64a

Browse files
authored
Merge 0b61cb1 into e47d2d5
2 parents e47d2d5 + 0b61cb1 commit d3ee64a

File tree

5 files changed

+166
-85
lines changed

5 files changed

+166
-85
lines changed

src/systems/physics/EntityFeatureMap.hh

+27-4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ namespace systems::physics_system
137137
return castEntity;
138138
}
139139
}
140+
140141
/// \brief Helper function to cast from an entity type with minimum features
141142
/// to an entity with a different set of features. This overload takes a
142143
/// physics entity as input
@@ -187,6 +188,21 @@ namespace systems::physics_system
187188
return kNullEntity;
188189
}
189190

191+
/// \brief Get the physics entity with required features that has a
192+
/// particular ID
193+
/// \param[in] _id The ID of the desired physics entity
194+
/// \return If found, returns the corresponding physics entity. Otherwise,
195+
/// nullptr
196+
public: RequiredEntityPtr GetPhysicsEntityPtr(std::size_t _id) const
197+
{
198+
auto it = this->physEntityById.find(_id);
199+
if (it != this->physEntityById.end())
200+
{
201+
return it->second;
202+
}
203+
return nullptr;
204+
}
205+
190206
/// \brief Check whether there is a physics entity associated with the given
191207
/// Gazebo entity
192208
/// \param[in] _entity Gazebo entity.
@@ -215,6 +231,7 @@ namespace systems::physics_system
215231
{
216232
this->entityMap[_entity] = _physicsEntity;
217233
this->reverseMap[_physicsEntity] = _entity;
234+
this->physEntityById[_physicsEntity->EntityID()] = _physicsEntity;
218235
}
219236

220237
/// \brief Remove entity from all associated maps
@@ -226,8 +243,9 @@ namespace systems::physics_system
226243
if (it != this->entityMap.end())
227244
{
228245
this->reverseMap.erase(it->second);
229-
this->entityMap.erase(it);
246+
this->physEntityById.erase(it->second->EntityID());
230247
this->castCache.erase(_entity);
248+
this->entityMap.erase(it);
231249
return true;
232250
}
233251
return false;
@@ -242,8 +260,9 @@ namespace systems::physics_system
242260
if (it != this->reverseMap.end())
243261
{
244262
this->entityMap.erase(it->second);
245-
this->reverseMap.erase(it);
263+
this->physEntityById.erase(it->first->EntityID());
246264
this->castCache.erase(it->second);
265+
this->reverseMap.erase(it);
247266
return true;
248267
}
249268
return false;
@@ -257,13 +276,13 @@ namespace systems::physics_system
257276
return this->entityMap;
258277
}
259278

260-
/// \brief Get the total number of entries in the three maps. Only used for
279+
/// \brief Get the total number of entries in the maps. Only used for
261280
/// testing.
262281
/// \return Number of entries in all the maps.
263282
public: std::size_t TotalMapEntryCount() const
264283
{
265284
return this->entityMap.size() + this->reverseMap.size() +
266-
this->castCache.size();
285+
this->castCache.size() + this->physEntityById.size();
267286
}
268287

269288
/// \brief Map from Gazebo entity to physics entities with required features
@@ -272,6 +291,10 @@ namespace systems::physics_system
272291
/// \brief Reverse map of entityMap
273292
private: std::unordered_map<RequiredEntityPtr, Entity> reverseMap;
274293

294+
/// \brief Map of physics entity IDs to the corresponding physics entity
295+
/// with required features
296+
private: std::unordered_map<std::size_t, RequiredEntityPtr> physEntityById;
297+
275298
/// \brief Cache map from Gazebo entity to physics entities with optional
276299
/// features
277300
private: mutable std::unordered_map<Entity, ValueType> castCache;

src/systems/physics/EntityFeatureMap_TEST.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ TEST_F(EntityFeatureMapFixture, AddCastRemoveEntity)
118118

119119
testMap.AddEntity(gazeboWorld1Entity, testWorld1);
120120

121-
// After adding the entity, there should be one entry each in two maps
122-
EXPECT_EQ(2u, testMap.TotalMapEntryCount());
121+
// After adding the entity, there should be one entry each in three maps
122+
EXPECT_EQ(3u, testMap.TotalMapEntryCount());
123123
EXPECT_EQ(testWorld1, testMap.Get(gazeboWorld1Entity));
124124
EXPECT_EQ(gazeboWorld1Entity, testMap.Get(testWorld1));
125125

@@ -128,42 +128,42 @@ TEST_F(EntityFeatureMapFixture, AddCastRemoveEntity)
128128
testMap.EntityCast<TestOptionalFeatures1>(gazeboWorld1Entity);
129129
ASSERT_NE(nullptr, testWorld1Feature1);
130130
// After the cast, there should be one more entry in the cache map.
131-
EXPECT_EQ(3u, testMap.TotalMapEntryCount());
131+
EXPECT_EQ(4u, testMap.TotalMapEntryCount());
132132

133133
// Cast to optional feature2
134134
auto testWorld1Feature2 =
135135
testMap.EntityCast<TestOptionalFeatures2>(gazeboWorld1Entity);
136136
ASSERT_NE(nullptr, testWorld1Feature2);
137137
// After the cast, the number of entries should remain the same because we
138138
// have not added an entity.
139-
EXPECT_EQ(3u, testMap.TotalMapEntryCount());
139+
EXPECT_EQ(4u, testMap.TotalMapEntryCount());
140140

141141
// Add another entity
142142
WorldPtrType testWorld2 = this->engine->ConstructEmptyWorld("world2");
143143
testMap.AddEntity(gazeboWorld2Entity, testWorld2);
144-
EXPECT_EQ(5u, testMap.TotalMapEntryCount());
144+
EXPECT_EQ(7u, testMap.TotalMapEntryCount());
145145
EXPECT_EQ(testWorld2, testMap.Get(gazeboWorld2Entity));
146146
EXPECT_EQ(gazeboWorld2Entity, testMap.Get(testWorld2));
147147

148148
auto testWorld2Feature1 =
149149
testMap.EntityCast<TestOptionalFeatures1>(testWorld2);
150150
ASSERT_NE(nullptr, testWorld2Feature1);
151151
// After the cast, there should be one more entry in the cache map.
152-
EXPECT_EQ(6u, testMap.TotalMapEntryCount());
152+
EXPECT_EQ(8u, testMap.TotalMapEntryCount());
153153

154154
auto testWorld2Feature2 =
155155
testMap.EntityCast<TestOptionalFeatures2>(testWorld2);
156156
ASSERT_NE(nullptr, testWorld2Feature2);
157157
// After the cast, the number of entries should remain the same because we
158158
// have not added an entity.
159-
EXPECT_EQ(6u, testMap.TotalMapEntryCount());
159+
EXPECT_EQ(8u, testMap.TotalMapEntryCount());
160160

161161
// Remove entitites
162162
testMap.Remove(gazeboWorld1Entity);
163163
EXPECT_FALSE(testMap.HasEntity(gazeboWorld1Entity));
164164
EXPECT_EQ(nullptr, testMap.Get(gazeboWorld1Entity));
165165
EXPECT_EQ(gazebo::kNullEntity, testMap.Get(testWorld1));
166-
EXPECT_EQ(3u, testMap.TotalMapEntryCount());
166+
EXPECT_EQ(4u, testMap.TotalMapEntryCount());
167167

168168
testMap.Remove(testWorld2);
169169
EXPECT_FALSE(testMap.HasEntity(gazeboWorld2Entity));

0 commit comments

Comments
 (0)