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

Add remove text3d #2088

Merged
merged 3 commits into from
Nov 26, 2017
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
71 changes: 56 additions & 15 deletions visualization/include/pcl/visualization/impl/pcl_visualizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand how this loop works. Could you please elaborate why initializing i with this value? Also, why breaking on line 690?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If viewport is 0, you're adding it to all viewport, and since they're numbered incrementally their id numbers will be {1, ..., #existing_viewports}. If viewport is bigger than 0, then you know the user is specifying the specific viewport id to add the text.

So this means that the ids you will be addressing are either from [1, #viewports] or simply the one specified in viewport, as long as its bigger then 0.

Regarding the break, you only cycle all possible ids when viewport = 0. If the user specified a single id, only that id is to be checked, that's why you break after a single iteration.

{
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 @@ -684,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
int i = 1;
while ((renderer = rens_->GetNextItem ()) != NULL)
{
// Should we add the actor to all renderers or just to i-nth renderer?
Expand All @@ -703,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 @@ -734,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 @@ -754,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
int 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
46 changes: 33 additions & 13 deletions visualization/src/pcl_visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,23 +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
ShapeActorMap::iterator am_it = shape_actor_map_->find (id);
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
2 changes: 2 additions & 0 deletions visualization/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
#add_executable(demo_shapes test_shapes.cpp)
#target_link_libraries(demo_shapes pcl_common pcl_io pcl_kdtree pcl_visualization)

#add_executable(demo_shapes_multiport test_shapes_multiport.cpp)
#target_link_libraries(demo_shapes_multiport pcl_common pcl_io pcl_kdtree pcl_visualization)
56 changes: 56 additions & 0 deletions visualization/test/test_shapes_multiport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h>

using pcl::PointCloud;
using pcl::PointXYZ;

int
main (int , char **)
{
srand (unsigned (time (0)));

PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ>);

cloud->points.resize (5);
for (size_t i = 0; i < cloud->points.size (); ++i)
{
cloud->points[i].x = float (i);
cloud->points[i].y = float (i / 2);
cloud->points[i].z = 0.0f;
}

// Start the visualizer
pcl::visualization::PCLVisualizer p ("test_shapes");
int leftPort(0);
int rightPort(0);
p.createViewPort(0, 0, 0.5, 1, leftPort);
p.createViewPort(0.5, 0, 1, 1, rightPort);
p.setBackgroundColor (1, 1, 1);
p.addCoordinateSystem (1.0, "first");

//p.addPolygon (cloud, "polygon");
p.addPolygon<PointXYZ> (cloud, 1.0, 0.0, 0.0, "polygon", leftPort);
p.setShapeRenderingProperties (pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 10, "polygon", leftPort);

p.addLine<PointXYZ, PointXYZ> (cloud->points[0], cloud->points[1], 0.0, 1.0, 0.0, "line", leftPort);
p.setShapeRenderingProperties (pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 50, "line", leftPort);

p.addSphere<PointXYZ> (cloud->points[0], 1, 0.0, 1.0, 0.0, "sphere", leftPort);
p.setShapeRenderingProperties (pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 5, "sphere", leftPort);
// p.removePolygon ("poly");

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.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.spin ();
p.removeText3D ("text3D_to_remove", rightPort);
p.spin ();
}