/usr/share/vtk/GUI/Tcl/ImageTracerWidget.tcl is in vtk-examples 5.8.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 275 276 277 278 279 280 281 282 283 284 285 | package require vtk
package require vtkinteraction
# This example demonstrates how to use the vtkImageTracerWidget
# to trace on a slice of a 3D image dataset on one of its orthogonal planes.
# The button actions and key modifiers are as follows for controlling the
# widget:
# 1) left button click over the image, hold and drag draws a free hand line.
# 2) left button click and release erases the widget line, if it exists, and
# repositions the handle.
# 3) middle button click starts a snap line. The snap line can be
# terminated by clicking the middle button while depressing the ctrl key.
# 4) when tracing or snap drawing a line, if the last cursor position is
# within specified tolerance to the first handle, the widget line will form
# a closed loop with only one handle.
# 5) right button clicking and holding on any handle that is part of a snap
# line allows handle dragging. Any existing line segments are updated
# accordingly.
# 6) ctrl key + right button down on any handle will erase it. Any existing
# snap line segments are updated accordingly. If the line was formed by
# continous tracing, the line is deleted leaving one handle.
# 7) shift key + right button down on any snap line segment will insert a
# handle at the cursor position. The snap line segment is split accordingly.
#
# Start by loading some data.
#
vtkVolume16Reader v16
v16 SetDataDimensions 64 64
v16 SetDataByteOrderToLittleEndian
v16 SetImageRange 1 93
v16 SetDataSpacing 3.2 3.2 1.5
v16 SetFilePrefix "$VTK_DATA_ROOT/Data/headsq/quarter"
v16 Update
set range [[v16 GetOutput] GetScalarRange]
set min [lindex $range 0]
set max [lindex $range 1]
set diff [expr $max - $min]
set slope [expr 255.0/$diff]
set inter [expr -$slope*$min]
set shift [expr $inter/$slope]
vtkImageShiftScale shifter
shifter SetShift $shift
shifter SetScale $slope
shifter SetOutputScalarTypeToUnsignedChar
shifter SetInputConnection [v16 GetOutputPort]
shifter ReleaseDataFlagOff
shifter Update
# Display a y-z plane.
#
vtkImageActor imageActor
imageActor SetInput [shifter GetOutput]
imageActor VisibilityOn
imageActor SetDisplayExtent 31 31 0 63 0 92
imageActor InterpolateOff
set spc [[shifter GetOutput] GetSpacing]
set orig [[shifter GetOutput] GetOrigin]
set x0 [lindex $orig 0]
set xspc [lindex $spc 0]
set pos [expr $x0 + $xspc*31.0]
# An alternative would be to formulate position in this case by:
# set bounds [imageActor GetBounds]
# set pos [lindex $bounds 0]
#
vtkRenderer ren1
ren1 SetBackground 0.4 0.4 0.5
vtkRenderer ren2
ren2 SetBackground 0.5 0.4 0.4
vtkRenderWindow renWin
renWin AddRenderer ren1
renWin AddRenderer ren2
renWin SetSize 600 300
ren1 SetViewport 0 0 0.5 1
ren2 SetViewport 0.5 0 1 1
vtkInteractorStyleImage interactor
vtkRenderWindowInteractor iren
iren SetInteractorStyle interactor
iren SetRenderWindow renWin
vtkExtractVOI extract
extract SetVOI 31 31 0 63 0 92
extract SetSampleRate 1 1 1
extract SetInputConnection [shifter GetOutputPort]
extract ReleaseDataFlagOff
extract Update
vtkImageActor imageActor2
imageActor2 SetInput [extract GetOutput]
imageActor2 VisibilityOn
imageActor2 SetDisplayExtent 31 31 0 63 0 92
imageActor2 InterpolateOff
# Set up the image tracer widget
#
vtkImageTracerWidget itw
#
# Set the tolerance for capturing last handle when near first handle
# to form closed paths.
#
itw SetCaptureRadius 1.5
[itw GetGlyphSource] SetColor 1 0 0
#
# Set the size of the glyph handle
#
[itw GetGlyphSource] SetScale 3.0
#
# Set the initial rotation of the glyph if desired. The default glyph
# set internally by the widget is a '+' so rotating 45 deg. gives a 'x'
#
[itw GetGlyphSource] SetRotationAngle 45.0
[itw GetGlyphSource] Modified
itw ProjectToPlaneOn
itw SetProjectionNormalToXAxes
itw SetProjectionPosition $pos
itw SetViewProp imageActor
itw SetInput [shifter GetOutput]
itw SetInteractor iren
itw PlaceWidget
#
# When the underlying vtkDataSet is a vtkImageData, the widget can be
# forced to snap to either nearest pixel points, or pixel centers. Here
# it is turned off.
#
itw SnapToImageOff
#
# Automatically form closed paths.
#
itw AutoCloseOn
# Set up a vtkSplineWidget in the second renderer and have
# its handles set by the tracer widget.
#
vtkSplineWidget isw
isw SetCurrentRenderer ren2
isw SetDefaultRenderer ren2
isw SetInput [extract GetOutput]
isw SetInteractor iren
set bnds [imageActor2 GetBounds]
isw PlaceWidget [lindex $bnds 0] [lindex $bnds 1] [lindex $bnds 2] [lindex $bnds 3] [lindex $bnds 4] [lindex $bnds 5]
isw ProjectToPlaneOn
isw SetProjectionNormalToXAxes
isw SetProjectionPosition $pos
# Have the widgets control each others handle positions.
#
itw AddObserver EndInteractionEvent AdjustSpline
isw AddObserver EndInteractionEvent AdjustTracer
itw On
isw On
vtkPolyData poly
vtkPoints points
vtkPolyData spoly
# Set up a pipleline to demonstrate extraction of a 2D
# region of interest. Defining a closed clockwise path using the
# tracer widget will extract all pixels within the loop. A counter
# clockwise path provides the dual region of interest.
#
vtkLinearExtrusionFilter extrude
extrude SetInput spoly
extrude SetScaleFactor 1
extrude SetExtrusionTypeToNormalExtrusion
extrude SetVector 1 0 0
# We force the "ribbon" generated by the extrusion to straddle the image plane.
#
vtkTransformPolyDataFilter filter
filter SetInputConnection [extrude GetOutputPort]
vtkTransform transform
transform Translate -0.5 0 0
filter SetTransform transform
# The spacing, origin and whole extent of the stencil generator must be set.
#
set spc [[extract GetOutput] GetSpacing]
set orig [[extract GetOutput] GetOrigin]
set wext [[extract GetOutput] GetWholeExtent]
vtkPolyDataToImageStencil dataToStencil
dataToStencil SetInputConnection [filter GetOutputPort]
dataToStencil SetOutputSpacing [lindex $spc 0] [lindex $spc 1] [lindex $spc 2]
dataToStencil SetOutputOrigin [lindex $orig 0] [lindex $orig 1] [lindex $orig 2]
dataToStencil SetOutputWholeExtent [lindex $wext 0] [lindex $wext 1] [lindex $wext 2] [lindex $wext 3] [lindex $wext 4] [lindex $wext 5]
vtkImageStencil stencil
stencil SetInputConnection [extract GetOutputPort]
stencil SetStencil [dataToStencil GetOutput]
stencil ReverseStencilOff
stencil SetBackgroundValue 128
# Add all the actors.
#
ren1 AddViewProp imageActor
ren2 AddViewProp imageActor2
# Render the image.
#
iren AddObserver UserEvent {wm deiconify .vtkInteract}
renWin Render
[ren1 GetActiveCamera] SetViewUp 0 1 0
[ren1 GetActiveCamera] Azimuth 270
[ren1 GetActiveCamera] Roll 270
[ren1 GetActiveCamera] Dolly 1.7
ren1 ResetCameraClippingRange
[ren2 GetActiveCamera] SetViewUp 0 1 0
[ren2 GetActiveCamera] Azimuth 270
[ren2 GetActiveCamera] Roll 270
[ren2 GetActiveCamera] Dolly 1.7
ren2 ResetCameraClippingRange
renWin Render
# Prevent the tk window from showing up then start the event loop.
wm withdraw .
proc AdjustSpline { } {
set closed [itw IsClosed]
if { $closed } {
isw ClosedOn
} else {
isw ClosedOff
imageActor2 SetInput [extract GetOutput]
}
set npts [ itw GetNumberOfHandles ]
if { $npts < 2 } {
return
}
itw GetPath poly
set pts [poly GetPoints]
isw InitializeHandles $pts
if { $closed } {
isw GetPolyData spoly
stencil Update
imageActor2 SetInput [stencil GetOutput]
}
}
proc AdjustTracer { } {
set npts [isw GetNumberOfHandles]
points Reset
for {set i 0} {$i < $npts} {incr i} {
set pt [isw GetHandlePosition $i]
points InsertNextPoint [lindex $pt 0] [lindex $pt 1] [lindex $pt 2]
}
set closed [isw IsClosed]
if { $closed } {
set ac [itw GetAutoClose]
if { $ac } {
set pt [isw GetHandlePosition 0]
points InsertNextPoint [lindex $pt 0] [lindex $pt 1] [lindex $pt 2]
}
isw GetPolyData spoly
stencil Update
imageActor2 SetInput [stencil GetOutput]
} else {
imageActor2 SetInput [extract GetOutput]
}
itw InitializeHandles points
}
|