
# This code creates the user interface for the hexa block smoother.
#
# Copyright (c) 1998 1999 Pacific Marketing and Consulting, Inc.
#       	All rights reserved.
# Author:  Jari Spaete jsp@icemcfd.de

proc mesh_smooth_glsm_unstruct {block} {
  etm_UnstructWin $block
}

proc etm_PerformUnstructHexa {} {
  global mesh_dir tool_dir etmUnstruct

  if {$etmUnstruct(How) != $etmUnstruct(ModeLaplace)} {
    if [string match "" $etmUnstruct(Threshold)] {
      mess "No threshold selected.\n" red
      return
    }
  }    
  set etm_dir $mesh_dir
   
  if {$etmUnstruct(Block) == "1"} {
    set mess "block smoothing"
  } elseif {$etmUnstruct(Block) == "2"} {
    set mess "super block smoothing"
  } else {
    set mess "unstruct smoothing"
  }
  mess "Performing $mess ...\n"

  busy 1
  set cmd "smooth $etmUnstruct(Iters)"
  if $etmUnstruct(Block) { 
    if { $etmUnstruct(Block) == "1" } { 
###   append cmd " blocks"
      append cmd " super_blocks"
    } elseif { $etmUnstruct(Block) == "2" } {
      append cmd " super_blocks"
    }
  }
  append cmd " [ volume_activename_string ] sm_param $mesh_dir"
  append cmd " type_selected $etmUnstruct(How)"
  if {$etmUnstruct(How) == $etmUnstruct(ModeLaplace)} {
     append cmd " no_step $etmUnstruct(Iters)"
  }
  append cmd " sfit $etmUnstruct(Sfit)"
  if {$etmUnstruct(How) == $etmUnstruct(ModeDeterminant) || \
      $etmUnstruct(How) == $etmUnstruct(ModeQuality) || \
      $etmUnstruct(How) == $etmUnstruct(ModeSkewness)} {
    append cmd " limit_bad_determ $etmUnstruct(Threshold)"
  }
  if {($etmUnstruct(How) == $etmUnstruct(ModeAngle)) || \
      ($etmUnstruct(How) == $etmUnstruct(ModeWarp))} {
    append cmd " limit_bad_angle $etmUnstruct(Threshold)"
  }
  append cmd " ign_prescribed_points $etmUnstruct(IgnPrescribedPoints)"
  append cmd " fix_boundaries $etmUnstruct(FixBoundaries)"  
  command "$cmd"
  catch { interrupt finish }
  etm_SetValuesUnstruct
  set cmd "gls_display_bucket"
  command $cmd
  beep
  update_display edge face block mesh
  busy 0
  mess "Done $mess.\n"
}

proc etm_ShowBuckets { what } {
global $what

  busy 1 
  set numB ""
  for {set i 0} {$i < [set $what\(NoIntervall)]} {incr i} {
    if [set $what\(ShowIntervall,$i)] { lappend numB " $i" }
  }
  set lenB [llength $numB]
  regsub -all \{ $numB "" numB; regsub -all \} $numB "" numB    
  set cmd "gls_display_bucket n_buckets $lenB buckets $numB"
  append cmd " uns"
  command $cmd
  busy 0
}

