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

Cleaned up VtkToPng code.

Added option to fix scalar range.
parent dc13d50b
/*
* VtkToPng.cpp
*
* Created on: 20 Feb 2013
* Author: cc
*/
//#include <vtkVersion.h>
//#if VTK_MAJOR_VERSION == 6
//int main(int, char *argv[])
//{
// std::cout << argv[0] << " requires VTK 5.10 or earlier. This VTK version is " << vtkVersion::GetVTKVersion() << std::endl;
// return EXIT_SUCCESS;
//}
//#else
///////////////////////////////////////////////////////////////////////////////
//
// File VtkToPng.cpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// 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 <vtkRenderWindow.h>
#include <vtkRenderer.h>
......@@ -34,83 +54,99 @@ using namespace std;
int main(int argc, char * argv[])
{
// Setup offscreen rendering
vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
vtkSmartPointer<vtkGraphicsFactory>::New();
graphics_factory->SetOffScreenOnlyMode( 1);
graphics_factory->SetUseMesaClasses( 1 );
vtkSmartPointer<vtkImagingFactory> imaging_factory =
vtkSmartPointer<vtkImagingFactory>::New();
imaging_factory->SetUseMesaClasses( 1 );
// Create a poly data reader
cout << "Reading file: " << argv[1] << endl;
vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
reader->SetFileName (argv[1]);
reader->Update();
vtkDataSet* data = reader->GetOutputAsDataSet();
data->GetPointData()->SetActiveScalars("u");
// Lookup table
vtkSmartPointer<vtkLookupTable> lookup = vtkSmartPointer<vtkLookupTable>::New();
lookup->SetHueRange(0.0,1.0);
lookup->SetSaturationRange(1,1);
lookup->SetTableRange(data->GetScalarRange());
lookup->SetValueRange(1,1);
lookup->Build();
// Create a mapper and actor
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInput(data);
mapper->ImmediateModeRenderingOn();
mapper->ScalarVisibilityOn();
mapper->SetScalarModeToUsePointData();
mapper->SetScalarRange(data->GetScalarRange());
mapper->SetLookupTable(lookup);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// The usual rendering stuff.
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(0.0,-1.0,1.0);
camera->SetFocalPoint(0,0,0);
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetOffScreenRendering( 1 );
renderWindow->AddRenderer(renderer);
// Add the actors to the scene
renderer->AddActor(actor);
renderer->SetBackground(0,0,0); // Background color white
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderWindow->Render();
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->SetMagnification(4);
windowToImageFilter->Update();
cout << "Writing file: " << argv[2] << endl;
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New();
writer->SetFileName(argv[2]);
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
return EXIT_SUCCESS;
if (argc != 2 && argc != 4)
{
cout << "Usage: VtkToPng vtk-file [lower upper]" << endl;
exit(-1);
}
string vInput = argv[1];
string vOutput = vInput.substr(0, vInput.find_last_of('.')) + ".png";
// Setup offscreen rendering
vtkSmartPointer<vtkGraphicsFactory> graphics_factory =
vtkSmartPointer<vtkGraphicsFactory>::New();
graphics_factory->SetOffScreenOnlyMode( 1);
graphics_factory->SetUseMesaClasses( 1 );
vtkSmartPointer<vtkImagingFactory> imaging_factory =
vtkSmartPointer<vtkImagingFactory>::New();
imaging_factory->SetUseMesaClasses( 1 );
// Create a poly data reader and retrieve dataset from file
vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
reader->SetFileName(vInput.c_str());
reader->Update();
vtkDataSet* data = reader->GetOutputAsDataSet();
data->GetPointData()->SetActiveScalars("u");
double scalar_range[2];
data->GetScalarRange(scalar_range);
if (argc == 4)
{
scalar_range[0] = atof(argv[2]);
scalar_range[1] = atof(argv[3]);
}
// Lookup table
vtkSmartPointer<vtkLookupTable> lookup = vtkSmartPointer<vtkLookupTable>::New();
lookup->SetHueRange(0.0,1.0);
lookup->SetSaturationRange(1,1);
lookup->SetTableRange(scalar_range);
lookup->SetValueRange(1,1);
lookup->Build();
// Create a mapper and actor
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInput(data);
mapper->ImmediateModeRenderingOn();
mapper->ScalarVisibilityOn();
mapper->SetScalarModeToUsePointData();
mapper->UseLookupTableScalarRangeOn();
//mapper->SetScalarRange(data->GetScalarRange());
mapper->SetLookupTable(lookup);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Configure camera position and direction
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(0.0,-1.0,1.0);
camera->SetFocalPoint(0,0,0);
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetOffScreenRendering( 1 );
renderWindow->AddRenderer(renderer);
// Add the actors to the scene
renderer->AddActor(actor);
renderer->SetBackground(0,0,0); // Background color white
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
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