How to create a mesh data from vtkPoints?

Hi all.

I have a question about the how to create a mesh data.

I’m using the VTK 9.0 with the 3D Scanner.
I can represent the point cloud data with vtkGaussainMapper.

Previous inquiry:

Now, I would like to represent the Mesh data from the vtkPoints.
But I’m not sure which example code to reference…

Could you please advise for me how to create a mesh data from the vtkPoints?

Thank you :slight_smile:

Best regards,
William Kim

The best way would be to create a vtkPolyData, and use the vtkPolyData::SetPoints to give the list of points and vtkPolyData::SetVerts to give the vtkCellArray describing the cells around the points (so VTK can render the points).

An example can be found in vtkPolyPointSource.

Thank you for your reply.
I saw your example but I can’t understand how to work it…

Do you mean that the vtkCellArray can represent the mesh data?

In VTK, points are only 3D coordinates. They serve as a support for cells.
To have a valid point cloud, you need:

  • a vtkDataSet that support explicit points, here we will use a vtkPolyData
  • the vtkPoints containing the points, which you already have
  • the vtkCellArray containing the cells so you have a valid VTK data set

We can construct one like that:

   // vtk data set to fill
   vtkNew<vtkPolyData> myPolyData;

   // compute a vtkIdList that contains ids of each points
  vtkIdType numPoints = myVtkPoints->GetNumberOfPoints();
  vtkSmartPointer<vtkIdList> pointIds = vtkSmartPointer<vtkIdList>::New();
  pointIds->SetNumberOfIds(numPoints);
  for (vtkIdType i = 0; i < numPoints; ++i)
  {
    pointIds->SetId(i, i);
  }

   // create a vtkCellArray from this list
  vtkSmartPointer<vtkCellArray> polyPoint = vtkSmartPointer<vtkCellArray>::New();
  polyPoint->InsertNextCell(pointIds);

   // give the points and cells to the final poly data
  myPolyData->SetPoints(myVtkPoints);
  myPolyData->SetVerts(polyPoint);

In this code, I assume you have myVtkPoints that is the vtkPoints containing the positions of points in your point cloud

Thank you for your guide!

I added the above code to my code, but the renderwindow still shows it as points.

below is my code:

vtkSmartPointer<vtkIdList> pointIds = vtkSmartPointer<vtkIdList>::New();
pointIds->SetNumberOfIds(numPoints);
for (vtkIdType i = 0; i < numPoints; ++i)
{
	pointIds->SetId(i, i);
}

// create a vtkCellArray from this list
vtkSmartPointer<vtkCellArray> polyPoint = vtkSmartPointer<vtkCellArray>::New();
polyPoint->InsertNextCell(pointIds);

// give the points and cells to the final poly data
m_vtkPolydata->SetPoints(m_vtkPCDPoints);
m_vtkPolydata->SetVerts(polyPoint);

vtkNew<vtkVertexGlyphFilter> m_vtkGlyphFilter;
m_vtkGlyphFilter->SetInputData(m_vtkPolydata);
m_vtkGlyphFilter->Update();

vtkNew<vtkPolyDataMapper> m_vtkPolydataMapper;
m_vtkPolydataMapper->SetInputData(m_vtkPolydata);

vtkNew<vtkActor> m_vtkActor;
m_vtkActor->SetMapper(m_vtkPolydataMapper);

vtkNew<vtkRenderer> m_vtkRenderer;
m_vtkRenderer->AddActor(m_vtkActor);
m_vtkRenderer->SetBackground(.1, .2, .3);

vtkNew<vtkRenderWindow> m_vtkRenderWindow;
m_vtkRenderWindow->AddRenderer(m_vtkRenderer);
m_vtkRenderWindow->Render();

I referred to the below link, but I couldn’t make mesh data.
https://kitware.github.io/vtk-examples/site/Cxx/Filtering/TriangulateTerrainMap/

I’m sorry to bother you but could you please advise for me again?

test.ply (3.5 MB)

I attached the sample ply file.

Oh I see, you want to create a surface from your points. Maybe you should have a look at vtkDelaunay2d. This filter can take the polydata we created as input and should provide a triangulated surface like the one you want as output.

Thanks for your reply!

I already have tested with the vtkDelaunay2d sample code.
I modified the code as below, the stack overflow exception occurred. :cry:

My code:

int main(int, char*[])
{
	vtkNew<vtkNamedColors> colors;

	vtkNew<vtkPLYReader> reader;
	reader->SetFileName("test.ply");
	reader->Update();
	// Create a set of heights on a grid.
	// This is often called a "terrain map".
	vtkNew<vtkPoints> points;
	points->DeepCopy(reader->GetOutput()->GetPoints());

	// Add the grid points to a polydata object
	vtkNew<vtkPolyData> polydata;
	polydata->SetPoints(points);

	//... same as the sample code
}

Can you show the full code with the cell array computation and the call to the vtkDelaunay2d filter ?

Exam_VTK.zip (2.0 MB)
Please see my full code. It is a modified code based on the vtkDelaunay2d sample code.

Thanks again…! :slight_smile:

=============== modified ===============
I found that the solution for the ‘stack overflow’ exception.
There is no error if I increase the stack size.
[Project > Properties > Linker > System > StackSize]

But the converting speed from the point to vtkDelaunay2d is so slow.
Is there any way to speed up the conversion?

Nice you succeeded to convert your file.
What is the approximate size of your point cloud ? In your scene, it looks like you represent points using glyphs. This can also be a bottleneck on a large scene. Can you try to use the Point Gaussian representation instead ? This one is computed in the screen space and does not create additional geometry for each point.

Thank you for your guide :slight_smile:
Actually what I want is just to represent the mesh data from the point data.
I think I have to study VTK library deeply, because there are so many functions.

So, if I have a further question I will create a new topic.
Thanks again!