Error with inserting vertex cells

I want to create a polyData with lots of vertex. So I followed example

auto vertexes = vtkSmartPointer<vtkCellArray>::New();
for (int i = 0; i < data->GetNumberOfPoints(); i++) {
    auto vertex = vtkSmartPointer<vtkVertex>::New();
    vertex->GetPointIds()->SetId(i, i);
    vertexes->InsertNextCell(vertex);
}
dataPoints->SetPoints(points);
dataPoints->SetVerts(vertexes);

This cause an error.

(gdb) bt
#0  0x00005564b54e5b68 in vtkCellArray::InsertNextCell (this=0x5564b5fea4b0, npts=1, pts=0x5564b62d8630) at /usr/local/include/vtk-8.2/vtkCellArray.h:304
#1  0x00005564b54e5c2a in vtkCellArray::InsertNextCell (this=0x5564b5fea4b0, cell=0x5564b60029c0) at /usr/local/include/vtk-8.2/vtkCellArray.h:345
#2  0x00005564b54e555f in main () at /home/wzx/CLionProjects/propeller_surface_reconstruction/main.cpp:39
#3  0x00007fb6d33c4223 in __libc_start_main () from /usr/lib/libc.so.6
#4  0x00005564b54e526e in _start ()

The following code performed well.

auto vertexes = vtkSmartPointer<vtkCellArray>::New();
for (int i = 0; i < data->GetNumberOfPoints(); i++) {
    vtkIdType pid[1];
    pid[0] = i;
    vertexes->InsertNextCell(1, pid);
}
dataPoints->SetPoints(points);
dataPoints->SetVerts(vertexes);

Why?

It’s because of this line:

vertex->GetPointIds()->SetId(i, i);
I believe it should be

vertex->GetPointIds()->SetId(0, i);

You are adding a single vertex to each cell, and the cell only refers to a single point. So the cell's only ID should refer to point 'i' in the points list.

This is done automatically in your second code fragment.

HTH,

Aron

I am very careless about this. Thank you for your help.