Example Code

Creating graphics and visualization applications is fairly simple with VTK. It consists of two basic steps. First, you construct a data pipeline (also called a visualization network) to process data. Second, you create the necessary graphics objects to display the data.

SPECIAL NOTE: the VTK architecture is based on a demand-driven pipeline architecture. Your applications must first create a pipeline and then EXECUTE it. This typically requires a Render() (sent to the render window) or an Update() (sent to a filter in the pipeline). Just instantiating the objects and hooking them together won't do anything - YOU HAVE TO REQUEST DATA to receive it.

Constructing a pipeline means connecting sources (load or create data), filters (process data), and mappers (map through lookup table and into graphics library). Many different types of sources, filters, and mappers are available, depending on the type of data you're processing and the desired functionality. Type checking (either at compile-time in C++ or run-time in Tcl) controls which filters can be connected together.

To create the graphics objects, you typically

create a rendering window to render into
create a renderer
create an interactor (allows you to interact with the graphics)
create one or more actors (each of which is linked to a mapper)
render

You may also wish to transform objects; set material properties; and/or create lights, cameras, texture maps, and lookup tables, and various other graphics objects. The next examples shows how this works for a sphere.

Creating a Sphere

sphere


VTK supplies a variety of geometric primitives like spheres, cones, lines, planes, cylinders, cubes, and so on. In this example we create a sphere which is represented by mesh of triangles with surface normals at the vertices.

C++

#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"

int main ()
{

  // create sphere geometry
  vtkSphereSource *sphere = vtkSphereSource::New();
  sphere->SetRadius(1.0);
  sphere->SetThetaResolution(18);
  sphere->SetPhiResolution(18);

  // map to graphics library
  vtkPolyDataMapper *map = vtkPolyDataMapper::New();
  map->SetInput(sphere->GetOutput());

  // actor coordinates geometry, properties, transformation
  vtkActor *aSphere = vtkActor::New();
  aSphere->SetMapper(map);
  aSphere->GetProperty()->SetColor(0,0,1); // sphere color blue

  // a renderer and render window
  vtkRenderer *ren1 = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer(ren1);

  // an interactor
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  // add the actor to the scene
  ren1->AddActor(aSphere);
  ren1->SetBackground(1,1,1); // Background color white

  // render an image (lights and cameras are created automatically)
  renWin->Render();

  // begin mouse interaction
   iren->Start();

  // release memory and return
  sphere->Delete();
  map->Delete();
  aSphere->Delete();
  ren1->Delete();
  renWin->Delete();
  iren->Delete();
  return EXIT_SUCCESS;
}

Tcl

#
# First we include the VTK Tcl packages which will make available
# all of the VTK commands from Tcl. The vtkinteraction package defines
# a simple Tcl/Tk interactor widget.
#
package require vtk
package require vtkinteraction

# create sphere geometry
vtkSphereSource sphere
sphere SetRadius 1.0
sphere SetThetaResolution 18
sphere SetPhiResolution 18

# map to graphics library
vtkPolyDataMapper map;
map SetInput [sphere GetOutput]

# actor coordinates geometry, properties, transformation
vtkActor aSphere
aSphere SetMapper map
[aSphere GetProperty] SetColor 0 0 1; # blue

# create a window to render into
vtkRenderWindow renWin
vtkRenderer ren1
renWin AddRenderer ren1

# create an interactor
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin

# add the sphere
ren1 AddActor aSphere
ren1 SetBackground 1 1 1;# Background color white

# Render an image; since no lights/cameras specified, created automatically
renWin Render

# prevent the tk window from showing up then start the event loop
wm withdraw .

Java

import vtk.*;

public class test {

  // in the static contructor we load in the native code
  // The libraries must be in your path to work
  static {
    System.loadLibrary("vtkCommonJava");
    System.loadLibrary("vtkFilteringJava");
    System.loadLibrary("vtkIOJava");
    System.loadLibrary("vtkImagingJava");
    System.loadLibrary("vtkGraphicsJava");
    System.loadLibrary("vtkRenderingJava");
  }

  // the main function
  public static void main (String[] args)
  {
        // create sphere geometry
        vtkSphereSource sphere = new vtkSphereSource();
        sphere.SetRadius(1.0);
        sphere.SetThetaResolution(18);
        sphere.SetPhiResolution(18);

        // map to graphics objects
        vtkPolyDataMapper map = new vtkPolyDataMapper();
        map.SetInput(sphere.GetOutput());

        // actor coordinates geometry, properties, transformation
        vtkActor aSphere = new vtkActor();
        aSphere.SetMapper(map);
        aSphere.GetProperty().SetColor(0,0,1); // color blue

        // a renderer for the data
        vtkRenderer ren1 = new vtkRenderer();
        ren1.AddActor(aSphere);
        ren1.SetBackground(1,1,1); // background color white

        // a render window to display the contents
        vtkRenderWindow renWin = new vtkRenderWindow();
        renWin.AddRenderer(ren1);
        renWin.SetSize(300,300);

        // an interactor to allow control of the objects
        vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
        iren.SetRenderWindow(renWin);

        // trigger the rendering and start the interaction renWin.Render();
        iren.Start();
  }
}

VTK supplies a wide variety of filters that you can use to process data once it is loaded or created. Filters can be chained together so that the input of one filter is the output of another. The final filter of the pipeline is typically connected to a mapper which converts the data into a form that we can render.

