3

I need to extract all the cell data from a .vtu (XML unstructured grid) for further manipulations in a c++ program. I am quite new to VTK...

      //read all the data from the file
      vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
      vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
      reader->SetFileName(filename.c_str());
      reader->Update();

      unsigned int cellNumber = reader->GetOutput()->GetNumberOfCells();
      cout << "There are " << cellNumber << " input cells." << endl;

This is correct - the cell number is displayed correctly. How do access now the names of the different CellArrays properties stored in the .vtu file and then their actual numeric values? Any help is appreciated! Cheers, Domanov

1 Answer 1

5
//read all the data from the file
  vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
  vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
  reader->SetFileName(filename.c_str());
  reader->Update();

  unsigned int cellNumber = reader->GetOutput()->GetNumberOfCells();
  cout << "There are " << cellNumber << " input cells." << endl;

To access the cell data of unstructured grid, you can do as following:

vtkUnstructuredGrid* ugrid = reader->GetOutput();
vtkCellData *cellData = ugrid->GetCellData();
for (int i = 0; i < cellData->GetNumberOfArrays(); i++)
{
    vtkDataArray* data = cellData->GetArray(i);
    cout << "name " << data->GetName() << endl;
    for (int j = 0; j < data->GetNumberOfTuples(); j++)
    {
        double value = data->GetTuple1(j);
        cout << "  value " << j << "th is " << value << endl;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Minor typo: iterate over j in the 2nd loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.