# This example demonstrates cell picking using vtkCellPicker.  It displays
# the results of picking using a vtkTextMapper.

#
# First we include the VTK Tcl packages which will make available
# all of the vtk commands to Tcl
#
package require vtk
package require vtkinteraction

# Pentagonal prism

vtkPoints hexaPoints
  hexaPoints SetNumberOfPoints 12 
  hexaPoints InsertPoint 0  1.0   0.0 0.0
  hexaPoints InsertPoint 1  0.5  -1.0 0.0001
  hexaPoints InsertPoint 2 -0.5  -1.0 0.0
  hexaPoints InsertPoint 3 -1.0   0.0 0.0
  hexaPoints InsertPoint 4 -0.5   1.0 0.0
  hexaPoints InsertPoint 5  0.5   1.0 0.0
  hexaPoints InsertPoint 6  1.0   0.0 1.0
  hexaPoints InsertPoint 7  0.5  -1.0 1.0001
  hexaPoints InsertPoint 8 -0.5  -1.0 1.0
  hexaPoints InsertPoint 9 -1.0   0.0 1.0
  hexaPoints InsertPoint 10 -0.5   1.0 1.0
  hexaPoints InsertPoint 11  0.5   1.0 1.0

vtkPentagonalPrism aPenta
  [aPenta GetPointIds] SetId 0 0
  [aPenta GetPointIds] SetId 1 1
  [aPenta GetPointIds] SetId 2 2
  [aPenta GetPointIds] SetId 3 3
  [aPenta GetPointIds] SetId 4 4
  [aPenta GetPointIds] SetId 5 6
  [aPenta GetPointIds] SetId 6 7
  [aPenta GetPointIds] SetId 7 8
  [aPenta GetPointIds] SetId 8 9
  [aPenta GetPointIds] SetId 9 10
  
  vtkHexagonalPrism aHexa
  [aHexa GetPointIds] SetId 0 0
  [aHexa GetPointIds] SetId 1 1
  [aHexa GetPointIds] SetId 2 2
  [aHexa GetPointIds] SetId 3 3
  [aHexa GetPointIds] SetId 4 4
  [aHexa GetPointIds] SetId 5 5
  [aHexa GetPointIds] SetId 6 6
  [aHexa GetPointIds] SetId 7 7
  [aHexa GetPointIds] SetId 8 8
  [aHexa GetPointIds] SetId 9 9
  [aHexa GetPointIds] SetId 10 10
  [aHexa GetPointIds] SetId 11 11

vtkUnstructuredGrid aGrid
  aGrid Allocate 1 1
  #aGrid InsertNextCell [aPenta GetCellType] [aPenta GetPointIds]
  aGrid InsertNextCell [aHexa GetCellType] [aHexa GetPointIds]
  aGrid SetPoints hexaPoints

vtkDataSetMapper aMapper
  aMapper SetInput aGrid

vtkActor aActor
  aActor SetMapper aMapper
  aActor AddPosition 0 0 0
#  [aActor GetProperty] BackfaceCullingOn

vtkAxesActor axes
  axes SetShaftTypeToCylinder
  axes SetXAxisLabelText "x"
  axes SetYAxisLabelText "y"
  axes SetZAxisLabelText "z"
  axes SetTotalLength 2 2 2
vtkTextProperty tprop
  tprop ItalicOn
  tprop ShadowOn
  tprop SetFontFamilyToTimes
  [ axes GetXAxisCaptionActor2D ] SetCaptionTextProperty tprop
  vtkTextProperty tprop2
  tprop2 ShallowCopy tprop
  [ axes GetYAxisCaptionActor2D ] SetCaptionTextProperty tprop2
  vtkTextProperty tprop3
  tprop3 ShallowCopy tprop
  [ axes GetZAxisCaptionActor2D ] SetCaptionTextProperty tprop3


# Create a cell picker.
vtkCellPicker picker
    picker AddObserver EndPickEvent annotatePick

# Create a text mapper and actor to display the results of picking.
vtkTextMapper textMapper
set tprop [textMapper GetTextProperty]
    $tprop SetFontFamilyToArial
    $tprop SetFontSize 14
    $tprop BoldOn
    $tprop ShadowOn
    $tprop SetColor 1 0 0
vtkActor2D textActor
    textActor VisibilityOff
    textActor SetMapper textMapper

# Create the Renderer, RenderWindow, and RenderWindowInteractor
#
vtkRenderer ren1
vtkRenderWindow renWin
    renWin AddRenderer ren1
vtkRenderWindowInteractor iren
    iren SetRenderWindow renWin
    iren SetPicker picker

# Add the actors to the renderer, set the background and size
#
ren1 AddActor2D textActor
ren1 AddActor aActor
ren1 AddActor axes

ren1 SetBackground 1 1 0
renWin SetSize 300 300

# Get the camera and zoom in closer to the image.
set cam1 [ren1 GetActiveCamera]
$cam1 Zoom 1.4

# Set the user method (bound to key 'u')
iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize

# Withdraw the default tk window
wm withdraw .

# Create a Tcl procedure to create the text for the text mapper used to
# display the results of picking.
proc annotatePick {} {
    if { [picker GetCellId] < 0 } {
	textActor VisibilityOff

    } else {
	set selPt [picker GetSelectionPoint]
	set x [lindex $selPt 0] 
	set y [lindex $selPt 1]
	set pickPos [picker GetPickPosition]
	set xp [lindex $pickPos 0] 
	set yp [lindex $pickPos 1]
	set zp [lindex $pickPos 2]

	textMapper SetInput "($xp, $yp, $zp)"
	textActor SetPosition $x $y
	textActor VisibilityOn
    }

    renWin Render
}

# Pick the cell at this location.
picker Pick 85 126 0 ren1
