我正在尝试将统一网格中的数据写入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;
}
如何为网格上的每个点添加额外的标量值?
答案 0 :(得分:6)
我根据这个答案中的python代码自己研究了如何做到这一点:
https://stackoverflow.com/a/7667417/2088135
如果有人对如何做同样的事情感兴趣,这里是“翻译过的”代码。
#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
#include <vtkDoubleArray.h>
int main()
{
int nx = 10, ny = 10, nz = 10;
vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetDimensions(nx, ny, nz);
vtkSmartPointer<vtkDoubleArray> director =
vtkSmartPointer<vtkDoubleArray>::New();
director->SetNumberOfComponents(3);
director->SetNumberOfTuples(nx * ny * nz);
vtkSmartPointer<vtkDoubleArray> energy =
vtkSmartPointer<vtkDoubleArray>::New();
energy->SetNumberOfComponents(1);
energy->SetNumberOfTuples(nx * ny * nz);
for (int i = 0; i < director->GetNumberOfTuples(); ++i) {
double t = 1.0;
double p = 0.0;
double e = 5.0;
double x = sin(t) * cos(p),
y = sin(t) * sin(p),
z = cos(t);
director->SetTuple3(i, x, y, z);
energy->SetValue(i, e);
}
imageData->GetPointData()->AddArray(director);
director->SetName("Director");
imageData->GetPointData()->AddArray(energy);
energy->SetName("Energy");
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;
}