如何在一个 .vtu 文件中写入多个 vtkUnstructuredGrid

how to write multiple vtkUnstructuredGrid in one .vtu file

我想在一个 .vtu 文件中写入多个非结构化网格。

我在下面试过了。 MakeHexagonalPrism() 和 MakeHexahedron() return vtkSmartPointer 类型。 结果是输出文件中只有一个非结构化网格。

  vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
    vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
  writer->SetFileName(filename.c_str());
  writer->SetInputData(MakeHexagonalPrism());
  writer->SetInputData(MakeHexahedron());
  writer->Write();

下面我也试过了。 cellArray1 和 cellArray2 的类型是 vtkSmartPointer。结果是输出文件中只有一种类型的非结构化网格。

  vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid =
    vtkSmartPointer<vtkUnstructuredGrid>::New();
  unstructuredGrid->SetPoints(points);
  unstructuredGrid->SetCells(VTK_TETRA, cellArray1);
  unstructuredGrid->SetCells(VTK_WEDGE, cellArray2);

我不知道如何在一个 .vtu 文件中写入多个非结构化网格。 如果有任何提示,我将不胜感激。

引用 vtkXMLUnstructuredGridWriter 的文档可用 here

One unstructured grid input can be written into one file in any number of streamed pieces (if supported by the rest of the pipeline).

所以我认为使用此编写器无法将多个非结构化网格数据集写入一个文件class。

您是否希望在同一个非结构化网格(可以写入单个 .vtu 文件)中包含多种类型的单元格,而不是在同一个 .vtu 文件中包含多个非结构化网格?如果是,您必须首先将两个元胞数组组合成一个元胞数组,并创建一个 int 数组,其中包含总元胞数组中每个元胞的类型。例如,

// Create a Type vector to store cell types
std::vector<int> types;

// Create a new cell array composed of cellArray1 and cellArray2
vtkSmartPointer<vtkCellArray> allCells = 
    vtkSmartPointer<vtkCellArray>::New();

// Traverse cellArray1 and add it's cells to allCells
vtkSmartPointer<vtkIdList> nextCell =
    vtkSmartPointer<vtkIdList>::New();
cellArray1->InitTraversal()
while( cellArray1->GetNextCell( nextCell ) ){
    allCells->InsertNextCell( nextCell );
    types.push_back( VTK_TETRA );
}
// Traverse cellArray2 and add it's cells to allCells
cellArray2->InitTraversal()
while( cellArray2->GetNextCell( nextCell ) ){
    allCells->InsertNextCell( nextCell );
    types.push_back( VTK_WEDGE );
}

//Finally, set allCells to unstructuredGrid
unstructuredGrid->SetCells( &(types[0]), allCells );

现在,当您将此非结构化网格写入 .vtu 文件时,我认为您应该在一个文件中同时包含楔形和四边形单元格。

如文档所述,vtkUnstructuredGrid class 非常通用。

dataset represents arbitrary combinations of all possible cell types

您可以使用 vtkAppendFilter 将不同的数据集附加到一个数据集中,然后将输出作为 vtkUnstructuredGrid 结果写入 .vtu 文件。

// create the append filter
vtkSmartPointer<vtkAppendFilter> append =
vtkSmartPointer<vtkAppendfilter>::New();

// add each data set
append->AddInputData(MakeHexagonalPrism());
append->AddInputData(MakeHexahedron());
append->Update();

// write the result
vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
writer->SetFileName(filename.c_str());
writer->SetInputData(append->GetOutput());

编辑:我按照 Amit Singh

的建议添加了缺少的 Update() 函数调用