37#include <vtkRenderWindow.h>
38#include <vtkRenderer.h>
40#if VTK_MAJOR_VERSION >= 9
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>
60int main(
int argc,
char *argv[])
62 if (argc != 2 && argc != 4)
64 cout <<
"Usage: VtkToPng vtk-file [lower upper]" << endl;
68 string vInput = argv[1];
69 string vOutput = vInput.substr(0, vInput.find_last_of(
'.')) +
".png";
72 vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
73 vtkSmartPointer<vtkGraphicsFactory>::New();
74 graphics_factory->SetOffScreenOnlyMode(1);
75 graphics_factory->SetUseMesaClasses(1);
78 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
79 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
80 reader->SetFileName(vInput.c_str());
83 vtkSmartPointer<vtkDataSet> data =
84 vtkSmartPointer<vtkDataSet>(reader->GetOutputAsDataSet());
85 data->GetPointData()->SetActiveScalars(
"u");
87 double scalar_range[2];
88 data->GetScalarRange(scalar_range);
91 scalar_range[0] = atof(argv[2]);
92 scalar_range[1] = atof(argv[3]);
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);
105 vtkSmartPointer<vtkDataSetMapper> mapper =
106 vtkSmartPointer<vtkDataSetMapper>::New();
107#if VTK_MAJOR_VERSION <= 5
108 mapper->SetInput(data);
110 mapper->SetInputData(data);
113#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
115 mapper->ImmediateModeRenderingOn();
118 mapper->ScalarVisibilityOn();
119 mapper->SetScalarModeToUsePointData();
120 mapper->UseLookupTableScalarRangeOn();
122 mapper->SetLookupTable(lookup);
124 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
125 actor->SetMapper(mapper);
128 vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
129 camera->SetPosition(0.0, -1.0, 1.0);
130 camera->SetFocalPoint(0, 0, 0);
133 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
134 vtkSmartPointer<vtkRenderWindow> renderWindow =
135 vtkSmartPointer<vtkRenderWindow>::New();
136 renderWindow->SetOffScreenRendering(1);
137 renderWindow->AddRenderer(renderer);
140 renderer->AddActor(actor);
141 renderer->SetBackground(0, 0, 0);
142 renderer->SetActiveCamera(camera);
143 renderer->ResetCamera();
145 renderWindow->Render();
148 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
149 vtkSmartPointer<vtkWindowToImageFilter>::New();
150 windowToImageFilter->SetInput(renderWindow);
152#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
154 windowToImageFilter->SetMagnification(4);
156 windowToImageFilter->SetScale(4, 4);
158 windowToImageFilter->Update();
161 vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
162 writer->SetFileName(vOutput.c_str());
163 writer->SetInputConnection(windowToImageFilter->GetOutputPort());
int main(int argc, char *argv[])