VTK Visualization Toolkit Introduction
Carlos A. Vinhais
cvinhais@gmail.com Department of Physics ISEP School of Engineering Polytechnic Institute of Porto Porto, PORTUGAL
Outline
VTK Introduction
Resources Installation
Computer Hands-On
Build VTK with Cmake Test VTK Helloworld Cone1.cxx Cone2.cxx Cone3.cxx SingleScreenShot
VTK Architecture
Low level object model VTK file format Rendering Engine Visualization pipeline Minimal Example VTK Programming VTK Interactors
Learn by Examples
3D Graphics Visualization Techniques Imaging
Carlos Vinhais
VTK - Visualization Toolkit
VTK - Visualization Toolkit Introduction
Free open source software for 3D computer graphics, image processing and visualization Consists of a C++ class library
VTK classes implemented with .h and .cxx file Several interpreted interface layers, including Python, Tcl/Tk, and Java
Supports a wide variety of visualization algorithms
including scalar, vector, tensor, texture, and volumetric
Advanced modeling techniques
implicit modeling, polygon reduction, mesh smoothing, cutting, contouring and Delaunay triangulation
Design and implementation influenced by object-oriented principles
Carlos Vinhais VTK - Visualization Toolkit 3
VTK - Visualization Toolkit Resources
Source distribution (source and binaries)
www.vtk.org, Kitware, Inc. Distribution comes with many examples
Documentation
Online help, HTML based http://www.vtk.org/doc/release/5.8/html/
Companion Text Books
The Visualization Toolkit The VTK Users Guide www.kitware.com
Mailing lists, Links, FAQ, Search
Carlos Vinhais VTK - Visualization Toolkit 4
VTK - Visualization Toolkit Installation
1. Choose OS and install compiler/IDE
Tested on Windows 7 MS Visual C++ 2010 Express Edition
2.
CMake installation required
www.cmake.org (cmake-2.8.7-win32x86.exe)
3. 4. 5. 6.
Get VTK source code
vtk-5.8.0.zip
Run Cmake to configure and generate the VTK project Compile VTK libraries Test VTK with the exrecises
VTK - Visualization Toolkit 5
Cmake used to generate projects, makefiles or workspaces For different compilers and OS. Cmake is Cross platform.
Carlos Vinhais
VTK Architecture Object Model
Dataset types found in VTK
Image data Rectilinear grid Structured grid Unstructured points Polygonal Data Unstructured grid
Data objects have geometric and topological structure (points and cells) Cells are topological arrangements of points
Carlos Vinhais VTK - Visualization Toolkit 6
VTK Architecture Object Model
Reference Counting
vtkObjectBase * obj = vtkExampleClass::New(); otheObject -> SetExample(obj); Obj -> Delete();
Smart Pointers
Class template vtkSmartPointer<>
vtkSmartPointer<vtkObjectBase> obj = vtkSmartPointer<vtkExampleClass> ::New(); otheObject -> SetExample(obj);
Carlos Vinhais
VTK - Visualization Toolkit
VTK Architecture Object Model
Associated with the points and cells of a dataset
scalar vector normal texture coordinate tensor field data
Carlos Vinhais
VTK - Visualization Toolkit
VTK File Format
STRUCTURED POINTS (e.g. volume leon22.vtk)
# vtk DataFile Version 3.0 VTK File Generated by Insight Segmentation and Registration Toolkit (ITK) BINARY DATASET STRUCTURED_POINTS DIMENSIONS 512 512 64 SPACING 0.703125 0.703125 5 ORIGIN -173.3 -180 -269.75 POINT_DATA 16777216 SCALARS scalars short 1 LOOKUP_TABLE default ... ...
POLYDATA - unstructured points (e.g. teste_02_001.vtk)
# vtk DataFile Version 3.0 vtk output ASCII DATASET POLYDATA POINTS 77268 float 75.9892 41.9058 932.349 75.7524 41.8747 932.064 ... VERTICES 77268 154536 1 0 1 1 ...
Carlos Vinhais
VTK - Visualization Toolkit
VTK Architecture Rendering Engine
A VTK scene consists of: vtkRenderWindowInteractor
window interaction
vtkRenderWindow
contains the final image
vtkRenderer
draws into the render window
vtkActor
combines properties/geometry
vtkMapper
represents geometry
vtkLights
illuminate actors
vtkProp, vtkProp3D
Superclasses
vtkCamera
renders the scene
vtkProperty
vtkTransform
position actors
VTK - Visualization Toolkit 10
Carlos Vinhais
VTK Architecture Visualization Pipeline
Visualization pipeline transforms information into graphical data
Uses a data flow approach Two basic types of objects:
vtkDataObject and vtkProcessObject
Pipeline topology based on filter I/O
Filters operate on data objects to produce new data objects
thisFilter -> setInput( thatFilter->getOutput() );
Pipeline execution
Visualization pipelines use lazy evaluation Only executes when data is required for computation
Carlos Vinhais VTK - Visualization Toolkit 11
VTK Architecture Visualization Pipeline
data objects combined with process object to create the visualization pipeline
Pipeline execution
Carlos Vinhais
VTK - Visualization Toolkit
12
VTK Architecture Visualization Pipeline
Types of algorithms
multiplicity of input and output
Carlos Vinhais
VTK - Visualization Toolkit
13
Minimal Example Cone1
Basic setup for VTK programs
Programming implementation of basic VTK exercises:
vtkActors
Combine object properties, geometries and orientation in virtual coordinates
Data source
Procedural data, cone, sphere, cylinder, etc. Or data read from a file
vtkRenderer
Coordinates lights, cameras and actors to create an image
vtkMapper and vtkLookupTable
Transform and render geometry Interface between the viz pipeline and the graphics model vtkScalarToColorsmaps data values to color
vtkRenderWindow
Manages the rendering process on the render window(s) on display device
vtkRenderWindowInteractor
Connection between the operating system and the VTK rendering engine
Carlos Vinhais
VTK - Visualization Toolkit
14
Minimal Example Cone1
// // // // C++ The basic setup of: source->mapper->actor->renderer->renderwindow is typical of most VTK programs. vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 ); vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); coneMapper -> SetInput(cone->GetOutput()); vtkActor *coneActor = vtkActor::New(); coneActor -> SetMapper( coneMapper ); vtkRenderer *renderer = vtkRenderer::New(); renderer -> AddActor( coneActor ); renderer -> SetBackground(0.1, 0.2, 0.4); vtkRenderWindow *renderWindow = vtkRenderWindow::New(); renderWindow -> AddRenderer( renderer ); renderWindow -> SetSize( 300, 300 ); vtkRenderWindowInteractor *renderWindowInteractor = vtkRenderWindowInteractor::New(); renderWindowInteractor -> SetRenderWindow( renderWindow ); renderWindow -> Render(); renderWindowInteractor -> Start();
Carlos Vinhais
VTK - Visualization Toolkit
15
VTK Programming
Conversions between languages relatively straightforward
Class names and method names remain the same Implementation details change (syntax) GUI details change
Example
C++ Python Tcl Java
ren1->GetActiveCamera()->Azimuth( 1 ); ren1.GetActiveCamera().Azimuth( 1 ) [ren1 GetActiveCamera] Azimuth 1 ren1.GetActiveCamera().Azimuth( 1 );
Carlos Vinhais
VTK - Visualization Toolkit
16
VTK Programming
// // // // C++ The basic setup of: source->mapper->actor->renderer->renderwindow is typical of most VTK programs. # # # # Python The basic setup of: source->mapper->actor->renderer->renderwindow is typical of most VTK programs. vtkConeSource *cone = vtkConeSource::New(); cone->SetHeight( 3.0 ); cone->SetRadius( 1.0 ); cone->SetResolution( 10 ); vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); coneMapper -> SetInput(cone->GetOutput()); vtkActor *coneActor = vtkActor::New(); coneActor -> SetMapper( coneMapper ); vtkRenderer *renderer = vtkRenderer::New(); renderer -> AddActor( coneActor ); renderer -> SetBackground(0.1, 0.2, 0.4); vtkRenderWindow *renderWindow = vtkRenderWindow::New(); renderWindow -> AddRenderer( renderer ); renderWindow -> SetSize( 300, 300 ); vtkRenderWindowInteractor *renderWindowInteractor = vtkRenderWindowInteractor::New(); renderWindowInteractor -> SetRenderWindow( renderWindow ); renderWindow -> Render(); renderWindowInteractor -> Start(); cone = vtk.vtkConeSource() cone.SetHeight( 3.0 ) cone.SetRadius( 1.0 ) cone.SetResolution( 10 ) coneMapper = vtk.vtkPolyDataMapper() coneMapper.SetInput( cone.GetOutput() )
coneActor= vtk.vtkActor() coneActor.SetMapper( coneMapper) ren1= vtk.vtkRenderer() ren1.AddActor( coneActor) ren1.SetBackground( 0.1, 0.2, 0.4 ) renWin= vtk.vtkRenderWindow() renWin.AddRenderer( ren1 ) renWin.SetSize( 300, 300 )
...
Carlos Vinhais
VTK - Visualization Toolkit
17
VTK Interactors
vtkRenderWindowInteractor
Keypress: j|t
Toggle between joystick or trackball mode In joystick style the motion occurs continuously as long as the mouse button is pressed In trackball style the motion occurs when the mouse button is pressed and the mouse cursor moves Toggle between camera and actor modes In camera mode, the mouse events affect the camera position and focal point In actor mode, the mouse events affect the object under the mouse pointer Exit/quit application Toggle in and out of stereo model Default is red-blue stereo pairs
Left Mouse Button
Rotate camera or actor Camera rotated around its focal point Actor rotated around its origin
Middle Mouse Button
Pan camera or translate actor In joystick mode direction of pan/translation is from center of the viewporttoward the mouse position In trackball mode, direction of motion in the direction of the mouse movement MMB
Keypress: c|a
Right Mouse Button
Zoom camera or scale actor Zoom in/increase scale in top half of the viewport Zoom out/decrease scale in lower half of the viewport In joystick mode amount is controlled by distance of the pointer from the horizontal center line
Keypress: e|q
Keypress: 3
Carlos Vinhais
VTK - Visualization Toolkit
18
VTK Interactors
Keypress u
Invokes user-defined mode brings up an command interactor window Rerender using irenRender Fly-to the point under the cursor Sets the focal point allowing rotations about that point Pick operation Render window has an internal instance of vtkPropPickerfor picking Reset the camera along the viewing direction Centers the actors All actors visible
Keypress f
Keypress p
solid
Keypress r
Keypress s
All actors represented as surfaces All actors represented in wire frame
wired
Keypress w
stereo
Carlos Vinhais VTK - Visualization Toolkit 19
Computer Hands-On Build and Test VTK
Build VTK
Follow the notes!
Install_Notes_VTK5_VC2010_Win64.pdf
Test VTK
Get the source code!
Helloworld.zip (CMakeLists.txt + Helloworld.cxx) Requires ITK installed!
Carlos Vinhais VTK - Visualization Toolkit 20
Computer Hands-On VTK Exercises
1 window, 3 viewports
Cone1 Cone2 Cone3
SingleScreenShot
Full screen, stereo
Carlos Vinhais VTK - Visualization Toolkit 21
Computer Hands-On SingleScreenShot
#include <vtkSmartPointer.h> #include <vtkSphereSource.h> #include <vtkWindowToImageFilter.h> #include <vtkPNGWriter.h> ... renderWindow -> SetSize( 512, 512 );// set window size renderWindow -> StereoCapableWindowOn(); renderWindow -> StereoRenderOn(); renderWindow -> SetStereoTypeToAnaglyph(); //renderWindow -> SetStereoTypeToCrystalEyes(); //renderWindow -> SetStereoTypeToLeft(); renderWindow -> StereoUpdate(); renderWindow->FullScreenOn(); //WILL RENDER IN STEREO renderWindow -> Render(); renderWindowInteractor -> Start();
Full screen, stereo
Carlos Vinhais
VTK - Visualization Toolkit
22
Computer Hands-On SingleScreenShot (cont.)
... renderWindow -> Render(); renderWindowInteractor -> Start(); vtkSmartPointer< vtkWindowToImageFilter > windowToImageFilter = vtkSmartPointer< vtkWindowToImageFilter >::New(); windowToImageFilter -> SetInput( renderWindow ); windowToImageFilter -> Update(); vtkSmartPointer< vtkPNGWriter > writer = vtkSmartPointer< vtkPNGWriter >::New(); writer -> SetFileName( "SingleScreenshot.png" ); writer -> SetInput( windowToImageFilter->GetOutput() ); writer -> Write(); ...
Carlos Vinhais
VTK - Visualization Toolkit
23
3D Graphics
Surface rendering Volume rendering
Ray casting
Texture mapping (2D) Lights and cameras Textures Save render window to .png, .jpg, ... (useful for movie creation) ...
VTK - Visualization Toolkit 24
Carlos Vinhais
Visualization Techniques
Scalar algorithms
Contouring Color mapping
Vector algorithms
Streamlines streamtubes
Tensor algorithms
Tensor ellipsoids
Carlos Vinhais VTK - Visualization Toolkit 25
Imaging
vtkImageToImageFilter
Diffusion High-pass / Low-pass (Fourier) Convolution Gradient (magnitude) Distance map Morphology Skeletons
Carlos Vinhais VTK - Visualization Toolkit 26
Visualization Techniques Contouring
Contouring
Also Iso-surfaces Filter vtkContourFilter performs the function Using SetValue() method
contours SetValue 0.0 0.5
Using GenerateValues() method
Contours generateValues 8 0.0 1.2 Specify range and number of contours
Many methods perform contouring
vtkMarchingCubes vtkMarchingSquares
Carlos Vinhais
VTK - Visualization Toolkit
27
Visualization Techniques Color Mapping
Coloring objects by scalar values Scalar values mapped through lookup table Color applied during rendering Modifies appearance of points or cells Use any data array
Method ColorByArrayComponent()
If not specified, a default lookup table is created by the mapper
Carlos Vinhais
VTK - Visualization Toolkit
28
3D Widgets
Watch for events invoked by vtkRenderWindowInteractor Subclasses of vtkInteractorObserver List of most important widgets
vtkScalarBarWidget vtkPointWidget vtkLineWidget vtkPlaneWidget vtkImplicitPlane vtkBoxWidget vtkImagePlaneWidget vtkSphereWidget vtkSplineWidget
Carlos Vinhais
VTK - Visualization Toolkit
29
Visualization Techniques Cutting
Create cross-section of dataset Any implicit function
Planes create planar cuts
Cutting surface interpolates the data Result is always type vtkPolyData vtkCutter needs an implicit function to cut May use more cut values
SetValue() method GenerateValues() method
Values specify the value of the implicit function Cutting values
Zero precisely on the implicit function Less than zero, below Greater than zero, above Only strictly true for vtkPlane
Carlos Vinhais
VTK - Visualization Toolkit
30
Visualization Techniques Merging and Probing
Merge data
Pipelines could have loops Multiple streams of the pipeline vtkMergeFilter merges data from several datasets to a new dataset
Probing
vtkAppendFilter builds new dataset by appending datasets
Specialized filter vtkAppendPolyData
Only data attributes common to all data input are appended
Carlos Vinhais
VTK - Visualization Toolkit
31
Visualization Techniques Glyphing
Represent data using symbols, or glyphs Simple glyphs
Cone oriented to a vector
Complex
Symbolic representation of the human face Expression controlled by the data values
vtkGlyph3D class
Scaled, colored Orientated along a direction Glyphs copied to each point of the dataset
Glyphs defined by second input to the filter Glyphs of type vtkPolyData
Carlos Vinhais VTK - Visualization Toolkit 32
Visualization Techniques Glyphing
The glyph uses the point attribute normals for orientation Use vector data using the SetVectorMethodToUseVector() Scale glyphs by scalar data using SetScaleModeToScaleByScalar() Turn data scaling off using SetScaleModeToDataScalingOff() Many other options
Carlos Vinhais
VTK - Visualization Toolkit
33
Visualization Techniques Streamlines
Streamlines
Path of a massless particle in a vector field Requires starting point(s) Integration direction
Along the flow Opposite the flow Both
Carlos Vinhais
VTK - Visualization Toolkit
34
Visualization Techniques Streamsurfaces
Series of ordered points, used to generate streamlines vtkRuledSurfaceFilter used to create a surface Points must be ordered carefully Assumes points lie next to one another Within a specified distance of neighbors (DistanceFactorvariable)
Carlos Vinhais
VTK - Visualization Toolkit
35
VTK Example The Virtual Frog
http://www-itg.lbl.gov/Frog
Carlos Vinhais VTK - Visualization Toolkit 36
End of VTK Lecture. Thank you!
Carlos A. Vinhais
cvinhais@gmail.com Department of Physics ISEP School of Engineering Polytechnic Institute of Porto Porto, PORTUGAL