Commit 65fea4b5 authored by Chris Cantwell's avatar Chris Cantwell

Cleaned up VtkToPng code.

Added option to fix scalar range.
parent dc13d50b
/* ///////////////////////////////////////////////////////////////////////////////
* VtkToPng.cpp //
* // File VtkToPng.cpp
* Created on: 20 Feb 2013 //
* Author: cc // For more information, please see: http://www.nektar.info
*/ //
// The MIT License
//#include <vtkVersion.h> //
//#if VTK_MAJOR_VERSION == 6 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
//int main(int, char *argv[]) // Department of Aeronautics, Imperial College London (UK), and Scientific
//{ // Computing and Imaging Institute, University of Utah (USA).
// std::cout << argv[0] << " requires VTK 5.10 or earlier. This VTK version is " << vtkVersion::GetVTKVersion() << std::endl; //
// return EXIT_SUCCESS; // License for the specific language governing rights and limitations under
//} // Permission is hereby granted, free of charge, to any person obtaining a
//#else // copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: Render a VTK Unstructured Grid file as a PNG
//
///////////////////////////////////////////////////////////////////////////////
#include <vtkActor.h> #include <vtkActor.h>
#include <vtkRenderWindow.h> #include <vtkRenderWindow.h>
#include <vtkRenderer.h> #include <vtkRenderer.h>
...@@ -34,83 +54,99 @@ using namespace std; ...@@ -34,83 +54,99 @@ using namespace std;
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
// Setup offscreen rendering if (argc != 2 && argc != 4)
vtkSmartPointer<vtkGraphicsFactory> graphics_factory = {
vtkSmartPointer<vtkGraphicsFactory>::New(); cout << "Usage: VtkToPng vtk-file [lower upper]" << endl;
graphics_factory->SetOffScreenOnlyMode( 1); exit(-1);
graphics_factory->SetUseMesaClasses( 1 ); }
vtkSmartPointer<vtkImagingFactory> imaging_factory = string vInput = argv[1];
vtkSmartPointer<vtkImagingFactory>::New(); string vOutput = vInput.substr(0, vInput.find_last_of('.')) + ".png";
imaging_factory->SetUseMesaClasses( 1 );
// Setup offscreen rendering
// Create a poly data reader vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
cout << "Reading file: " << argv[1] << endl; vtkSmartPointer<vtkGraphicsFactory>::New();
vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New(); graphics_factory->SetOffScreenOnlyMode( 1);
reader->SetFileName (argv[1]); graphics_factory->SetUseMesaClasses( 1 );
reader->Update();
vtkDataSet* data = reader->GetOutputAsDataSet(); vtkSmartPointer<vtkImagingFactory> imaging_factory =
vtkSmartPointer<vtkImagingFactory>::New();
data->GetPointData()->SetActiveScalars("u"); imaging_factory->SetUseMesaClasses( 1 );
// Lookup table // Create a poly data reader and retrieve dataset from file
vtkSmartPointer<vtkLookupTable> lookup = vtkSmartPointer<vtkLookupTable>::New(); vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
lookup->SetHueRange(0.0,1.0); reader->SetFileName(vInput.c_str());
lookup->SetSaturationRange(1,1); reader->Update();
lookup->SetTableRange(data->GetScalarRange());
lookup->SetValueRange(1,1); vtkDataSet* data = reader->GetOutputAsDataSet();
lookup->Build(); data->GetPointData()->SetActiveScalars("u");
// Create a mapper and actor double scalar_range[2];
vtkSmartPointer<vtkDataSetMapper> mapper = data->GetScalarRange(scalar_range);
vtkSmartPointer<vtkDataSetMapper>::New(); if (argc == 4)
mapper->SetInput(data); {
mapper->ImmediateModeRenderingOn(); scalar_range[0] = atof(argv[2]);
mapper->ScalarVisibilityOn(); scalar_range[1] = atof(argv[3]);
mapper->SetScalarModeToUsePointData(); }
mapper->SetScalarRange(data->GetScalarRange());
mapper->SetLookupTable(lookup); // Lookup table
vtkSmartPointer<vtkLookupTable> lookup = vtkSmartPointer<vtkLookupTable>::New();
lookup->SetHueRange(0.0,1.0);
vtkSmartPointer<vtkActor> actor = lookup->SetSaturationRange(1,1);
vtkSmartPointer<vtkActor>::New(); lookup->SetTableRange(scalar_range);
actor->SetMapper(mapper); lookup->SetValueRange(1,1);
lookup->Build();
// The usual rendering stuff.
vtkCamera *camera = vtkCamera::New(); // Create a mapper and actor
camera->SetPosition(0.0,-1.0,1.0); vtkSmartPointer<vtkDataSetMapper> mapper =
camera->SetFocalPoint(0,0,0); vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInput(data);
mapper->ImmediateModeRenderingOn();
// A renderer and render window mapper->ScalarVisibilityOn();
vtkSmartPointer<vtkRenderer> renderer = mapper->SetScalarModeToUsePointData();
vtkSmartPointer<vtkRenderer>::New(); mapper->UseLookupTableScalarRangeOn();
vtkSmartPointer<vtkRenderWindow> renderWindow = //mapper->SetScalarRange(data->GetScalarRange());
vtkSmartPointer<vtkRenderWindow>::New(); mapper->SetLookupTable(lookup);
renderWindow->SetOffScreenRendering( 1 );
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkActor> actor =
// Add the actors to the scene vtkSmartPointer<vtkActor>::New();
renderer->AddActor(actor); actor->SetMapper(mapper);
renderer->SetBackground(0,0,0); // Background color white
renderer->SetActiveCamera(camera); // Configure camera position and direction
renderer->ResetCamera(); vtkCamera *camera = vtkCamera::New();
camera->SetPosition(0.0,-1.0,1.0);
renderWindow->Render(); camera->SetFocalPoint(0,0,0);
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = // A renderer and render window
vtkSmartPointer<vtkWindowToImageFilter>::New(); vtkSmartPointer<vtkRenderer> renderer =
windowToImageFilter->SetInput(renderWindow); vtkSmartPointer<vtkRenderer>::New();
windowToImageFilter->SetMagnification(4); vtkSmartPointer<vtkRenderWindow> renderWindow =
windowToImageFilter->Update(); vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetOffScreenRendering( 1 );
cout << "Writing file: " << argv[2] << endl; renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New(); // Add the actors to the scene
writer->SetFileName(argv[2]); renderer->AddActor(actor);
writer->SetInputConnection(windowToImageFilter->GetOutputPort()); renderer->SetBackground(0,0,0); // Background color white
writer->Write(); renderer->SetActiveCamera(camera);
renderer->ResetCamera();
return EXIT_SUCCESS;
renderWindow->Render();
// Create an image of scene
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->SetMagnification(4);
windowToImageFilter->Update();
// Write image to PNG
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New();
writer->SetFileName(vOutput.c_str());
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
return EXIT_SUCCESS;
} }
//#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment