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:
Configure VTK with CMake.
Make a separate build directory:
$ mkdir projects/VTK-build
$ cd projects/VTK-build
Run ccmake
$ ccmake ../VTK
ccmake has a simple terminal based interface to help us customize the VTK build with the desired options. Once you run CMake using ccmake, you will be presented with a list of options that can be modified to customize the VTK build. CMake will be able to set most of these options to reasonable default values.
Hit the 'c' key to configure. (Note: If you are initially seeing a blank interface, hit the 'c' key.)
Keep hitting the 'c' key until the generate option is available (g key), and then hit the 'g' key.
Once VTK has been successfully configured, the interface should quit itself. The last step is to build VTK:
$ cd /path/to/VTK-build
$ make
To install on Windows:
Prerequisite: make sure you have Visual Studio installed.
Type cmake-gui in your terminal to open up Cmake.
Select the appropriate source and build directory (e.g. VTK and VTK-build, respectively), and select the default generator.
(Same as Step 2 for mac and linux machines)
ccmake has a simple terminal based interface to help us customize the VTK build with the desired options. Once you run CMake using ccmake, you will be presented with a list of options that can be modified to customize the VTK build. CMake will be able to set most of these options to reasonable default values.
Click the Configure button. (Note: If you are initially seeing a blank interface, click Configure.)
Keep clicking Configure until the Generate button becomes available, and then click Generate.
Click Open Project. This will open the (large) VTK directory in Visual Studio.
In Visual Studio, go to Project, then Build All.
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
First, create a new directory to store this program.
In the same directory where your HelloWorld program is, create a CMakeLists.txt with the following:
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
Create a build directory in the same level as your program directory and cd into the build directory.
Type "ccmake .." in your terminal and fill in the paths to VTK as needed in the GUI.
Type "make".
Finally to run, type "./<program name>".
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
First, create a new directory to store this program.
In the same directory of your program, create a CMakeLists.txt with the following:
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
Create a build directory in the same level as your program directory and cd into the build directory.
Type "ccmake .." in your terminal and fill in the paths to VTK as needed in the GUI.
Type "make".
Finally to run, type "./<program name> <path to iron-prot.vtk>". It should look something like "./density.app/Contents/MacOS/density ../ironProt.vtk"