proc etm_UnstructWin { what } {
global etmUnstruct last_line
  
  if [ winfo exists .unstrsm ] {
    destroy .unstrsm
    catch { etmUnstruct }
  }
  set etmUnstruct(Block) $what
  etm_DefaultParameterUnstruct
  if {$etmUnstruct(Block) == "1"} { 
    set title "Block smoothing"
  } elseif {$etmUnstruct(Block) == "2"} {
    set title "Super Block smoothing"
  } else {
    set title "Unstruct mesh smoothing"
  }
  set etmUnstruct(How) $etmUnstruct(ModeDeterminant)
  set cmd "mesh_dimension"
  command $cmd 0
  if {$last_line == "2"} {
    set etmUnstruct(TypeQuad) 1
    set etmUnstruct(MeshType) $etmUnstruct(MeshTypeQuad)
  } elseif {$last_line == "3"} {
    set etmUnstruct(TypeHexa) 1
    set etmUnstruct(MeshType) $etmUnstruct(MeshTypeHexa)
  }
  if ![ string match Windows* [ winfo server . ] ] {
    catch { wm geometry .unstrsm -15-30 }
  } else {
    catch { wm geometry .unstrsm +50+50 }
  }
  catch { wm withdraw .unstrsm }
  
  etm_SetCancelParameterUnstruct

  set buttons {
    {"Perform" "etm_PerformUnstructHexa"}
    {"Parameter" "etm_ParameterUnstruct"}
    {"Dismiss" "etm_DoneUnstruct"}
  }

  set win [form_init .unstrsm "$title" "" $buttons]
  if {$win == ""} { return }
  set etmUnstruct(MainWin) $win
  form_frame $win.1 ridge 3 "0 0"
  form_frame $win.1.0 flat 0 "0 0"
  form_frame $win.1.1 flat 0 "1 0"
  form_frame $win.1.2 flat 0 "1 1"
  form_frame $win.1.3 flat 0 "1 2"
  form_frame $win.2 ridge 3 "1 0"
  form_label $win.1.0.m "Smoothing methods:" "0 0 -pady 10"
  set com "etm_SetValuesUnstruct"  
  set val $etmUnstruct(ModeDeterminant)
  form_radiobutton $win.1.1.det "Determinant" etmUnstruct(How) $val "0 0" "" "$com"
  set val $etmUnstruct(ModeLaplace)
  form_radiobutton $win.1.1.lap "Laplace" etmUnstruct(How) $val "1 0" "" "$com"
  set val $etmUnstruct(ModeSkewness)
  form_radiobutton $win.1.2.ske "Skewness" etmUnstruct(How) $val "0 1" "" "$com"
  set val $etmUnstruct(ModeQuality)
  form_radiobutton $win.1.2.qua "Quality" etmUnstruct(How) $val "1 1" "" "$com"
  set val $etmUnstruct(ModeAngle)
  form_radiobutton $win.1.3.ang "Orthogonality" etmUnstruct(How) $val "0 2" "" "$com"
  set val $etmUnstruct(ModeWarp)
  form_radiobutton $win.1.3.war "Warp" etmUnstruct(How) $val "1 2" "" "$com"
  
  etm_SetModesUnstruct $win 0  
  for {set i 0} {$i < $etmUnstruct(NoIntervall)} {incr i} {
    if !$i {
      form_frame $win.2.t flat 0 "0 0 -cols 6"
      form_label $win.2.t.t1 "Elements" "0 0"
      $win.2.t.t1 configure -width 8
      form_label $win.2.t.t2 "" "0 1"
      $win.2.t.t2 configure -width 5
      form_label $win.2.t.t3 "between" "0 2"
      $win.2.t.t3 configure -width 8
      form_label $win.2.t.t4 "" "0 3"
      $win.2.t.t4 configure -width 5
      form_label $win.2.t.t5 "Show" "0 4"
      $win.2.t.t5 configure -width 7
      form_label $win.2.t.t6 "Smooth" "0 5"
      $win.2.t.t6 configure -width 7
    }
    form_frame $win.2.$i flat 0 "[expr $i + 1] 0 -cols 7"
    form_label $win.2.$i.1 "$etmUnstruct(ShowElements,$i)" "0 0"
    $win.2.$i.1 configure -textvariable etmUnstruct(ShowElements,$i) \
    	-bg white -relief ridge -bd 3 -width 8
    form_label $win.2.$i.2 "" "0 1"
    $win.2.$i.2 configure -textvariable etmUnstruct(Elements,from,$i) \
    	-bg white -relief ridge -bd 3 -width 7
    form_label $win.2.$i.3 "and" "0 2"
    form_label $win.2.$i.4 "" "0 3"
    $win.2.$i.4 configure -textvariable etmUnstruct(Elements,to,$i) \
    	-bg white -relief ridge -bd 3 -width 7
    form_label $win.2.$i.5 "" "0 4"
    $win.2.$i.5 configure -width 1
    set com "etm_ShowBuckets etmUnstruct"
    form_checkbutton $win.2.$i.6 "" etmUnstruct(ShowIntervall,$i) "0 5" "" "$com"
    $win.2.$i.6 configure -width 4
    form_radiobutton $win.2.$i.7 "" etmUnstruct(Threshold) $i "0 6" "" ""
    $win.2.$i.7 configure -width 6
  }    
  etm_SetValuesUnstruct
  if ![string match Windows* [winfo server .]] {
    form_finish -15-30 1 0
  } else {
    form_finish +50+50 1 0
  }
}

