[vtkusers] vtkPNGWriter: Write reduced vtkUnstructuredGrid field data to PNG-file via vtkResampleWithDataset

Stephan Lenz stephan.lenz at tu-bs.de
Tue Jan 16 03:49:57 EST 2018


Hello everybody,

I have problems setting up a pipeline to write data to an PNG-file.

I have a 2-D simulation on a large rectangular non-uniform grid ( 
~60.000.000 Cells ). For visualization of the results I would like to 
write PNG files (or other Imagefiles) in order to create an animation.

I am already able to write VTK-ImageData files (.vti) and make an 
animation in Paraview. Nevertheless I would prefer to generate the 
animation directly from images, as the simulation domain should fill the 
whole animation domain. With paraview fitting the simulation domain 
seamlessly in the view is not trivial.

My current pipeline is:

           // this adapter function couples my simulation datastructures 
to VTK
             vtkSmartPointer<vtkUnstructuredGrid> grid = 
getVtkUnstructuredGrid(dataBase);

             // generate the field data
             vtkSmartPointer<vtkUnsignedCharArray> data = 
vtkSmartPointer<vtkUnsignedCharArray>::New();
             data->SetNumberOfComponents( 1 );
             data->SetName( "T" );
             for( Idx cellIdx = 0; cellIdx < numberOfCells; cellIdx++ ){
                 unsigned char value = getValue( cellIdx );
                 data->InsertNextValue( Tint );
             }
             grid->GetCellData()->AddArray( data );

             // define the grid for the output
             vtkSmartPointer<vtkImageData> image = 
vtkSmartPointer<vtkImageData>::New();
             image->SetDimensions(512+1,256+1,1);
             image->SetSpacing( L/512, H/256, 0 );

             // resample the field data to the coarser vtkImageData
             vtkSmartPointer<vtkResampleWithDataSet> resample = 
vtkSmartPointer<vtkResampleWithDataSet>::New();
             resample->SetSourceData( grid );
             resample->SetInputData( image );

             // write the data to PNG-file (does not work!)
             vtkSmartPointer<vtkPNGWriter> writerPNG = 
vtkSmartPointer<vtkPNGWriter>::New();
             writerPNG->SetFileName( ( simulationName + ".png").c_str() );
             writerPNG->SetInputData( resample->GetOutput() );
             writerPNG->Write();

             // write the data to VTI-file (works!)
             vtkSmartPointer<vtkXMLImageDataWriter> writer = 
vtkSmartPointer<vtkXMLImageDataWriter>::New();
             writer->SetFileName( ( simulationName + "." + 
writer->GetDefaultFileExtension() ).c_str() );
             writer->SetDataMode( vtkXMLWriter::Binary );
             writer->SetInputData( resample->GetOutput() );
             writer->Write();

The above code compiles and runs without errors, but I do not get any 
PNG-Files. The VTI-files are written and I can open them in Paraview, as 
usual. I tried many different things, but did not get single PNG-file 
out of this.

When I, for example, change

writerPNG->SetInputData( resample->GetOutput() );

to

writerPNG->SetInputConnection( resample->GetOutputPort() );

I get a VTK Error message, that says "PNGWriter only supports unsigned 
char and unsigned short inputs". But as far as I see it the field data 
is unsigned char and this is confirmed by Paraview, when I look into the 
VTI-files (generated with both SetInputData and SetInputConnection).

Do you see any obvious mistakes I make? How do I have to set up such a 
pipeline to get a PNG (or other image file) output. I would be glad for 
any help.

Thank you very much,

Stephan Lenz



More information about the vtkusers mailing list