448 lines (324 with data), 10.9 kB
#LyX 1.3 created this file. For more info see http://www.lyx.org/
\lyxformat 221
\textclass amsbook
\language english
\inputencoding auto
\fontscheme default
\graphics default
\paperfontsize default
\papersize Default
\paperpackage a4
\use_geometry 0
\use_amsmath 0
\use_natbib 0
\use_numerical_citations 0
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\quotes_times 2
\papercolumns 1
\papersides 1
\paperpagestyle default
\layout Chapter
3D visualization with VTK
\begin_inset OptArg
collapsed false
\layout Standard
VTK
\end_inset
\layout Standard
The Visualization Toolkit is a library for creating, analyzing, and visualizing
3D data, and is a high level library that sits on top of a low-level library
like OpenGL.
Because 3D interaction and visualization is so computationally intensive,
video cards come with special processors to do computations for 3D geometry
at the hardware level, and low-level software libraries like OpenGL are
used to communicate with the video card.
However, low level libraries are just that, and do not support the higher
level geometrical concepts that describe the problem at hand; eg OpenGL
has no concept of a cube -- you can create a cube by making six rectangular
faces placed in the proper positions -- but you can't say "draw me a cube"
\begin_inset LatexCommand \cite{Redbook}
\end_inset
.
\layout Standard
That is where high level libraries like VTK come in.
VTK is an enormously powerful and complex visualization library written
in C++ that can drive low level 3D libraries such as Mesa, a pure software
OpenGL implementation, and OpenGL itself.
It relies heavily on principles of object oriented design, and can be plugged
into all widely used graphical user interfaces: Cocoa, Win32, Tkinter,
WX, GTK, FLTK and more.
Wrappers of the C++ code exist for Python, Java and TCL.
VTK provides python users the ability to do cross-platform, hardware accelerate
d rendering from the comfort of the python interpreter, all with better
quality than money can buy.
\layout Standard
VTK is a complex library -- with over 1000 classes and a deep inheritance
scheme, it has an API rivaling Java's in complexity.
The system was initially developed by Bill Schroeder and Will Lorenson,
who designed the library after working in visualization and animation for
10 years.
The initial design identified only 25 core classes, and four software professio
nals spent 10 months designing the library before touching a keyboard! These
original 25 classes still exist in the library today
\begin_inset LatexCommand \cite{SchroederEtal2002}
\end_inset
.
\layout Standard
\begin_inset Float figure
wide false
collapsed false
\layout Standard
\align center
\begin_inset Graphics
filename fig/vtk_pbrain.png
lyxscale 50
width 4in
\end_inset
\layout Caption
\begin_inset LatexCommand \label{fig:vtkpbrain}
\end_inset
Screenshots from pbrain, a python application for localizing subdural electrodes
and mapping statistical quantities from the ECoG onto their spatial coordinates
which utilizes VTK for visualization.
\end_inset
\layout Section
Hello world in VTK
\begin_inset OptArg
collapsed false
\layout Standard
Hello VTK
\end_inset
\layout Standard
We'll start with a minimal example that creates and displays a cube.
The example below creates a cube.
The src for this example is
\family typewriter
examples/vtk_hello.py
\family default
and the output is shon in Figure
\family typewriter
\begin_inset LatexCommand \ref{fig:vtkcube}
\end_inset
.
\layout Standard
\begin_inset ERT
status Collapsed
\layout Standard
\backslash
lstinputlisting[caption={A minimal VTK example to display a cube}]{examples/vtk_hello.py}
\end_inset
\layout Standard
\begin_inset Float figure
wide false
collapsed false
\layout Standard
\align center
\begin_inset Graphics
filename fig/vtkcube.png
lyxscale 50
width 3in
\end_inset
\layout Caption
\begin_inset LatexCommand \label{fig:vtkcube}
\end_inset
A Cube, brought to you by VTK
\end_inset
\layout Exercise
\shape italic
Learning to fly
\shape default
.
It takes a little while to get used to the navigation controls in VTK.
Every mouse button: left middle and right controls navigation, and there
are multiple modes of interaction: camera versus actor and joystick versus
trackball.
For example, in camera mode, the left mouse button rotates the camera around
its focal point, the middle mouse button pans the camera, and the right
mouse button zooms.
These controls have different meanings in actor mode (toggle camera and
actor mode by pressing 'c' and toggle joystick versus trackball mode by
pressing 'j'.
Visit the
\family typewriter
vtkInteractorStyle
\family default
page and read more about the ways you can interact with the data, and experimen
t with the differnent controls.
Much like a video game, it takes a while before you are comfortable at
the controls: we call this process
\shape italic
learning to fly
\shape default
.
\begin_inset Foot
collapsed true
\layout Standard
http://www.vtk.org/doc/nightly/html/classvtkInteractorStyle.html
\end_inset
\layout Standard
\layout Exercise
\shape italic
Make a translucent sphere.
\shape default
Modify
\family typewriter
examples/vtk_hello.py
\family default
to add a translucent blue sphere to the to scene.
\shape italic
Translucency hint
\shape default
: translucency is an actor property -- as you see in the code above, you
call
\family typewriter
actor.GetProperty()
\family default
to get the actor property reference.
Visit the VTK class docs web page and click on the vtk class
\family typewriter
vtkActor
\family default
and find the method
\family typewriter
GetProperty()
\family default
.
If you click on the return value of this method (
\family typewriter
vtkProperty
\family default
) you'll be taken to the right page.
Read over the available methods to determine the appropriate one for setting
the translucency and note all the other properties that you can control.
\layout Section
Working with medical image data
\begin_inset OptArg
collapsed false
\layout Standard
Medical images
\end_inset
\layout Standard
VTK has very strong support for medical image data, including volume data
readers, DICOM readers, surface contour filters, plane slice widgets, and
so on.
In addition, the Insight Toolkit (ITK), which was developed by a consortium
of universities and private companties, provides a large number state-of-the-ar
t image segmentation and registration algorithms
\begin_inset LatexCommand \cite{IbanezSchroeder2003}
\end_inset
.
Like VTK, ITK is a large, sophisticated C++ library which comes with wrappers
for a number of interpreted languages: Java, Tcl and Python.
The first step in the pipeline to work with image/volume data is to create
a reader, and VTK ships with readers and writers for all popular formats
for medical image data, eg
\family typewriter
vtkDICOMImageReader
\family default
, to read the ubiquitous DICOM format, and
\family typewriter
vtkVolumeReader
\family default
, to work with raw binary images.
Once the image is loaded, you can pass it to various filters or viewers.
In the example below, we create an MRI viewer shown in Figure
\begin_inset LatexCommand \ref{fig:vtkslicer}
\end_inset
.
\layout Standard
\begin_inset ERT
status Collapsed
\layout Standard
\backslash
lstinputlisting[caption={A simple 3D image viewer in VTK }]{examples/vtk_slice_viewer.py}
\end_inset
\layout Standard
\begin_inset Float figure
wide false
collapsed false
\layout Standard
\align center
\begin_inset Graphics
filename fig/vtk_slicer.png
lyxscale 50
width 3in
\end_inset
\layout Caption
\begin_inset LatexCommand \label{fig:vtkslicer}
\end_inset
A simple slice viewer, in 30 lines of python
\end_inset
\layout Standard
A common need in medical image data analysis and visualization is segmentation
of different anatomical regions.
VTK provides support for this with isosurface contouring via the patented
marching cubes algorithm.
Although marching cubes is patented, it is free for noncommerical use,
though it is unclear whether use in an academic research setting qualifies
as noncommercial.
\begin_inset Foot
collapsed true
\layout Standard
http://www.imakenews.com/bakerbotts/e_article000166656.cfm
\end_inset
However the patent is due to expire in the summer of 2005, so this issue
will soon be moot.
The marching cube algorithm takes a seed value which is an image intensity
value, and generates one or more 2D level surfaces from the 3D volume data.
For more sophisticated image segmentation routines, see the Insight Segmentatio
n and Registration Toolkit, referenced above.
In the example code below, we will read the image data set as we did in
the slice viewer application above, and feed this data to the marching
cubes algorithm.
The output will be stored in a VTK data file, in this case a plain text
file which stores vertex, edge and polygon information, and shown in Figure
\begin_inset LatexCommand \ref{fig:vtkmarching}
\end_inset
.
These files can be read and the data in them manipulated, analyzed and
visualized by external tools, eg MayaVi.
\layout Standard
\begin_inset ERT
status Open
\layout Standard
\backslash
lstinputlisting[caption={Constructing surfaces from MRI data}]{examples/vtk_marching_cubes.py}
\end_inset
\layout Standard
\begin_inset Float figure
wide false
collapsed false
\layout Standard
\align center
\begin_inset Graphics
filename fig/vtk_cortex.png
lyxscale 50
width 3in
\end_inset
\layout Caption
\begin_inset LatexCommand \label{fig:vtkmarching}
\end_inset
The cortical isosurface generated by a simple intensity based marching cubes
application (40 lines of python listed above).
More sophisticated image segmentation is available in the Insight Toolkit.
\end_inset
\layout Standard
The possibilities for visualizing and analyzing 2D and 3D data with VTK
and ITK are almost endless, and a person could spend many years mastering
these libraries.
Fortunately, there are many examples and several fine textbooks to speed
you on your way.
This introduction has only scratched the surface, using just a few of the
1000 or so classes available.
Because the library is so large and daunting, and because the typical scientist
doesn't have several years to master it, it is fortunate that MayaVi, a
python application written by Prabhu Ramachandran, wraps this complexity
into an easy to use graphical user interface.
MayaVi is under active development, and will be a plugin component of the
enthought envisage application framework for scientific computing in python.
\the_end