Skip to content

Commit

Permalink
Fix behavior for add/removeText3D with multi viewports
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRAgostinho committed Nov 15, 2017
1 parent 32c5cd4 commit 14ee053
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 33 deletions.
72 changes: 56 additions & 16 deletions visualization/include/pcl/visualization/impl/pcl_visualizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,11 +669,26 @@ pcl::visualization::PCLVisualizer::addText3D (
else
tid = id;

std::string id_to_check = viewport > 0 ? tid.append (viewport, '*') : tid;
if (contains (id_to_check))
if (viewport < 0)
return false;

// check all or an individual viewport for a similar id
rens_->InitTraversal ();
rens_->GetNextItem (); //discard first because it's not a renderer for the viewps
for (size_t i = std::max (viewport, 1); rens_->GetNextItem () != NULL; ++i)
{
PCL_WARN ("[addText3D] The id <%s> already exists in viewport [%d]! Please choose a different id and retry.\n", id.c_str (), viewport);
return (false);
const std::string uid = tid + std::string (i, '*');
if (contains (uid))
{
PCL_ERROR ( "[addText3D] The id <%s> already exists in viewport [%d]! "
"Please choose a different id and retry.\n",
tid.c_str (),
i);
return false;
}

if (viewport > 0)
break;
}

vtkSmartPointer<vtkVectorText> textSource = vtkSmartPointer<vtkVectorText>::New ();
Expand All @@ -685,8 +700,8 @@ pcl::visualization::PCLVisualizer::addText3D (

// Since each follower may follow a different camera, we need different followers
rens_->InitTraversal ();
vtkRenderer* renderer = NULL;
int i = 0;
vtkRenderer* renderer = rens_->GetNextItem (); //discard first because it's not a renderer for the viewps
size_t i = 1;
while ((renderer = rens_->GetNextItem ()) != NULL)
{
// Should we add the actor to all renderers or just to i-nth renderer?
Expand All @@ -704,10 +719,8 @@ pcl::visualization::PCLVisualizer::addText3D (

// Save the pointer/ID pair to the global actor map. If we are saving multiple vtkFollowers
// for multiple viewport
std::string alternate_tid = tid;
alternate_tid.append(i, '*');

(*shape_actor_map_)[(viewport == 0) ? tid : alternate_tid] = textActor;
const std::string uid = tid + std::string (i, '*');
(*shape_actor_map_)[uid] = textActor;
}

++i;
Expand Down Expand Up @@ -735,10 +748,26 @@ pcl::visualization::PCLVisualizer::addText3D (
else
tid = id;

if (contains (tid))
if (viewport < 0)
return false;

// check all or an individual viewport for a similar id
rens_->InitTraversal ();
rens_->GetNextItem (); //discard first because it's not a renderer for the viewps
for (size_t i = std::max (viewport, 1); rens_->GetNextItem () != NULL; ++i)
{
PCL_WARN ("[addText3D] The id <%s> already exists! Please choose a different id and retry.\n", id.c_str ());
return (false);
const std::string uid = tid + std::string (i, '*');
if (contains (uid))
{
PCL_ERROR ( "[addText3D] The id <%s> already exists in viewport [%d]! "
"Please choose a different id and retry.\n",
tid.c_str (),
i);
return false;
}

if (viewport > 0)
break;
}

vtkSmartPointer<vtkVectorText> textSource = vtkSmartPointer<vtkVectorText>::New ();
Expand All @@ -755,10 +784,21 @@ pcl::visualization::PCLVisualizer::addText3D (
textActor->GetProperty ()->SetColor (r, g, b);
textActor->SetOrientation (orientation);

addActorToRenderer (textActor, viewport);

// Save the pointer/ID pair to the global actor map. If we are saving multiple vtkFollowers
(*shape_actor_map_)[tid] = textActor;
rens_->InitTraversal ();
rens_->GetNextItem (); //discard first because it's not a renderer for the viewps
size_t i = 1;
for ( vtkRenderer* renderer = rens_->GetNextItem ();
renderer != NULL;
renderer = rens_->GetNextItem (), ++i)
{
if (viewport == 0 || viewport == i)
{
renderer->AddActor (textActor);
const std::string uid = tid + std::string (i, '*');
(*shape_actor_map_)[uid] = textActor;
}
}

return (true);
}
Expand Down
48 changes: 33 additions & 15 deletions visualization/src/pcl_visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,25 +915,43 @@ pcl::visualization::PCLVisualizer::removeShape (const std::string &id, int viewp
bool
pcl::visualization::PCLVisualizer::removeText3D (const std::string &id, int viewport)
{
// Check to see if the given ID entry exists
std::string id_to_check (id);
if (viewport > 0) id_to_check.append (viewport, '*');
ShapeActorMap::iterator am_it = shape_actor_map_->find (id_to_check);
if (viewport < 0)
return false;

if (am_it == shape_actor_map_->end ())
{
//pcl::console::print_warn (stderr, "[removeSape] Could not find any shape with id <%s>!\n", id.c_str ());
return (false);
}
bool success = true;

// Remove it from all renderers
if (removeActorFromRenderer (am_it->second, viewport))
// check all or an individual viewport for a similar id
rens_->InitTraversal ();
rens_->GetNextItem (); //discard first because it's not a renderer for the viewps
for (size_t i = std::max (viewport, 1); rens_->GetNextItem () != NULL; ++i)
{
// Remove the pointer/ID pair to the global actor map
shape_actor_map_->erase (am_it);
return (true);
const std::string uid = id + std::string (i, '*');
ShapeActorMap::iterator am_it = shape_actor_map_->find (uid);

// was it found
if (am_it == shape_actor_map_->end ())
{
if (viewport > 0)
return (false);

continue;
}

// Remove it from all renderers
if (removeActorFromRenderer (am_it->second, i))
{
// Remove the pointer/ID pair to the global actor map
shape_actor_map_->erase (am_it);
if (viewport > 0)
return (true);

success &= true;
}
else
success = false;
}
return (false);

return success;
}

/////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions visualization/test/test_shapes_multiport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ main (int , char **)

p.addText ("text", 200, 200, 1.0, 0, 0, "text", leftPort);

p.addText3D ("text3D", cloud->points[0], 1.0, 1.0, 0.0, 0.0, rightPort);
p.addText3D ("text3D", cloud->points[0], 1.0, 1.0, 0.0, 0.0, "", rightPort);
p.spin ();
p.removeCoordinateSystem ("first", 0);
p.spin ();
p.addCoordinateSystem (1.0, 5, 3, 1, "second");
p.spin ();
p.removeCoordinateSystem ("second", 0);
p.spin ();
p.addText3D ("text3D_to_remove", cloud->points[1], 1.0, 0.0, 1.0, 0.0, rightPort);
p.addText3D ("text3D_to_remove", cloud->points[1], 1.0, 0.0, 1.0, 0.0, "", rightPort);
p.spin ();
p.removeText3D ("text3D_to_remove", rightPort);
p.spin ();
Expand Down

0 comments on commit 14ee053

Please sign in to comment.