
# This code creates the user interface for tetra.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc modify_smooth {tetra} {
    global aspect_show aspect_smooth aspect_iterations botform_geo
    global aspect_numvals improver_prompt last_line smooth_show_solid
    global aspect_minval aspect_maxval
    
    #set fp [improver_get $tetra]
    #set lines [read_until_prompt $fp $improver_prompt "ratio_histogram 20"]
    
    command "initialize_aspect $tetra relaspect"
    if !$last_line { error "Can't run smoother -- no tetras are available." }
    
    command "ratio_histogram $tetra 20" 0
    set lines $last_line
    if ![info exists aspect_iterations] {
	set aspect_iterations 20
	set smooth_show_solid 0
    }
    
    form_kill .aspect
    
    set d [form_init .aspect "Smoother (aspect ratio)" modify done]
    form_frame $d.fr sunken 3 {0 0}; set d $d.fr
    form_label $d.minl "Minimum" {0 1}
    form_label $d.maxl "Maximum" {0 3}
    form_label $d.numl "Count" {0 5}
    form_label $d.showl "Show" {0 7}
    form_label $d.smoothl "Smooth" {0 9}
    foreach i {2 4 6 8} { form_colwidth $d $i 4 }
    foreach i {1 3 5 7 9} {
	form_frame $d.ul_$i flat 1 "1 $i"
	$d.ul_$i configure -background black
    }
    
    set total_elements 0
    set lno 0
    foreach l $lines {
	set i [expr {$lno + 5}]
	set num [lindex $l 0]
	set min [lindex $l 1]
	set max [lindex $l 2]
	if {$max == ""} continue
	
	form_label $d.min$i [format %.2f $min] "$i 1"
	form_label $d.max$i [format %.2f $max] "$i 3"
	form_label $d.num$i $num "$i 5"
	$d.num$i configure -textvariable aspect_numvals($lno)
	set aspect_numvals($lno) $num
	set aspect_minval($lno) $min
	set aspect_maxval($lno) $max
	incr total_elements $num
	
	form_checkbutton $d.show$i "" aspect_show($lno) "$i 7"
	$d.show$i configure -command "aspect_show_tetras $tetra $lno"
	form_radiobutton $d.smooth$i "" aspect_smooth $max "$i 9"
	if {($aspect_smooth == "") && ($lno == 10)} {
	    set aspect_smooth $max
	}
	incr lno
    }
    form_colwidth $d 5 [expr {log10($total_elements) * 2 + 6}] m
    form_rowheight $d 30 10
    form_label $d.itersl Iterations {31 1 -cols 4}
    form_entry $d.iters aspect_iterations int {31 5 -cols 3}
    bind $d.iters <Return> "aspect_do_smooth $tetra; break"
    
    form_frame $d.buts flat 0 {32 1 -cols -}
    form_button $d.buts.smooth "Perform smoothing" "aspect_do_smooth $tetra" \
	    {0 0}
    #form_checkbutton $d.buts.solid "Show solid" smooth_show_solid {0 1}
    form_finish $botform_geo
}

proc aspect_show_tetras {tetra num} {
    global aspect_show last_line smooth_show_solid aspect_minval aspect_maxval
    
    set vals "$aspect_show($num) $aspect_minval($num) $aspect_maxval($num)"
    
    command "show_tetras_aspect $tetra $smooth_show_solid $vals"
    mess "Number of elements = $last_line\n"
}

proc aspect_do_smooth {tetra} {
    global aspect_smooth aspect_iterations aspect_show improver_prompt
    global last_line
    
    if {$aspect_smooth == ""} {
	mess "No ratios have been selected for smoothing.\n" red
	return
    }
    
    if $tetra {
	command "clear_tetras_aspect 1"
	command "smooth_tetras $aspect_smooth $aspect_iterations"
	command "ratio_histogram 1 20"
	set lines $last_line
    } else {
	command "aspect_mark $tetra $aspect_smooth"
	mess "Marked [lindex $last_line 0] out of [lindex $last_line 1]\n"
	command "smooth_marked $tetra 20 $aspect_iterations"
	set lines $last_line
    }
    command "initialize_aspect $tetra relaspect"
    if [info exists aspect_show] {
	aspect_update_nums $lines $tetra
    }
}

proc aspect_update_nums {lines tetra} {
    global aspect_numvals aspect_show improver_prompt smooth_show_solid
    
    set lno 0
    set stuff ""
    foreach l $lines {
	set num [lindex $l 0]
	set min [lindex $l 1]
	set max [lindex $l 2]
	if {$max == ""} continue
	
	if [info exists aspect_numvals($lno)] {
	    set v $aspect_numvals($lno)
	    set onum [lindex $v [expr {[llength $v] - 1}]]
	    set num "$onum -> $num"
	}
	set aspect_numvals($lno) $num
	append stuff " $aspect_show($lno) $min $max"
	incr lno
    }
    command "show_tetras_aspect $tetra $smooth_show_solid $stuff"
}

# This proc returns the file pointer for the improver process.  If there
# is none then it starts the improver.  If something has changed then
# it exists the improver.

proc old__improver_get {tetra} {
    global improver_fp leo_mesh_dirty cut_domain tetin_file improver_bin
    global config_dir tri_improver_bin improver_is_tetra improver_prompt
    
    if {[info exists improver_fp] && ($improver_is_tetra != $tetra)} {
	puts $improver_fp return
	close $improver_fp
	unset improver_fp
	unset improver_is_tetra
    }
    if $tetra {
	set improver_prompt improver%
	set path $improver_bin
    } else {
	set improver_prompt tsmooth%
	set path $tri_improver_bin
    }
    
    busy 1
    # This is screwed up ... ******
    command "write_tetra_domain $config_dir(domains)/__temp_smooth" 0
    if [info exists improver_fp] {
	puts $improver_fp return
	close $improver_fp
	unset improver_fp
	unset improver_is_tetra
    }
    if ![info exists improver_fp] {
	set command "$path $config_dir(domains)/__temp_smooth \
		$config_dir(mesh)/$tetin_file"
	set improver_fp [open "| $command" w+]
	read_until_prompt $improver_fp $improver_prompt
	set improver_is_tetra $tetra
    }
    busy 0
    if [eof $improver_fp] {
	mess "Improver has exited\n" red
	close $improver_fp
	unset improver_fp
	unset improver_is_tetra
	return ""
    } else {
	return $improver_fp
    }
}

# Call this when we are done with the improver and it may have changed
# something.

proc old__improver_update {} {
    global improver_fp cut_domain config_dir improver_prompt
    
    read_until_prompt $improver_fp $improver_prompt save
    reload_leo_domain $config_dir(domains)/__temp_smooth 0
}
