Skip to content

Commit

Permalink
Refactor visualizer (#2112)
Browse files Browse the repository at this point in the history
Improve Visualizer performance, fix compilation issues with the latest VTK
  • Loading branch information
denix56 authored and taketwo committed Dec 18, 2017
1 parent ae4f57f commit 415315e
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 129 deletions.
56 changes: 30 additions & 26 deletions visualization/include/pcl/visualization/impl/pcl_visualizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,11 @@ pcl::visualization::PCLVisualizer::convertPointCloudToVTKPolyData (
float *data = (static_cast<vtkFloatArray*> (points->GetData ()))->GetPointer (0);

// Set the points
vtkIdType ptr = 0;
if (cloud->is_dense)
{
for (vtkIdType i = 0; i < nr_points; ++i)
memcpy (&data[i * 3], &cloud->points[i].x, 12); // sizeof (float) * 3
for (vtkIdType i = 0; i < nr_points; ++i, ptr += 3)
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[ptr]);
}
else
{
Expand All @@ -261,7 +262,7 @@ pcl::visualization::PCLVisualizer::convertPointCloudToVTKPolyData (
!pcl_isfinite (cloud->points[i].z))
continue;

memcpy (&data[j * 3], &cloud->points[i].x, 12); // sizeof (float) * 3
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[ptr]);
j++;
}
nr_points = j;
Expand Down Expand Up @@ -600,7 +601,9 @@ pcl::visualization::PCLVisualizer::addSphere (const PointT &center, double radiu
actor->GetProperty ()->SetRepresentationToSurface ();
actor->GetProperty ()->SetInterpolationToFlat ();
actor->GetProperty ()->SetColor (r, g, b);
#if VTK_RENDERING_BACKEND_OPENGL_VERSION < 2
actor->GetMapper ()->ImmediateModeRenderingOn ();
#endif
actor->GetMapper ()->StaticOn ();
actor->GetMapper ()->ScalarVisibilityOff ();
actor->GetMapper ()->Update ();
Expand Down Expand Up @@ -1531,7 +1534,9 @@ pcl::visualization::PCLVisualizer::updatePointCloud (const typename pcl::PointCl
double minmax[2];
minmax[0] = std::numeric_limits<double>::min ();
minmax[1] = std::numeric_limits<double>::max ();
#if VTK_RENDERING_BACKEND_OPENGL_VERSION < 2
am_it->second.actor->GetMapper ()->ImmediateModeRenderingOff ();
#endif
am_it->second.actor->GetMapper ()->SetScalarRange (minmax);

// Update the mapper
Expand Down Expand Up @@ -1567,7 +1572,9 @@ pcl::visualization::PCLVisualizer::updatePointCloud (const typename pcl::PointCl
double minmax[2];
minmax[0] = std::numeric_limits<double>::min ();
minmax[1] = std::numeric_limits<double>::max ();
#if VTK_RENDERING_BACKEND_OPENGL_VERSION < 2
am_it->second.actor->GetMapper ()->ImmediateModeRenderingOff ();
#endif
am_it->second.actor->GetMapper ()->SetScalarRange (minmax);

// Update the mapper
Expand Down Expand Up @@ -1605,12 +1612,12 @@ pcl::visualization::PCLVisualizer::updatePointCloud (const typename pcl::PointCl
// Get a pointer to the beginning of the data array
float *data = (static_cast<vtkFloatArray*> (points->GetData ()))->GetPointer (0);

int pts = 0;
vtkIdType pts = 0;
// If the dataset is dense (no NaNs)
if (cloud->is_dense)
{
for (vtkIdType i = 0; i < nr_points; ++i, pts += 3)
memcpy (&data[pts], &cloud->points[i].x, 12); // sizeof (float) * 3
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[pts]);
}
else
{
Expand All @@ -1620,8 +1627,7 @@ pcl::visualization::PCLVisualizer::updatePointCloud (const typename pcl::PointCl
// Check if the point is invalid
if (!isFinite (cloud->points[i]))
continue;

memcpy (&data[pts], &cloud->points[i].x, 12); // sizeof (float) * 3
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[pts]);
pts += 3;
j++;
}
Expand All @@ -1642,8 +1648,9 @@ pcl::visualization::PCLVisualizer::updatePointCloud (const typename pcl::PointCl
scalars->GetRange (minmax);
// Update the data
polydata->GetPointData ()->SetScalars (scalars);

#if VTK_RENDERING_BACKEND_OPENGL_VERSION < 2
am_it->second.actor->GetMapper ()->ImmediateModeRenderingOff ();
#endif
am_it->second.actor->GetMapper ()->SetScalarRange (minmax);

// Update the mapper
Expand Down Expand Up @@ -1683,17 +1690,16 @@ pcl::visualization::PCLVisualizer::addPolygonMesh (
colors = vtkSmartPointer<vtkUnsignedCharArray>::New ();
colors->SetNumberOfComponents (3);
colors->SetName ("Colors");

pcl::RGB rgb_data;
uint32_t offset = fields[rgb_idx].offset;
for (size_t i = 0; i < cloud->size (); ++i)
{
if (!isFinite (cloud->points[i]))
continue;
memcpy (&rgb_data, reinterpret_cast<const char*> (&cloud->points[i]) + fields[rgb_idx].offset, sizeof (pcl::RGB));
const pcl::RGB* const rgb_data = reinterpret_cast<const pcl::RGB*>(reinterpret_cast<const char*> (&cloud->points[i]) + offset);
unsigned char color[3];
color[0] = rgb_data.r;
color[1] = rgb_data.g;
color[2] = rgb_data.b;
color[0] = rgb_data->r;
color[1] = rgb_data->g;
color[2] = rgb_data->b;
colors->InsertNextTupleValue (color);
}
}
Expand All @@ -1707,13 +1713,13 @@ pcl::visualization::PCLVisualizer::addPolygonMesh (
// Get a pointer to the beginning of the data array
float *data = static_cast<vtkFloatArray*> (points->GetData ())->GetPointer (0);

int ptr = 0;
vtkIdType ptr = 0;
std::vector<int> lookup;
// If the dataset is dense (no NaNs)
if (cloud->is_dense)
{
for (vtkIdType i = 0; i < nr_points; ++i, ptr += 3)
memcpy (&data[ptr], &cloud->points[i].x, sizeof (float) * 3);
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[ptr]);
}
else
{
Expand All @@ -1726,7 +1732,7 @@ pcl::visualization::PCLVisualizer::addPolygonMesh (
continue;

lookup[i] = static_cast<int> (j);
memcpy (&data[ptr], &cloud->points[i].x, sizeof (float) * 3);
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[ptr]);
j++;
ptr += 3;
}
Expand Down Expand Up @@ -1866,7 +1872,7 @@ pcl::visualization::PCLVisualizer::updatePolygonMesh (
if (cloud->is_dense)
{
for (vtkIdType i = 0; i < nr_points; ++i, ptr += 3)
memcpy (&data[ptr], &cloud->points[i].x, sizeof (float) * 3);
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[ptr]);
}
else
{
Expand All @@ -1879,7 +1885,7 @@ pcl::visualization::PCLVisualizer::updatePolygonMesh (
continue;

lookup [i] = static_cast<int> (j);
memcpy (&data[ptr], &cloud->points[i].x, sizeof (float) * 3);
std::copy (&cloud->points[i].x, &cloud->points[i].x + 3, &data[ptr]);
j++;
ptr += 3;
}
Expand All @@ -1898,19 +1904,17 @@ pcl::visualization::PCLVisualizer::updatePolygonMesh (
rgb_idx = pcl::getFieldIndex (*cloud, "rgba", fields);
if (rgb_idx != -1 && colors)
{
pcl::RGB rgb_data;
int j = 0;
uint32_t offset = fields[rgb_idx].offset;
for (size_t i = 0; i < cloud->size (); ++i)
{
if (!isFinite (cloud->points[i]))
continue;
memcpy (&rgb_data,
reinterpret_cast<const char*> (&cloud->points[i]) + fields[rgb_idx].offset,
sizeof (pcl::RGB));
const pcl::RGB* const rgb_data = reinterpret_cast<const pcl::RGB*>(reinterpret_cast<const char*> (&cloud->points[i]) + offset);
unsigned char color[3];
color[0] = rgb_data.r;
color[1] = rgb_data.g;
color[2] = rgb_data.b;
color[0] = rgb_data->r;
color[1] = rgb_data->g;
color[2] = rgb_data->b;
colors->SetTupleValue (j++, color);
}
}
Expand Down
3 changes: 2 additions & 1 deletion visualization/include/pcl/visualization/pcl_visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ namespace pcl
*/
PCLVisualizer (const std::string &name = "", const bool create_interactor = true);

/** \brief PCL Visualizer constructor.
/** \brief PCL Visualizer constructor. It looks through the passed argv arguments to find the "-cam *.cam" argument.
* If the search failed, the name for cam file is calculated with boost uuid. If there is no such file, camera is not initilalized.
* \param[in] argc
* \param[in] argv
* \param[in] name the window name (empty by default)
Expand Down
Loading

0 comments on commit 415315e

Please sign in to comment.