Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VtkToPng.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File VtkToPng.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Render a VTK Unstructured Grid file as a PNG
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #include <vtkActor.h>
37 #include <vtkRenderWindow.h>
38 #include <vtkRenderer.h>
39 #include <vtkPolyData.h>
40 #include <vtkSmartPointer.h>
41 #include <vtkSphereSource.h>
42 #include <vtkWindowToImageFilter.h>
43 #include <vtkPNGWriter.h>
44 #include <vtkGraphicsFactory.h>
45 #include <vtkXMLUnstructuredGridReader.h>
46 #include <vtkDataSetMapper.h>
47 #include <vtkLookupTable.h>
48 #include <vtkPointData.h>
49 #include <vtkCamera.h>
50 
51 #include <iostream>
52 using namespace std;
53 
54 int main(int argc, char * argv[])
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  vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
73  reader->SetFileName(vInput.c_str());
74  reader->Update();
75 
76  vtkDataSet* data = reader->GetOutputAsDataSet();
77  data->GetPointData()->SetActiveScalars("u");
78 
79  double scalar_range[2];
80  data->GetScalarRange(scalar_range);
81  if (argc == 4)
82  {
83  scalar_range[0] = atof(argv[2]);
84  scalar_range[1] = atof(argv[3]);
85  }
86 
87  // Lookup table
88  vtkSmartPointer<vtkLookupTable> lookup = vtkSmartPointer<vtkLookupTable>::New();
89  lookup->SetHueRange(0.0,1.0);
90  lookup->SetSaturationRange(1,1);
91  lookup->SetTableRange(scalar_range);
92  lookup->SetValueRange(1,1);
93  lookup->Build();
94 
95  // Create a mapper and actor
96  vtkSmartPointer<vtkDataSetMapper> mapper =
97  vtkSmartPointer<vtkDataSetMapper>::New();
98 #if VTK_MAJOR_VERSION <= 5
99  mapper->SetInput(data);
100 #else
101  mapper->SetInputData(data);
102 #endif
103  mapper->ImmediateModeRenderingOn();
104  mapper->ScalarVisibilityOn();
105  mapper->SetScalarModeToUsePointData();
106  mapper->UseLookupTableScalarRangeOn();
107  //mapper->SetScalarRange(data->GetScalarRange());
108  mapper->SetLookupTable(lookup);
109 
110 
111  vtkSmartPointer<vtkActor> actor =
112  vtkSmartPointer<vtkActor>::New();
113  actor->SetMapper(mapper);
114 
115  // Configure camera position and direction
116  vtkCamera *camera = vtkCamera::New();
117  camera->SetPosition(0.0,-1.0,1.0);
118  camera->SetFocalPoint(0,0,0);
119 
120  // A renderer and render window
121  vtkSmartPointer<vtkRenderer> renderer =
122  vtkSmartPointer<vtkRenderer>::New();
123  vtkSmartPointer<vtkRenderWindow> renderWindow =
124  vtkSmartPointer<vtkRenderWindow>::New();
125  renderWindow->SetOffScreenRendering( 1 );
126  renderWindow->AddRenderer(renderer);
127 
128  // Add the actors to the scene
129  renderer->AddActor(actor);
130  renderer->SetBackground(0,0,0); // Background color white
131  renderer->SetActiveCamera(camera);
132  renderer->ResetCamera();
133 
134  renderWindow->Render();
135 
136  // Create an image of scene
137  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
138  vtkSmartPointer<vtkWindowToImageFilter>::New();
139  windowToImageFilter->SetInput(renderWindow);
140  windowToImageFilter->SetMagnification(4);
141  windowToImageFilter->Update();
142 
143  // Write image to PNG
144  vtkSmartPointer<vtkPNGWriter> writer =
145  vtkSmartPointer<vtkPNGWriter>::New();
146  writer->SetFileName(vOutput.c_str());
147  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
148  writer->Write();
149 
150  return EXIT_SUCCESS;
151 }