VTK Tutorial
Data structures,
filtering and rendering
Stefano Perticoni – [Link]@[Link]
Live material
[Link]
2
Prerequisites
3
Prerequisites
The following Python 2.7 and vtk 5.10 execution environment for
Windows is available on your pc through the Spyder GUI:
Run Spyder (pythonxy gui) from startup icon or command line:
C:\Python27\Scripts\[Link]
4
Tools -> Preferences
5
Exercise: learn vtkArray
#1 Make an array
import vtk
myArray = [Link]()
list_dir(myArray)
help([Link])
print(myArray)
[Link]('my first array')
[Link](1)
[Link](500*500) #going to make a 500x500 picture
#2 Fill it with data
from math import sin, cos
for x in range(0,500):
for y in range(0,500):
[Link](x*500+y, 127.5+(1.0+sin(x/25.0)*cos(y/25.0)))
6
Exercise: learn vtkArray
#1. Create the Data structure
id = [Link]()
#2. Define its Geometry
[Link](0,0,0)
[Link](1,1,1)
#3. Define its Topology
[Link](500,500,1)
#4. Assign Data to the Structure, Geometry and/or Topology
[Link](vtk.VTK_DOUBLE)
[Link]().SetScalars(myArray)
#5. Inspect it
print(id)
print([Link]())
array = [Link]().GetArray('my first array')
[Link]()
7
The VTK Graphics Subsystem
8
vtkRenderWindow
• SetSize() — set the size of the window
• AddRenderer() — add another renderer which draws into this
• SetInteractor() — set class to handles mouse/key events
- vtkRenderWindowInteractor->SetInteractorStyle()
• Render() — updates pipeline and draws scene
9
vtkRenderer
• SetViewport() - specify where to draw in the render window
• SetLayer() - set pane/depth in render window to draw on
• AddViewProp() - add objects to be rendered
• AddLight() - add a light to illuminate the scene
• SetAmbient() - set the intensity of the ambient lighting
• SetBackground() - set background color
• SetActiveCamera() - specify the camera to use to render the scene
• ResetCamera() - reset the camera so that all actors are visible
10
vtkCamera
• Position - where the camera is located
• FocalPoint - where the camera is pointing
• ViewUp - which direction is "up"
• ClippingRange - data outside of this range is clipped
• ViewAngle - the camera view angle controls perspective effects
• ParallelProjection - turn parallel projection
on/off (no perspective effects)
• Roll, Pitch, Yaw, Elevation, Azimuth
move the camera in a variety of ways
• Zoom, Dolly - changes view angle (Zoom);
move camera closer (Dolly)
11
vtkCamera
12
vtkActor (subclass of vtkProp)
• Visibility - is the actor visible?
• Pickable - is the actor pickable?
• Texture - a texture map associated with the actor
• SetOrigin/Scale/UserTransform - control where it is drawn
• GetBounds
• vtkProperty - surface lighting properties
13
Exercise: make a window
#1. Make a window
renwin = [Link]()
[Link](500,500)
#2. Make a renderer for that window
renderer = [Link]()
[Link](renderer)
#3. Control how it all looks
renderer.SetBackground2(1,1,1)
[Link](1)
#4. Show it
[Link]()
14
Exercise: show some data
#1. Access the data processing pipeline that has your data
mapper = [Link]()
[Link](id)
[Link]() # we'll talk about this soon
#2. Link that to the display system
actor = [Link]()
[Link](mapper)
[Link](actor)
[Link]()
#3. Adjust the camera for a better view
[Link]()
[Link]()
15
Color control by vtkActor and vtkMapper
16
vtkProperty (Actor has)
• AmbientColor, DiffuseColor, SpecularColor — a different
color for ambient, diffuse, and specular lighting
• Color — sets the three colors above to the same
• Interpolation - shading interpolation method (Flat,
Gouraud)
• Representation — how to represent itself
(Points, Wireframe, Surface)
• Opacity — control transparency
17
vtkMapper (Actor also has)
• ScalarVisibilityOn()/Off()
- Color cells/points by data values or entire object by actor color
• Choose which array to color by
- SetScalarModeToDefault()
- SetScalarModeToUsePointData()
- SetScalarModeToUseCellData()
- SelectColorArray(array name)
• SetLookupTable(lut)
• SetScalarRange(min, max)
- range of data values for lut
• InterpolateScalarBeforeMappingOn()/Off()
- whether to interpolate colors across cells in color or data space
18
vtkLookupTable (Mapper has)
• NumberOfColors - number of colors in the table
• TableRange - the min/max scalar value range to map
• If building a table from linear HSVA ramp:
- HueRange - mm/max hue range
- SaturationRange - min/max saturation range
- ValueRange - min/max value range
- AlphaRange - min/max transparency range
• If manually building a table
- Build (after setting NumberOfColors)
- SetTableValue( idx, rgba) for each NumberOfColors entries
19
Exercise : Visualize the topology
#1. Specify whole Prop color
actorProperty = [Link]()
[Link](0,1,1)
[Link]()
#2. Change from surface to edges rendering
[Link]()
[Link]()
[Link]().Zoom(10)
[Link]()
#3. Reset
[Link]()
[Link]()
20
Exercise : Visualize the topology
#1. Turn on color from values
[Link]()
[Link]()
#2. Match up lookuptable range
[Link]()
[Link](127,129)
[Link]()
21
Algorithms
22
Read a data file, inspect and visualize
#1. Create a reader, tell it what file and run it
reader = [Link]()
[Link]("c:/VTKSchool/Perticoni/MaterialeEsercitazioneVTK/data/Saint
[Link]")
#2. Examine the result
id = [Link]()
print [Link]().GetArray(0)
[Link]()
print [Link]().GetArray(0).GetRange()
[Link]([Link]())
[Link](682.0, 2543.0)
[Link]()
[Link]()
[Link]()
23
Pipeline execution model
24
Demand Driven Pipeline
• Lazy evaluation
- Pipeline only produces results when you ask it to Update or Render()
- Changing a parameter or rearranging the pipeline doesn't do that.
- Each filter caches its most recent output
• Modified time
- Each filter keeps track of when it last produced data, and when its
parameters were last changed
- Pipeline only updates as far back as it has to
- Examples:
• Camera motion - data isn't reread, only mapper has to execute
• Change isovalue parameter
• Change filename
25
Exercise : manipulate the read in data
#1. Make filter to convert to a less constrained data structure
triangles = [Link]()
#2. Connect it
[Link]([Link]())
#3. Run it
[Link]()
print([Link]().GetClassName())
print([Link]().GetClassName())
26
Exercise: manipulate the read in data
#1. Make and use a filter to change the geometry
warp = [Link]()
[Link]([Link]())
[Link]()
print([Link]().GetBounds())
print([Link]().GetBounds())
#2. Show it
[Link]([Link]())
[Link]()
27
Exercise: manipulate the read in data
#3 Get a hold of window events
iren = [Link]()
[Link](iren)
[Link]()
[Link]()
# Press “e” to exit from the interaction
# Press “t” to selec camera
Trackball interactor
28
Exercise: manipulate the data
#1. Make a clip filter and put it in pipeline
clip = [Link]()
[Link]([Link]())
[Link]([Link]())
#2. Make a source to orient clip filter with
plane = [Link]()
[Link](plane)
[Link](560000,5120000,2000)
#3. Inspect the result
[Link]()
print [Link]().GetBounds()
[Link]()
29
Interaction
• Events
- Instances of vtk classes can fire
events and watch events fired by
others
- watcher executes some code
whenever the event occurs
• Interactors
- Watch mouse, keyboard,
window system events to move
camera call render etc
• Widgets
- Special purpose classes that are
drawn in scene and watch events
30
Exercise: use a widget to interact with
the data
#1. Get a hold of window events
iren = [Link]()
[Link](iren)
#2. Make and initially place the widget
widget = [Link]()
[Link]([Link]().GetBounds())
[Link]([[Link]()[x] for x in 0,1,2])
[Link]([[Link]()[x] for x in 0,1,2])
#3. Connect it to the renderwindow's events
[Link](iren)
31
Exercise: use a widget to interact with the data
#1. Connect the widget's events to our pipeline
def eventhandler(obj , event):
global plane
[Link](plane)
[Link]("InteractionEvent", eventhandler)
#2. Configure the widget
[Link](1)
[Link]()
[Link]()
#3. Turn on interaction
[Link]()
[Link]()
[Link]()
32
Exercises
From your browser open the Summary page
[Link]
33