[vtkusers] vtkImagePlaneWidget: interpolated cursor position

Dean Inglis dean.inglis at camris.ca
Fri Jan 9 17:16:32 EST 2004


Mostly this is for John Hunter, but others may be interested

here is a tcl example using vtkProbeFilter with vtkImageTracerWidget,
just use the right mouse button to drag the single handle around.

If I get more time, I may try to add this functinality into vtkIPW:
should be a bit of cut and paste and simplification from the Execute
of vtkProbeFilter...


Dean




package require vtk
package require vtkinteraction


# 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 SetInput [v16 GetOutput]
  shifter ReleaseDataFlagOff
  shifter Update

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

vtkRenderWindow renWin
  renWin AddRenderer ren1
  renWin SetSize 600 600

vtkInteractorStyleImage interactor

vtkRenderWindowInteractor iren
  iren SetInteractorStyle interactor
  iren SetRenderWindow renWin

vtkTextActor textActor
    textActor ScaledTextOff
    textActor SetInput " "
    [textActor GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
    [textActor GetPositionCoordinate] SetValue 0.2 0.05
    set tprop [textActor GetTextProperty]
    $tprop SetFontSize 18
    $tprop SetFontFamilyToArial
    $tprop BoldOn
    $tprop ItalicOff
    $tprop ShadowOff
    $tprop SetLineSpacing 0.9
    $tprop SetJustificationToLeft
    $tprop SetVerticalJustificationToTop
    $tprop SetColor 1 1 1
    textActor VisibilityOn

# 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 SetGlyphAngle 45.0
  [itw GetGlyphSource] Modified
  itw ProjectToPlaneOn
  itw SetProjectionNormalToXAxes
  itw SetProjectionPosition $pos
  itw SetProp imageActor
  itw SetInput [shifter GetOutput]
  itw AddObserver InteractionEvent UpdateProbe
  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
  itw On

vtkVertex vertex
  [vertex GetPointIds ] SetId 0 0

vtkPoints points
  points SetDataTypeToDouble
  points InsertNextPoint 0 0 0

vtkCellArray cell
  cell InsertNextCell vertex

vtkPolyData poly
 poly SetVerts cell
 poly SetPoints points

vtkProbeFilter probe
  probe SetSource [v16 GetOutput]
   probe SetInput poly

# Add all the actors
#

ren1 AddProp imageActor
ren1 AddProp textActor

# 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.8
ren1 ResetCameraClippingRange

renWin Render

# Prevent the tk window from showing up then start the event loop.
wm withdraw .

proc UpdateProbe { } {
  set pt [itw GetHandlePosition 0]
  points SetPoint 0 [lindex $pt 0] [lindex $pt 1] [lindex $pt 2]
  poly Modified
  probe Update
  set v [[[[ probe GetPolyDataOutput ] GetPointData] GetScalars] GetValue 0]

  set atext {}
  append atext "pt: "
  append atext [string toupper [lindex $pt 0]]
  append atext ", "
  append atext [string toupper [lindex $pt 1]]
  append atext ", "
  append atext [string toupper [lindex $pt 2]]
  append atext ": "
  append atext [string toupper $v ]
  textActor SetInput $atext
  textActor Modified
}





More information about the vtkusers mailing list