Visualizing a Quadric Function

quadric

In the next example we'll visualize an equation for a quadric function. The equation is

F(x,y,z) = 0.5*x^2 + 1.0*y^2 + 0.2*z^2 + 0.1*y*z + 0.2*y

To visualize it we sample it over a regular array of points (i.e., a volume or structured point dataset), and then create iso-contours of the quadric F(x,y,z) = c, where c is a constant (i.e., the contour value).

Our visualization pipeline consists of three filters. The first, vtkSampleFunction, samples the quadric equation over a structured point set. The second filter, vtkContourFilter, creates iso-contours for 0, 1, 2, or 3D data. The last filter, vtkPolyMapper, maps the data to the graphics system as in our previous example. We also create an outline around the data for context.

C++

#include "vtkQuadric.h"
#include "vtkSampleFunction.h"
#include "vtkContourFilter.h"
#include "vtkOutlineFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageData.h"
int main ()
{
  // -- create the quadric function object --

  // create the quadric function definition
  vtkQuadric *quadric = vtkQuadric::New();
  quadric->SetCoefficients(.5,1,.2,0,.1,0,0,.2,0,0);

  // sample the quadric function
  vtkSampleFunction *sample = vtkSampleFunction::New();
  sample->SetSampleDimensions(50,50,50);
  sample->SetImplicitFunction(quadric);

  // Create five surfaces F(x,y,z) = constant between range specified
  vtkContourFilter *contours = vtkContourFilter::New();
  contours->SetInput(sample->GetOutput());
  contours->GenerateValues(5, 0.0, 1.2);

  // map the contours to graphical primitives
  vtkPolyDataMapper *contMapper = vtkPolyDataMapper::New();
  contMapper->SetInput(contours->GetOutput());
  contMapper->SetScalarRange(0.0, 1.2);

  // create an actor for the contours
  vtkActor *contActor = vtkActor::New();
  contActor->SetMapper(contMapper);

  // -- create a box around the function to indicate the sampling volume --

  // create outline
  vtkOutlineFilter *outline = vtkOutlineFilter::New();
  outline->SetInput(sample->GetOutput());

  // map it to graphics primitives
  vtkPolyDataMapper *outlineMapper = vtkPolyDataMapper::New();
  outlineMapper->SetInput(outline->GetOutput());

  // create an actor for it
  vtkActor *outlineActor = vtkActor::New();
  outlineActor->SetMapper(outlineMapper);
  outlineActor->GetProperty()->SetColor(0,0,0);

  // -- render both of the objects --

  // a renderer and render window
  vtkRenderer *ren1 = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer(ren1);

  // an interactor
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

// add the actors to the scene
  ren1->AddActor(contActor);
  ren1->AddActor(outlineActor);
  ren1->SetBackground(1,1,1); // Background color white

  // render an image (lights and cameras are created automatically)
  renWin->Render();

  // begin mouse interaction
  iren->Start();

  // release memory and return
  quadric->Delete();
  sample->Delete();
  contours->Delete();
  contMapper->Delete();
  contActor->Delete();
  outline->Delete();
  outlineMapper->Delete();
  outlineActor->Delete();
  ren1->Delete();
  renWin->Delete();
  iren->Delete();
  return EXIT_SUCCESS;
}

Tcl

# This example demonstrates the use of the contour filter, and the use of
# the vtkSampleFunction to generate a volume of data samples from an
# implicit function.

#
# First we include the VTK Tcl packages which will make available
# all of the VTK commands from Tcl. The vtkinteraction package defines
# a simple Tcl/Tk interactor widget.
#
package require vtk
package require vtkinteraction

# VTK supports implicit functions of the form f(x,y,z)=constant. These
# functions can represent things spheres, cones, etc. Here we use a
# general form for a quadric to create an elliptical data field.
vtkQuadric quadric
  quadric SetCoefficients .5 1 .2 0 .1 0 0 .2 0 0

# vtkSampleFunction samples an implicit function over the x-y-z range
# specified (here it defaults to -1,1 in the x,y,z directions).
vtkSampleFunction sample
  sample SetSampleDimensions 30 30 30
  sample SetImplicitFunction quadric

# Create five surfaces F(x,y,z) = constant between range specified. The
# GenerateValues() method creates n isocontour values between the range
# specified.
vtkContourFilter contours
  contours SetInput [sample GetOutput]
  contours GenerateValues 5 0.0 1.2

vtkPolyDataMapper contMapper
  contMapper SetInput [contours GetOutput]
  contMapper SetScalarRange 0.0 1.2

vtkActor contActor
  contActor SetMapper contMapper

# We'll put a simple outline around the data.
vtkOutlineFilter outline
  outline SetInput [sample GetOutput]

vtkPolyDataMapper outlineMapper
  outlineMapper SetInput [outline GetOutput]

vtkActor outlineActor
  outlineActor SetMapper outlineMapper
  eval [outlineActor GetProperty] SetColor 0 0 0

# The usual rendering stuff.
vtkRenderer ren1
vtkRenderWindow renWin
  renWin AddRenderer ren1
vtkRenderWindowInteractor iren
  iren SetRenderWindow renWin

ren1 SetBackground 1 1 1
ren1 AddActor contActor
ren1 AddActor outlineActor

iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize

wm withdraw .

For more examples, please visit our wiki. You can also find more examples in the Examples/ directory of the VTK source code.
We hope you enjoy using VTK!