VTK:在c ++中从vtu非结构化网格中提取单元数据

时间:2016-03-09 16:49:53

标签: c++ vtk

我需要从.vtu(XML非结构化网格)中提取所有单元格数据,以便在c ++程序中进一步操作。我对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;

这是正确的 - 正确显示单元格编号。现在如何访问存储在.vtu文件中的不同CellArrays属性的名称,然后访问它们的实际数值?任何帮助表示赞赏! 干杯, Domanov

1 个答案:

答案 0 :(得分:3)

//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;

要访问非结构化网格的单元格数据,您可以执行以下操作:

vtkUnstructuredGrid* ugrid = reader->GetOutput();
vtkCellData *cellData = ugrid->GetCellData();
for (int i = 0; i < cellData->GetNumberOfArrays(); i++)
{
    vtkDataArray* data = cellData->GetArray(j);
    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;
    }
}