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

Bugfix in InteractiveMarkerViewer #560

Merged
merged 4 commits into from
Jan 13, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
* Fixed bug of not joining Viewer threads when stopping auto-update: [#463](https://github.com/personalrobotics/aikido/pull/463)
* Fixed bug of not passing full file path to RViz when MeshShape is used: [#518](https://github.com/personalrobotics/aikido/pull/518)
* Merged WorldInteractiveMarkerViewer into InteractiveMarkerViewer, removing the former: [#520](https://github.com/personalrobotics/aikido/pull/520)
* Fixed bug of not removing SkeletonMarkers of skeletons removed from the associated World: [#560](https://github.com/personalrobotics/aikido/pull/560)

* IO

Expand Down
116 changes: 63 additions & 53 deletions src/rviz/InteractiveMarkerViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,49 @@ SkeletonMarkerPtr InteractiveMarkerViewer::addSkeletonMarker(
return marker;
}

//==============================================================================
void InteractiveMarkerViewer::updateSkeletonMarkers()
{
// Update SkeletonMarkers from the world.
if (mWorld)
{
for (std::size_t i = 0; i < mWorld->getNumSkeletons(); ++i)
{
const dart::dynamics::SkeletonPtr skeleton = mWorld->getSkeleton(i);
if (skeleton)
{
// Adds skeleton if previously not in the world.
mSkeletonMarkers.emplace(
skeleton,
std::make_shared<SkeletonMarker>(
nullptr, &mMarkerServer, skeleton, mFrameId));
}
}
}

// Update existing skeletons, and delete erased skeletons.
auto it = std::begin(mSkeletonMarkers);
while (it != std::end(mSkeletonMarkers))
{
// If the skeleton is either a nullptr or no longer exists in an associated world, delete.
if (!it->first || mWorld && !mWorld->hasSkeleton(it->first))
{
it = mSkeletonMarkers.erase(it);
}
else
{
// In any other case, since the skeleton exists, update it, increment iterator.
std::unique_lock<std::mutex> skeleton_lock(
brianhou marked this conversation as resolved.
Show resolved Hide resolved
it->first->getMutex(), std::try_to_lock);
if (skeleton_lock.owns_lock())
{
it->second->update();
}
++it;
}
}
}

//==============================================================================
FrameMarkerPtr InteractiveMarkerViewer::addFrameMarker(
dart::dynamics::Frame* frame, double length, double thickness, double alpha)
Expand All @@ -64,6 +107,13 @@ FrameMarkerPtr InteractiveMarkerViewer::addFrameMarker(
return marker;
}

//==============================================================================
void InteractiveMarkerViewer::updateFrameMarkers()
{
for (const auto& marker : mFrameMarkers)
marker->update();
}

//==============================================================================
TSRMarkerPtr InteractiveMarkerViewer::addTSRMarker(
const constraint::dart::TSR& tsr, int nSamples, const std::string& basename)
Expand Down Expand Up @@ -134,6 +184,13 @@ TrajectoryMarkerPtr InteractiveMarkerViewer::addTrajectoryMarker(
return marker;
}

//==============================================================================
void InteractiveMarkerViewer::updateTrajectoryMarkers()
{
for (const auto& marker : mTrajectoryMarkers)
marker->update();
}

//==============================================================================
void InteractiveMarkerViewer::setAutoUpdate(bool flag)
{
Expand Down Expand Up @@ -165,61 +222,14 @@ void InteractiveMarkerViewer::update()
{
std::lock_guard<std::mutex> lock(mMutex);

if (mWorld)
{
// Update SkeletonMarkers from the world.
for (std::size_t i = 0; i < mWorld->getNumSkeletons(); ++i)
{
const dart::dynamics::SkeletonPtr skeleton = mWorld->getSkeleton(i);
// Update skeleton markers.
updateSkeletonMarkers();

if (skeleton)
{
// Either a new SkeletonMarker or a previously-inserted SkeletonMarker
auto result = mSkeletonMarkers.emplace(
skeleton,
std::make_shared<SkeletonMarker>(
nullptr, &mMarkerServer, skeleton, mFrameId));

std::unique_lock<std::mutex> skeleton_lock(
skeleton->getMutex(), std::try_to_lock);
if (skeleton_lock.owns_lock())
result.first->second->update();
}
}
}
// Update frame markers.
updateFrameMarkers();

// Clear removed skeletons
auto it = std::begin(mSkeletonMarkers);
while (it != std::end(mSkeletonMarkers))
{
// Skeleton still exists in the World, do nothing.
if (mWorld && mWorld->hasSkeleton(it->first))
{
++it;
}
// Skeleton not in the world, but a marker, update with lock on skeleton.
else if (it->first)
{
std::unique_lock<std::mutex> skeleton_lock(
it->first->getMutex(), std::try_to_lock);
if (skeleton_lock.owns_lock())
{
it->second->update();
}
++it;
}
// Skeleton does not exist anymore, remove from markers.
else
{
it = mSkeletonMarkers.erase(it);
}
}

for (const FrameMarkerPtr& marker : mFrameMarkers)
marker->update();

for (const auto& marker : mTrajectoryMarkers)
marker->update();
// Update trajectory markers.
updateTrajectoryMarkers();

// Apply changes anytime a marker has been modified.
mMarkerServer.applyChanges();
Expand Down