1#!/usr/bin/env python
2
3# This is a simple volume rendering example that uses a
4# vtkVolumeRayCast mapper
5
6import vtk
7from vtk.util.misc import vtkGetDataRoot
8VTK_DATA_ROOT = vtkGetDataRoot()
9
10# Create the standard renderer, render window and interactor
11ren = vtk.vtkRenderer()
12renWin = vtk.vtkRenderWindow()
13renWin.AddRenderer(ren)
14iren = vtk.vtkRenderWindowInteractor()
15iren.SetRenderWindow(renWin)
16
17# Create the reader for the data
18reader = vtk.vtkStructuredPointsReader()
19reader.SetFileName(VTK_DATA_ROOT + "/Data/ironProt.vtk")
20
21# Create transfer mapping scalar value to opacity
22opacityTransferFunction = vtk.vtkPiecewiseFunction()
23opacityTransferFunction.AddPoint(20, 0.0)
24opacityTransferFunction.AddPoint(255, 0.2)
25
26# Create transfer mapping scalar value to color
27colorTransferFunction = vtk.vtkColorTransferFunction()
28colorTransferFunction.AddRGBPoint(0.0, 0.0, 0.0, 0.0)
29colorTransferFunction.AddRGBPoint(64.0, 1.0, 0.0, 0.0)
30colorTransferFunction.AddRGBPoint(128.0, 0.0, 0.0, 1.0)
31colorTransferFunction.AddRGBPoint(192.0, 0.0, 1.0, 0.0)
32colorTransferFunction.AddRGBPoint(255.0, 0.0, 0.2, 0.0)
33
34# The property describes how the data will look
35volumeProperty = vtk.vtkVolumeProperty()
36volumeProperty.SetColor(colorTransferFunction)
37volumeProperty.SetScalarOpacity(opacityTransferFunction)
38volumeProperty.ShadeOn()
39volumeProperty.SetInterpolationTypeToLinear()
40
41# The mapper / ray cast function know how to render the data
42compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
43volumeMapper = vtk.vtkVolumeRayCastMapper()
44volumeMapper.SetVolumeRayCastFunction(compositeFunction)
45volumeMapper.SetInputConnection(reader.GetOutputPort())
46
47# The volume holds the mapper and the property and
48# can be used to position/orient the volume
49volume = vtk.vtkVolume()
50volume.SetMapper(volumeMapper)
51volume.SetProperty(volumeProperty)
52
53ren.AddVolume(volume)
54ren.SetBackground(1, 1, 1)
55renWin.SetSize(600, 600)
56renWin.Render()
57
58def CheckAbort(obj, event):
59    if obj.GetEventPending() != 0:
60        obj.SetAbortRender(1)
61
62renWin.AddObserver("AbortCheckEvent", CheckAbort)
63
64iren.Initialize()
65renWin.Render()
66iren.Start()
67