

proc aie_v3_dynamic_surface {what args} {
   global dynamic_info

   switch -- $what {
      multiple_step {
         multiple_dynamic_surfaces step $dynamic_info(mulcuts)
      }
      copy_current {
         V3_dynamic_surface copy_current
      }
      set_selected {
         update_dynamic_selected
      }
   }
}


proc aie_update_dynamic_scan {i args} {
   global dynamic_info v3_viewer

   set rr ""
   if {$i == 0} {
      set rr [V3_dynamic_surface scan $dynamic_info(an_val) 0]
   }  elseif {$i < 0} {
      set s $dynamic_info(an_val)
      set rr [V3_dynamic_surface scan $dynamic_info(an_val)]
      set dynamic_info(an_val) $s
   }
   if {$rr == 0} {
      if {![info exist dynamic_info(win)] || \
          ![winfo exist $dynamic_info(win)]} {set dynamic_info(win) $v3_viewer}
      redraw_now $dynamic_info(win)
   } 
}

proc onv3DynamicIsoDEZApply {} {
   global iso_info
   if ![string is double -strict $iso_info(value)] {return ""}

   set val [V3_iso_surface_get val]
   if {$iso_info(value) != $val} { update_iso_form val }
}

proc onv3DynamicIsoDEZLoad {} {
   global iso_info

   if {![info exist ::iso_info(active)] || ($::iso_info(active) != 1)} {
      aie_v3_dynamic_iso active 1
      ::AIEnv::GUIManagement::ToggleGrey v3DynamicIsoOn 0
   }

   variable v3pSVarList
   set v3pSVarList [V3_variables_get sv_namelist]
}

proc onV3DynamicIsoVarModify {args} {
   set vn [lindex $args 1]

   update_iso_form zvar
}


proc aie_v3_dynamic_iso {what args} {
   global dynamic_info iso_info cutp_info

   switch -- $what {
      active {
         set on [lindex $args 0]
         if {$on == ""} {set on $iso_info(active)}
         if {$on} {
            set dynamic_info(current_type) iso
            V3_dynamic_surface type iso

            V3_iso_surface update
            set cutp_info(active) 0
         } else {
            V3_dynamic_surface type none
         }
         set iso_info(active) $on
      }
   }
}


proc onv3DynamicCutPDEZApply {} {
   global dynamic_info cutp_info

   set which [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName \
                   v3DynamicCutM]

   set what ""; set abcd ""
   switch -- $which {
      v3DynamicCutCf {
         set abcd [list a b c d]
         set what coeffs
      }
      v3DynamicCutPn {
         foreach v $cutp_info(xyz) s {px py pz} { set cutp_info($s) $v }
         set abcd [list px py pz nx ny nz]
         set what ptnorm
      } 
      v3DynamicCutCp {
         set abcd [list pt1 pt2 pt3]
         set what corner
      }
   }
   if {$abcd != ""} {
      set rr [V3_cut_plane_get $what]
      if {$rr == ""} {return ""}
      set did 0
      foreach v $rr s $abcd {
         if {($v == "") || ($s == "")} {break}
         foreach k $cutp_info($s) {
            if ![string is double -strict $k] {return ""}
         }
         if {$v != $cutp_info($s)} {incr did}
      }
      if {$did} {
         set pp ""
         foreach s $abcd {eval lappend pp $cutp_info($s)}
         eval V3_cut_plane $what $pp
      }
   }
}

proc onv3DynamicCutPDEZLoad {} {
   global dynamic_info cutp_info

   if {![info exist ::cutp_info(active)] || ($::cutp_info(active) != 1)} {
      aie_v3_dynamic_cut active 1
      ::AIEnv::GUIManagement::ToggleGrey v3DynamicCutOn 0
   } 
}

proc onv3DynamicCutSXLoad {} {
   global dynamic_info cutp_info

   V3_cut_plane preset 0 
   v3DynamicCutPDEZ set V3CutMethod 1
}

proc onv3DynamicCutSYLoad {} {
   global dynamic_info cutp_info

   V3_cut_plane preset 1
   v3DynamicCutPDEZ set V3CutMethod 1
}

proc onv3DynamicCutSZLoad {} {
   global dynamic_info cutp_info

   V3_cut_plane preset 2
   v3DynamicCutPDEZ set V3CutMethod 1
}


