how to write multiple vtkUnstructuredGrid in one .vtu file

ぃ、小莉子 提交于 2019-12-10 23:08:35

问题


I want to write multiple unstructured grids in one .vtu file.

I tried below. MakeHexagonalPrism() and MakeHexahedron() return vtkSmartPointer type. The result is there was only one unstructured grid in the output file.

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

I also tried below. The type of cellArray1 and cellArray2 is vtkSmartPointer. The result is there was only one type of unstructured grid in the output file.

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

I do not know how to write multiple unstructured grids in one .vtu file. I'd be grateful for any hints.


回答1:


As described by the documentation, the vtkUnstructuredGrid class is very versatile.

dataset represents arbitrary combinations of all possible cell types

You could use the vtkAppendFilter in order to append different data sets into one then write the output as a vtkUnstructuredGrid result in a .vtu file.

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

EDIT: I added the missing Update() function call as suggested by Amit Singh




回答2:


Quoting from the documentation for vtkXMLUnstructuredGridWriter available 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).

So I think it is not possible to write multiple unstructured grid datasets to one file using this writer class.

Do you want multiple types of cells inside the same unstructured grid (which can be written to a single .vtu file) rather than multiple unstructured grids in the same .vtu file? If yes, you must first combine the two cell arrays into a single cell array and also create a int array which contains type of each cell in the total cell array. For example,

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

Now when you write this unstructured grid to a .vtu file, I think you should have both wedge type and tetra type of cells in one file.



来源:https://stackoverflow.com/questions/47349086/how-to-write-multiple-vtkunstructuredgrid-in-one-vtu-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!