gpt4 book ai didi

c++ vtk xml writer (vtkImageData) - 同一个vti文件中的多个变量

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:53 26 4
gpt4 key购买 nike

我正在尝试将数据从统一网格写入 vtk XML 文件,以便我可以使用 Paraview 呈现它。我希望我的输出文件包含两个变量:一个 3 分量 vector 和一个标量值在每一点。我设法写了一个 .vti 文件,只包含 vector ,如下所示:

#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;
}

如何为网格上的每个点添加一个额外的标量值?

最佳答案

根据这个答案中的 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;
}

关于c++ vtk xml writer (vtkImageData) - 同一个vti文件中的多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14964312/

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