proc onv3DynamicCutITLoad {} {
   global cutp_info

   global dynamic_info
  #set dynamic_info(win) [aie_get_v3p_gwin dynamic_info(win)]
   set dynamic_info(win) [aie_get_v3p_gwin]

   set cutp_info(spectype_m) pick
   update_cuts_form spectype_m

   switch -- $cutp_info(spectype_m) {
      coeffs {v3DynamicCutPDEZ set V3CutMethod 1}
      corner - corners {v3DynamicCutPDEZ set V3CutMethod 2}
      ptnorm {v3DynamicCutPDEZ set V3CutMethod 0}
   }
}

proc onv3DynamicCut3PLoad {} {
   global cutp_info

   global dynamic_info
   #set dynamic_info(win) [aie_get_v3p_gwin dynamic_info(win)]
   set dynamic_info(win) [aie_get_v3p_gwin]

   set cutp_info(spectype_m) 3pts
   update_cuts_form spectype_m

   switch -- $cutp_info(spectype_m) {
      coeffs {v3DynamicCutPDEZ set V3CutMethod 1}
      corner - corners {v3DynamicCutPDEZ set V3CutMethod 2}
      ptnorm {v3DynamicCutPDEZ set V3CutMethod 0}
   }
}

proc aie_v3_dynamic_cut {what args} {
   global cutp_info iso_info

   if {![info exist cutp_info(cur)] || ($cutp_info(cur) == "")} {
      set rr [V3_cut_plane create_default "" ]
      if {$rr == ""} {return ""}
      set cutp_info(cur) $rr
   }

   switch -- $what {
      get_pt {
         set w [aie_get_v3p_gwin]
         if {[anno_setup_mouse_binding $w] == 0} {return ""}

         global button_events
         banner "Click a button to set the location." $w
         catch {$w configure -cursor {crosshair red white}}
         set button_events($w) ""

         eval tkwait variable button_events($w)
        #if {[lindex $button_events($w) 0] >= 0} {return ""}

         set x [lindex $button_events($w) 1]
         set y [lindex $button_events($w) 2]

         for {set i -1} {$i < 2} {incr i} {
            set dd [$w get_3d_point $x $y $i]
            if {[llength $dd] >= 2} {break}
         }

         anno_reset_mouse_binding $w

         set varName [lindex $args 0]

         if {[llength $dd] < 3} {
            mess "failed to locate a point at ($x $y) on the screen\n" red
            set ::$varName ""
         } else {
            set ::$varName [lrange $dd 0 2]
         }
      }
      active {
         set on [lindex $args 0]
         if {$on == ""} {set on $cutp_info(active)}
         if {$on} {
            if [info exist cutp_info(display_saved)] {
               set cutp_info(display) $cutp_info(display_saved)
               unset cutp_info(display_saved)
            }
            if {$cutp_info(display)} {
               if {$cutp_info(display_type) == 0} {
                  set $cutp_info(display_type) 7
               }
               V3_cut_plane display_type $cutp_info(display_type)
               ::AIEnv::GUIManagement::ToggleGrey v3DynamicCutDraw 0
            }
            V3_dynamic_surface type cut
            V3_cut_plane update_only
            set iso_info(active) 0
         } else {
            set cutp_info(display_saved) $cutp_info(display)
            if {$cutp_info(display)} {
               set cutp_info(display) 0
               V3_cut_plane display_type 0
               ::AIEnv::GUIManagement::ToggleGrey v3DynamicCutDraw 1
            }
            V3_dynamic_surface type none
         }
         set cutp_info(active) $on
      }
      displays {
         if {!$cutp_info(display)} {
            V3_cut_plane display_type 0
         } else {
            set ty 0
            if {$cutp_info(display_bd)} {set ty [expr $ty | 0x1]}
            if {$cutp_info(display_tsh) != 0} {
               set ty [expr $ty | 0x2]
               V3_cut_plane display_transp $cutp_info(display_tsh)
            }
            if {$cutp_info(display_n)}  {set ty [expr $ty | 0x4]}
            V3_cut_plane display_type $ty

            set cutp_info(display_type) $ty
         }
      }
      display_color {
         set display_col  [lindex $args 0]
         if {$display_col == ""} {set display_col $cutp_info(display_color)}
         V3_cut_plane display_color $display_col
      }
   }
}


proc onv3SurfThrPlaneDEZLoad {} {
   aie_v3p_surfthr_cmd load
}

