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// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Render a VTK Unstructured Grid file as a PNG
32//
33///////////////////////////////////////////////////////////////////////////////
34
36#include <vtkActor.h>
37#include <vtkRenderWindow.h>
38#include <vtkRenderer.h>
39
40#if VTK_MAJOR_VERSION >= 9
41#include <limits>
42#include <stdexcept>
43#endif
44
45#include <vtkCamera.h>
46#include <vtkDataSetMapper.h>
47#include <vtkGraphicsFactory.h>
48#include <vtkLookupTable.h>
49#include <vtkPNGWriter.h>
50#include <vtkPointData.h>
51#include <vtkPolyData.h>
52#include <vtkSmartPointer.h>
53#include <vtkSphereSource.h>
54#include <vtkWindowToImageFilter.h>
55#include <vtkXMLUnstructuredGridReader.h>
56
57#include <iostream>
58using namespace std;
59
60int main(int argc, char *argv[])
61{
62 if (argc != 2 && argc != 4)
63 {
64 cout << "Usage: VtkToPng vtk-file [lower upper]" << endl;
65 exit(-1);
66 }
67
68 string vInput = argv[1];
69 string vOutput = vInput.substr(0, vInput.find_last_of('.')) + ".png";
70
71 // Setup offscreen rendering
72 vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
73 vtkSmartPointer<vtkGraphicsFactory>::New();
74 graphics_factory->SetOffScreenOnlyMode(1);
75 graphics_factory->SetUseMesaClasses(1);
76
77 // Create a poly data reader and retrieve dataset from file
78 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
79 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
80 reader->SetFileName(vInput.c_str());
81 reader->Update();
82
83 vtkSmartPointer<vtkDataSet> data =
84 vtkSmartPointer<vtkDataSet>(reader->GetOutputAsDataSet());
85 data->GetPointData()->SetActiveScalars("u");
86
87 double scalar_range[2];
88 data->GetScalarRange(scalar_range);
89 if (argc == 4)
90 {
91 scalar_range[0] = atof(argv[2]);
92 scalar_range[1] = atof(argv[3]);
93 }
94
95 // Lookup table
96 vtkSmartPointer<vtkLookupTable> lookup =
97 vtkSmartPointer<vtkLookupTable>::New();
98 lookup->SetHueRange(0.0, 1.0);
99 lookup->SetSaturationRange(1, 1);
100 lookup->SetTableRange(scalar_range);
101 lookup->SetValueRange(1, 1);
102 lookup->Build();
103
104 // Create a mapper and actor
105 vtkSmartPointer<vtkDataSetMapper> mapper =
106 vtkSmartPointer<vtkDataSetMapper>::New();
107#if VTK_MAJOR_VERSION <= 5
108 mapper->SetInput(data);
109#else
110 mapper->SetInputData(data);
111#endif
112
113#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
114 // deprecated as of vtk 8.1
115 mapper->ImmediateModeRenderingOn();
116#endif
117
118 mapper->ScalarVisibilityOn();
119 mapper->SetScalarModeToUsePointData();
120 mapper->UseLookupTableScalarRangeOn();
121 // mapper->SetScalarRange(data->GetScalarRange());
122 mapper->SetLookupTable(lookup);
123
124 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
125 actor->SetMapper(mapper);
126
127 // Configure camera position and direction
128 vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
129 camera->SetPosition(0.0, -1.0, 1.0);
130 camera->SetFocalPoint(0, 0, 0);
131
132 // A renderer and render window
133 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
134 vtkSmartPointer<vtkRenderWindow> renderWindow =
135 vtkSmartPointer<vtkRenderWindow>::New();
136 renderWindow->SetOffScreenRendering(1);
137 renderWindow->AddRenderer(renderer);
138
139 // Add the actors to the scene
140 renderer->AddActor(actor);
141 renderer->SetBackground(0, 0, 0); // Background color white
142 renderer->SetActiveCamera(camera);
143 renderer->ResetCamera();
144
145 renderWindow->Render();
146
147 // Create an image of scene
148 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
149 vtkSmartPointer<vtkWindowToImageFilter>::New();
150 windowToImageFilter->SetInput(renderWindow);
151
152#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
153 // deprecated as of vtk 8.1
154 windowToImageFilter->SetMagnification(4);
155#else
156 windowToImageFilter->SetScale(4, 4);
157#endif
158 windowToImageFilter->Update();
159
160 // Write image to PNG
161 vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
162 writer->SetFileName(vOutput.c_str());
163 writer->SetInputConnection(windowToImageFilter->GetOutputPort());
164 writer->Write();
165
166 return EXIT_SUCCESS;
167}
int main(int argc, char *argv[])
Definition: VtkToPng.cpp:60
STL namespace.