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>
54 int main(
int argc,
char * argv[])
56 if (argc != 2 && argc != 4)
58 cout <<
"Usage: VtkToPng vtk-file [lower upper]" << endl;
62 string vInput = argv[1];
63 string vOutput = vInput.substr(0, vInput.find_last_of(
'.')) +
".png";
66 vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
67 vtkSmartPointer<vtkGraphicsFactory>::New();
68 graphics_factory->SetOffScreenOnlyMode( 1);
69 graphics_factory->SetUseMesaClasses( 1 );
72 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
73 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
74 reader->SetFileName(vInput.c_str());
77 vtkSmartPointer<vtkDataSet> data =
78 vtkSmartPointer<vtkDataSet>(reader->GetOutputAsDataSet());
79 data->GetPointData()->SetActiveScalars(
"u");
81 double scalar_range[2];
82 data->GetScalarRange(scalar_range);
85 scalar_range[0] = atof(argv[2]);
86 scalar_range[1] = atof(argv[3]);
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);
98 vtkSmartPointer<vtkDataSetMapper> mapper =
99 vtkSmartPointer<vtkDataSetMapper>::New();
100 #if VTK_MAJOR_VERSION <= 5
101 mapper->SetInput(data);
103 mapper->SetInputData(data);
105 mapper->ImmediateModeRenderingOn();
106 mapper->ScalarVisibilityOn();
107 mapper->SetScalarModeToUsePointData();
108 mapper->UseLookupTableScalarRangeOn();
110 mapper->SetLookupTable(lookup);
113 vtkSmartPointer<vtkActor> actor =
114 vtkSmartPointer<vtkActor>::New();
115 actor->SetMapper(mapper);
118 vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
119 camera->SetPosition(0.0,-1.0,1.0);
120 camera->SetFocalPoint(0,0,0);
123 vtkSmartPointer<vtkRenderer> renderer =
124 vtkSmartPointer<vtkRenderer>::New();
125 vtkSmartPointer<vtkRenderWindow> renderWindow =
126 vtkSmartPointer<vtkRenderWindow>::New();
127 renderWindow->SetOffScreenRendering( 1 );
128 renderWindow->AddRenderer(renderer);
131 renderer->AddActor(actor);
132 renderer->SetBackground(0,0,0);
133 renderer->SetActiveCamera(camera);
134 renderer->ResetCamera();
136 renderWindow->Render();
139 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
140 vtkSmartPointer<vtkWindowToImageFilter>::New();
141 windowToImageFilter->SetInput(renderWindow);
142 windowToImageFilter->SetMagnification(4);
143 windowToImageFilter->Update();
146 vtkSmartPointer<vtkPNGWriter> writer =
147 vtkSmartPointer<vtkPNGWriter>::New();
148 writer->SetFileName(vOutput.c_str());
149 writer->SetInputConnection(windowToImageFilter->GetOutputPort());
int main(int argc, char *argv[])