Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
VtkToPng.cpp File Reference
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
#include <vtkGraphicsFactory.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkDataSetMapper.h>
#include <vtkLookupTable.h>
#include <vtkPointData.h>
#include <vtkCamera.h>
#include <iostream>
Include dependency graph for VtkToPng.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 54 of file VtkToPng.cpp.

{
if (argc != 2 && argc != 4)
{
cout << "Usage: VtkToPng vtk-file [lower upper]" << endl;
exit(-1);
}
string vInput = argv[1];
string vOutput = vInput.substr(0, vInput.find_last_of('.')) + ".png";
// Setup offscreen rendering
vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
vtkSmartPointer<vtkGraphicsFactory>::New();
graphics_factory->SetOffScreenOnlyMode( 1);
graphics_factory->SetUseMesaClasses( 1 );
// Create a poly data reader and retrieve dataset from file
vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
reader->SetFileName(vInput.c_str());
reader->Update();
vtkDataSet* data = reader->GetOutputAsDataSet();
data->GetPointData()->SetActiveScalars("u");
double scalar_range[2];
data->GetScalarRange(scalar_range);
if (argc == 4)
{
scalar_range[0] = atof(argv[2]);
scalar_range[1] = atof(argv[3]);
}
// Lookup table
vtkSmartPointer<vtkLookupTable> lookup = vtkSmartPointer<vtkLookupTable>::New();
lookup->SetHueRange(0.0,1.0);
lookup->SetSaturationRange(1,1);
lookup->SetTableRange(scalar_range);
lookup->SetValueRange(1,1);
lookup->Build();
// Create a mapper and actor
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapper->SetInput(data);
#else
mapper->SetInputData(data);
#endif
mapper->ImmediateModeRenderingOn();
mapper->ScalarVisibilityOn();
mapper->SetScalarModeToUsePointData();
mapper->UseLookupTableScalarRangeOn();
//mapper->SetScalarRange(data->GetScalarRange());
mapper->SetLookupTable(lookup);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Configure camera position and direction
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(0.0,-1.0,1.0);
camera->SetFocalPoint(0,0,0);
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetOffScreenRendering( 1 );
renderWindow->AddRenderer(renderer);
// Add the actors to the scene
renderer->AddActor(actor);
renderer->SetBackground(0,0,0); // Background color white
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderWindow->Render();
// Create an image of scene
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->SetMagnification(4);
windowToImageFilter->Update();
// Write image to PNG
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New();
writer->SetFileName(vOutput.c_str());
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
return EXIT_SUCCESS;
}