Skip to content

Commit

Permalink
Merge pull request #2492 from SergioRAgostinho/ply-mesh-io
Browse files Browse the repository at this point in the history
vtk2mesh: Add parsing support to the new RGBA scalar field added in vtk8
  • Loading branch information
taketwo authored Sep 29, 2018
2 parents 8bf8edb + 85e73ec commit 362183a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 17 additions & 11 deletions io/src/vtk_lib_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,31 +288,37 @@ pcl::io::vtk2mesh (const vtkSmartPointer<vtkPolyData>& poly_data, pcl::PolygonMe
// Then the color information, if any
vtkUnsignedCharArray* poly_colors = NULL;
if (poly_data->GetPointData() != NULL)
{
poly_colors = vtkUnsignedCharArray::SafeDownCast (poly_data->GetPointData ()->GetScalars ("Colors"));

// some applications do not save the name of scalars (including PCL's native vtk_io)
if (!poly_colors && poly_data->GetPointData () != NULL)
poly_colors = vtkUnsignedCharArray::SafeDownCast (poly_data->GetPointData ()->GetScalars ("scalars"));
// some applications do not save the name of scalars (including PCL's native vtk_io)
if (!poly_colors)
poly_colors = vtkUnsignedCharArray::SafeDownCast (poly_data->GetPointData ()->GetScalars ("scalars"));

if (!poly_colors)
poly_colors = vtkUnsignedCharArray::SafeDownCast (poly_data->GetPointData ()->GetScalars ("RGB"));

if (!poly_colors && poly_data->GetPointData () != NULL)
poly_colors = vtkUnsignedCharArray::SafeDownCast (poly_data->GetPointData ()->GetScalars ("RGB"));
if (!poly_colors)
poly_colors = vtkUnsignedCharArray::SafeDownCast (poly_data->GetPointData ()->GetScalars ("RGBA"));
}

// TODO: currently only handles rgb values with 3 components
if (poly_colors && (poly_colors->GetNumberOfComponents () == 3))
if (poly_colors && (poly_colors->GetNumberOfComponents () == 3 || poly_colors->GetNumberOfComponents () == 4))
{
pcl::PointCloud<pcl::RGB>::Ptr rgb_cloud (new pcl::PointCloud<pcl::RGB> ());
rgb_cloud->points.resize (nr_points);
rgb_cloud->width = static_cast<uint32_t> (rgb_cloud->points.size ());
rgb_cloud->height = 1;
rgb_cloud->is_dense = true;

unsigned char point_color[3];
unsigned char point_color[4] = {0, 0, 0, 255};
for (vtkIdType i = 0; i < mesh_points->GetNumberOfPoints (); i++)
{
poly_colors->GetTupleValue (i, &point_color[0]);
rgb_cloud->points[i].r = point_color[0];
rgb_cloud->points[i].g = point_color[1];
rgb_cloud->points[i].b = point_color[2];
// individual component copy due to different memory layout
(*rgb_cloud)[i].r = point_color[0];
(*rgb_cloud)[i].g = point_color[1];
(*rgb_cloud)[i].b = point_color[2];
(*rgb_cloud)[i].a = point_color[3];
}

pcl::PCLPointCloud2 rgb_cloud2;
Expand Down
8 changes: 8 additions & 0 deletions test/io/test_ply_mesh_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ TEST (PCL, PLYPolygonMeshIO)
EXPECT_EQ (mesh_binary_pcl.polygons[i].vertices[j], mesh.polygons[i].vertices[j]);
}
}

remove ("test_mesh_ascii.ply");
remove ("test_mesh_binary.ply");
}

TEST (PCL, PLYPolygonMeshColoredIO)
Expand Down Expand Up @@ -276,6 +279,11 @@ TEST (PCL, PLYPolygonMeshColoredIO)
EXPECT_EQ (mesh_rgba_binary_pcl.polygons[i].vertices[j], mesh.polygons[i].vertices[j]);
}
}

remove ("test_mesh_rgb_ascii.ply");
remove ("test_mesh_rgba_ascii.ply");
remove ("test_mesh_rgb_binary.ply");
remove ("test_mesh_rgba_binary.ply");
}

/* ---[ */
Expand Down

0 comments on commit 362183a

Please sign in to comment.