Ignore:
Timestamp:
Jun 2, 2006 9:40:16 PM (18 years ago)
Author:
mmc
Message:

Added the start of a legend component to the 3D field viewer.

Fixed problems with <sequence> objects when there's only 1 value
in the sequence. Also, added 3D fields as a recognized object
for sequences.

Fixed the 3D field viewer to fail more gracefully when it can't
make a connection to the viz server.

File:
1 edited

Legend:

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

    r450 r456  
    5151    protected method _receive {}
    5252    protected method _receive_image {option size}
     53    protected method _receive_legend {ivol vmin vmax size}
    5354
    5455    protected method _rebuild {}
     
    6061    protected method _state {comp}
    6162    protected method _fixSettings {what {value ""}}
     63    protected method _fixLegend {}
    6264    protected method _color2rgb {color}
    6365    protected method _euler2xyz {theta phi psi}
     66
     67    private variable _dispatcher "" ;# dispatcher for !events
    6468
    6569    private variable _nvhost ""    ;# host name for nanovis server
     
    6872    private variable _parser ""    ;# interpreter for incoming commands
    6973    private variable _buffer       ;# buffer for incoming/outgoing commands
    70     private variable _image ""     ;# image displayed in plotting area
     74    private variable _image        ;# image displayed in plotting area
    7175
    7276    private variable _dlist ""     ;# list of data objects
    7377    private variable _dims ""      ;# dimensionality of data objects
    7478    private variable _obj2style    ;# maps dataobj => style settings
     79    private variable _obj2ovride   ;# maps dataobj => style override
    7580    private variable _obj2id       ;# maps dataobj => volume ID in server
    7681    private variable _sendobjs ""  ;# list of data objs to send to server
     
    9095# ----------------------------------------------------------------------
    9196itcl::body Rappture::NanovisViewer::constructor {nvhost nvport args} {
    92 puts "nanovisviewer $nvhost $nvport = $this"
     97    Rappture::dispatcher _dispatcher
     98    $_dispatcher register !legend
     99    $_dispatcher dispatch $this !legend "[itcl::code $this _fixLegend]; list"
     100
    93101    set _buffer(in) ""
    94102    set _buffer(out) ""
     
    102110    }
    103111    $_parser alias image [itcl::code $this _receive_image]
     112    $_parser alias legend [itcl::code $this _receive_legend]
    104113
    105114    #
     
    355364    # RENDERING AREA
    356365    #
    357     set _image [image create photo]
     366    set _image(legend) [image create photo]
     367    itk_component add legend {
     368        canvas $itk_interior.legend -height 50 -highlightthickness 0
     369    } {
     370        usual
     371        ignore -highlightthickness
     372        rename -background -plotbackground plotBackground Background
     373    }
     374    pack $itk_component(legend) -side bottom -fill x
     375    bind $itk_component(legend) <Configure> \
     376        [list $_dispatcher event -idle !legend]
     377
     378    set _image(plot) [image create photo]
    358379    itk_component add area {
    359         label $itk_interior.area -image $_image
     380        label $itk_interior.area -image $_image(plot) -highlightthickness 0
     381    } {
     382        usual
     383        ignore -highlightthickness
    360384    }
    361385    pack $itk_component(area) -expand yes -fill both
     
    383407    after cancel [itcl::code $this _send_dataobjs]
    384408    after cancel [itcl::code $this _rebuild]
    385     image delete $_image
     409    image delete $_image(plot)
     410    image delete $_image(legend)
    386411    interp delete $_parser
    387412}
     
    416441    if {$pos < 0} {
    417442        lappend _dlist $dataobj
    418         set _obj2style($dataobj-color) $params(-color)
    419         set _obj2style($dataobj-width) $params(-width)
    420         set _obj2style($dataobj-raise) $params(-raise)
     443        set _obj2ovride($dataobj-color) $params(-color)
     444        set _obj2ovride($dataobj-width) $params(-width)
     445        set _obj2ovride($dataobj-raise) $params(-raise)
    421446
    422447        after cancel [itcl::code $this _rebuild]
     
    435460    set dlist $_dlist
    436461    foreach obj $dlist {
    437         if {[info exists _obj2style($obj-raise)] && $_obj2style($obj-raise)} {
     462        if {[info exists _obj2ovride($obj-raise)] && $_obj2ovride($obj-raise)} {
    438463            set i [lsearch -exact $dlist $obj]
    439464            if {$i >= 0} {
     
    463488        if {$pos >= 0} {
    464489            set _dlist [lreplace $_dlist $pos $pos]
    465             foreach key [array names _obj2style $dataobj-*] {
    466                 unset _obj2style($key)
     490            foreach key [array names _obj2ovride $dataobj-*] {
     491                unset _obj2ovride($key)
    467492            }
    468493            set changed 1
     
    530555            #
    531556            set tmpfile /tmp/image[pid].jpg
    532             $_image write $tmpfile -format jpeg
     557            $_image(plot) write $tmpfile -format jpeg
    533558            set fid [open $tmpfile r]
    534559            fconfigure $fid -encoding binary -translation binary
     
    553578# ----------------------------------------------------------------------
    554579itcl::body Rappture::NanovisViewer::connect {{host ""} {port ""}} {
    555 puts " connecting... $host $port"
    556580    disconnect
    557581
     
    572596    #
    573597    while {1} {
    574 puts "connecting to $_nvhost:$_nvport"
    575598        if {[catch {socket $_nvhost $_nvport} sid]} {
    576599            return 0
     
    593616            [expr {$b4 & 0xff}]]
    594617
    595 puts "  got back $addr"
    596618        if {[string equal $addr "0.0.0.0"]} {
    597619            fconfigure $sid -buffering line
    598620            fileevent $sid readable [itcl::code $this _receive]
    599621            set _sid $sid
     622tk_messageBox -message "Continue?"
    600623            return 1
    601624        }
     
    668691            append _buffer(out) $args "\n"
    669692        } else {
    670 puts "send: $args"
     693set fid [open /tmp/debug a]
     694puts $fid "send: $args"
     695close $fid
    671696            puts $_sid $args
    672697        }
     
    696721                return
    697722            }
     723set fid [open /tmp/debug a]
     724puts $fid $cmdstr
     725puts $fid $data
     726close $fid
    698727
    699728            while {[string length $data] > 0} {
     
    716745            set _obj2id($dataobj-$comp) $_obj2id(count)
    717746            incr _obj2id(count)
     747
     748            #
     749            # Determine the transfer function needed for this volume
     750            # and make sure that it's defined on the server.
     751            #
     752            catch {unset style}
     753            array set style {
     754                -color rainbow
     755                -levels 6
     756                -opacity 0.5
     757            }
     758            array set style [lindex [$dataobj components -style $comp] 0]
     759            set sname "$style(-color):$style(-levels):$style(-opacity)"
     760
     761            if {$style(-color) == "rainbow"} {
     762                set style(-color) "white:yellow:green:cyan:blue:magenta"
     763            }
     764            set clist [split $style(-color) :]
     765            set cmap "0.0 [_color2rgb white] "
     766            for {set i 0} {$i < [llength $clist]} {incr i} {
     767                set xval [expr {double($i+1)/([llength $clist]+1)}]
     768                set color [lindex $clist $i]
     769                append cmap "$xval [_color2rgb $color] "
     770            }
     771            append cmap "1.0 [_color2rgb $color]"
     772
     773            set max $style(-opacity)
     774            set levels $style(-levels)
     775            set wmap "0.0 0.0 "
     776            set delta [expr {0.125/($levels+1)}]
     777            for {set i 1} {$i <= $levels} {incr i} {
     778                # add spikes in the middle
     779                set xval [expr {double($i)/($levels+1)}]
     780                append wmap "[expr {$xval-$delta-0.01}] 0.0  [expr {$xval-$delta}] $max [expr {$xval+$delta}] $max  [expr {$xval+$delta+0.01}] 0.0 "
     781            }
     782            append wmap "1.0 0.0 "
     783
     784            set cmdstr [list transfunc define $sname $cmap $wmap]
     785puts "send: $cmdstr"
     786set fid [open /tmp/debug a]
     787puts $fid $cmdstr
     788close $fid
     789            if {[catch {puts $_sid $cmdstr} err]} {
     790                disconnect
     791                set x [expr {[winfo rootx $itk_component(area)]+10}]
     792                set y [expr {[winfo rooty $itk_component(area)]+10}]
     793                Rappture::Tooltip::cue @$x,$y "Lost connection to visualization server"
     794puts "oops: $err"
     795                return
     796            }
     797
     798            set _obj2style($dataobj-$comp) $sname
    718799        }
    719800    }
     
    725806        set state [string match $first-* $key]
    726807        _send volume state $state $_obj2id($key)
     808        if {[info exists _obj2style($key)]} {
     809            _send volume shading transfunc $_obj2style($key) $_obj2id($key)
     810        }
    727811    }
    728812
     
    735819
    736820    # if there are any commands in the buffer, send them now that we're done
     821set fid [open /tmp/debug a]
     822puts $fid $_buffer(out)
     823close $fid
    737824    if {[catch {puts $_sid $_buffer(out)} err]} {
    738825        disconnect
     
    742829    }
    743830    set _buffer(out) ""
     831
     832    $_dispatcher event -idle !legend
    744833}
    745834
     
    779868    if {"" != $_sid} {
    780869        set bytes [read $_sid $size]
    781         $_image configure -data $bytes
     870        $_image(plot) configure -data $bytes
    782871puts "--IMAGE"
     872    }
     873}
     874
     875# ----------------------------------------------------------------------
     876# USAGE: _receive_legend <volume> <vmin> <vmax> <size>
     877#
     878# Invoked automatically whenever the "legend" command comes in from
     879# the rendering server.  Indicates that binary image data with the
     880# specified <size> will follow.
     881# ----------------------------------------------------------------------
     882itcl::body Rappture::NanovisViewer::_receive_legend {ivol vmin vmax size} {
     883    if {"" != $_sid} {
     884        set bytes [read $_sid $size]
     885        $_image(legend) configure -data $bytes
     886puts "--LEGEND $vmin - $vmax"
     887
     888        set c $itk_component(legend)
     889        set w [winfo width $c]
     890        set h [winfo height $c]
     891        if {"" == [$c find withtag transfunc]} {
     892            $c create image [expr {$w/2}] [expr {$h/2}] -anchor c \
     893                 -image $_image(legend) -tags transfunc
     894        } else {
     895            $c coords transfunc [expr {$w/2}] [expr {$h/2}]
     896        }
    783897    }
    784898}
     
    821935            set state [string match $first-* $key]
    822936            _send volume state $state $_obj2id($key)
     937            if {[info exists _obj2style($key)]} {
     938                _send volume shading transfunc $_obj2style($key) $_obj2id($key)
     939            }
    823940        }
    824941
     
    829946        }
    830947        eval _send volume data state [_state volume] [_currentVolumeIds]
     948        $_dispatcher event -idle !legend
    831949    }
    832950
     
    862980    set first [lindex [get] 0]
    863981    foreach key [array names _obj2id *-*] {
    864         set state [string match $first-* $key]
    865         lappend rlist $_obj2id($key)
     982        if {[string match $first-* $key]} {
     983            lappend rlist $_obj2id($key)
     984        }
    866985    }
    867986    return $rlist
     
    8931012            eval _send camera angle [_euler2xyz $_view(theta) $_view(phi) $_view(psi)]
    8941013            _send camera zoom $_view(zoom)
    895 
    896             _send transfunc define default { 0 0 0 1 0.5  1 1 0 0 0.8 }
    8971014        }
    8981015    }
     
    11171234
    11181235# ----------------------------------------------------------------------
     1236# USAGE: _fixLegend
     1237#
     1238# Used internally to update the legend area whenever it changes size
     1239# or when the field changes.  Asks the server to send a new legend
     1240# for the current field.
     1241# ----------------------------------------------------------------------
     1242itcl::body Rappture::NanovisViewer::_fixLegend {} {
     1243    set w [expr {[winfo width $itk_component(legend)]-20}]
     1244    set h [expr {[winfo height $itk_component(legend)]-20}]
     1245    set ivol ""
     1246
     1247    set dataobj [lindex [get] 0]
     1248    set comp [lindex [$dataobj components] 0]
     1249    if {[info exists _obj2id($dataobj-$comp)]} {
     1250        set ivol $_obj2id($dataobj-$comp)
     1251    }
     1252puts "fixLegend $w $h $ivol in [get]"
     1253
     1254    if {$w > 0 && $h > 0 && "" != $ivol} {
     1255        _send legend $ivol $w $h
     1256    } else {
     1257        $itk_component(legend) delete all
     1258    }
     1259}
     1260
     1261# ----------------------------------------------------------------------
    11191262# USAGE: _color2rgb <color>
    11201263#
Note: See TracChangeset for help on using the changeset viewer.