We can use vtkXMLStructuredGridWriter to save vtkStructuredGrid data in XML file.
The relative reading class is vtkXMLStructuredGridReader.
The following code is wrote after I read a question at stack overflow:
https://stackoverflow.com/questions/55315403/how-to-write-scalar-filed-with-vtk-in-c

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkActor2D.h>
#include <vtkLineSource.h>
#include <vtkAppendPolyData.h>
#include <vtkStructuredGrid.h>
#include <vtkXMLStructuredGridWriter.h>
#include <vtkPointData.h>
#include <vtkXMLStructuredGridReader.h>
#include <vtkStructuredGridReader.h>
#include <vtkStructuredGridGeometryFilter.h>
#include <vtkFloatArray.h>

using namespace std;

int main()
{
    // 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);

    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 );
    scalars->FastDelete();

    // Write file
    vtkSmartPointer<vtkXMLStructuredGridWriter> writer = vtkSmartPointer<vtkXMLStructuredGridWriter>::New();
    writer->SetFileName("output.vts");
    writer->SetInputData(structuredGrid);
    writer->Write();

    // Read the file
    vtkSmartPointer<vtkXMLStructuredGridReader> reader =
      vtkSmartPointer<vtkXMLStructuredGridReader>::New();
    reader->SetFileName("output.vts");
    reader->Update();

    vtkSmartPointer<vtkStructuredGridGeometryFilter> geometryFilter =
      vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
    geometryFilter->SetInputConnection(reader->GetOutputPort());
    geometryFilter->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData( geometryFilter->GetOutput() );

    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->SetMapper( mapper );

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 );

    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer( renderer );

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}


 

Categories: C plus plus

Leave a Reply

Your email address will not be published. Required fields are marked *