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 vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
73 reader->SetFileName(vInput.c_str());
76 vtkDataSet* data = reader->GetOutputAsDataSet();
77 data->GetPointData()->SetActiveScalars(
"u");
79 double scalar_range[2];
80 data->GetScalarRange(scalar_range);
83 scalar_range[0] = atof(argv[2]);
84 scalar_range[1] = atof(argv[3]);
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);
96 vtkSmartPointer<vtkDataSetMapper> mapper =
97 vtkSmartPointer<vtkDataSetMapper>::New();
98 #if VTK_MAJOR_VERSION <= 5
99 mapper->SetInput(data);
101 mapper->SetInputData(data);
103 mapper->ImmediateModeRenderingOn();
104 mapper->ScalarVisibilityOn();
105 mapper->SetScalarModeToUsePointData();
106 mapper->UseLookupTableScalarRangeOn();
108 mapper->SetLookupTable(lookup);
111 vtkSmartPointer<vtkActor> actor =
112 vtkSmartPointer<vtkActor>::New();
113 actor->SetMapper(mapper);
116 vtkCamera *camera = vtkCamera::New();
117 camera->SetPosition(0.0,-1.0,1.0);
118 camera->SetFocalPoint(0,0,0);
121 vtkSmartPointer<vtkRenderer> renderer =
122 vtkSmartPointer<vtkRenderer>::New();
123 vtkSmartPointer<vtkRenderWindow> renderWindow =
124 vtkSmartPointer<vtkRenderWindow>::New();
125 renderWindow->SetOffScreenRendering( 1 );
126 renderWindow->AddRenderer(renderer);
129 renderer->AddActor(actor);
130 renderer->SetBackground(0,0,0);
131 renderer->SetActiveCamera(camera);
132 renderer->ResetCamera();
134 renderWindow->Render();
137 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
138 vtkSmartPointer<vtkWindowToImageFilter>::New();
139 windowToImageFilter->SetInput(renderWindow);
140 windowToImageFilter->SetMagnification(4);
141 windowToImageFilter->Update();
144 vtkSmartPointer<vtkPNGWriter> writer =
145 vtkSmartPointer<vtkPNGWriter>::New();
146 writer->SetFileName(vOutput.c_str());
147 writer->SetInputConnection(windowToImageFilter->GetOutputPort());