1#!/usr/bin/env python 2import vtk 3from vtk.test import Testing 4from vtk.util.misc import vtkGetDataRoot 5VTK_DATA_ROOT = vtkGetDataRoot() 6 7# Simple volume rendering example. 8reader = vtk.vtkSLCReader() 9reader.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/sphere.slc") 10reader.Update() 11# Create transfer functions for opacity and color 12opacityTransferFunction = vtk.vtkPiecewiseFunction() 13opacityTransferFunction.AddPoint(20,0.0) 14opacityTransferFunction.AddPoint(255,1.0) 15colorTransferFunction = vtk.vtkColorTransferFunction() 16# Improve coverage 17colorTransferFunction.SetColorSpaceToRGB() 18colorTransferFunction.AddRGBPoint(100,1,1,1) 19colorTransferFunction.AddRGBPoint(0,0,0,0) 20colorTransferFunction.AddRGBPoint(200,1,0,1) 21colorTransferFunction.AddRGBPoint(100,0,0,0) 22colorTransferFunction.RemovePoint(100) 23colorTransferFunction.RemovePoint(0) 24colorTransferFunction.RemovePoint(200) 25colorTransferFunction.AddHSVPoint(100,1,1,1) 26colorTransferFunction.AddHSVPoint(0,0,0,0) 27colorTransferFunction.AddHSVPoint(200,1,0,1) 28colorTransferFunction.AddHSVPoint(100,0,0,0) 29colorTransferFunction.RemovePoint(0) 30colorTransferFunction.RemovePoint(200) 31colorTransferFunction.RemovePoint(100) 32colorTransferFunction.AddRGBSegment(0,1,1,1,100,0,0,0) 33colorTransferFunction.AddRGBSegment(50,1,1,1,150,0,0,0) 34colorTransferFunction.AddRGBSegment(60,1,1,1,90,0,0,0) 35colorTransferFunction.AddHSVSegment(90,1,1,1,105,0,0,0) 36colorTransferFunction.AddHSVSegment(40,1,1,1,155,0,0,0) 37colorTransferFunction.AddHSVSegment(30,1,1,1,95,0,0,0) 38colorTransferFunction.RemoveAllPoints() 39colorTransferFunction.AddHSVPoint(0.0,0.01,1.0,1.0) 40colorTransferFunction.AddHSVPoint(127.5,0.50,1.0,1.0) 41colorTransferFunction.AddHSVPoint(255.0,0.99,1.0,1.0) 42colorTransferFunction.SetColorSpaceToHSV() 43# Create properties, mappers, volume actors, and ray cast function 44volumeProperty = vtk.vtkVolumeProperty() 45volumeProperty.SetColor(colorTransferFunction) 46volumeProperty.SetScalarOpacity(opacityTransferFunction) 47volumeProperty.SetInterpolationTypeToLinear() 48compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() 49volumeMapper = vtk.vtkVolumeRayCastMapper() 50volumeMapper.SetInputConnection(reader.GetOutputPort()) 51volumeMapper.SetVolumeRayCastFunction(compositeFunction) 52volume = vtk.vtkVolume() 53volume.SetMapper(volumeMapper) 54volume.SetProperty(volumeProperty) 55# Create geometric sphere 56sphereSource = vtk.vtkSphereSource() 57sphereSource.SetCenter(25,25,25) 58sphereSource.SetRadius(30) 59sphereSource.SetThetaResolution(15) 60sphereSource.SetPhiResolution(15) 61sphereMapper = vtk.vtkPolyDataMapper() 62sphereMapper.SetInputConnection(sphereSource.GetOutputPort()) 63sphereActor = vtk.vtkActor() 64sphereActor.SetMapper(sphereMapper) 65# Set up the planes 66plane1 = vtk.vtkPlane() 67plane1.SetOrigin(25,25,20) 68plane1.SetNormal(0,0,1) 69plane2 = vtk.vtkPlane() 70plane2.SetOrigin(25,25,30) 71plane2.SetNormal(0,0,-1) 72plane3 = vtk.vtkPlane() 73plane3.SetOrigin(20,25,25) 74plane3.SetNormal(1,0,0) 75plane4 = vtk.vtkPlane() 76plane4.SetOrigin(30,25,25) 77plane4.SetNormal(-1,0,0) 78sphereMapper.AddClippingPlane(plane1) 79sphereMapper.AddClippingPlane(plane2) 80volumeMapper.AddClippingPlane(plane3) 81volumeMapper.AddClippingPlane(plane4) 82# Okay now the graphics stuff 83ren1 = vtk.vtkRenderer() 84renWin = vtk.vtkRenderWindow() 85renWin.AddRenderer(ren1) 86renWin.SetSize(256,256) 87iren = vtk.vtkRenderWindowInteractor() 88iren.SetRenderWindow(renWin) 89ren1.GetCullers().InitTraversal() 90culler = ren1.GetCullers().GetNextItem() 91culler.SetSortingStyleToBackToFront() 92ren1.AddViewProp(sphereActor) 93ren1.AddViewProp(volume) 94ren1.SetBackground(0.1,0.2,0.4) 95renWin.Render() 96ren1.GetActiveCamera().Azimuth(45) 97ren1.GetActiveCamera().Elevation(15) 98ren1.GetActiveCamera().Roll(45) 99ren1.GetActiveCamera().Zoom(2.0) 100iren.Initialize() 101# --- end of script -- 102