
# Copyright (c) 2000 Pacific Marketing and Consulting, Inc,
#       All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc list_coordinate_systems {} {
    
    return [ic_csystem_list]
}

proc get_coordinate_system {name} {
    
    set cc [ic_csystem_get $name]
    if {$cc == ""} { return "" }
    return [eval concat system name $name type [lindex $cc 0] \
		origin [lindex $cc 1] xaxis [lindex $cc 2] \
		yaxis [lindex $cc 3] zaxis [lindex $cc 4]]
}

proc set_coordinate_system {name} {
    global coord_system_label tdv_height
    global tdv_default

    set cc [ic_csystem_get $name]
    if {$cc == ""} {
	error "No such coordinate system $name"
    }
    ic_csystem_set_current $name
    mess "Current coordinate system is $name\n"
    
    if [info exists coord_system_label] {
	$coord_system_label delete
	unset coord_system_label
    }
    if {($name != "") && ($name != "global")} {
	set str "Coord system = $name"
	set x 10
	set y [expr {$tdv_height($tdv_default) - 15}]
	
	set coord_system_label [tdv_text_im -point "$x $y 0" \
				    -transform 0 -text $str -color white]
	$tdv_default add_image $coord_system_label
    }
}

proc csystem_select_point {how var basevar} {    
    global csystem_tmp_select
    upvar \#0 $var lvar $basevar lbasevar
    
    switch $how {
	graphic {
	    disp_get_location csystem_tmp_select 0 "the location" 1
	}
	pre_point {
	    set name [geo_select point 1]
	    if {$name != ""} {
		set csystem_tmp_select [ic_get_point_location $name]
	    }
	}
    }
    set lvar ""
    foreach i {0 1 2} {
	if {$basevar == ""} {
	    lappend lvar [lindex $csystem_tmp_select $i]
	} else {
	    lappend lvar [expr [lindex $csystem_tmp_select $i] - \
			      [lindex $lbasevar $i]]
	}
    }
}

proc delete_coordinate_system {name} {
    
    ic_csystem_delete $name
    
    mess "Deleted coordinate system \"$name\"\n"
}

proc create_coordinate_system {type name center axis0 axis1 axis2} {
    set axis(0) $axis0
    set axis(1) $axis1
    set axis(2) $axis2
    foreach i { 0 1 2 } {
	if { $axis($i) == "" } {
		set axis($i) [vec_cross $axis([expr ($i + 1) % 3]) \
                              $axis([expr ($i + 2) % 3])]
	}
    }
    foreach i {0 1 2} v [vec_orthonormal_righthanded_basis $axis(0) $axis(1)] {
	set axis($i) $v
    }

    if {[ic_csystem_get $name] != ""} {
        mess "Modified $type coordinate system \"$name\"\n"
    } else {
	mess "Created a new $type coordinate system \"$name\"\n"
    }
    ic_csystem_create $name $type $center $axis(0) $axis(1) $axis(2)
}

proc axes_update_command {args} {
    # do nothing
}

# -----------------------------------------------------------------

proc famboco_after_load_param_callback {} {
    global param_values
    
    # Just blow away the coord systems we find.
    foreach p [array names param_values] {
	if [string match :coordsys/* $p] { unset param_values($p) }
    }
}

proc famboco_before_save_param_callback {} {
    global param_values
    
    foreach name [ic_csystem_list] {
	foreach p {type origin axis0 axis1 axis2} val [ic_csystem_get $name] {
	    set param_values(:coordsys/$name,$p) $val
	}
    }
}

proc famboco_list_csystems {} {
    set ret [list "global global"]
    foreach cs [ic_csystem_list] {
	lappend ret [list $cs $cs]
    }
    return $ret
}