proc etm_SetModesUnstruct { win on } {
global etmUnstruct
    
  for { set i 0 } { $i < $etmUnstruct(NoIntervall) } { incr i } {
    set etmUnstruct(ShowElements,$i) ""
    set etmUnstruct(ShowedElements,$i) 0
    set etmUnstruct(ShowIntervall,$i) 0
  }
  set etmUnstruct(Threshold) ""
  if { $etmUnstruct(MeshType) == $etmUnstruct(MeshTypeHexa) } {
      $win.1.1.det configure -state normal
      $win.1.1.lap configure -state normal
      $win.1.2.ske configure -state normal
      $win.1.2.qua configure -state normal
      $win.1.3.ang configure -state normal
      $win.1.3.war configure -state normal    
  } elseif { $etmUnstruct(MeshType) == $etmUnstruct(MeshTypeQuad) } { 
      $win.1.1.det configure -state normal
      $win.1.1.lap configure -state normal
      $win.1.2.ske configure -state disabled
      $win.1.2.qua configure -state disabled
      $win.1.3.ang configure -state disabled
      $win.1.3.war configure -state normal
  }
  if $on {
    etm_SetValuesUnstruct
  }
}

proc etm_SetValuesUnstruct {} {
global etmUnstruct

  catch { unset etmUnstruct(Threshold) }
  if { $etmUnstruct(How) == $etmUnstruct(ModeLaplace) } {
    set etmUnstruct(Min) ""
    set etmUnstruct(Max) ""
    set etmUnstruct(Threshold) 20.0
    set etmUnstruct(C_FixBoundaries) $etmUnstruct(FixBoundaries)
    set etmUnstruct(FixBoundaries) 1
    foreach item [winfo children $etmUnstruct(MainWin).2.t] {
      $item configure -state disabled
    }
    for {set i 0} {$i < $etmUnstruct(NoIntervall)} {incr i} {
      foreach item [winfo children $etmUnstruct(MainWin).2.$i] {
        $item configure -state disabled
      }
    }
    return
  } else {
    foreach item [winfo children $etmUnstruct(MainWin).2.t] {
      $item configure -state normal
    }
    for {set i 0} {$i < $etmUnstruct(NoIntervall)} {incr i} {
      foreach item [winfo children $etmUnstruct(MainWin).2.$i] {
        $item configure -state normal
      }
    }
  }
  busy 1  
  if [info exists etmUnstruct(C_FixBoundaries)] {
    set etmUnstruct(FixBoundaries) $etmUnstruct(C_FixBoundaries)
  }
  if {($etmUnstruct(How) == $etmUnstruct(ModeDeterminant)) || \
      ($etmUnstruct(How) == $etmUnstruct(ModeSkewness)) || \
      ($etmUnstruct(How) == $etmUnstruct(ModeQuality)) || \
      ($etmUnstruct(How) == $etmUnstruct(ModeAspectRatio))} {
    set etmUnstruct(GetIntervall) 2
  } elseif {($etmUnstruct(How) == $etmUnstruct(ModeAngle)) || \
	    ($etmUnstruct(How) == $etmUnstruct(ModeWarp))} {
    set etmUnstruct(GetIntervall) 1
  }
  set etmUnstruct(Intervall) [ etm_CreateBucketsListUnstruct ] 
  for {set i 0} {$i < $etmUnstruct(NoIntervall)} {incr i} {
    set iv [lindex $etmUnstruct(Intervall) $i]
    set etmUnstruct(ShowElements,$i) [lindex $iv 0]
    set etmUnstruct(ShowedElements,$i) 0
    set etmUnstruct(Elements,from,$i) [lindex $iv 1]
    set etmUnstruct(Elements,to,$i) [lindex $iv 2]
    set etmUnstruct(ShowIntervall,$i) 0
    set etmUnstruct(Threshold) [lindex $iv $etmUnstruct(GetIntervall)]
  }
  busy 0
}

proc etm_CreateBucketsListUnstruct {} {
global etmUnstruct last_line
    
    set cmd "smooth 1"
    if $etmUnstruct(Block) { 
      if { $etmUnstruct(Block) == "1" } { 
###        append cmd " blocks"
        append cmd " super_blocks"
      } elseif { $etmUnstruct(Block) == "2" } {
        append cmd " super_blocks"
      }
    }
    append cmd " [ volume_activename_string ] histogram sm_param"
    append cmd " $etmUnstruct(NoIntervall) type_selected $etmUnstruct(How)"
    command $cmd 0
    return $last_line
}

