40 #ifndef PCL_IO_VTK_IO_IMPL_H_
41 #define PCL_IO_VTK_IO_IMPL_H_
50 #pragma GCC system_header
52 #include <vtkFloatArray.h>
53 #include <vtkPointData.h>
54 #include <vtkPoints.h>
55 #include <vtkPolyData.h>
56 #include <vtkUnsignedCharArray.h>
57 #include <vtkSmartPointer.h>
58 #include <vtkStructuredGrid.h>
59 #include <vtkVertexGlyphFilter.h>
62 template <
typename Po
intT>
void
71 cloud.
width = polydata->GetNumberOfPoints ();
76 typename CloudT::PointType test_point = cloud.
points[0];
78 bool has_x =
false;
bool has_y =
false;
bool has_z =
false;
79 float x_val = 0.0f;
float y_val = 0.0f;
float z_val = 0.0f;
85 if (has_x && has_y && has_z)
87 for (
size_t i = 0; i < cloud.
points.size (); ++i)
90 polydata->GetPoint (i, coordinate);
91 typename CloudT::PointType p = cloud.
points[i];
100 bool has_normal_x =
false;
bool has_normal_y =
false;
bool has_normal_z =
false;
101 float normal_x_val = 0.0f;
float normal_y_val = 0.0f;
float normal_z_val = 0.0f;
103 "normal_x", has_normal_x, normal_x_val));
105 "y_normal", has_normal_y, normal_y_val));
107 "z_normal", has_normal_z, normal_z_val));
109 vtkFloatArray* normals = vtkFloatArray::SafeDownCast (polydata->GetPointData ()->GetNormals ());
110 if (has_normal_x && has_normal_y && has_normal_z && normals)
112 for (
size_t i = 0; i < cloud.
points.size (); ++i)
115 normals->GetTupleValue (i, normal);
116 typename CloudT::PointType p = cloud.
points[i];
125 bool has_r =
false;
bool has_g =
false;
bool has_b =
false;
126 unsigned char r_val = 0.0f;
unsigned char g_val = 0.0f;
unsigned char b_val = 0.0f;
134 vtkUnsignedCharArray* colors = vtkUnsignedCharArray::SafeDownCast (polydata->GetPointData ()->GetScalars ());
135 if (has_r && has_g && has_b && colors)
137 for (
size_t i = 0; i < cloud.
points.size (); ++i)
139 unsigned char color[3];
140 colors->GetTupleValue (i, color);
141 typename CloudT::PointType p = cloud.
points[i];
151 template <
typename Po
intT>
void
157 structured_grid->GetDimensions (dimensions);
158 cloud.
width = dimensions[0];
159 cloud.
height = dimensions[1];
163 typename CloudT::PointType test_point = cloud.
points[0];
167 bool has_x =
false;
bool has_y =
false;
bool has_z =
false;
168 float x_val = 0.0f;
float y_val = 0.0f;
float z_val = 0.0f;
173 if (has_x && has_y && has_z)
175 for (
size_t i = 0; i < cloud.
width; ++i)
177 for (
size_t j = 0; j < cloud.
height; ++j)
179 int queryPoint[3] = {i, j, 0};
180 vtkIdType pointId = vtkStructuredData::ComputePointId (dimensions, queryPoint);
181 double coordinate[3];
182 if (structured_grid->IsPointVisible (pointId))
184 structured_grid->GetPoint (pointId, coordinate);
185 typename CloudT::PointType p = cloud (i, j);
200 bool has_normal_x =
false;
bool has_normal_y =
false;
bool has_normal_z =
false;
201 float normal_x_val = 0.0f;
float normal_y_val = 0.0f;
float normal_z_val = 0.0f;
203 "x_normal", has_normal_x, normal_x_val));
205 "y_normal", has_normal_y, normal_y_val));
207 "z_normal", has_normal_z, normal_z_val));
209 vtkFloatArray* normals = vtkFloatArray::SafeDownCast (structured_grid->GetPointData ()->GetNormals ());
211 if (has_x && has_y && has_z)
213 for (
size_t i = 0; i < cloud.
width; ++i)
215 for (
size_t j = 0; j < cloud.
height; ++j)
217 int queryPoint[3] = {i, j, 0};
218 vtkIdType pointId = vtkStructuredData::ComputePointId (dimensions, queryPoint);
220 if(structured_grid->IsPointVisible (pointId))
222 normals->GetTupleValue (i, normal);
223 typename CloudT::PointType p = cloud (i, j);
238 bool has_r =
false;
bool has_g =
false;
bool has_b =
false;
239 unsigned char r_val = 0.0f;
unsigned char g_val = 0.0f;
unsigned char b_val = 0.0f;
246 vtkUnsignedCharArray* colors = vtkUnsignedCharArray::SafeDownCast(structured_grid->GetPointData()->GetArray(
"Colors"));
248 if (has_r && has_g && has_b && colors)
250 for (
size_t i = 0; i < cloud.
width; ++i)
252 for (
size_t j = 0; j < cloud.
height; ++j)
254 int queryPoint[3] = {i, j, 0};
255 vtkIdType pointId = vtkStructuredData::ComputePointId(dimensions, queryPoint);
256 unsigned char color[3];
257 if (structured_grid->IsPointVisible (pointId))
259 colors->GetTupleValue (i, color);
260 typename CloudT::PointType p = cloud (i, j);
276 template <
typename Po
intT>
void
281 typename CloudT::PointType test_point = cloud.
points[0];
286 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New ();
287 for (
size_t i = 0; i < cloud.
points.size (); ++i)
293 points->InsertNextPoint (p);
297 vtkSmartPointer<vtkPolyData> temp_polydata = vtkSmartPointer<vtkPolyData>::New ();
298 temp_polydata->SetPoints (points);
301 bool has_normal_x =
false;
bool has_normal_y =
false;
bool has_normal_z =
false;
302 float normal_x_val = 0.0f;
float normal_y_val = 0.0f;
float normal_z_val = 0.0f;
306 if (has_normal_x && has_normal_y && has_normal_z)
308 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New ();
309 normals->SetNumberOfComponents (3);
310 normals->SetNumberOfTuples (cloud.
points.size ());
311 normals->SetName (
"Normals");
313 for (
size_t i = 0; i < cloud.
points.size (); ++i)
315 typename CloudT::PointType p = cloud.
points[i];
319 float normal[3] = {normal_x_val, normal_y_val, normal_z_val};
320 normals->SetTupleValue (i, normal);
322 temp_polydata->GetPointData ()->SetNormals (normals);
326 bool has_r =
false;
bool has_g =
false;
bool has_b =
false;
327 unsigned char r_val = 0;
unsigned char g_val = 0;
unsigned char b_val = 0;
331 if (has_r && has_g && has_b)
333 vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New ();
334 colors->SetNumberOfComponents (3);
335 colors->SetNumberOfTuples (cloud.
points.size ());
336 colors->SetName (
"RGB");
338 for (
size_t i = 0; i < cloud.
points.size (); ++i)
340 typename CloudT::PointType p = cloud[i];
344 unsigned char color[3] = {r_val, g_val, b_val};
345 colors->SetTupleValue (i, color);
347 temp_polydata->GetPointData ()->SetScalars (colors);
351 vtkSmartPointer<vtkVertexGlyphFilter> vertex_glyph_filter = vtkSmartPointer<vtkVertexGlyphFilter>::New ();
352 #if VTK_MAJOR_VERSION <= 5
353 vertex_glyph_filter->AddInputConnection (temp_polydata->GetProducerPort ());
355 vertex_glyph_filter->SetInputData (temp_polydata);
357 vertex_glyph_filter->Update ();
359 pdata->DeepCopy (vertex_glyph_filter->GetOutput ());
363 template <
typename Po
intT>
void
368 typename CloudT::PointType test_point = cloud.
points[0];
372 int dimensions[3] = {cloud.
width, cloud.
height, 1};
373 structured_grid->SetDimensions (dimensions);
375 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New ();
376 points->SetNumberOfPoints (cloud.
width * cloud.
height);
378 unsigned int numberOfInvalidPoints = 0;
380 for (
size_t i = 0; i < cloud.
width; ++i)
382 for (
size_t j = 0; j < cloud.
height; ++j)
384 int queryPoint[3] = {i, j, 0};
385 vtkIdType pointId = vtkStructuredData::ComputePointId (dimensions, queryPoint);
386 typename CloudT::PointType point = cloud (i, j);
390 float p[3] = {point.x, point.y, point.z};
391 points->SetPoint (pointId, p);
399 structured_grid->SetPoints (points);
402 bool has_normal_x =
false;
bool has_normal_y =
false;
bool has_normal_z =
false;
403 float normal_x_val = 0.0f;
float normal_y_val = 0.0f;
float normal_z_val = 0.0f;
407 if (has_normal_x && has_normal_y && has_normal_z)
409 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New ();
410 normals->SetNumberOfComponents (3);
411 normals->SetNumberOfTuples (cloud.
width * cloud.
height);
412 normals->SetName (
"Normals");
413 for (
size_t i = 0; i < cloud.
width; ++i)
415 for (
size_t j = 0; j < cloud.
height; ++j)
417 int queryPoint[3] = {i, j, 0};
418 vtkIdType pointId = vtkStructuredData::ComputePointId (dimensions, queryPoint);
419 typename CloudT::PointType point = cloud (i, j);
423 typename CloudT::PointType p = cloud.
points[i];
427 float normal[3] = {normal_x_val, normal_y_val, normal_z_val};
428 normals->SetTupleValue (pointId, normal);
436 structured_grid->GetPointData ()->SetNormals (normals);
440 bool has_r =
false;
bool has_g =
false;
bool has_b =
false;
441 unsigned char r_val = 0;
unsigned char g_val = 0;
unsigned char b_val = 0;
445 if (has_r && has_g && has_b)
447 vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
448 colors->SetNumberOfComponents (3);
449 colors->SetNumberOfTuples (cloud.
width * cloud.
height);
450 colors->SetName (
"Colors");
451 for (
size_t i = 0; i < cloud.
width; ++i)
453 for (
size_t j = 0; j < cloud.
height; ++j)
455 int queryPoint[3] = {i, j, 0};
456 vtkIdType pointId = vtkStructuredData::ComputePointId (dimensions, queryPoint);
457 typename CloudT::PointType point = cloud (i, j);
461 typename CloudT::PointType p = cloud[i];
465 unsigned char color[3] = {r_val, g_val, b_val};
466 colors->SetTupleValue (i, color);
473 structured_grid->GetPointData ()->AddArray (colors);
477 #endif //#ifndef PCL_IO_VTK_IO_H_