Nektar++
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  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 }
STL namespace.
int main(int argc, char *argv[])
Definition: VtkToPng.cpp:54