[vtkusers] Is Picking Possible?

Tron Darvann tdarvann at lab3d.odont.ku.dk
Tue Apr 26 09:24:09 EDT 2005


Hello Federico,
I use vtkCellPicker (see example in VTK user's guide) and modified the vtkImagePlaneWidget tcl example.
Here is some tcl code if you need inspiration...
Best, Tron Darvann

package require vtk
package require vtkinteraction

  

scan [[reslice1 GetOutput] GetWholeExtent] "%d %d %d %d %d %d" \
        xMin xMax yMin yMax zMin zMax

# Compute the middle of the volume - for initial slice positioning
set xMid [expr (( $xMax - $xMin ) / 2 )]
set yMid [expr (( $yMax - $yMin ) / 2 )]
set zMid [expr (( $zMax - $zMin ) / 2 )]

set spacing [[reslice1 GetOutput] GetSpacing]
set sx [lindex $spacing 0]
set sy [lindex $spacing 1]
set sz [lindex $spacing 2]

set origin [[reslice1 GetOutput] GetOrigin]
set ox [lindex $origin 0]
set oy [lindex $origin 1]
set oz [lindex $origin 2]

# An outline is shown for context.
#
vtkOutlineFilter poutline
  poutline SetInput [reslice1 GetOutput]

vtkPolyDataMapper poutlineMapper
  poutlineMapper SetInput [poutline GetOutput]

vtkActor poutlineActor
  poutlineActor SetMapper poutlineMapper

# The shared picker enables us to use 3 planes at one time
# and gets the picking order right
#
iren SetPicker picker
vtkCellPicker ppicker
  ppicker SetTolerance 0.005
  #picker SetEndPickMethod annotatePickClosest
  #picker SetEndPickMethod  annotatePickOutlineMode
  picker SetEndPickMethod annotatePick

# The 3 image plane widgets are used to probe the dataset.
#
vtkImagePlaneWidget planeWidgetX
  planeWidgetX DisplayTextOn
  planeWidgetX SetInput [reslice1 GetOutput]
  planeWidgetX SetPlaneOrientationToXAxes
  planeWidgetX SetSliceIndex $xMid
  planeWidgetX SetPicker ppicker
  planeWidgetX SetKeyPressActivationValue "x"
  set prop1 [planeWidgetX GetPlaneProperty]
  $prop1 SetColor 1 0 0  

vtkImagePlaneWidget planeWidgetY
  planeWidgetY DisplayTextOn
  planeWidgetY SetInput [reslice1 GetOutput]
  planeWidgetY SetPlaneOrientationToYAxes
  planeWidgetY SetSliceIndex $yMid
  planeWidgetY SetPicker ppicker
  planeWidgetY SetKeyPressActivationValue "y"
  set prop2 [planeWidgetY GetPlaneProperty]
  $prop2 SetColor 0 1 0
  planeWidgetY SetLookupTable [planeWidgetX GetLookupTable]

vtkImagePlaneWidget planeWidgetZ
  planeWidgetZ DisplayTextOn
  planeWidgetZ SetInput [reslice1 GetOutput]
  planeWidgetZ SetPlaneOrientationToZAxes
  planeWidgetZ SetSliceIndex $zMid
  planeWidgetZ SetPicker ppicker
  planeWidgetZ SetKeyPressActivationValue "z"
  set prop3 [planeWidgetZ GetPlaneProperty]
  $prop3 SetColor 0 0 1
  planeWidgetZ SetLookupTable [planeWidgetX GetLookupTable]

ppicker SetEndPickMethod annotatePPick

set current_widget planeWidgetZ
set mode_widget  planeWidgetZ

# Create the RenderWindow and Renderer
#
vtkRenderer ren2
vtkRenderWindow renWin2
  renWin2 AddRenderer ren2

# Add the outline actor to the renderer, set the background color and size
#
ren2 AddActor poutlineActor
renWin2 SetSize 800 800
ren2 SetBackground  0.1 0.1 0.2

# Create a cube to be shown at cursor center
vtkCubeSource cursorCube
     cursorCube SetXLength [expr $lmDiameter * 1]
     cursorCube SetYLength [expr $lmDiameter * 1]
     cursorCube SetZLength [expr $lmDiameter * 1]
     cursorCube SetCenter 0 0 0
vtkPolyDataMapper cursorCubeMapper
     cursorCubeMapper SetInput [cursorCube GetOutput]
vtkActor cursorCubeActor 
     cursorCubeActor SetMapper cursorCubeMapper
     [cursorCubeActor GetProperty] SetColor 0 1 1 
     [cursorCubeActor GetProperty] SetOpacity 1.0
     cursorCubeActor PickableOff
     cursorCubeActor VisibilityOn
     ren2 AddActor cursorCubeActor

# Create a cube to be shown at cursor center
vtkCubeSource cursorCube2
     cursorCube2 SetXLength [expr $lmDiameter * 20]
     cursorCube2 SetYLength [expr $lmDiameter * 20]
     cursorCube2 SetZLength [expr $lmDiameter * 20]
     cursorCube2 SetCenter 0 0 0
vtkPolyDataMapper cursorCubeMapper2
     cursorCubeMapper2 SetInput [cursorCube2 GetOutput]
vtkActor cursorCubeActor2 
     cursorCubeActor2 SetMapper cursorCubeMapper2
     [cursorCubeActor2 GetProperty] SetColor 1 1 1 
     [cursorCubeActor2 GetProperty] SetOpacity 1.0
     cursorCubeActor2 PickableOff
     cursorCubeActor2 VisibilityOn
     #ren2 AddActor cursorCubeActor2


# Create the GUI
#
##wm withdraw .
##destroy .top
toplevel .top2
wm title .top2 "Volume Slicer"
wm protocol .top2 WM_DELETE_WINDOW ::vtk::cb_exit

set popm [menu .top2.mm -tearoff 0]
set mode 1
$popm add radiobutton -label "nearest" -variable mode -value 0  \
           -command SetInterpolation
$popm add radiobutton -label "linear" -variable mode -value 1  \
           -command SetInterpolation
$popm add radiobutton -label "cubic" -variable mode -value 2  \
           -command SetInterpolation

set display_frame [frame .top2.f1]

set ctrl_buttons [frame .top2.btns]

pack $display_frame $ctrl_buttons \
        -side top -anchor n \
        -fill both -expand f

set quit_button [button $ctrl_buttons.btn1  \
        -text "Quit" \
        -command  ::vtk::cb_exit]

set capture_button [button $ctrl_buttons.btn2  \
        -text "Tif" \
        -command CaptureImage]

set x_button [button $ctrl_buttons.btn3  \
        -text "x" \
        -command AlignXaxis]

set y_button [button $ctrl_buttons.btn4  \
        -text "y" \
        -command AlignYaxis]

set z_button [button $ctrl_buttons.btn5  \
        -text "z" \
        -command AlignZaxis]

set goto_button [button $ctrl_buttons.btn6  \
        -text "GOTO" \
        -command GotoPosition]


set last_btn -1
bind $x_button <Button-3> "set last_btn 0; configMenu; $popm post %X %Y"
bind $y_button <Button-3> "set last_btn 1; configMenu; $popm post %X %Y"
bind $z_button <Button-3> "set last_btn 2; configMenu; $popm post %X %Y"

# Share the popup menu among buttons, keeping
# track of associated widget's interpolation mode
#
proc configMenu { } {
  global last_btn popm mode mode_widget
  if { $last_btn == 0 } {
    set mode_widget planeWidgetX
  } elseif { $last_btn == 1 } {
    set mode_widget planeWidgetY
  } else {
    set mode_widget planeWidgetZ
  }
  set mode [$mode_widget GetResliceInterpolate]    
  $popm entryconfigure $last_btn -variable mode   
}

pack $quit_button $capture_button $x_button $y_button $z_button $goto_button \
        -side left \
        -expand t -fill both

# Create the render widget
#
set renderer_frame [frame $display_frame.rFm]

pack $renderer_frame \
        -padx 3 -pady 3 \
        -side left -anchor n \
        -fill both -expand f

set render_widget [vtkTkRenderWidget $renderer_frame.r \
        -width 800 \
        -height 800 \
        -rw renWin2]

pack $render_widget $display_frame  \
        -side top -anchor n \
        -fill both -expand f

# Add a slice scale to browse the current slice stack
#

set imslice_number [$current_widget GetSliceIndex]

scale .top2.slice \
        -from $zMin \
        -to $zMax \
        -orient horizontal \
        -command SetImSlice \
        -variable imslice_number \
        -label "Slice"

pack .top2.slice \
        -fill x -expand f

proc SetImSlice {slice} {
  global current_widget 
  global imslice_number
  $current_widget SetSliceIndex $slice
  ren2 ResetCameraClippingRange
  renWin2 Render
}

::vtk::bind_tk_render_widget $render_widget
# Set the interactor for the widgets
#
set iact [[$render_widget GetRenderWindow] GetInteractor]
planeWidgetX SetInteractor $iact
planeWidgetX On
planeWidgetY SetInteractor $iact
planeWidgetY On
planeWidgetZ SetInteractor $iact
planeWidgetZ On

# Create an initial interesting view
#
set cam1 [ren2 GetActiveCamera]
$cam1 Elevation 110
$cam1 SetViewUp 0 0 -1
$cam1 Azimuth 45
ren2 ResetCameraClippingRange

[ren2 GetActiveCamera] ParallelProjectionOn

# Modify some bindings, use the interactor style 'switch'
#
set iren2 [renWin2 GetInteractor]
set istyle2 [vtkInteractorStyleSwitch istyleswitch2]
$iren2 SetInteractorStyle $istyle2
$istyle2 SetCurrentStyleToTrackballCamera


# Render it
#
$render_widget Render


# Supporting procedures
#

# Align the camera so that it faces the desired widget
#
proc AlignCamera { } {
  global ox oy oz sx sy sz xMax xMin yMax yMin zMax zMin imslice_number
  global current_widget
  set cx [expr $ox + (0.5*($xMax - $xMin))*$sx]
  set cy [expr $oy + (0.5*($yMax - $yMin))*$sy]
  set cz [expr $oy + (0.5*($zMax - $zMin))*$sz]
  set vx 0
  set vy 0
  set vz 0
  set nx 0
  set ny 0
  set nz 0
  set iaxis [$current_widget GetPlaneOrientation]
  if { $iaxis == 0 } {
    set vz -1
    set nx [expr $ox + $xMax*$sx]
    set cx [expr $ox + $imslice_number*$sx]
  }  elseif  { $iaxis == 1 } {
    set vz -1
    set ny [expr $oy + $yMax*$sy]
    set cy [expr $oy + $imslice_number*$sy]
  } else {
    set vy 1
    set nz [expr $oz + $zMax*$sz]
    set cz [expr $oz + $imslice_number*$sz]
  }
  set px [expr $cx + $nx*2]
  set py [expr $cy + $ny*2]
  set pz [expr $cz + $nz*3]

  set camera [ ren2 GetActiveCamera ]
  $camera SetViewUp $vx $vy $vz
  $camera SetFocalPoint $cx $cy $cz
  $camera SetPosition $px $py $pz
  $camera OrthogonalizeViewUp
  ren2 ResetCameraClippingRange 
  renWin2 Render
}

# Capture the display and place in a tiff
#
proc CaptureImage { } {
  vtkWindowToImageFilter w2i
  vtkTIFFWriter writer

  w2i SetInput renWin2
  w2i Update
  writer SetInput [w2i GetOutput]
  writer SetFileName image.tif
  renWin2 Render
  writer Write

  writer Delete
  w2i Delete
}

proc GotoPosition { } {
   global xp yp zp
   planeWidgetX SetSlicePosition $xp
   planeWidgetY SetSlicePosition $yp
   planeWidgetZ SetSlicePosition $zp
   cursorCube SetCenter $xp $yp $zp
   renWin2 Render
}


# Align the widget back into orthonormal position,
# set the slider to reflect the widget's position,
# call AlignCamera to set the camera facing the widget
#
proc AlignXaxis { } {
  global xMax xMin current_widget imslice_number
  set po [ planeWidgetX GetPlaneOrientation ]
  if { $po == 3 } {
    planeWidgetX SetPlaneOrientationToXAxes
    set imslice_number [expr ($xMax - $xMin)/2]
    planeWidgetX SetSliceIndex $imslice_number
  } else {
    set imslice_number [planeWidgetX GetSliceIndex]
  }
  set current_widget planeWidgetX
  .top2.slice config -from $xMin -to $xMax 
  .top2.slice set $imslice_number
  AlignCamera
}

proc AlignYaxis { } {
  global yMin yMax current_widget imslice_number
  set po [ planeWidgetY GetPlaneOrientation ]
  if { $po == 3 } {
    planeWidgetY SetPlaneOrientationToYAxes
    set imslice_number [expr ($yMax - $yMin)/2]
    planeWidgetY SetSliceIndex $imslice_number
  } else {
    set imslice_number [planeWidgetY GetSliceIndex]
  }
  set current_widget planeWidgetY
  .top2.slice config -from $yMin -to $yMax
  .top2.slice set $imslice_number
  AlignCamera
}

proc AlignZaxis { } {
  global zMin zMax current_widget imslice_number
  set po [ planeWidgetZ GetPlaneOrientation ]
  if { $po == 3 } {
    planeWidgetZ SetPlaneOrientationToZAxes
    set imslice_number [expr ($zMax - $zMin)/2]
    planeWidgetZ SetSliceIndex $imslice_number
  } else {
    set imslice_number [planeWidgetZ GetSliceIndex]
  }
  set current_widget planeWidgetZ
  .top2.slice config -from $zMin -to $zMax 
  .top2.slice set $imslice_number
  AlignCamera
}

# Set the widget's reslice interpolation mode
# to the corresponding popup menu choice
#
proc SetInterpolation { } {
  global mode_widget mode 
  if { $mode == 0 } {
    $mode_widget TextureInterpolateOff
  } else {
    $mode_widget TextureInterpolateOn
  }
  $mode_widget SetResliceInterpolate $mode
  renWin2 Render
}

proc annotatePPick {} {
    global lmCoord lmIndex setbuttonvalue setarr CurrentPos CurrentId CurrentVal
    global aSplineX aSplineY aSplineZ SplineCheck xp yp zp
    global colors existcolorfile
    global lmCellIdArr
    if { [ppicker GetCellId] < 0 } {
        #lmsphereActor$lmIndex VisibilityOff
        #set pickId -1
    } else {
 
	#set selPt [ppicker GetSelectionPoint]
        #set pickId [picker GetCellId]
        #set lmCellIdArr($lmIndex) $pickId
        #set CurrentId [format "Id: %10d" $pickId]
        #set CurrentVal " "
        #if {$existcolorfile == 1} {
        #    set pickVal [colors GetScalar $pickId]
        #    set CurrentVal [format "Value: %18.6f" $pickVal]
        #}
	#set x [lindex $selPt 0] 
	#set y [lindex $selPt 1]
	set pickPos [ppicker GetPickPosition]
	set xp [lindex $pickPos 0] 
	set yp [lindex $pickPos 1]
	set zp [lindex $pickPos 2]

      #planeWidgetX SetSlicePosition $xp
      #planeWidgetY SetSlicePosition $yp
      #planeWidgetZ SetSlicePosition $zp
        cursorCube SetCenter $xp $yp $zp
        cursorCube2 SetCenter $xp $yp $zp

        lmsphere$lmIndex SetCenter $xp $yp $zp
        lmsphereActor$lmIndex VisibilityOn
        lmCoord InsertPoint $lmIndex $xp $yp $zp
        set setarr($lmIndex) 1
        set setbuttonvalue 1  
        #SetIndex -1
        #SetSetLandmark 
        SetSetPos 
        if {$SplineCheck == 1} {
          UpdateSpline
        }
    }
    renWin Render
}









More information about the vtkusers mailing list