1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#!/usr/bin/env python
# Run this test like so:
# $ vtkpython TestImagePlaneWidget.py -D $VTK_DATA_ROOT \
# -B $VTK_DATA_ROOT/Baseline/Widgets/
#
# $ vtkpython TestImagePlaneWidget.py --help
# provides more details on other options.
import os
import os.path
import vtk
from vtk.test import Testing
class TestImagePlaneWidget(Testing.vtkTest):
def testBug(self):
# Uncomment the next line if you want to run this via
# `gdb python`.
#raw_input('Hit Ctrl-C')
# Load some data.
v16 = vtk.vtkVolume16Reader()
v16.SetDataDimensions(64, 64)
v16.SetDataByteOrderToLittleEndian()
v16.SetFilePrefix(os.path.join(Testing.VTK_DATA_ROOT,
"Data", "headsq", "quarter"))
v16.SetImageRange(1, 93)
v16.SetDataSpacing(3.2, 3.2, 1.5)
v16.Update()
xMin, xMax, yMin, yMax, zMin, zMax = v16.GetOutput().GetWholeExtent()
img_data = v16.GetOutput()
# **************************************************
# Look here for wierdness.
# Lets create this data using the data from the reader.
my_img_data = vtk.vtkImageData()
my_img_data.SetDimensions(img_data.GetDimensions())
my_img_data.SetWholeExtent(img_data.GetWholeExtent())
my_img_data.SetExtent(img_data.GetExtent())
my_img_data.SetUpdateExtent(img_data.GetUpdateExtent())
my_img_data.SetSpacing(img_data.GetSpacing())
my_img_data.SetOrigin(img_data.GetOrigin())
my_img_data.SetScalarType(img_data.GetScalarType())
my_img_data.GetPointData().SetScalars(img_data.GetPointData().GetScalars())
my_img_data.Update()
# hang on to original image data.
orig_img_data = img_data
# hijack img_data with our own. If you comment this out everything is
# fine.
img_data = my_img_data
# **************************************************
spacing = img_data.GetSpacing()
sx, sy, sz = spacing
origin = img_data.GetOrigin()
ox, oy, oz = origin
# An outline is shown for context.
outline = vtk.vtkOutlineFilter()
outline.SetInput(img_data)
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)
# The shared picker enables us to use 3 planes at one time
# and gets the picking order right
picker = vtk.vtkCellPicker()
picker.SetTolerance(0.005)
# The 3 image plane widgets are used to probe the dataset.
planeWidgetX = vtk.vtkImagePlaneWidget()
planeWidgetX.DisplayTextOn()
planeWidgetX.SetInput(img_data)
planeWidgetX.SetPlaneOrientationToXAxes()
planeWidgetX.SetSliceIndex(32)
planeWidgetX.SetPicker(picker)
planeWidgetX.SetKeyPressActivationValue("x")
prop1 = planeWidgetX.GetPlaneProperty()
prop1.SetColor(1, 0, 0)
planeWidgetY = vtk.vtkImagePlaneWidget()
planeWidgetY.DisplayTextOn()
planeWidgetY.SetInput(img_data)
planeWidgetY.SetPlaneOrientationToYAxes()
planeWidgetY.SetSliceIndex(32)
planeWidgetY.SetPicker(picker)
planeWidgetY.SetKeyPressActivationValue("y")
prop2 = planeWidgetY.GetPlaneProperty()
prop2.SetColor(1, 1, 0)
planeWidgetY.SetLookupTable(planeWidgetX.GetLookupTable())
# for the z-slice, turn off texture interpolation:
# interpolation is now nearest neighbour, to demonstrate
# cross-hair cursor snapping to pixel centers
planeWidgetZ = vtk.vtkImagePlaneWidget()
planeWidgetZ.DisplayTextOn()
planeWidgetZ.SetInput(img_data)
planeWidgetZ.SetPlaneOrientationToZAxes()
planeWidgetZ.SetSliceIndex(46)
planeWidgetZ.SetPicker(picker)
planeWidgetZ.SetKeyPressActivationValue("z")
prop3 = planeWidgetZ.GetPlaneProperty()
prop3.SetColor(0, 0, 1)
planeWidgetZ.SetLookupTable(planeWidgetX.GetLookupTable())
# Create the RenderWindow and Renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
# Add the outline actor to the renderer, set the background
# color and size
ren.AddActor(outlineActor)
renWin.SetSize(600, 600)
ren.SetBackground(0.1, 0.1, 0.2)
current_widget = planeWidgetZ
mode_widget = planeWidgetZ
# Set the interactor for the widgets
iact = vtk.vtkRenderWindowInteractor()
iact.SetRenderWindow(renWin)
planeWidgetX.SetInteractor(iact)
planeWidgetX.On()
planeWidgetY.SetInteractor(iact)
planeWidgetY.On()
planeWidgetZ.SetInteractor(iact)
planeWidgetZ.On()
# Create an initial interesting view
ren.ResetCamera();
cam1 = ren.GetActiveCamera()
cam1.Elevation(110)
cam1.SetViewUp(0, 0, -1)
cam1.Azimuth(45)
ren.ResetCameraClippingRange()
iact.Initialize()
renWin.Render()
# Compare the images and test.
img_file = "TestImagePlaneWidget.png"
Testing.compareImage(renWin, Testing.getAbsImagePath(img_file))
# Interact if necessary.
if Testing.isInteractive():
iact.Start()
if __name__ == "__main__":
Testing.main([(TestImagePlaneWidget, 'test')])
|