Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with removing skeletons without shapes #1625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions dart/collision/CollisionGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ void CollisionGroup::removeShapeFramesOf()
// Do nothing
}

//==============================================================================
void CollisionGroup::unsubscribeFrom()
{
// Do nothing
}

//==============================================================================
bool CollisionGroup::isSubscribedTo()
{
return true;
}

//==============================================================================
void CollisionGroup::removeAllShapeFrames()
{
Expand Down
18 changes: 18 additions & 0 deletions dart/collision/CollisionGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ class CollisionGroup
void unsubscribeFrom(
const dynamics::Skeleton* skeleton, const Others*... others);

/// Do nothing. This function is for terminating the recursive variadic
/// template.
void unsubscribeFrom();

/// Check if this is subscribed to bodyNode and the other sources
template <typename... Others>
bool isSubscribedTo(
const dynamics::BodyNode* bodyNode, const Others*... others);

/// Check if this is subscribed to skeleton and the other sources
template <typename... Others>
bool isSubscribedTo(
const dynamics::Skeleton* skeleton, const Others*... others);

/// Return true. This function is for terminating the recursive variadic
/// template
bool isSubscribedTo();

/// Return true if this CollisionGroup contains shapeFrame
bool hasShapeFrame(const dynamics::ShapeFrame* shapeFrame) const;

Expand Down
18 changes: 18 additions & 0 deletions dart/collision/detail/CollisionGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,24 @@ void CollisionGroup::unsubscribeFrom(
unsubscribeFrom(others...);
}

//==============================================================================
template <typename... Others>
bool CollisionGroup::isSubscribedTo(
const dynamics::BodyNode* bodyNode, const Others*... others)
{
auto it = mBodyNodeSources.find(bodyNode);
return (it != mBodyNodeSources.end()) && isSubscribedTo(others...);
}

//==============================================================================
template <typename... Others>
bool CollisionGroup::isSubscribedTo(
const dynamics::Skeleton* skeleton, const Others*... others)
{
auto it = mSkeletonSources.find(skeleton);
return (it != mSkeletonSources.end()) && isSubscribedTo(others...);
}

} // namespace collision
} // namespace dart

Expand Down
2 changes: 1 addition & 1 deletion dart/constraint/ConstraintSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void ConstraintSolver::removeSkeleton(const SkeletonPtr& skeleton)
<< "', which doesn't exist in the ConstraintSolver.\n";
}

mCollisionGroup->removeShapeFramesOf(skeleton.get());
mCollisionGroup->unsubscribeFrom(skeleton.get());
mSkeletons.erase(
remove(mSkeletons.begin(), mSkeletons.end(), skeleton), mSkeletons.end());
mConstrainedGroups.reserve(mSkeletons.size());
Expand Down
66 changes: 66 additions & 0 deletions unittests/unit/test_CollisionGroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,72 @@ TEST_P(CollisionGroupsTest, BodyNodeSubscription)
EXPECT_FALSE(group->collide());
}

TEST_P(CollisionGroupsTest, RemovedSkeletonSubscription)
{
if (!dart::collision::CollisionDetector::getFactory()->canCreate(GetParam()))
{
std::cout << "Skipping test for [" << GetParam() << "], because it is not "
<< "available" << std::endl;
return;
}
else
{
std::cout << "Running CollisionGroups test for [" << GetParam() << "]"
<< std::endl;
}
// Note: When skeletons are added to a world, the constraint solver will
// subscribe to them.
dart::simulation::WorldPtr world = dart::simulation::World::create();
auto cd
= dart::collision::CollisionDetector::getFactory()->create(GetParam());

world->getConstraintSolver()->setCollisionDetector(cd);

dart::dynamics::SkeletonPtr skel_A = dart::dynamics::Skeleton::create("A");
dart::dynamics::SkeletonPtr skel_B = dart::dynamics::Skeleton::create("B");

auto group = world->getConstraintSolver()->getCollisionGroup();

// Check that there are no subscribtions before adding the skeletons to the
// world
EXPECT_FALSE(group->isSubscribedTo(skel_A.get()));
EXPECT_FALSE(group->isSubscribedTo(skel_B.get()));

world->addSkeleton(skel_A);
world->addSkeleton(skel_B);

// Check that there are subscribtions after adding the skeletons to the
// world
EXPECT_TRUE(group->isSubscribedTo(skel_A.get()));
EXPECT_TRUE(group->isSubscribedTo(skel_B.get()));

// Add a shape to one of the skeletons to test that removal works for
// skeletons with and without shapes
auto boxShape = std::make_shared<dart::dynamics::BoxShape>(
Eigen::Vector3d::Constant(1.0));

auto pair = skel_B->createJointAndBodyNodePair<dart::dynamics::FreeJoint>();
auto sn = pair.second->createShapeNodeWith<dart::dynamics::CollisionAspect>(
boxShape);

// Needed to update subscribtions
world->step();

EXPECT_TRUE(group->hasShapeFrame(sn));
const auto* skel_A_ptr = skel_A.get();
const auto* skel_B_ptr = skel_B.get();
// Check that there are no subscribtions after removing the skeletons from the
// world
world->removeSkeleton(skel_A);
world->removeSkeleton(skel_B);

world->step();

EXPECT_FALSE(group->hasShapeFrame(sn));
EXPECT_FALSE(group->isSubscribedTo(skel_A_ptr));
EXPECT_FALSE(group->isSubscribedTo(skel_B_ptr));
}

INSTANTIATE_TEST_CASE_P(
CollisionEngine,
CollisionGroupsTest,
Expand Down