# How to create a mesh data from vtkPoints?

**URL:** https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585
**Category:** Support
**Created:** [September 9, 2021, 3:16am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585 "2021-09-09T03:16:58Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 9, 2021, 3:16am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/1 "2021-09-09T03:16:58Z")

</div>

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:

> [@The PLY file is not working.](https://discourse.vtk.org/t/the-ply-file-is-not-working/924):
>
> Hi all. I have a question about PLY file load. I have two PLY files. (A) PLY file: It made by 3D Scanner. (B) PLY file: The sample PLY file from the link. Link: [http://graphics.im.ntu.edu.tw/~robin/courses/cg04/model/index.html](http://graphics.im.ntu.edu.tw/~robin/courses/cg04/model/index.html) The B file work normally but A file is not working on the same code. My code snippet is as below: vtkSmartPointer pPLYReader = vtkSmartPointer::New(); pPLYReader-\>SetFileName(“myPLY.ply”); pPLYReader-\>Update(); vtkSmartPointer mapper = vtkSmartPointer::New(); …

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 🙂

Best regards,  
William Kim

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [September 9, 2021, 7:54am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/2 "2021-09-09T07:54:23Z")

</div>

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](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Filters/Sources/vtkPolyPointSource.cxx#L133).

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 9, 2021, 8:34am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/3 "2021-09-09T08:34:58Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [September 9, 2021, 8:47am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/4 "2021-09-09T08:47:00Z")

</div>

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:

```cpp
   // 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

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 13, 2021, 7:39am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/5 "2021-09-13T07:39:40Z")

</div>

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:

```auto
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/](https://kitware.github.io/vtk-examples/site/Cxx/Filtering/TriangulateTerrainMap/)

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

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 13, 2021, 7:40am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/6 "2021-09-13T07:40:43Z")

</div>

[test.ply](https://discourse.vtk.org/uploads/short-url/jE0peHTLciZ4TbAR14Fsn2wGFiV.ply) (3.5 MB)

I attached the sample ply file.

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [September 13, 2021, 8:20am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/7 "2021-09-13T08:20:59Z")

</div>

Oh I see, you want to create a surface from your points. Maybe you should have a look at [vtkDelaunay2d](https://vtk.org/doc/nightly/html/classvtkDelaunay2D.html). This filter can take the polydata we created as input and should provide a triangulated surface like the one you want as output.

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 13, 2021, 8:46am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/8 "2021-09-13T08:46:45Z")

</div>

Thanks for your reply!

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

My code:

```auto
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
}

```

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [September 13, 2021, 8:50am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/9 "2021-09-13T08:50:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 13, 2021, 11:20pm UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/10 "2021-09-13T23:20:00Z")

</div>

[Exam\_VTK.zip](https://discourse.vtk.org/uploads/short-url/uaooty8JII6mWOsAVb3jihgACne.zip) (2.0 MB)  
Please see my full code. It is a modified code based on the vtkDelaunay2d sample code.

Thanks again…! 🙂

=============== 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?

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [September 14, 2021, 11:20am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/11 "2021-09-14T11:20:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 15, 2021, 2:57am UTC](https://discourse.vtk.org/t/how-to-create-a-mesh-data-from-vtkpoints/6585/12 "2021-09-15T02:57:36Z")

</div>

Thank you for your guide 🙂  
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!
