gpt4 book ai didi

c++ - 如何在 C++ 中使用 VTK 编写标量?

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:41 25 4
gpt4 key购买 nike

我开始学习 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();

最佳答案

可以为vtkStructuredGrid对象的属性PointData设置标量。
然后 vtkXMLStructuredGridWriter 对象将 structuredGrid 写入 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();

关于c++ - 如何在 C++ 中使用 VTK 编写标量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com