Skip to content

Commit

Permalink
add ManagedArticulatedObject attribute for directly querying the link…
Browse files Browse the repository at this point in the history
…_id to object_id map (#2410)
  • Loading branch information
aclegg3 committed Jun 19, 2024
1 parent ca1494b commit 5153e89
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/esp/bindings/PhysicsObjectBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ void declareArticulatedObjectWrapper(py::module& m,
("Get a dict mapping Habitat object ids to this " +
objType + "'s link ids.")
.c_str())
.def_property_readonly(
"link_ids_to_object_ids",
&ManagedArticulatedObject::getLinkIdsToObjectIds,
("Get a dict mapping local link ids to Habitat object ids for this " +
objType + "'s link ids.")
.c_str())
.def("get_link_id_from_name",
&ManagedArticulatedObject::getLinkIdFromName,
("Get this " + objType +
Expand Down
11 changes: 11 additions & 0 deletions src/esp/physics/ArticulatedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,15 @@ class ArticulatedObject : public esp::physics::PhysicsObjectBase {
return objectIdToLinkId_;
}

/**
* @brief Get a map of link ids to object ids.
*
* @return A a map of link ids to Habitat object ids for this AO's links.
*/
std::unordered_map<int, int> getLinkIdsToObjectIds() const {
return linkIdToObjectId_;
}

/**
* @brief Given the list of passed points in this object's local space, return
* those points transformed to world space.
Expand Down Expand Up @@ -934,6 +943,8 @@ class ArticulatedObject : public esp::physics::PhysicsObjectBase {

//! map PhysicsManager objectId to local multibody linkId
std::unordered_map<int, int> objectIdToLinkId_;
//! map local multibody linkId to PhysicsManager objectId
std::unordered_map<int, int> linkIdToObjectId_;

/**
* @brief Returns the @ref
Expand Down
1 change: 1 addition & 0 deletions src/esp/physics/bullet/BulletPhysicsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ int BulletPhysicsManager::addArticulatedObjectInternal(
++linkIx) {
int linkObjectId = allocateObjectID();
articulatedObject->objectIdToLinkId_[linkObjectId] = linkIx;
articulatedObject->linkIdToObjectId_[linkIx] = linkObjectId;
collisionObjToObjIds_->emplace(
articulatedObject->btMultiBody_->getLinkCollider(linkIx), linkObjectId);
ArticulatedLink& linkObject = articulatedObject->getLink(linkIx);
Expand Down
7 changes: 7 additions & 0 deletions src/esp/physics/objectWrappers/ManagedArticulatedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ class ManagedArticulatedObject
return {};
}

std::unordered_map<int, int> getLinkIdsToObjectIds() const {
if (auto sp = getObjectReference()) {
return sp->getLinkIdsToObjectIds();
}
return {};
}

void setRootLinearVelocity(const Mn::Vector3& linVel) {
if (auto sp = getObjectReference()) {
sp->setRootLinearVelocity(linVel);
Expand Down
13 changes: 13 additions & 0 deletions tests/test_physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,19 @@ def test_articulated_object_add_remove():
assert robot.is_alive
assert robot.object_id == habitat_sim.stage_id + 1 # first robot added

# test object id to link mapping
link_ids = robot.get_link_ids()
obj_ids_to_link_ids = robot.link_object_ids
link_ids_to_object_ids = robot.link_ids_to_object_ids
assert len(obj_ids_to_link_ids) == len(link_ids_to_object_ids)
assert len(link_ids_to_object_ids) == robot.num_links
for link_id, obj_id in link_ids_to_object_ids.items():
assert obj_ids_to_link_ids[obj_id] == link_id
for obj_id, link_id in obj_ids_to_link_ids.items():
assert link_ids_to_object_ids[link_id] == obj_id
for link_id in link_ids:
assert link_id in link_ids_to_object_ids

# add a second robot
robot2 = art_obj_mgr.add_articulated_object_from_urdf(
filepath=robot_file, global_scale=2.0
Expand Down

0 comments on commit 5153e89

Please sign in to comment.