如何在C ++中用VTK编写标量文件?

时间:2019-03-23 15:35:10

标签: c++ vtk

我开始学习VTK。我想在.vts文件中写一个标量字段。 使用下面包含的代码生成结构化网格。

我尝试在用户指南和教程中查找信息,但我无法做到这一点。

// Create a grid
vtkSmartPointer<vtkStructuredGrid> structuredGrid = vtkSmartPointer<vtkStructuredGrid>::New();

vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
unsigned int numi = 20;
unsigned int numj = 30;
unsigned int numk = 1;

for (unsigned int k = 0; k < numk; k++) {
    for (unsigned int j = 0; j < numj; j++) {
        for (unsigned int i = 0; i < numi; i++) {
            points->InsertNextPoint(i, j, k);
        }
    }
}

// Specify the dimensions of the grid
structuredGrid->SetDimensions(numi, numj, numk);
structuredGrid->SetPoints(points);

// Write file
vtkSmartPointer<vtkXMLStructuredGridWriter> writer = vtkSmartPointer<vtkXMLStructuredGridWriter>::New();
writer->SetFileName("output.vts");
writer->SetInputData(structuredGrid);
writer->Write();

1 个答案:

答案 0 :(得分:0)

可以为vtkStructuredGrid对象的PointData属性设置标量。
然后,vtkXMLStructuredGridWriter对象将结构化网格写入vts文件,如下所示:

    vtkFloatArray *scalars = vtkFloatArray::New();
    for (int i=0; i< numi*numj*numk; i++) scalars->InsertTuple1(i, 1.0 * i /(numi*numj*numk));
    structuredGrid->GetPointData()->SetScalars( scalars );

    // Write file
    vtkSmartPointer<vtkXMLStructuredGridWriter> writer = vtkSmartPointer<vtkXMLStructuredGridWriter>::New();
    writer->SetFileName("output.vts");
    writer->SetInputData(structuredGrid);
    writer->Write();