#! /usr/bin/vtk


#
#                     Copyright Notice
#
# This software was developed at the National Institute of Standards
# and Technology (NIST) by employees of the Federal Government in the
# course of their official duties. Pursuant to title 17 Section 105
# of the United States Code this software is not subject to copyright
# protection and is in the public domain.
#
# This is an experimental system.  NIST assumes no responsibility
# whatsoever for its use by other parties, and makes no guarantees,
# expressed or implied, about its quality, reliability, or any other
# characteristic.
#
# We would appreciate acknowledgment if the software is used.
#

#

catch {load vtktcl}
#source ~/.wishrc
#source vtkInt.tcl
#source colors.tcl


if {$argc != 3} {
   puts "Usage: hev-tclVtkIsosurface value input.vtk output.ply"
   exit
}

puts "nr arguments = $argc"
puts "argv = $argv"
puts "argv0 = $argv0"

set isoValue  [lindex $argv 0]
set infile    [lindex $argv 1]
set outfile   [lindex $argv 2]

puts "isoValue = $isoValue"
puts "infile = $infile"
puts "outfile = $outfile"


vtkRenderer ren1
vtkRenderWindow renWin
  renWin AddRenderer ren1
vtkRenderWindowInteractor iren
  iren SetRenderWindow renWin

# create pipeline

vtkStructuredPointsReader reader
    reader SetFileName $infile
# niet nodig reader Update

set level $isoValue

#
# The following doesn't do what I want. I want to try to compute a median
# or an average as the isoValue. Median might be interesting with the
# equal number of values above and below
#    set valuerange [[reader GetOutput] GetScalarRange]
#    set minv [lindex $valuerange 0]
#    set maxv [lindex $valuerange 1]
#    set level [expr 0.5*($maxv + $minv)]
#puts "valuerange $valuerange"
#puts "minv = $minv   maxv = $maxv"
puts "level = $level"

#vtkMarchingCubes iso
#    iso SetInput [reader GetOutput]
#    iso SetValue 0 $level

vtkContourFilter iso
    iso SetInput [reader GetOutput]
    iso SetValue 0 $level
#    iso ComputeScalarsOff
#    iso ComputeGradientsOff

vtkPolyDataMapper isoMapper
    isoMapper SetInput [iso GetOutput]
    isoMapper ScalarVisibilityOff

vtkActor isoActor
    isoActor SetMapper isoMapper
#    eval [isoActor GetProperty] SetColor $antique_white
    eval [isoActor GetProperty] SetColor 0.9804 0.9216 0.8431

# assign our actor to the renderer
    ren1 AddActor isoActor

#    ren1 SetBackground 1 1 1
#    renWin SetSize 500 500




vtkPolyDataConnectivityFilter connect
    connect SetInput [iso GetOutput]
    connect SetExtractionModeToLargestRegion




vtkPLYWriter ply
  ply SetFileTypeToASCII
#  ply SetInput [renWin GetOutput]
#  ply SetInput [iso GetOutput]
  ply SetInput [connect GetOutput]
#  iv SetFileName foo.iv
  ply SetFileName $outfile
  ply Write


# render the image
#iren Initialize

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