VtkToPng.cpp 5.65 KB
Newer Older
Chris Cantwell's avatar
Chris Cantwell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
///////////////////////////////////////////////////////////////////////////////
//
// 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
//
///////////////////////////////////////////////////////////////////////////////

Dave Moxey's avatar
Dave Moxey committed
36
#include <LibUtilities/BasicUtils/VtkUtil.hpp>
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
#include <vtkGraphicsFactory.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkDataSetMapper.h>
#include <vtkLookupTable.h>
#include <vtkPointData.h>
#include <vtkCamera.h>

#include <iostream>
using namespace std;

int main(int argc, char * argv[])
{
Chris Cantwell's avatar
Chris Cantwell committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    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 );

    // Create a poly data reader and retrieve dataset from file
73 74
    vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
            vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
Chris Cantwell's avatar
Chris Cantwell committed
75 76 77
    reader->SetFileName(vInput.c_str());
    reader->Update();

78 79
    vtkSmartPointer<vtkDataSet> data = 
            vtkSmartPointer<vtkDataSet>(reader->GetOutputAsDataSet());
Chris Cantwell's avatar
Chris Cantwell committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    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();
101
#if VTK_MAJOR_VERSION <= 5
Chris Cantwell's avatar
Chris Cantwell committed
102
    mapper->SetInput(data);
103 104 105
#else
    mapper->SetInputData(data);
#endif
106 107 108

#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
    //deprecated as of vtk 8.1
Chris Cantwell's avatar
Chris Cantwell committed
109
    mapper->ImmediateModeRenderingOn();
110 111
#endif
    
Chris Cantwell's avatar
Chris Cantwell committed
112 113 114 115 116 117 118 119 120 121 122 123
    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
124
    vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
Chris Cantwell's avatar
Chris Cantwell committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    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);
148 149 150

#if VTK_MAJOR_VERSION < 8 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION <= 1)
    //deprecated as of vtk 8.1
151 152
    windowToImageFilter->SetMagnification(4);
#else
153
    windowToImageFilter->SetScale(4, 4);
154
#endif
Chris Cantwell's avatar
Chris Cantwell committed
155 156 157 158 159 160 161 162 163 164
    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;
165
}