proc etm_DoneUnstruct {} {
global etmUnstruct

  if $etmUnstruct(Block) { 
    command "gls_init uns_blocks"
  } else {
    command "gls_init uns_mesh"
  }
  command "gls_display_bucket n_buckets 0 uns"  
  unset etmUnstruct
  destroy .unstrsm
}

proc etm_ParameterUnstruct {} {
global etmUnstruct

  if [ winfo exists .unstrpm ] { destroy .unstrpm }  
  if {$etmUnstruct(Block) == "1"} { 
    set title "Block parameter"
  } elseif {$etmUnstruct(Block) == "2"} {
    set title "Super Block parameter"
  } else {
    set title "Unstruct parameter"
  }
  etm_SetCancelParameterUnstruct
  set buttons {
    {"Accept" "etm_DoneParameterUnstruct"}
    {"Reset" "etm_ResetParameterUnstruct"}
  }
  set win [form_init .unstrpm "$title" "" $buttons]
  if {$win == ""} { return }
  set etmUnstruct(ParamsWin) .unstrpm
  form_frame $win.1 ridge 3 "0 0"
  form_frame $win.1.1 flat 3 "1 0"
  form_frame $win.1.2 flat 3 "2 0"
  form_frame $win.2 flat 3 "1 0" 

  form_label $win.1.1.txt "Iterations" "0 0"
  futil_updown $win.1.1.ite etmUnstruct(Iters) "1" "999" "0 1"
  form_checkbutton $win.1.2.sur "Surface fitting" etmUnstruct(Sfit) "1 0" "" ""
  form_checkbutton $win.1.2.ign "Ignore Prescribed Points" etmUnstruct(IgnPrescribedPoints) "2 0" "" ""
  form_checkbutton $win.1.2.fix "Fix boundaries" etmUnstruct(FixBoundaries) "3 0" "" ""
  if ![string match Windows* [winfo server .]] {
    form_finish -15-30 1 0
  } else {
    form_finish +50+50 1 0
  }
}

proc etm_DoneParameterUnstruct {} {
global etmUnstruct
  
  destroy $etmUnstruct(ParamsWin)
}

proc etm_DefaultParameterUnstruct {} { 
global etmUnstruct

  set etmUnstruct(ModeDeterminant) 0
  set etmUnstruct(ModeAngle) 1
  set etmUnstruct(ModeWarp) 2
  set etmUnstruct(ModeLaplace) 3
  set etmUnstruct(ModeQuality) 4
  set etmUnstruct(ModeSkewness) 5
  set etmUnstruct(ModeAspectRatio) 6
  set etmUnstruct(Iters) 5
  set etmUnstruct(Sfit) 1
  set etmUnstruct(IgnPrescribedPoints) 0
  set etmUnstruct(FixBoundaries) 0
  set etmUnstruct(AlgorithmUnstruct) 0
  set etmUnstruct(AlgorithmPost) 1
  set etmUnstruct(WhichVisible) visible
  set etmUnstruct(WhichAll) all
  set etmUnstruct(Which) $etmUnstruct(WhichAll)
  set etmUnstruct(MeshTypeHexa) 0
  set etmUnstruct(MeshTypePrism) 1
  set etmUnstruct(MeshTypeQuad) 2
  set etmUnstruct(MeshTypeTetra) 3
  set etmUnstruct(MeshTypeTri) 4
  set etmUnstruct(MeshTypePyra) 5
  set etmUnstruct(NoIntervall) 20
  set etmUnstruct(L2Q) 0
}

proc etm_SetCancelParameterUnstruct {} { 
global etmUnstruct

  set etmUnstruct(C_Iters) $etmUnstruct(Iters) 
  set etmUnstruct(C_Sfit) $etmUnstruct(Sfit) 
  set etmUnstruct(C_IgnPrescribedPoints) $etmUnstruct(IgnPrescribedPoints) 
  set etmUnstruct(C_FixBoundaries) $etmUnstruct(FixBoundaries)
}

proc etm_ResetParameterUnstruct {} {
global etmUnstruct

  set etmUnstruct(Iters) $etmUnstruct(C_Iters) 
  set etmUnstruct(Sfit) $etmUnstruct(C_Sfit) 
  set etmUnstruct(IgnPrescribedPoints) $etmUnstruct(C_IgnPrescribedPoints) 
  set etmUnstruct(FixBoundaries) $etmUnstruct(C_FixBoundaries)
}
