6

我正在尝试将数据从统一网格写入 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;
}

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

4

1 回答 1

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;
}
于 2013-02-25T18:33:55.610 回答