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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#!/usr/bin/env python
'''
This little example shows how a cursor can be created in
image viewers, and renderers. The standard TkImageViewerWidget and
TkRenderWidget bindings are used. There is a new binding:
middle button in the image viewer sets the position of the cursor.
'''
import sys
from functools import partial
import vtk
from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot
from Tkinter import Pack
VTK_DATA_ROOT = vtkGetDataRoot()
import Tkinter
#from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor
from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
from vtk.tk.vtkTkImageViewerWidget import vtkTkImageViewerWidget
# Tkinter constants.
E = Tkinter.E
W = Tkinter.W
N = Tkinter.N
S = Tkinter.S
HORIZONTAL = Tkinter.HORIZONTAL
VERTICAL = Tkinter.VERTICAL
RIGHT = Tkinter.RIGHT
LEFT = Tkinter.LEFT
TOP = Tkinter.TOP
BOTTOM = Tkinter.BOTTOM
X = Tkinter.X
BOTH = Tkinter.BOTH
NO = Tkinter.NO
YES = Tkinter.YES
NORMAL = Tkinter.NORMAL
DISABLED = Tkinter.DISABLED
TRUE = Tkinter.TRUE
FALSE = Tkinter.FALSE
# Global values.
CURSOR_X = 20
CURSOR_Y = 20
CURSOR_Z = 20
IMAGE_MAG_X = 4
IMAGE_MAG_Y = 4
IMAGE_MAG_Z = 1
class Cursor3DViewer(Testing.vtkTest):
'''
Provide a testing framework for for cursor3D.
Note:
root, the top-level widget for Tk,
tkrw, the vtkTkRenderWidget and
viewer, the Image viewer
are accessible from any function in this class
after SetUp() has run.
'''
def SetUp(self):
'''
Set up cursor3D
'''
def OnClosing():
self.root.quit()
def ViewerDown(viewer):
ViewerSetZSlice(viewer, viewer.GetZSlice() - 1)
def ViewerUp(viewer):
ViewerSetZSlice(viewer, viewer.GetZSlice() + 1)
def ViewerSetZSlice(viewer, z):
viewer.SetZSlice(z)
txt = 'slice: ' + str(z)
sliceLabel.configure(text=txt)
viewer.Render()
def SetCursorFromViewer(event):
x = int(event.x)
y = int(event.y)
# We have to flip y axis because tk uses upper right origin.
self.root.update_idletasks()
height = int(self.tkvw.configure()['height'][4])
y = height - y
print height
z = self.viewer.GetZSlice()
SetCursor( x / IMAGE_MAG_X, y / IMAGE_MAG_Y, z / IMAGE_MAG_Z )
def SetCursor(x, y, z):
CURSOR_X = x
CURSOR_Y = y
CURSOR_Z = z
axes.SetOrigin(CURSOR_X,CURSOR_Y,CURSOR_Z)
imageCursor.SetCursorPosition(
CURSOR_X * IMAGE_MAG_X,
CURSOR_Y * IMAGE_MAG_Y,
CURSOR_Z * IMAGE_MAG_Z)
self.viewer.Render()
self.renWin.Render()
# Pipeline stuff.
reader = vtk.vtkSLCReader()
reader.SetFileName(VTK_DATA_ROOT + "/Data/neghip.slc")
# Cursor stuff
magnify = vtk.vtkImageMagnify()
magnify.SetInputConnection(reader.GetOutputPort())
magnify.SetMagnificationFactors(IMAGE_MAG_X, IMAGE_MAG_Y ,IMAGE_MAG_Z)
imageCursor = vtk.vtkImageCursor3D()
imageCursor.SetInputConnection(magnify.GetOutputPort())
imageCursor.SetCursorPosition(
CURSOR_X*IMAGE_MAG_X,
CURSOR_Y*IMAGE_MAG_Y,
CURSOR_Z*IMAGE_MAG_Z)
imageCursor.SetCursorValue(255)
imageCursor.SetCursorRadius(50*IMAGE_MAG_X)
axes = vtk.vtkAxes()
axes.SymmetricOn()
axes.SetOrigin(CURSOR_X, CURSOR_Y, CURSOR_Z)
axes.SetScaleFactor(50.0)
axes_mapper = vtk.vtkPolyDataMapper()
axes_mapper.SetInputConnection(axes.GetOutputPort())
axesActor = vtk.vtkActor()
axesActor.SetMapper(axes_mapper)
axesActor.GetProperty().SetAmbient(0.5)
# Image viewer stuff.
self.viewer = vtk.vtkImageViewer()
self.viewer.SetInputConnection(imageCursor.GetOutputPort())
self.viewer.SetZSlice(CURSOR_Z*IMAGE_MAG_Z)
self.viewer.SetColorWindow(256)
self.viewer.SetColorLevel(128)
# Create transfer functions for opacity and color.
opacity_transfer_function = vtk.vtkPiecewiseFunction()
opacity_transfer_function.AddPoint(20, 0.0)
opacity_transfer_function.AddPoint(255, 0.2)
color_transfer_function = vtk.vtkColorTransferFunction()
color_transfer_function.AddRGBPoint(0, 0, 0, 0)
color_transfer_function.AddRGBPoint(64, 1, 0, 0)
color_transfer_function.AddRGBPoint(128, 0, 0, 1)
color_transfer_function.AddRGBPoint(192, 0, 1, 0)
color_transfer_function.AddRGBPoint(255, 0, .2, 0)
# Create properties, mappers, volume actors, and ray cast function.
volume_property = vtk.vtkVolumeProperty()
volume_property.SetColor(color_transfer_function)
# volume_property.SetColor(color_transfer_function[0],
# color_transfer_function[1],
# color_transfer_function[2])
volume_property.SetScalarOpacity(opacity_transfer_function)
composite_function = vtk.vtkVolumeRayCastCompositeFunction()
volume_mapper = vtk.vtkVolumeRayCastMapper()
volume_mapper.SetInputConnection(reader.GetOutputPort())
volume_mapper.SetVolumeRayCastFunction(composite_function)
volume = vtk.vtkVolume()
volume.SetMapper(volume_mapper)
volume.SetProperty(volume_property)
# Create outline.
outline = vtk.vtkOutlineFilter()
outline.SetInputConnection(reader.GetOutputPort())
outline_mapper = vtk.vtkPolyDataMapper()
outline_mapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outline_mapper)
outlineActor.GetProperty().SetColor(1, 1, 1)
# Create the renderer.
ren = vtk.vtkRenderer()
ren.AddActor(axesActor)
ren.AddVolume(volume)
ren.SetBackground(0.1, 0.2, 0.4)
self.renWin = vtk.vtkRenderWindow()
self.renWin.AddRenderer(ren)
self.renWin.SetSize(256, 256)
# Create the GUI: two renderer widgets and a quit button.
self.root = Tkinter.Tk()
self.root.title("cursor3D")
# Define what to do when the user explicitly closes a window.
self.root.protocol("WM_DELETE_WINDOW", OnClosing)
# Help label, frame and quit button
helpLabel = Tkinter.Label(self.root,
text=
"MiddleMouse (or shift-LeftMouse) in image viewer to place cursor")
displayFrame = Tkinter.Frame(self.root)
quitButton = Tkinter.Button(self.root, text= "Quit", command=OnClosing)
# Pack the GUI.
helpLabel.pack()
displayFrame.pack(fill=BOTH, expand=TRUE)
quitButton.pack(fill=X)
# Create the viewer widget.
viewerFrame = Tkinter.Frame(displayFrame)
viewerFrame.pack(padx=3, pady=3, side=LEFT, anchor=N,
fill=BOTH, expand=FALSE)
self.tkvw = vtkTkImageViewerWidget(viewerFrame, iv=self.viewer,
width=264, height=264)
viewerControls = Tkinter.Frame(viewerFrame)
viewerControls.pack(side=BOTTOM, anchor=S, fill=BOTH, expand=TRUE)
self.tkvw.pack(side=TOP, anchor=N, fill=BOTH, expand=FALSE)
downButton = Tkinter.Button(viewerControls, text="Down",
command=[ViewerDown,self.viewer])
upButton = Tkinter.Button(viewerControls, text="Up",
command=[ViewerUp,self.viewer])
sliceLabel = Tkinter.Label(viewerControls,
text="slice: "+str(CURSOR_Z*IMAGE_MAG_Z))
downButton.pack(side=LEFT, expand=TRUE, fill=BOTH)
upButton.pack(side=LEFT, expand=TRUE, fill=BOTH)
sliceLabel.pack(side=LEFT, expand=TRUE, fill=BOTH)
# Create the render widget
renderFrame = Tkinter.Frame(displayFrame)
renderFrame.pack(padx=3, pady=3, side=LEFT, anchor=N,
fill=BOTH, expand=TRUE)
self.tkrw = vtkTkRenderWidget(renderFrame, rw=self.renWin,
width=264, height=264)
self.tkrw.pack(side=TOP, anchor=N, fill=BOTH, expand=TRUE)
# Bindings
self.tkvw.BindTkImageViewer()
self.tkrw.BindTkRenderWidget()
# Lets add an extra binding of the middle button in the image viewer
# to set the cursor location.
self.tkvw.bind('<Button-2>',SetCursorFromViewer)
self.tkvw.bind('<Shift-Button-1>',SetCursorFromViewer)
# Associate the functions with the buttons and label.
#
downButton.config(command=partial(ViewerDown, self.viewer))
upButton.config(command=partial(ViewerUp, self.viewer))
def DoIt(self):
self.SetUp()
self.viewer.Render()
self.tkrw.Render()
self.root.update()
# If you want to interact and use the sliders etc,
# uncomment the following line.
#self.root.mainloop()
img_file = "cursor3D.png"
Testing.compareImage(self.viewer.GetRenderWindow(), Testing.getAbsImagePath(img_file))
# Testing.interact()
if __name__ == '__main__':
cases = [(Cursor3DViewer, 'DoIt')]
del Cursor3DViewer
Testing.main(cases)
|