VTKTutorial
VTKTutorial
Data structures,
filtering and rendering
Stefano Perticoni – [email protected]
Live material
http://notepad.stefanoperticoni.org
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:
C:\Python27\Scripts\spyder.exe
4
Tools -> Preferences
5
Exercise: learn vtkArray
#1 Make an array
import vtk
myArray = vtk.vtkDoubleArray()
list_dir(myArray)
help(myArray.SetValue)
print(myArray)
myArray.SetName('my first array')
myArray.SetNumberOfComponents(1)
myArray.SetNumberOfTuples(500*500) #going to make a 500x500 picture
6
Exercise: learn vtkArray
#1. Create the Data structure
id = vtk.vtkImageData()
#5. Inspect it
print(id)
print(id.GetPointData())
array = id.GetPointData().GetArray('my first array')
array.GetRange()
7
The VTK Graphics Subsystem
8
vtkRenderWindow
9
vtkRenderer
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)
11
vtkCamera
12
vtkActor (subclass of vtkProp)
13
Exercise: make a window
#1. Make a window
renwin = vtk.vtkRenderWindow()
renwin.SetSize(500,500)
#4. Show it
renwin.Render()
14
Exercise: show some data
#1. Access the data processing pipeline that has your data
mapper = vtk.vtkDataSetMapper()
mapper.SetInput(id)
mapper.ScalarVisibilityOff() # we'll talk about this soon
15
Color control by vtkActor and vtkMapper
16
vtkProperty (Actor has)
17
vtkMapper (Actor also has)
• ScalarVisibilityOn()/Off()
- Color cells/points by data values or entire object by actor color
• 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)
19
Exercise : Visualize the topology
#1. Specify whole Prop color
actorProperty = actor.GetProperty()
actorProperty.SetDiffuseColor(0,1,1)
renwin.Render()
#3. Reset
actorProperty.SetRepresentationToSurface()
renderer.ResetCamera()
20
Exercise : Visualize the topology
21
Algorithms
22
Read a data file, inspect and visualize
#1. Create a reader, tell it what file and run it
reader = vtk.vtkDataSetReader()
reader.SetFileName("c:/VTKSchool/Perticoni/MaterialeEsercitazioneVTK/data/Saint
HelenSP.vtk")
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
#2. Connect it
triangles.SetInputConnection(reader.GetOutputPort())
#3. Run it
triangles.Update()
print(reader.GetOutput().GetClassName())
print(triangles.GetOutput().GetClassName())
26
Exercise: manipulate the read in data
#2. Show it
mapper.SetInputConnection(warp.GetOutputPort())
renwin.Render()
27
Exercise: manipulate the read in data
28
Exercise: manipulate the data
#1. Make a clip filter and put it in pipeline
clip = vtk.vtkClipDataSet()
clip.SetInputConnection(warp.GetOutputPort())
mapper.SetInputConnection(clip.GetOutputPort())
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
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
obj.GetPlane(plane)
widget.AddObserver("InteractionEvent", eventhandler)
32
Exercises
From your browser open the Summary page
file:///C:/VTKSchool/Perticoni/MaterialeEsercitazioneVTK/index.html
33