VTK Home

Kitware Home / Newsletter

public.kitware.com

What is VTK?

VTK In Use

Buy The Books

Download

Mailing List

Quality Dashboard

Bug Tracker

Search

Manual: 5.0, 5.x (nightly)

Manual: 4.0.2, 4.2.1

VTK FAQ

VTK Wiki

VTK Technical Documents

How To Contribute

Coding Standards

Copyright Information

Example Code

Sebastien's VTK Links

Other VTK Links

 

 

Example Code

Creating graphics and visualization applications is fairly simple with VTK. It consists of two basic parts. First, you construct a data pipeline (i.e., 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 (i.e., a visualization network). Your applications must first create a network, and then EXECUTE it. This typically requires a Render() (sent to the rendering 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 get data.)

Constructing a pipeline means connecting sources (ingest 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


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

C++

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

void 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();
}

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();
  }
}


Now to create a visualization pipeline the steps are the same. The difference is that inbetween step #3 (i.e., data creation) and step #4 (i.e., mapping the data to the rendering system) we insert a dataflow network. The network consists of a buch of filters connected together. The filters process various data types (we call them datasets) and eventually convert them to a form we can render.

Visualizing a Quadric Function


In the next example we'll visualize a complicated equation for a
quadric. 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 network will consist of three filters. The first, vtkSampleFunction, samples the quadric equation. The second, 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"

void 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();
}


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 information and examples you can refer to the text. There are
many other examples included in the source distribution. Enjoy!