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

Stephan Lenz stephan.lenz at tu-bs.de
Fri Jan 19 05:40:50 EST 2018


Thank you very much for your help. Both suggestions did not do the 
trick, though.

I found out that my mistake was to assume that the vtkPNGWriter could 
access the PointData arrays of the vtkImageData. What I had to do is, to 
assign the array to the PointData Scalar. Then the PNG output works very 
well.

Below, I shortly post the solution to my problem:

void writePNG( vtkDataObject * inputData, int nx, int ny, double L, 
double H, std::string filename )
{
     vtkSmartPointer<vtkImageData> image = 
vtkSmartPointer<vtkImageData>::New();
     image->SetDimensions( nx, ny, 1 );
     image->SetSpacing( L / ( nx - 1 ), H / ( ny - 1 ), 0 );

     vtkSmartPointer<vtkResampleWithDataSet> resample = 
vtkSmartPointer<vtkResampleWithDataSet>::New();
     resample->SetSourceData( inputData );
     resample->SetInputData( image );
     resample->Update();

     vtkSmartPointer<vtkImageData> image2 = (vtkImageData*) 
resample->GetOutput();

     image2->GetPointData()->SetScalars( 
image2->GetPointData()->GetArray( 0 ) );

     vtkSmartPointer<vtkPNGWriter> writerPNG = 
vtkSmartPointer<vtkPNGWriter>::New();
     writerPNG->SetFileName( ( filename + ".png" ).c_str() );
     writerPNG->SetInputData( image2 );
     writerPNG->Write();
}

This function assumes that the vtkDataObject inputData has only one 
PointData array. If it has multiple image2->GetPointData()->GetArray( 0 
) has to be changed to 
image2->GetPointData()->GetArray("nameOfTheArray"). This PointData array 
has to be unsigned char or unsigned short (I tested only the former). It 
can be one component for a gray scale PNG or three components for a 
color PNG.

I guess this is a nice and short way to reduce simulation data to images 
bypassing Paraview.

Best regards

Stephan Lenz

Am 16.01.2018 um 15:37 schrieb Bill Lorensen:
> After
>   resample->SetInputData( image );
> do
> resample->Update();
>
>
> On Tue, Jan 16, 2018 at 5:36 AM, David E DeMarle
> <dave.demarle at kitware.com> wrote:
>> My guess is that the PNG writer expect three components, not a single
>> component array. vtkImageAppendComponents would be one way to make a three
>> tuple, but it would be more straightforward to do it in the array
>> construction with
>> SetNumberOfComponents(3) followed by
>> yourloop {
>>     InsertNextTuple3(Tint,Tint,Tint);
>> }
>>
>>
>> David E DeMarle
>> Kitware, Inc.
>> Principal Engineer
>> 21 Corporate Drive
>> Clifton Park, NY 12065-8662
>> Phone: 518-881-4909
>>
>> On Tue, Jan 16, 2018 at 3:49 AM, Stephan Lenz <stephan.lenz at tu-bs.de> wrote:
>>> 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
>>>
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Please keep messages on-topic and check the VTK FAQ at:
>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>
>>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> https://vtk.org/mailman/listinfo/vtkusers
>>
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> https://vtk.org/mailman/listinfo/vtkusers
>>
>
>



More information about the vtkusers mailing list