Ignore:
Timestamp:
Feb 26, 2008 7:18:06 PM (16 years ago)
Author:
gah
Message:

VisViewer? base class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gui/scripts/nanovisviewer.tcl

    r904 r907  
     1
    12# ----------------------------------------------------------------------
    23#  COMPONENT: nanovisviewer - 3D volume rendering
     
    2829    -*-helvetica-medium-r-normal-*-12-* widgetDefault
    2930
     31# must use this name -- plugs into Rappture::resources::load
     32proc NanovisViewer_init_resources {} {
     33    Rappture::resources::register \
     34        nanovis_server [list Rappture::VisViewer::SetServerList "nanovis" ]
     35}
     36
    3037itcl::class Rappture::NanovisViewer {
    31     inherit itk::Widget
    32 
     38    inherit Rappture::VisViewer
     39 
    3340    itk_option define -plotforeground plotForeground Foreground ""
    3441    itk_option define -plotbackground plotBackground Background ""
    3542    itk_option define -plotoutline plotOutline PlotOutline ""
    36     itk_option define -sendcommand sendCommand SendCommand ""
    37     itk_option define -receivecommand receiveCommand ReceiveCommand ""
    38 
    39     constructor {hostlist args} { # defined below }
    40     destructor { # defined below }
    41 
     43   
     44    constructor { hostlist args } {
     45        Rappture::VisViewer::constructor $hostlist
     46    } {
     47        # defined below
     48    }
     49    destructor {
     50        # defined below
     51    }
    4252    public method add {dataobj {settings ""}}
    4353    public method get {args}
     
    4858    }
    4959    public method download {option args}
    50     public method parameters {title args} { # do nothing }
    51     public method connect {{hostlist ""}}
    52     public method disconnect {}
     60    public method parameters {title args} {
     61        # do nothing
     62    }
    5363    public method isconnected {}
    5464
    55     protected method _send {args}
    56     protected method _send_text {string}
     65    protected method Connect {}
     66    protected method Disconnect {}
     67    protected method _send {string}
    5768    protected method _send_dataobjs {}
    5869    protected method _send_transfuncs {}
    59     protected method _send_echo {channel {data ""}}
    60     protected method _receive {}
     70
    6171    protected method _receive_image {option size}
    6272    protected method _receive_legend {ivol vmin vmax size}
    6373    protected method _receive_data {args}
    64     protected method _receive_echo {channel {data ""}}
    6574
    6675    protected method _rebuild {}
     
    7685    protected method _fixSettings {what {value ""}}
    7786    protected method _fixLegend {}
    78     protected method _serverDown {}
    79     protected method _genTransfuncData {dataobj comp}
    80     public method update_transfer_function {}
    81     public method remove_duplicate_isomarker { m x }
    82     public method over_isomarker { m x }
    83     public method _addIsoMarker { x y }
    84     protected method _initIsoMarkers {dataobj comp}
    85     protected method _hideIsoMarkers {dataobj}
    86     protected method _showIsoMarkers {dataobj}
    87     protected method _color2rgb {color}
    88     protected method _euler2xyz {theta phi psi}
    89 
    90     private variable _dispatcher "" ;# dispatcher for !events
    91 
    92     private variable _nvhosts ""   ;# list of hosts for nanovis server
    93     private variable _sid ""       ;# socket connection to nanovis server
    94     private variable _parser ""    ;# interpreter for incoming commands
    95     private variable _buffer       ;# buffer for incoming/outgoing commands
    96     private variable _image        ;# image displayed in plotting area
     87    protected method GenTransfuncData {dataobj comp}
     88    protected method UpdateTransferFunction {}
     89    protected method RemoveDuplicateIsoMarker { m x }
     90    protected method OverIsoMarker { m x }
     91    protected method AddIsoMarker { x y }
     92    protected method InitIsoMarkers {dataobj comp}
     93    protected method HideIsoMarkers {dataobj}
     94    protected method ShowIsoMarkers {dataobj}
     95
     96    private variable _outbuf       ;# buffer for outgoing commands
    9797
    9898    private variable _dlist ""     ;# list of data objects
     
    112112    private variable _limits       ;# autoscale min/max for all axes
    113113    private variable _view         ;# view params for 3D view
    114 
     114       
    115115    private variable _isomarkers    ;# array of isosurface level values 0..1
    116116    private common _isosurface     ;# indicates to use isosurface shading
     
    122122}
    123123
    124 itcl::class Rappture::NanovisViewer::IsoMarker {
    125     private variable _value    ""; # Absolute value of marker.
    126     private variable _label    ""
    127     private variable _tick     ""
    128     private variable _canvas   ""
    129     private variable _nvobj    ""
    130     private common _normalIcon [Rappture::icon nvlegendmark]
    131     private common _activeIcon [Rappture::icon nvlegendmark2]
    132     private variable _active_motion   0
    133     private variable _active_press    0
    134 
    135     constructor {c obj args} {
    136         set _canvas $c
    137         set _nvobj $obj
    138 
    139         set w [winfo width $_canvas]
    140         set h [winfo height $_canvas]
    141         set _tick [$c create image 0 $h \
    142                 -image $_normalIcon -anchor s \
    143                 -tags "$this $obj" -state hidden]
    144         set _label [$c create text 0 $h \
    145                 -anchor n -fill white -font "Helvetica 6" \
    146                 -tags "$this $obj" -state hidden]
    147         $c bind $_tick <Enter> [itcl::code $this handle_event "enter"]
    148         $c bind $_tick <Leave> [itcl::code $this handle_event "leave"]
    149         $c bind $_tick <ButtonPress-1> \
    150             [itcl::code $this handle_event "start" %x %y]
    151         $c bind $_tick <B1-Motion> \
    152             [itcl::code $this handle_event "update" %x %y]
    153         $c bind $_tick <ButtonRelease-1> \
    154             [itcl::code $this handle_event "end" %x %y]
    155     }
    156     destructor {
    157         $_canvas delete $this
    158     }
    159 
    160     public method get_absolute_value {} {
    161         return $_value
    162     }
    163     public method get_relative_value {} {
    164         array set limits [$_nvobj get_limits]
    165         if { $limits(vmax) == $limits(vmin) } {
    166             set limits(vmin) 0.0
    167             set limits(vmax) 1.0
    168         }
    169         return [expr {($_value-$limits(vmin))/($limits(vmax) - $limits(vmin))}]
    170     }
    171     public method activate { bool } {
    172         if  { $bool || $_active_press || $_active_motion } {
    173             $_canvas itemconfigure $_label -state normal
    174             $_canvas itemconfigure $_tick -image $_activeIcon
    175         } else {
    176             $_canvas itemconfigure $_label -state hidden
    177             $_canvas itemconfigure $_tick -image $_normalIcon
    178         }
    179     }
    180     public method show {} {
    181         set_absolute_value $_value
    182         $_canvas itemconfigure $_tick -state normal
    183         $_canvas raise $_tick
    184     }
    185     public method hide {} {
    186         $_canvas itemconfigure $_tick -state hidden
    187     }
    188     public method get_screen_position { } {
    189         set x [get_relative_value]
    190         if { $x < 0.0 } {
    191             set x 0.0
    192         } elseif { $x > 1.0 } {
    193             set x 1.0
    194         }
    195         set low 10
    196         set w [winfo width $_canvas]
    197         set high [expr {$w  - 10}]
    198         set x [expr {round($x*($high - $low) + $low)}]
    199         return $x
    200     }
    201     public method set_absolute_value { x } {
    202         set _value $x
    203         set y 31
    204         $_canvas itemconfigure $_label -text [format %.4g $_value]
    205         set x [get_screen_position]
    206         $_canvas coords $_tick $x [expr {$y+3}]
    207         $_canvas coords $_label $x [expr {$y+5}]
    208     }
    209     public method set_relative_value { x } {
    210         array set limits [$_nvobj get_limits]
    211         if { $limits(vmax) == $limits(vmin) } {
    212             set limits(vmin) 0.0
    213             set limits(vmax) 1.0
    214         }
    215         set r [expr $limits(vmax) - $limits(vmin)]
    216         set_absolute_value [expr {($x * $r) + $limits(vmin)}]
    217     }
    218     public method handle_event { option args } {
    219         switch -- $option {
    220             enter {
    221                 set _active_motion 1
    222                 activate yes
    223                 $_canvas raise $_tick
    224             }
    225             leave {
    226                 set _active_motion 0
    227                 activate no
    228             }
    229             start {
    230                 $_canvas raise $_tick
    231                 set _active_press 1
    232                 activate yes
    233             }
    234             update {
    235                 set w [winfo width $_canvas]
    236                 set x [lindex $args 0]
    237                 set_relative_value [expr {double($x-10)/($w-20)}]
    238                 $_nvobj over_isomarker $this $x
    239                 $_nvobj update_transfer_function
    240             }
    241             end {
    242                 set x [lindex $args 0]
    243                 if { ![$_nvobj remove_duplicate_isomarker $this $x]} {
    244                     eval handle_event update $args
    245                 }
    246                 set _active_press 0
    247                 activate no
    248             }
    249             default {
    250                 error "bad option \"$option\": should be start, update, end"
    251             }
    252         }
    253     }
    254 }
    255 
    256124# ----------------------------------------------------------------------
    257125# CONSTRUCTOR
    258126# ----------------------------------------------------------------------
    259127itcl::body Rappture::NanovisViewer::constructor {hostlist args} {
    260     Rappture::dispatcher _dispatcher
     128
     129    # Draw legend event
    261130    $_dispatcher register !legend
    262131    $_dispatcher dispatch $this !legend "[itcl::code $this _fixLegend]; list"
    263     $_dispatcher register !serverDown
    264     $_dispatcher dispatch $this !serverDown "[itcl::code $this _serverDown]; list"
    265 
    266     set _buffer(in) ""
    267     set _buffer(out) ""
    268 
    269     #
    270     # Create a parser to handle incoming requests
    271     #
    272     set _parser [interp create -safe]
    273     foreach cmd [$_parser eval {info commands}] {
    274         $_parser hide $cmd
    275     }
     132    # Send dataobjs event
     133    $_dispatcher register !send_dataobjs
     134    $_dispatcher dispatch $this !send_dataobjs \
     135        "[itcl::code $this _send_dataobjs]; list"
     136    # Send transfer functions event
     137    $_dispatcher register !send_transfuncs
     138    $_dispatcher dispatch $this !send_transfuncs \
     139        "[itcl::code $this _send_transfuncs]; list"
     140    # Rebuild event
     141    $_dispatcher register !rebuild
     142    $_dispatcher dispatch $this !rebuild "[itcl::code $this _rebuild]; list"
     143
     144    set _outbuf ""
     145
     146    #
     147    # Populate parser with commands handle incoming requests
     148    #
    276149    $_parser alias image [itcl::code $this _receive_image]
    277150    $_parser alias legend [itcl::code $this _receive_legend]
    278151    $_parser alias data [itcl::code $this _receive_data]
    279 
    280     #
    281     # Set up the widgets in the main body
    282     #
    283     option add hull.width hull.height
    284     pack propagate $itk_component(hull) no
    285152
    286153    set _view(theta) 45
     
    296163    set _limits(vmax) 1.0
    297164
    298     itk_component add controls {
    299         frame $itk_interior.cntls
    300     } {
    301         usual
    302         rename -background -controlbackground controlBackground Background
    303     }
    304     pack $itk_component(controls) -side right -fill y
    305 
    306165    itk_component add zoom {
    307166        frame $itk_component(controls).zoom
     
    556415
    557416
    558     #
    559     # RENDERING AREA
    560     #
    561     itk_component add area {
    562         frame $itk_interior.area
    563     }
    564     pack $itk_component(area) -expand yes -fill both
     417    # Legend
    565418
    566419    set _image(legend) [image create photo]
     
    575428    bind $itk_component(legend) <Configure> \
    576429        [list $_dispatcher event -idle !legend]
    577 
    578     set _image(plot) [image create photo]
    579     itk_component add 3dview {
    580         label $itk_component(area).vol -image $_image(plot) \
    581             -highlightthickness 0
    582     } {
    583         usual
    584         ignore -highlightthickness
    585         rename -background -plotbackground plotBackground Background
    586     }
    587     pack $itk_component(3dview) -expand yes -fill both
    588430
    589431    # set up bindings for rotation
     
    595437        [itcl::code $this _move release %x %y]
    596438    bind $itk_component(3dview) <Configure> \
    597         [itcl::code $this _send screen %w %h]
     439        [itcl::code $this _send "screen %w %h"]
    598440
    599441    set _image(download) [image create photo]
     
    601443    eval itk_initialize $args
    602444
    603     connect $hostlist
     445    Connect
    604446}
    605447
     
    610452    set _sendobjs ""  ;# stop any send in progress
    611453    set _sendobjs2 ""  ;# stop any send in progress
    612     after cancel [itcl::code $this _send_dataobjs]
    613     after cancel [itcl::code $this _rebuild]
     454    $_dispatcher cancel !rebuild
     455    $_dispatcher cancel !send_dataobjs
     456    $_dispatcher cancel !send_transfuncs
    614457    image delete $_image(plot)
    615458    image delete $_image(legend)
    616459    image delete $_image(download)
    617     interp delete $_parser
    618460}
    619461
     
    654496        set _obj2ovride($dataobj-raise) $params(-raise)
    655497
    656         after cancel [itcl::code $this _rebuild]
    657         after idle [itcl::code $this _rebuild]
     498        $_dispatcher event -idle !rebuild
    658499    }
    659500}
     
    736577    # if anything changed, then rebuild the plot
    737578    if {$changed} {
    738         after cancel [itcl::code $this _rebuild]
    739         after idle [itcl::code $this _rebuild]
     579        $_dispatcher event -idle !rebuild
    740580    }
    741581}
     
    818658
    819659# ----------------------------------------------------------------------
    820 # USAGE: connect ?<host:port>,<host:port>...?
     660# USAGE: Connect ?<host:port>,<host:port>...?
    821661#
    822662# Clients use this method to establish a connection to a new
     
    824664# Any existing connection is automatically closed.
    825665# ----------------------------------------------------------------------
    826 itcl::body Rappture::NanovisViewer::connect {{hostlist ""}} {
    827     disconnect
    828 
    829     if {"" != $hostlist} { set _nvhosts $hostlist }
    830 
    831     if {"" == $_nvhosts} {
     666itcl::body Rappture::NanovisViewer::Connect {} {
     667    Disconnect
     668    set _hosts [GetServerList "nanovis"]
     669    if { "" == $_hosts } {
    832670        return 0
    833671    }
    834 
    835     blt::busy hold $itk_component(hull); update idletasks
    836 
    837     # HACK ALERT! punt on this for now
    838     set memorySize 10000
    839 
    840     #
    841     # Connect to the nanovis server.  Send the server some estimate
    842     # of the size of our job.  If it's too busy, that server may
    843     # forward us to another.
    844     #
    845     set try [split $_nvhosts ,]
    846     foreach {hostname port} [split [lindex $try 0] :] break
    847     set try [lrange $try 1 end]
    848 
    849     while {1} {
    850         _send_echo <<line "connecting to $hostname:$port..."
    851         if {[catch {socket $hostname $port} sid]} {
    852             if {[llength $try] == 0} {
    853                 return 0
    854             }
    855             foreach {hostname port} [split [lindex $try 0] :] break
    856             set try [lrange $try 1 end]
    857             continue
    858         }
    859         fconfigure $sid -translation binary -encoding binary
    860 
    861         # send memory requirement to the load balancer
    862         puts -nonewline $sid [binary format I $memorySize]
    863         flush $sid
    864 
    865         # read back a reconnection order
    866         set data [read $sid 4]
    867         if {[binary scan $data cccc b1 b2 b3 b4] != 4} {
    868             error "couldn't read redirection request"
    869         }
    870         set addr [format "%u.%u.%u.%u" \
    871             [expr {$b1 & 0xff}] \
    872             [expr {$b2 & 0xff}] \
    873             [expr {$b3 & 0xff}] \
    874             [expr {$b4 & 0xff}]]
    875         _receive_echo <<line $addr
    876 
    877         if {[string equal $addr "0.0.0.0"]} {
    878             fconfigure $sid -buffering line
    879             fileevent $sid readable [itcl::code $this _receive]
    880             set _sid $sid
    881             blt::busy release $itk_component(hull)
    882             return 1
    883         }
    884         set hostname $addr
    885     }
    886     blt::busy release $itk_component(hull)
    887 
    888     return 0
    889 }
    890 
    891 # ----------------------------------------------------------------------
    892 # USAGE: disconnect
     672    set result [VisViewer::Connect $_hosts]
     673    return $result
     674}
     675
     676# ----------------------------------------------------------------------
     677# USAGE: isconnected ?<host:port>,<host:port>...?
     678#
     679# Clients use this method to establish a connection to a new
     680# server, or to reestablish a connection to the previous server.
     681# Any existing connection is automatically closed.
     682# ----------------------------------------------------------------------
     683itcl::body Rappture::NanovisViewer::isconnected {} {
     684    return [VisViewer::IsConnected]
     685}
     686
     687# ----------------------------------------------------------------------
     688# USAGE: Disconnect
    893689#
    894690# Clients use this method to disconnect from the current rendering
    895691# server.
    896692# ----------------------------------------------------------------------
    897 itcl::body Rappture::NanovisViewer::disconnect {} {
    898     if {"" != $_sid} {
    899         catch {close $_sid}
    900         set _sid ""
    901     }
    902 
    903     set _buffer(in) ""
    904     set _buffer(out) ""
     693itcl::body Rappture::NanovisViewer::Disconnect {} {
     694    VisViewer::Disconnect
    905695
    906696    # disconnected -- no more data sitting on server
     697    set _outbuf ""
    907698    catch {unset _obj2id}
    908699    array unset _id2obj
     
    914705
    915706# ----------------------------------------------------------------------
    916 # USAGE: isconnected
    917 #
    918 # Clients use this method to see if we are currently connected to
    919 # a server.
    920 # ----------------------------------------------------------------------
    921 itcl::body Rappture::NanovisViewer::isconnected {} {
    922     return [expr {"" != $_sid}]
    923 }
    924 
    925 # ----------------------------------------------------------------------
    926 # USAGE: _send <arg> <arg> ...
     707# USAGE: _send <string>
    927708#
    928709# Used internally to send commands off to the rendering server.
    929 # This is a more convenient form of _send_text, which actually
    930 # does the sending.
    931 # ----------------------------------------------------------------------
    932 itcl::body Rappture::NanovisViewer::_send {args} {
    933     _send_text $args
    934 }
    935 
    936 # ----------------------------------------------------------------------
    937 # USAGE: _send_text <string>
    938 #
    939 # Used internally to send commands off to the rendering server.
    940 # ----------------------------------------------------------------------
    941 itcl::body Rappture::NanovisViewer::_send_text {string} {
    942     if {"" == $_sid} {
     710# ----------------------------------------------------------------------
     711itcl::body Rappture::NanovisViewer::_send {string} {
     712    if { ![isconnected]} {
    943713        $_dispatcher cancel !serverDown
    944714        set x [expr {[winfo rootx $itk_component(area)]+10}]
     
    946716        Rappture::Tooltip::cue @$x,$y "Connecting..."
    947717
    948         if {[catch {connect} ok] == 0 && $ok} {
     718        set code [catch { Connect } ok]
     719        if { $code == 0 && $ok} {
    949720            set w [winfo width $itk_component(3dview)]
    950721            set h [winfo height $itk_component(3dview)]
    951722
    952             if {[catch {puts $_sid "screen $w $h"}]} {
    953                 disconnect
    954                 _receive_echo closed
    955                 $_dispatcher event -after 750 !serverDown
    956             } else {
    957                 _send_echo >>line "screen $w $h"
    958 
     723            if { [Send "screen $w $h"] } {
    959724                set _view(theta) 45
    960725                set _view(phi) 45
    961726                set _view(psi) 0
    962727                set _view(zoom) 1.0
    963                 after idle [itcl::code $this _rebuild]
     728                $_dispatcher event -idle !rebuild
    964729                Rappture::Tooltip::cue hide
    965730            }
    966             return
    967         }
    968         Rappture::Tooltip::cue @$x,$y "Can't connect to visualization server.  This may be a network problem.  Wait a few moments and try resetting the view."
    969         return
    970     }
    971     if {"" != $_sid} {
    972         # if we're transmitting objects, then buffer this command
     731        } else {
     732            Rappture::Tooltip::cue @$x,$y "Can't connect to visualization server.  This may be a network problem.  Wait a few moments and try resetting the view."
     733        }
     734    } else {
     735        # If we're transmitting objects, then buffer this command.
    973736        if {[llength $_sendobjs] > 0} {
    974             append _buffer(out) $string "\n"
     737            append _outbuf $string "\n"
    975738        } else {
    976             if {[catch {puts $_sid $string}]} {
    977                 disconnect
    978                 _receive_echo closed
    979                 $_dispatcher event -after 750 !serverDown
    980             } else {
     739            if { [Send $string] } {
    981740                foreach line [split $string \n] {
    982                     _send_echo >>line $line
     741                    SendEcho >>line $line
    983742                }
    984743            }
     
    1004763            # tell the engine to expect some data
    1005764            set cmdstr "volume data follows [string length $data]"
    1006             _send_echo >>line $cmdstr
    1007             if {[catch {puts $_sid $cmdstr} err]} {
    1008                 disconnect
    1009                 $_dispatcher event -after 750 !serverDown
     765            if { ![Send $cmdstr] } {
    1010766                return
    1011767            }
    1012 
    1013768            while {[string length $data] > 0} {
    1014769                update
     
    1016771                set chunk [string range $data 0 8095]
    1017772                set data [string range $data 8096 end]
    1018 
    1019                 _send_echo >>line $chunk
    1020                 if {[catch {puts -nonewline $_sid $chunk} err]} {
    1021                     disconnect
    1022                     $_dispatcher event -after 750 !serverDown
     773                if { ![Send $chunk -nonewline] } {
    1023774                    return
    1024775                }
    1025                 catch {flush $_sid}
    1026             }
    1027             _send_echo >>line ""
    1028             puts $_sid ""
     776                Flush
     777            }
     778            Send ""
    1029779
    1030780            set volId $_obj2id(count)
     
    1044794        set axis [$first hints updir]
    1045795        if {"" != $axis} {
    1046             _send up $axis
     796            _send "up $axis"
    1047797        }
    1048798    }
     
    1051801    set vols [_currentVolumeIds -cutplanes]
    1052802    foreach axis {x y z} {
    1053         eval _send cutplane state [_state ${axis}slice] $axis $vols
     803        _send "cutplane state [_state ${axis}slice] $axis $vols"
    1054804        set pos [expr {0.01*[$itk_component(${axis}slicer) get]}]
    1055         eval _send cutplane position $pos $axis $vols
    1056     }
    1057     eval _send volume data state [_state volume] $vols
     805        _send "cutplane position $pos $axis $vols"
     806    }
     807    _send "volume data state [_state volume] $vols"
    1058808
    1059809    # if there are any commands in the buffer, send them now that we're done
    1060     _send_echo >>line $_buffer(out)
    1061     if {[catch {puts $_sid $_buffer(out)} err]} {
    1062         disconnect
    1063         $_dispatcher event -after 750 !serverDown
    1064     }
    1065     set _buffer(out) ""
    1066 
    1067 #    $_dispatcher event -idle !legend
     810    Send $_outbuf
     811    set _outbuf ""
     812
     813    $_dispatcher event -idle !legend
    1068814}
    1069815
     
    1080826            #
    1081827            if { ![info exists _isomarkers($dataobj)] } {
    1082                 _initIsoMarkers $dataobj $comp
     828                InitIsoMarkers $dataobj $comp
    1083829            } else {
    1084                 _hideIsoMarkers $dataobj
     830                HideIsoMarkers $dataobj
    1085831            }
    1086             foreach {sname cmap wmap} [_genTransfuncData $dataobj $comp] break
     832            foreach {sname cmap wmap} [GenTransfuncData $dataobj $comp] break
    1087833            set cmdstr [list transfunc define $sname $cmap $wmap]
    1088             _send_echo >>line $cmdstr
    1089             if {[catch {puts $_sid $cmdstr} err]} {
    1090                 disconnect
    1091                 $_dispatcher event -after 750 !serverDown
     834            if {![Send $cmdstr] } {
    1092835                return
    1093836            }
     
    1102845    foreach key [array names _obj2id *-*] {
    1103846        set state [string match $first-* $key]
    1104         _send volume state $state $_obj2id($key)
     847        _send "volume state $state $_obj2id($key)"
    1105848        if {[info exists _obj2style($key)]} {
    1106             _send volume shading transfunc $_obj2style($key) $_obj2id($key)
    1107         }
    1108     }
    1109     _showIsoMarkers $first
     849            _send "volume shading transfunc $_obj2style($key) $_obj2id($key)"
     850        }
     851    }
     852    ShowIsoMarkers $first
    1110853    # if there are any commands in the buffer, send them now that we're done
    1111     _send_echo >>line $_buffer(out)
    1112     if {[catch {puts $_sid $_buffer(out)} err]} {
    1113         disconnect
    1114         $_dispatcher event -after 750 !serverDown
    1115     }
    1116     set _buffer(out) ""
     854    Send $_outbuf
     855    set _outbuf ""
    1117856    $_dispatcher event -idle !legend
    1118 }
    1119 
    1120 # ----------------------------------------------------------------------
    1121 # USAGE: _send_echo <channel> ?<data>?
    1122 #
    1123 # Used internally to echo sent data to clients interested in
    1124 # this widget.  If the -sendcommand option is set, then it is
    1125 # invoked in the global scope with the <channel> and <data> values
    1126 # as arguments.  Otherwise, this does nothing.
    1127 # ----------------------------------------------------------------------
    1128 itcl::body Rappture::NanovisViewer::_send_echo {channel {data ""}} {
    1129     if {[string length $itk_option(-sendcommand)] > 0} {
    1130         uplevel #0 $itk_option(-sendcommand) [list $channel $data]
    1131     }
    1132 }
    1133 
    1134 # ----------------------------------------------------------------------
    1135 # USAGE: _receive
    1136 #
    1137 # Invoked automatically whenever a command is received from the
    1138 # rendering server.  Reads the incoming command and executes it in
    1139 # a safe interpreter to handle the action.
    1140 # ----------------------------------------------------------------------
    1141 itcl::body Rappture::NanovisViewer::_receive {} {
    1142     if {"" != $_sid} {
    1143         if {[gets $_sid line] < 0} {
    1144             disconnect
    1145             _receive_echo closed
    1146             $_dispatcher event -after 750 !serverDown
    1147         } elseif {[string equal [string range $line 0 2] "nv>"]} {
    1148             _receive_echo <<line $line
    1149             append _buffer(in) [string range $line 3 end]
    1150             if {[info complete $_buffer(in)]} {
    1151                 set request $_buffer(in)
    1152                 set _buffer(in) ""
    1153                 $_parser eval $request
    1154             }
    1155         } else {
    1156             # this shows errors coming back from the engine
    1157             _receive_echo <<error $line
    1158             puts stderr $line
    1159         }
    1160     }
    1161857}
    1162858
     
    1169865# ----------------------------------------------------------------------
    1170866itcl::body Rappture::NanovisViewer::_receive_image {option size} {
    1171     if {"" != $_sid} {
    1172         set bytes [read $_sid $size]
     867    if { [isconnected] } {
     868        set bytes [Receive $size]
    1173869        $_image(plot) configure -data $bytes
    1174         _receive_echo <<line "<read $size bytes for [image width $_image(plot)]x[image height $_image(plot)] image>"
     870        ReceiveEcho <<line "<read $size bytes for [image width $_image(plot)]x[image height $_image(plot)] image>"
    1175871    }
    1176872}
     
    1183879# specified <size> will follow.
    1184880# ----------------------------------------------------------------------
    1185 itcl::body Rappture::NanovisViewer::_receive_legend {ivol vmin vmax size} {
    1186     if {"" != $_sid} {
    1187         set bytes [read $_sid $size]
     881itcl::body Rappture::NanovisViewer::_receive_legend { ivol vmin vmax size } {
     882    if { [isconnected] } {
     883        set bytes [Receive $size]
    1188884        $_image(legend) configure -data $bytes
    1189         _receive_echo <<line "<read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
     885        ReceiveEcho <<line "<read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
    1190886
    1191887        set c $itk_component(legend)
     
    1201897            $c lower transfunc
    1202898            $c bind transfunc <ButtonRelease-1> \
    1203                 [itcl::code $this _addIsoMarker %x %y]
     899                [itcl::code $this AddIsoMarker %x %y]
    1204900        }
    1205901        $c itemconfigure vmin -text $vmin
     
    1209905        $c coords vmax [expr {$w-10}] [expr {$h-8}]
    1210906        set first [lindex [get] 0]
    1211         _showIsoMarkers $first
     907        ShowIsoMarkers $first
    1212908    }
    1213909}
     
    1221917# ----------------------------------------------------------------------
    1222918itcl::body Rappture::NanovisViewer::_receive_data { args } {
    1223     if {"" != $_sid} {
     919    if { [isconnected] } {
    1224920        array set info $args
    1225921        set volume $info(id)
    1226922        foreach { dataobj comp } $_id2obj($volume) break
    1227         if { ![info exists _limits($dataobj-vmin] } {
     923       
     924        if { ![info exists _limits($dataobj-vmin)] } {
    1228925            set _limits($dataobj-vmin) $info(min)
    1229926            set _limits($dataobj-vmax) $info(max)
     
    1234931            if { $_limits($dataobj-vmax) > $info(max) } {
    1235932                set _limits($dataobj-vmax) $info(max)
    1236             } 
    1237         }           
     933            }
     934        }
    1238935        set _limits(vmin) $info(vmin)
    1239936        set _limits(vmax) $info(vmax)
     
    1241938        unset _receiveids($info(id))
    1242939        if { [array size _receiveids] == 0 } {
    1243             after idle [itcl::code $this _send_transfuncs]
     940            $_dispatcher event -idle !send_transfuncs
    1244941        }
    1245     }
    1246 }
    1247 
    1248 # ----------------------------------------------------------------------
    1249 # USAGE: _receive_echo <channel> ?<data>?
    1250 #
    1251 # Used internally to echo received data to clients interested in
    1252 # this widget.  If the -receivecommand option is set, then it is
    1253 # invoked in the global scope with the <channel> and <data> values
    1254 # as arguments.  Otherwise, this does nothing.
    1255 # ----------------------------------------------------------------------
    1256 itcl::body Rappture::NanovisViewer::_receive_echo {channel {data ""}} {
    1257     if {[string length $itk_option(-receivecommand)] > 0} {
    1258         uplevel #0 $itk_option(-receivecommand) [list $channel $data]
    1259942    }
    1260943}
     
    1290973    if {[llength $_sendobjs] > 0} {
    1291974        # send off new data objects
    1292         after idle [itcl::code $this _send_dataobjs]
     975        $_dispatcher event -idle !send_dataobjs
    1293976    } else {
    1294977        # nothing to send -- activate the proper volume
     
    1297980            set axis [$first hints updir]
    1298981            if {"" != $axis} {
    1299                 _send up $axis
    1300             }
    1301         }
    1302 
    1303         _showIsoMarkers $first
    1304         update_transfer_function
     982                _send "up $axis"
     983            }
     984        }
     985        ShowIsoMarkers $first
     986        UpdateTransferFunction
    1305987
    1306988        # sync the state of slicers
    1307989        set vols [_currentVolumeIds -cutplanes]
    1308990        foreach axis {x y z} {
    1309             eval _send cutplane state [_state ${axis}slice] $axis $vols
     991            _send "cutplane state [_state ${axis}slice] $axis $vols"
    1310992            set pos [expr {0.01*[$itk_component(${axis}slicer) get]}]
    1311             eval _send cutplane position $pos $axis $vols
    1312         }
    1313         eval _send volume data state [_state volume] $vols
     993            _send "cutplane position $pos $axis $vols"
     994        }
     995        _send "volume data state [_state volume] $vols"
    1314996        $_dispatcher event -idle !legend
    1315997    }
     
    13181000    # Reset the camera and other view parameters
    13191001    #
    1320     eval _send camera angle [_euler2xyz $_view(theta) $_view(phi) $_view(psi)]
    1321     _send camera zoom $_view(zoom)
     1002    _send "camera angle [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]"
     1003    _send "camera zoom $_view(zoom)"
    13221004
    13231005    _fixSettings light
     
    13281010
    13291011    if {"" == $itk_option(-plotoutline)} {
    1330         _send volume outline state off
     1012        _send "volume outline state off"
    13311013    } else {
    1332         _send volume outline state on
    1333         _send volume outline color [_color2rgb $itk_option(-plotoutline)]
    1334     }
    1335     _send volume axis label x ""
    1336     _send volume axis label y ""
    1337     _send volume axis label z ""
     1014        _send "volume outline state on"
     1015        set rgb [Color2RGB $itk_option(-plotoutline)]
     1016        _send "volume outline color $rgb"
     1017    }
     1018    _send "volume axis label x \"\""
     1019    _send "volume axis label y \"\""
     1020    _send "volume axis label z \"\""
    13381021}
    13391022
     
    13771060        in {
    13781061            set _view(zoom) [expr {$_view(zoom)*1.25}]
    1379             _send camera zoom $_view(zoom)
     1062            _send "camera zoom $_view(zoom)"
    13801063        }
    13811064        out {
    13821065            set _view(zoom) [expr {$_view(zoom)*0.8}]
    1383             _send camera zoom $_view(zoom)
     1066            _send "camera zoom $_view(zoom)"
    13841067        }
    13851068        reset {
     
    13881071            set _view(psi) 0
    13891072            set _view(zoom) 1.0
    1390             eval _send camera angle [_euler2xyz $_view(theta) $_view(phi) $_view(psi)]
    1391             _send camera zoom $_view(zoom)
     1073            set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
     1074            _send "camera angle $xyz"
     1075            _send "camera zoom $_view(zoom)"
    13921076        }
    13931077    }
     
    14551139                set _view(phi) $phi
    14561140                set _view(psi) $psi
    1457                 eval _send camera angle [_euler2xyz $_view(theta) $_view(phi) $_view(psi)]
    1458 
     1141                set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
     1142                _send "camera angle $xyz"
    14591143                set _click(x) $x
    14601144                set _click(y) $y
     
    14961180                if {$current == "on"} { set op "off" } else { set op "on" }
    14971181            }
    1498 
    14991182            if {$op} {
    15001183                $itk_component(${axis}slicer) configure -state normal
    1501                 eval _send cutplane state on $axis [_currentVolumeIds -cutplanes]
     1184                _send "cutplane state 1 $axis [_currentVolumeIds -cutplanes]"
    15021185                $itk_component(${axis}slice) configure -relief sunken
    15031186            } else {
    15041187                $itk_component(${axis}slicer) configure -state disabled
    1505                 eval _send cutplane state off $axis [_currentVolumeIds -cutplanes]
     1188                _send "cutplane state 0 $axis [_currentVolumeIds -cutplanes]"
    15061189                $itk_component(${axis}slice) configure -relief raised
    15071190            }
     
    15221205#puts "readout: $axis = $newval"
    15231206
    1524             eval _send cutplane position $newpos $axis [_currentVolumeIds -cutplanes]
     1207            set ids [_currentVolumeIds -cutplanes]
     1208            _send "cutplane position $newpos $axis $ids"
    15251209        }
    15261210        volume {
     
    15371221
    15381222            if {$op} {
    1539                 eval _send volume data state on [_currentVolumeIds]
     1223                _send "volume data state on [_currentVolumeIds]"
    15401224                $itk_component(volume) configure -relief sunken
    15411225            } else {
    1542                 eval _send volume data state off [_currentVolumeIds]
     1226                _send "volume data state off [_currentVolumeIds]"
    15431227                $itk_component(volume) configure -relief raised
    15441228            }
     
    15921276                set val [$inner.scales.light get]
    15931277                set sval [expr {0.1*$val}]
    1594                 _send volume shading diffuse $sval
     1278                _send "volume shading diffuse $sval"
    15951279
    15961280                set sval [expr {sqrt($val+1.0)}]
    1597                 _send volume shading specular $sval
     1281                _send "volume shading specular $sval"
    15981282            }
    15991283        }
     
    16021286                set val [$inner.scales.transp get]
    16031287                set sval [expr {0.2*$val+1}]
    1604                 _send volume shading opacity $sval
     1288                _send "volume shading opacity $sval"
    16051289            }
    16061290        }
     
    16121296                    set sval [expr {0.01*double($val)}]
    16131297                    set _opacity($dataobj) $sval
    1614                     update_transfer_function
     1298                    UpdateTransferFunction
    16151299                }
    16161300            }
     
    16231307                    # Scale values between 0.00001 and 0.01000
    16241308                    set sval [expr {0.00001*double($val)}]
    1625                     puts stderr "thickness($dataobj) = $sval"
    16261309                    set _thickness($dataobj) $sval
    1627                     update_transfer_function
     1310                    UpdateTransferFunction
    16281311                }
    16291312            }
     
    16331316                set val $Rappture::NanovisViewer::_isosurface($this)
    16341317                set dataobj [lindex [get] 0]
    1635                 _send "volume" "shading" "isosurface" $val
     1318                _send "volume shading isosurface $val"
    16361319            }
    16371320        }           
     
    16631346    }
    16641347    if {$w > 0 && $h > 0 && "" != $ivol} {
    1665         _send legend $ivol $w $h
     1348        _send "legend $ivol $w $h"
    16661349    } else {
    16671350        #$itk_component(legend) delete all
     
    16701353
    16711354# ----------------------------------------------------------------------
    1672 # USAGE: _serverDown
    1673 #
    1674 # Used internally to let the user know when the connection to the
    1675 # visualization server has been lost.  Puts up a tip encouraging the
    1676 # user to press any control to reconnect.
    1677 # ----------------------------------------------------------------------
    1678 itcl::body Rappture::NanovisViewer::_serverDown {} {
    1679     set x [expr {[winfo rootx $itk_component(area)]+10}]
    1680     set y [expr {[winfo rooty $itk_component(area)]+10}]
    1681     Rappture::Tooltip::cue @$x,$y "Lost connection to visualization server.  This happens sometimes when there are too many users and the system runs out of memory.\n\nTo reconnect, reset the view or press any other control.  Your picture should come right back up."
    1682 }
    1683 
    1684 # ----------------------------------------------------------------------
    1685 # USAGE: _genTransfuncData <dataobj> <comp>
     1355# USAGE: GenTransfuncData <dataobj> <comp>
    16861356#
    16871357# Used internally to compute the colormap and alpha map used to define
     
    16891359# Returns: name {v r g b ...} {v w ...}
    16901360# ----------------------------------------------------------------------
    1691 itcl::body Rappture::NanovisViewer::_genTransfuncData {dataobj comp} {
     1361itcl::body Rappture::NanovisViewer::GenTransfuncData {dataobj comp} {
    16921362    array set style {
    16931363        -color rainbow
     
    17021372    }
    17031373    set clist [split $style(-color) :]
    1704     set cmap "0.0 [_color2rgb white] "
     1374    set cmap "0.0 [Color2RGB white] "
    17051375    for {set i 0} {$i < [llength $clist]} {incr i} {
    17061376        set x [expr {double($i+1)/([llength $clist]+1)}]
    17071377        set color [lindex $clist $i]
    1708         append cmap "$x [_color2rgb $color] "
    1709     }
    1710     append cmap "1.0 [_color2rgb $color]"
     1378        append cmap "$x [Color2RGB $color] "
     1379    }
     1380    append cmap "1.0 [Color2RGB $color]"
    17111381
    17121382    set max $style(-opacity)
     
    17271397        set delta $_thickness($dataobj)
    17281398    }
    1729     puts stderr "delta=$delta thickness($dataobj)=$_thickness($dataobj)"
    17301399    set first [lindex $isovalues 0]
    17311400    set last [lindex $isovalues end]
     
    17641433
    17651434# ----------------------------------------------------------------------
    1766 # USAGE: _color2rgb <color>
    1767 #
    1768 # Used internally to convert a color name to a set of {r g b} values
    1769 # needed for the engine.  Each r/g/b component is scaled in the
    1770 # range 0-1.
    1771 # ----------------------------------------------------------------------
    1772 itcl::body Rappture::NanovisViewer::_color2rgb {color} {
    1773     foreach {r g b} [winfo rgb $itk_component(hull) $color] break
    1774     set r [expr {$r/65535.0}]
    1775     set g [expr {$g/65535.0}]
    1776     set b [expr {$b/65535.0}]
    1777     return [list $r $g $b]
    1778 }
    1779 
    1780 # ----------------------------------------------------------------------
    1781 # USAGE: _euler2xyz <theta> <phi> <psi>
    1782 #
    1783 # Used internally to convert euler angles for the camera placement
    1784 # the to angles of rotation about the x/y/z axes, used by the engine.
    1785 # Returns a list:  {xangle, yangle, zangle}.
    1786 # ----------------------------------------------------------------------
    1787 itcl::body Rappture::NanovisViewer::_euler2xyz {theta phi psi} {
    1788     set xangle [expr {$theta-90.0}]
    1789     set yangle [expr {180-$phi}]
    1790     set zangle $psi
    1791     return [list $xangle $yangle $zangle]
    1792 }
    1793 
    1794 # ----------------------------------------------------------------------
    17951435# CONFIGURATION OPTION: -plotbackground
    17961436# ----------------------------------------------------------------------
    17971437itcl::configbody Rappture::NanovisViewer::plotbackground {
    1798     foreach {r g b} [_color2rgb $itk_option(-plotbackground)] break
    1799     #fix this!
    1800     #_send color background $r $g $b
     1438    if { [isconnected] } {
     1439        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
     1440        #fix this!
     1441        #_send "color background $r $g $b"
     1442    }
    18011443}
    18021444
     
    18051447# ----------------------------------------------------------------------
    18061448itcl::configbody Rappture::NanovisViewer::plotforeground {
    1807     foreach {r g b} [_color2rgb $itk_option(-plotforeground)] break
    1808     #fix this!
    1809     #_send color background $r $g $b
     1449    if { [isconnected] } {
     1450        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
     1451        #fix this!
     1452        #_send "color background $r $g $b"
     1453    }
    18101454}
    18111455
     
    18141458# ----------------------------------------------------------------------
    18151459itcl::configbody Rappture::NanovisViewer::plotoutline {
    1816     if {[isconnected]} {
     1460    # Must check if we are connected because this routine is called from the
     1461    # class body when the -plotoutline itk_option is defined.  At that point
     1462    # the NanovisViewer class constructor hasn't been called, so we can't
     1463    # start sending commands to visualization server.
     1464    if { [isconnected] } {
    18171465        if {"" == $itk_option(-plotoutline)} {
    1818             _send volume outline state off
     1466            _send "volume outline state off"
    18191467        } else {
    1820             _send volume outline state on
    1821             _send volume outline color [_color2rgb $itk_option(-plotoutline)]
    1822         }
    1823     }
    1824 }
    1825 
    1826 itcl::body Rappture::NanovisViewer::_hideIsoMarkers {dataobj} {
     1468            _send "volume outline state on"
     1469            _send "volume outline color [Color2RGB $itk_option(-plotoutline)]"
     1470        }
     1471    }
     1472}
     1473
     1474itcl::body Rappture::NanovisViewer::HideIsoMarkers {dataobj} {
    18271475    if { [info exists _isomarkers($dataobj)] } {
    18281476        foreach m $_isomarkers($dataobj) {
     
    18321480}
    18331481
    1834 itcl::body Rappture::NanovisViewer::_showIsoMarkers {dataobj} {
     1482itcl::body Rappture::NanovisViewer::ShowIsoMarkers {dataobj} {
    18351483    foreach obj [array names _all_data_objs] {
    1836         _hideIsoMarkers $obj
     1484        HideIsoMarkers $obj
    18371485    }
    18381486    if { ![info exists _isomarkers($dataobj)] } {
     
    18441492}
    18451493
    1846 itcl::body Rappture::NanovisViewer::_initIsoMarkers {dataobj comp} {
     1494itcl::body Rappture::NanovisViewer::InitIsoMarkers {dataobj comp} {
    18471495    array set style {
    18481496        -levels 6x
     
    18911539# legend.
    18921540# ----------------------------------------------------------------------
    1893 itcl::body Rappture::NanovisViewer::update_transfer_function {} {
     1541itcl::body Rappture::NanovisViewer::UpdateTransferFunction {} {
    18941542    set dataobj [lindex [get] 0]
    18951543    if {"" == $dataobj} {
     
    19021550    }
    19031551    # Compute a transfer function for the current data set.
    1904     foreach {sname cmap wmap} [_genTransfuncData $dataobj $comp] break
    1905     _send transfunc define $_obj2style($key) $cmap $wmap
    1906     _send volume shading transfunc $_obj2style($key) $_obj2id($key)
     1552    foreach {sname cmap wmap} [GenTransfuncData $dataobj $comp] break
     1553    _send [list transfunc define $_obj2style($key) $cmap $wmap]
     1554    _send [list volume shading transfunc $_obj2style($key) $_obj2id($key)]
    19071555    _fixLegend
    19081556}
    19091557
    1910 itcl::body Rappture::NanovisViewer::_addIsoMarker { x y } {
     1558itcl::body Rappture::NanovisViewer::AddIsoMarker { x y } {
    19111559    set dataobj [lindex [get] 0]
    19121560    if {$dataobj == ""} {
     
    19191567    $m show
    19201568    lappend _isomarkers($dataobj) $m
    1921     update_transfer_function
     1569    UpdateTransferFunction
    19221570    return 1
    19231571}
    19241572
    1925 itcl::body Rappture::NanovisViewer::remove_duplicate_isomarker { marker x } {
     1573itcl::body Rappture::NanovisViewer::RemoveDuplicateIsoMarker { marker x } {
    19261574    set dataobj [lindex [get] 0]
    19271575    if {"" == $dataobj} {
     
    19461594        }
    19471595        set _isomarkers($dataobj) $list
    1948         update_transfer_function
     1596        UpdateTransferFunction
    19491597    }
    19501598    return $bool
    19511599}
    19521600
    1953 itcl::body Rappture::NanovisViewer::over_isomarker { marker x } {
     1601itcl::body Rappture::NanovisViewer::OverIsoMarker { marker x } {
    19541602    set dataobj [lindex [get] 0]
    19551603    if {"" == $dataobj} {
Note: See TracChangeset for help on using the changeset viewer.