VTK

By: Elaine Jiang

Downloading and Installing

Time needed: ≈ 1 hour

To compile VTK, you will first need to install CMake. An installer for CMake is available from http://www.cmake.org/download/. If you already have CMake, you can skip this step. Once you have installed CMake, run the following command in your terminal in order to run CMake:

$ sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install

Next, you'll need to download the VTK source code. You can either download a zip/tar file here or download using git:

Prepare directory for download:

$ mkdir projects

$ cd projects

To download the source code:

$ git clone git://vtk.org/VTK.git

$ cd VTK

To update the code (to get the newest version):

$ git fetch origin

$ git rebase origin/master

To install on a Mac or Linux:

To install on Windows:

Prerequisite: make sure you have Visual Studio installed. 

Tutorial: Hello World

Time needed (as an intermediate user): 30 min

We will now create a simple program with VTK that will add a "Hello World" text object to the scene.

Writing the program

The first thing we'll do is create a renderer, a render window, and a render window interactor. Note that vtkSmartPointer automatically manages the reference it owns, so there is not need to call Delete() when a function returns. 

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();

vtkSmartPointer<vtkRenderWindow> rendererWindow = vtkSmartPointer<vtkRenderWindow>::New();

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();

renderWindow->AddRenderer( renderer );

renderWindowInteractor->SetRenderWindow( renderWindow );

Next, we'll create the text actor and add it to the renderer.

vtkSmartPointer<vtkTextActor> textActor  = vtkSmartPointer<vtkTextActor>::New();

textActor->SetInput ( "Hello world" );

textActor->SetPosition2 ( 10, 40 );

textActor->GetTextProperty()->SetFontSize ( 64 );

textActor->GetTextProperty()->SetColor ( 0.0, 0.0, 0.0 );

renderer->AddActor ( textActor );

Finally, we will start the render loop.

renderWindow->Render();

renderWindowInteractor->Start();

Click here for the full program.

Creating CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

 

PROJECT(<ProjectName>)

 

find_package(VTK REQUIRED)

include(${VTK_USE_FILE})

 

add_executable(<ProjectName> MACOSX_BUNDLE <ProjectName>.cxx)

 

target_link_libraries(<ProjectName> ${VTK_LIBRARIES})

Running the program

Tutorial: Visualizing Volumetric Data

Time needed: 30 min

Next, we'll learn how to read in and visualize volumetric data. We will be using ironProt.vtk, which is sample data located in the data directory of your VTK folder. If you haven't already done so, I recommend starting with the Hello World tutorial above. This tutorial will introduce new functions specific to volume rendering. You can download the stencil code here. 

Writing the program

First, we'll create the renderers, render window, and interactor.

    vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();

    renWin->AddRenderer(ren);

    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();

    iren->SetRenderWindow(renWin);

Next, read the data from the vtk file.

    vtkSmartPointer<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();

    reader->SetFileName("argv[1]");

    reader->Update();    

In VTK, there are two important volume rendering-specific functions -- the opacity transfer function created by a vtkPiecewiseFunction instance, and the color transfer function (created by a vtkColorTransferFunction). The opacity transfer function maps scalar values to opacity values, while the color transfer function maps scalar values to colors. 

Create a transfer function that maps scalar value to opacity.

    vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();

    opacityTransferFunction->AddPoint(20, 0.0);

    opacityTransferFunction->AddPoint(255, 0.2);

Create a transfer function mapping scalar value to color.

    vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New();

    colorTransferFunction->AddRGBPoint(0.0, 0.0, 0.0, 0.0);

    colorTransferFunction->AddRGBPoint(64.0, 1.0, 0.0, 0.0);

    colorTransferFunction->AddRGBPoint(128.0, 0.0, 0.0, 1.0);

    colorTransferFunction->AddRGBPoint(192.0, 0.0, 1.0, 0.0);

    colorTransferFunction->AddRGBPoint(255.0, 0.0, 0.2, 0.0);

Create a volume property instance to add the two functions.

    volumeProperty->SetColor(colorTransferFunction);

    volumeProperty->SetScalarOpacity(opacityTransferFunction);

    volumeProperty->ShadeOn();

    volumeProperty->SetInterpolationTypeToLinear();    // opacity/color values change linearly between  specified scalar values

Create a the volume mapper function that handles the rendering of the data.

    vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> volumeMapper vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();

    volumeMapper->SetInputConnection(reader->GetOutputPort());

The volume holds the mapper and the property and can be used to position/orient the volume.

    vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();

    volume->SetMapper(volumeMapper);

    volume->SetProperty(volumeProperty);

Add volume to renderer, set the background color, and start the render loop. 

    ren->AddVolume(volume);

   vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();

   ren->SetBackground(colors->GetColor3d("Wheat").GetData());

  renWin->SetSize(600, 600); // sets the size of the render window

  renWin->Render();

  iren->Start();


Creating CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

 

PROJECT(<ProjectName>)

 

find_package(VTK REQUIRED)

include(${VTK_USE_FILE})

 

add_executable(<ProjectName> MACOSX_BUNDLE <ProjectName>.cxx)

 

target_link_libraries(<ProjectName> ${VTK_LIBRARIES})

Running the program