Nektar++
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.

55 {
56  if (argc != 2 && argc != 4)
57  {
58  cout << "Usage: VtkToPng vtk-file [lower upper]" << endl;
59  exit(-1);
60  }
61 
62  string vInput = argv[1];
63  string vOutput = vInput.substr(0, vInput.find_last_of('.')) + ".png";
64 
65  // Setup offscreen rendering
66  vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
67  vtkSmartPointer<vtkGraphicsFactory>::New();
68  graphics_factory->SetOffScreenOnlyMode( 1);
69  graphics_factory->SetUseMesaClasses( 1 );
70 
71  // Create a poly data reader and retrieve dataset from file
72  vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
73  vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
74  reader->SetFileName(vInput.c_str());
75  reader->Update();
76 
77  vtkSmartPointer<vtkDataSet> data =
78  vtkSmartPointer<vtkDataSet>(reader->GetOutputAsDataSet());
79  data->GetPointData()->SetActiveScalars("u");
80 
81  double scalar_range[2];
82  data->GetScalarRange(scalar_range);
83  if (argc == 4)
84  {
85  scalar_range[0] = atof(argv[2]);
86  scalar_range[1] = atof(argv[3]);
87  }
88 
89  // Lookup table
90  vtkSmartPointer<vtkLookupTable> lookup = vtkSmartPointer<vtkLookupTable>::New();
91  lookup->SetHueRange(0.0,1.0);
92  lookup->SetSaturationRange(1,1);
93  lookup->SetTableRange(scalar_range);
94  lookup->SetValueRange(1,1);
95  lookup->Build();
96 
97  // Create a mapper and actor
98  vtkSmartPointer<vtkDataSetMapper> mapper =
99  vtkSmartPointer<vtkDataSetMapper>::New();
100 #if VTK_MAJOR_VERSION <= 5
101  mapper->SetInput(data);
102 #else
103  mapper->SetInputData(data);
104 #endif
105  mapper->ImmediateModeRenderingOn();
106  mapper->ScalarVisibilityOn();
107  mapper->SetScalarModeToUsePointData();
108  mapper->UseLookupTableScalarRangeOn();
109  //mapper->SetScalarRange(data->GetScalarRange());
110  mapper->SetLookupTable(lookup);
111 
112 
113  vtkSmartPointer<vtkActor> actor =
114  vtkSmartPointer<vtkActor>::New();
115  actor->SetMapper(mapper);
116 
117  // Configure camera position and direction
118  vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
119  camera->SetPosition(0.0,-1.0,1.0);
120  camera->SetFocalPoint(0,0,0);
121 
122  // A renderer and render window
123  vtkSmartPointer<vtkRenderer> renderer =
124  vtkSmartPointer<vtkRenderer>::New();
125  vtkSmartPointer<vtkRenderWindow> renderWindow =
126  vtkSmartPointer<vtkRenderWindow>::New();
127  renderWindow->SetOffScreenRendering( 1 );
128  renderWindow->AddRenderer(renderer);
129 
130  // Add the actors to the scene
131  renderer->AddActor(actor);
132  renderer->SetBackground(0,0,0); // Background color white
133  renderer->SetActiveCamera(camera);
134  renderer->ResetCamera();
135 
136  renderWindow->Render();
137 
138  // Create an image of scene
139  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
140  vtkSmartPointer<vtkWindowToImageFilter>::New();
141  windowToImageFilter->SetInput(renderWindow);
142  windowToImageFilter->SetMagnification(4);
143  windowToImageFilter->Update();
144 
145  // Write image to PNG
146  vtkSmartPointer<vtkPNGWriter> writer =
147  vtkSmartPointer<vtkPNGWriter>::New();
148  writer->SetFileName(vOutput.c_str());
149  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
150  writer->Write();
151 
152  return EXIT_SUCCESS;
153 }