Anda di halaman 1dari 6

Introduction to Programming in VTK

October 31, 2000

Quick Overview: What is VTK?


an enormous library of C++ classes for interactive visualization of 3D data
What does VTK do well?
creation and manipulation of geometrical data
efficent image processing (parallel implementation)
interactive data visualization
What does VTK not do well?
memory hog (not as bad as MATLAB!)
producing graphs (use Excel or MATLAB)
linear algebra (use MATLAB or Python)
Another Caveat:
VTK is rumoured to have a steep learning curve
(which is why we have this course to help you out)

VTK Resources at Robarts


Hua and Rick
My Puny VTK Web Page (with VTK Documentation)
All SGIs in the lab have VTK installed (except the purple footstools)
Most computer in the lab can run VTK, if installed
VTK Books and Other Documentation
Only two books: VTK Users Guide and The Visualization Toolkit 2nd Ed.
Main web site: http://www.kitware.com/vtk.html
Python Documentation
Best books: Learning Python and Python Essential Reference
Main web site: http://www.python.org

The VTK Pipeline


You write a VTK program by creating objects and joining them together
Every VTK program has at lease one pipeline

Cone.py Pipeline Diagram (type "python Cone.py" to run)


Either reads the data from a
file or creates the data from
scratch.

from vtkpython import *

Moves the data from VTK


into OpenGL.

coneMapper = vtkPolyDataMapper()
coneMapper.SetInput(cone.GetOutput())

For setting colors, surface


properties, and the position
of the object.
The rectangle of the
computer screen that
VTK draws into.
The window, including title
bar and decorations.
Allows the mouse to be used
to interact with the data.

cone = vtkConeSource()
cone.SetResolution(10)

coneActor = vtkActor()
coneActor.SetMapper(coneMapper)
ren = vtkRenderer()
ren.AddActor(coneActor)
renWin = vtkRenderWindow()
renWin.SetWindowName("Cone")
renWin.SetSize(300,300)
renWin.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Initialize()
iren.Start()

Image.py Pipeline Diagram (type "python Image.py" to run)


from vtkpython import *

Reads the data from a file.

reader = vtkBMPReader()
reader.SetDataSpacing(1.0, 1.0, 2.0)
reader.SetDataOrigin(0.0, 0.0, 0.0)
reader.SetFileName("image.bmp")

Processes the image


(enlarge the image, in
this case).

resize = vtkImageReslice()
resize.SetInput(reader.GetOutput())
resize.SetOutputSpacing(0.25, 0.25, 2)
resize.SetInterpolationModeToCubic()

Viewer (has an ImageMapper,


Actor2D, and ImageWindow
inside).

viewer = vtkImageViewer()
viewer.SetInput(resize.GetOutput())
viewer.SetColorWindow(255)
viewer.SetColorLevel(127.5)
viewer.Render()

The VTK Pipeline


You write a VTK program by creating objects and joining them together
The primary pipeline object types are as follows
Methods vary from source to source, e.g.
vtkConeSource::SetHeight(5.0)
vtkSphereSource::SetRadius(2.0)
Methods vary from filter to filter, e.g.
vtkImageMapToColors::SetLookupTable(table)
vtkMarchingCubes::SetValue(0,1043)
vtkTransformPolyDataFilter::SetTransform(transform)
All Mappers have a Render() method.
The vtkImageMapper has two important methods:
vtkImageMapper::SetColorWindow(255.5)
vtkImageMapper::SetColorLevel(127.5)
All Readers have a SetFileName("filename") method

All Writers have a SetFileName("filename") method


and a Write() method.

Pipeline Execution (When do the computations actually occur)


The pipeline doesnt compute anything until:
the Write() method of the Writer object at the end of the pipeline is called
the Render() method of a Window associated with the pipeline is called
you drag the mouse in an Interactor associated with the pipeline

A VTK Filter can never "force" any changes to occur further down the pipeline.
It has to wait until it is requested to Update, then it can only do the following:
request an Update of its Input data
create new data (the Output) from the Input data

Data does not "flow" along the pipeline. Each filter has its own copy of data,
which it creates from its Input data.

If you have one Source and two Filters, you will have three copies of the data
(yup, VTK can be a memory hog). You can force a filter to delete its data once
the next filter is done with it.

Homework:
Copy these files to your home directory
(this page is at http://osric.irus.rri.on.ca/vtk/vtkcourse/ )
Cone.py
Image.py
WindowLevelInterface.py
image.bmp (use shift-click to download)
1. Run Cone.py and Image.py to ensure that they work
2. Edit Cone.py and change the cone resolution to 100. Run Cone.py again.
3. Look at the on-line documentation for vtkProperty and vtkActor, and change the
Cone.py so that the cone is red.
4. Comment out the last line with a "#", i.e. #iren.Start(), and think about what
happens. Can you explain it?
5. In Image.py, change reader.SetDataSpacing to (0.5, 0.5, 0.5) and explain what
happens and why.
You can also look at this more complicated example: Slice.py

Anda mungkin juga menyukai