proc onv3SurfThrPlaneDEZApply {} {
   aie_v3p_surfthr_cmd apply

   global sfthr_data
   if {[info exist sfthr_data(win)] && \
       [winfo exist $sfthr_data(win)]} {
      $sfthr_data(win) redraw
   }
}

proc onv3SurfThrPlaneDEZOK {} {
   aie_v3p_surfthr_cmd apply
   aie_v3p_surfthr_cmd not_show
}

proc onv3SurfThrPlaneDEZCancel {} {
   aie_v3p_surfthr_cmd cancel
}

proc onv3SurfThrCutSXLoad {} {
    aie_v3p_surfthr_cmd select_preset 0 
}
proc onv3SurfThrCutSYLoad {} {
    aie_v3p_surfthr_cmd select_preset 1
}
proc onv3SurfThrCutSZLoad {} {
    aie_v3p_surfthr_cmd select_preset 2
}

proc onv3SurfThrCut3PLoad {} {
    aie_v3p_surfthr_cmd select_3pts
}

proc onv3SurfThrCutITLoad {} {
    aie_v3p_surfthr_cmd select_move
}

proc aie_v3p_surfthr_cmd {what args} {
   global sfthr_data

   switch -- $what {
      load {
         if ![info exist sfthr_data(plane_list)] {
            set sfthr_data(plane_list) ""
         }

         if {![info exist sfthr_data(win)] || \
             ![winfo exist $sfthr_data(win)]} {
            global tdv_current_viewer
            if {[info exist tdv_current_viewer] && \
                [winfo exist $tdv_current_viewer]} {
               set sfthr_data(win) $tdv_current_viewer
            } else {
               set sfthr_data(win) ""
            }
         }
         if [winfo exist $sfthr_data(win)] {
            foreach {t n} $sfthr_data(plane_list) {
               if {[info command $n] == ""} { continue }
               $sfthr_data(win) add_image $n
               $n set_material 0.5
            }
         }

         if {$sfthr_data(plane_list) == ""} {
          # aie_v3p_surfthr_cmd add_new   
         } else {
            set sfthr_data(iplane) [lindex $sfthr_data(plane_list) end] 
            set sfthr_data(cut) $sfthr_data(iplane)

            $sfthr_data(cut) set_material 1.0
            aie_v3p_surfthr_cmd show

            v3SurfThrPlaneDEZ set v3SurfThrPlaneTypeVar 1
         }
      }
      apply {
         set rr ""
         foreach {t n} $sfthr_data(plane_list) {
            if {[info command $n] == ""} { continue }
            set cf [$n get_coeffs]   
            if {[llength $cf] == 4} {
               lappend rr [lindex $cf 0] [lindex $cf 1] \
                           [lindex $cf 2] [lindex $cf 3]
            }
         }

         eval V3_surface_cmd selected cut_by_planes $rr
      }
      not_show - cancel {
         if {[info exist sfthr_data(win)] && \
             [winfo exist $sfthr_data(win)]} {
            foreach {t n} $sfthr_data(plane_list) {
               if {[info command $n] == ""} { continue }
               $sfthr_data(win) remove_image $n
            }
         }
      }
      rm_plane {
         if {![info exist sfthr_data(cut)] || \
             ([info command $sfthr_data(cut)] == "")} {return ""}

         set j [lsearch $sfthr_data(plane_list) $sfthr_data(cut)]
         if {$j > 0} {
            set j1 [expr $j - 1]
            set sfthr_data(plane_list) \
                    [lreplace $sfthr_data(plane_list) $j1 $j]
         } 
         $sfthr_data(cut) delete
         unset sfthr_data(cut)

         set sfthr_data(iplane) [lindex $sfthr_data(plane_list) end]
         if {[info command $sfthr_data(iplane)] != ""} {
            aie_v3p_surfthr_cmd plane_list
         }
      }
      clean {
         foreach {t n} $sfthr_data(plane_list) {
            if {[info command $n] == ""} {continue}
            $n delete
         }

         set sfthr_data(plane_list) ""
         set sfthr_data(iplane) ""
         ::AIEnv::GUIManagement::DEZ::SetDirty v3SurfThrPlaneDEZ
      }
      rec_plane {
         if ![info exist sfthr_data(nplane)] {
            set sfthr_data(nplane) 0
         }

         set name [v3SurfThrPlaneDEZ get v3SurfThrPlaneTitle]

         if {($name != "") && ([lsearch $sfthr_data(plane_list) $name] >= 0)} {
            mess "Plane name $name is in use, use a default name\n" blue
            set name ""
         }

         set i $sfthr_data(nplane)
         incr sfthr_data(nplane)

         if {$name == ""} {set name ThrPlane_$i}

         lappend sfthr_data(plane_list) $name $sfthr_data(cut)

         v3SurfThrPlaneDEZ set v3SurfThrPlaneTitle ""
      }
      add_new {
         if {[info exist sfthr_data(cut)] && \
             ([info command $sfthr_data(cut)] != "")} {
            $sfthr_data(cut) set_material 0.5

            if {($sfthr_data(method) == "ptnorm") || \
                ($sfthr_data(method) == "coeffs")} {
               if [aie_v3p_surfthr_cmd diff_$sfthr_data(method)] {
                  aie_v3p_surfthr_cmd select_$sfthr_data(method)
               }
            }
         }
 
         set mm [V3_events_loop_get model_bbox]

         set cut0 [pgeom_cut -interesting 0]
         $cut0 configure -color #ffbb22 \
                  -mbmin [lindex $mm 0] -mbmax [lindex $mm 1] \
                  -display_type 7

         set sfthr_data(cut) $cut0

            #$cut0 set_default 2  --- the following will do this
         if {[info exist sfthr_data(method)] && \
             ([v3SurfThrPlaneDEZ get v3SurfThrPlaneTypeVar] == 4)} {
            aie_v3p_surfthr_cmd select_preset 2
         } else {   
            v3SurfThrPlaneDEZ set v3SurfThrPlaneTypeVar 4
         }

         if {![info exist sfthr_data(win)] || \
             ![winfo exist $sfthr_data(win)]} {
            global tdv_current_viewer
            if {[info exist tdv_current_viewer] && \
                [winfo exist $tdv_current_viewer]} {
               set sfthr_data(win) $tdv_current_viewer
            } else {
               set sfthr_data(win) ""
            }
         }
         if [winfo exist $sfthr_data(win)] { 
            $sfthr_data(win) add_image $sfthr_data(cut)
         }

         $sfthr_data(cut) set_material 1.0

         aie_v3p_surfthr_cmd rec_plane

         aie_v3p_surfthr_cmd show
         set sfthr_data(iplane) $sfthr_data(cut)
      }
      plane_list {
         if {[info exist sfthr_data(cut)] && \
             ([info command $sfthr_data(cut)] != "")} {

            $sfthr_data(cut) set_material 0.5

            if {($sfthr_data(method) == "ptnorm") || \
                ($sfthr_data(method) == "coeffs")} {
               if [aie_v3p_surfthr_cmd diff_$sfthr_data(method)] {
                  aie_v3p_surfthr_cmd select_$sfthr_data(method)
               }
            }

          # set name [v3SurfThrPlaneDEZ get v3SurfThrPlaneTitle]
          # set j [lsearch $sfthr_data(plane_list) $sfthr_data(cut)]

          # if {($j > 0) && ($name != "")} {
          #    set j1 [expr $j -1]
          #    set sfthr_data(plane_list) [lreplace $sfthr_data(plane_list) \
          #                   $j1 $j $name $sfthr_data(cut)]
          # }
         }

         set sfthr_data(cut) $sfthr_data(iplane)

         $sfthr_data(cut) set_material 1.0
         aie_v3p_surfthr_cmd show

         v3SurfThrPlaneDEZ set v3SurfThrPlaneTypeVar 1
      }
      show {
         if {![info exist sfthr_data(cut)] || \
                ([info command $sfthr_data(cut)] == "")} {return ""}

         aie_v3p_surfthr_cmd update \
                       coeffs [$sfthr_data(cut) get_coeffs] \
                       ptnorm [$sfthr_data(cut) get_ptnorm] \
                       scan   [$sfthr_data(cut) get_scan]

         if {[info exist sfthr_data(win)] && \
             [winfo exist $sfthr_data(win)]} {
            $sfthr_data(win) redraw
         } 
      }
      select_type {
         if {![info exist sfthr_data(cut)] || \
                ([info command $sfthr_data(cut)] == "")} {return ""}

         set itype [v3SurfThrPlaneDEZ get v3SurfThrPlaneTypeVar]
         switch -- $itype {
            0 {set sfthr_data(method) ptnorm}
            1 {set sfthr_data(method) coeffs}
            5 {set sfthr_data(method) 3pts}
            6 {set sfthr_data(method) move}
            default {set sfthr_data(method) preset}
         }
      }
      select_preset {
         if {![info exist sfthr_data(cut)] || \
                ([info command $sfthr_data(cut)] == "")} {return ""}

         set k [lindex $args 0]
         if ![string is integer  -strict $k] {return ""}
         if {($k < 0) || ($k > 2)} {return ""}
         $sfthr_data(cut) set_default $k

         aie_v3p_surfthr_cmd show
      }
      select_coeffs {
         if {![info exist sfthr_data(cut)] || \
                ([info command $sfthr_data(cut)] == "")} {return ""}

         foreach j {a b c d} {
            if ![string is double -strict $sfthr_data($j)] {return ""}
            set $j $sfthr_data($j)
         }
         $sfthr_data(cut) set_coeffs $a $b $c $d

         aie_v3p_surfthr_cmd show
      }
      select_ptnorm {
         if {![info exist sfthr_data(cut)] || \
                ([info command $sfthr_data(cut)] == "")} {return ""}

         set sfthr_data(px) [lindex $sfthr_data(xyz) 0]
         set sfthr_data(py) [lindex $sfthr_data(xyz) 1]
         set sfthr_data(pz) [lindex $sfthr_data(xyz) 2]

         foreach c {px py pz nx ny nz} {
            if ![string is double -strict $sfthr_data($c)] {return ""}
            set $c $sfthr_data($c)
         }
         $sfthr_data(cut) set_ptnorm $px $py $pz $nx $ny $nz

         aie_v3p_surfthr_cmd show
      }
      select_3pts {

      }
      select_move {

      }
      diff_coeffs {
         set rr [$sfthr_data(cut) get_coeffs]          
         set s 0
         foreach i {0 1 2 3} j {a b c d} {
            set d [expr [lindex $rr $i] - $sfthr_data($j)]
            set s [expr $s + $d * $d]
         }
         return [expr ($s < 1.0e-20) ? 0 : 1]
      }
      diff_ptnorm {
         set rr [$sfthr_data(cut) get_ptnorm]

         set sfthr_data(px) [lindex $sfthr_data(xyz) 0]
         set sfthr_data(py) [lindex $sfthr_data(xyz) 1]
         set sfthr_data(pz) [lindex $sfthr_data(xyz) 2]

         set s 0
         foreach i {0 1 2 3 4 5} j {px py pz nx ny nz} {
            set d [expr [lindex $rr $i] - $sfthr_data($j)]
            set s [expr $s + $d * $d]
         }
         return [expr ($s < 1.0e-20) ? 0 : 1]
      }
      scan {
         if {![info exist sfthr_data(cut)] || \
                ([info command $sfthr_data(cut)] == "")} {return ""}

         if {($sfthr_data(method) == "ptnorm") || \
             ($sfthr_data(method) == "coeffs")} {
            if [aie_v3p_surfthr_cmd diff_$sfthr_data(method)] {
               aie_v3p_surfthr_cmd select_$sfthr_data(method)
            }
         }

         set an_val $sfthr_data(an_val)
         if ![string is double -strict $an_val] {set an_val 0.5}
         $sfthr_data(cut) reset_scan $an_val

         aie_v3p_surfthr_cmd show
      }
      update {
         foreach {s r} $args {
            if {$r == ""} {continue}
            switch $s {
               coeffs {
                  set sfthr_data(a) [lindex $r 0]
                  set sfthr_data(b) [lindex $r 1]
                  set sfthr_data(c) [lindex $r 2]
                  set sfthr_data(d) [lindex $r 3]
               }
               ptnorm {
                  set sfthr_data(px) [lindex $r 0]
                  set sfthr_data(py) [lindex $r 1]
                  set sfthr_data(pz) [lindex $r 2]
                  set sfthr_data(xyz) [lrange $r 0 2]
                  set sfthr_data(nx) [lindex $r 3]
                  set sfthr_data(ny) [lindex $r 4]
                  set sfthr_data(nz) [lindex $r 5]
               }
               scan {
                  set sfthr_data(an_val) $r
               }
            }
         }
      }
   }
}
