我正在尝试将数据从统一网格写入 vtk XML 文件,以便我可以使用 Paraview 呈现它。我希望我的输出文件包含两个变量:一个 3 分量向量和每个点的一个标量值。我设法编写了一个仅包含向量的 .vti 文件,如下所示:
#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkImageData.h>
int main()
{
int nx = 10, ny = 10, nz = 10;
vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetDimensions(nx, ny, nz);
#if VTK_MAJOR_VERSION <= 5
imageData->SetNumberOfScalarComponents(3);
imageData->SetScalarTypeToDouble();
#else
imageData->AllocateScalars(VTK_DOUBLE, 3);
#endif
for (int k = 0; k < nz; ++k) {
for (int j = 0; j < ny; ++j) {
for (int i = 0; i < nx; ++i) {
double * voxel = static_cast<double*>(imageData->GetScalarPointer(i,j,k));
int coord = i + j * nx + k * nx * ny;
double t = 0.0;
double p = 1.0;
voxel[0] = sin(t) * cos(p);
voxel[1] = sin(t) * sin(p);
voxel[2] = cos(t);
}
}
}
vtkSmartPointer<vtkXMLImageDataWriter> writer =
vtkSmartPointer<vtkXMLImageDataWriter>::New();
writer->SetFileName("test.vti");
#if VTK_MAJOR_VERSION <= 5
writer->SetInputConnection(imageData->GetProducerPort());
#else
writer->SetInputData(imageData);
#endif
writer->Write();
return EXIT_SUCCESS;
}
如何为网格上的每个点添加额外的标量值?