shashashamti2008
shashashamti2008

Reputation: 2337

How to get direct access to polygons in VTK?

I found this post online (dates back to 2013) when I had trouble getting direct access to a specific cell in a vtkPolyData. I am using the latest version: VTK 8.1.1 and it seems like the newer version of VTK still has this issue.

polys->InitTraversal();
for(int i = 0; i < polys->GetNumberOfCells(); i++)
{
    polys->GetNextCell(idList); // This sequential method gets the point IDs correctly
    int a = idList->GetId(0);
    int b = idList->GetId(1);
    int c = idList->GetId(2);
}

However, the direct access method seems to have issues

polys->InitTraversal();
for(int i = 0; i < polys->GetNumberOfCells(); i++)
{
    polys->GetCell(i, idList);    // This method returns wrong ids
    int a = idList->GetId(0);
    int b = idList->GetId(1);
    int c = idList->GetId(2);
}

How can I get the point IDs in a specific cell without looping through all the cell? Isn't polys->GetCell(i, idList) meant to give you direct access to a specific cell?

Upvotes: 1

Views: 1330

Answers (1)

user6764549
user6764549

Reputation:

For direct access, we can use vtkPolyData::GetCellPoints() method. For example we can do

vtkNew<vtkIdList> idL; // or auto idL = vtkSmartPointer<vtkIdList>::New();
poly->GetCellPoints( 13, idL ); // Assuming you want the points for 13th cell
for(auto i = 0; i < idL->GetNumberOfIds(); ++i)
    std::cout<< idL->GetId(i) << std::endl;

For looping over all cells I prefer a while loop:

vtkNew<vtkIdList> idL;
poly->GetPolys()->InitTraversal();
while(poly->GetPolys()->GetNextCell(idL)){
    for(auto i = 0; i < idL->GetNumberOfIds(); ++i)
        std::cout<< idL->GetId(i) << std::endl;
}

Upvotes: 1

Related Questions