source: trunk/gui/scripts/nanovisviewer.tcl @ 3330

Last change on this file since 3330 was 3330, checked in by gah, 12 years ago

merge (by hand) with Rappture1.2 branch

File size: 72.3 KB
RevLine 
[3330]1# -*- mode: tcl; indent-tabs-mode: nil -*-
[1373]2
[1254]3# ----------------------------------------------------------------------
[436]4#  COMPONENT: nanovisviewer - 3D volume rendering
5#
6#  This widget performs volume rendering on 3D scalar/vector datasets.
7#  It connects to the Nanovis server running on a rendering farm,
8#  transmits data, and displays the results.
9# ======================================================================
10#  AUTHOR:  Michael McLennan, Purdue University
[3177]11#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
[436]12#
13#  See the file "license.terms" for information on usage and
14#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15# ======================================================================
16package require Itk
17package require BLT
18package require Img
[1694]19                                       
[1549]20#
21# FIXME:
[2744]22#       Need to Add DX readers this client to examine the data before
23#       it's sent to the server.  This will eliminate 90% of the insanity in
24#       computing the limits of all the volumes.  I can rip out all the
25#       "receive data" "send transfer function" event crap.
[1549]26#
27#       This means we can compute the transfer function (relative values) and
[2744]28#       draw the legend min/max values without waiting for the information to
29#       come from the server.  This will also prevent the flashing that occurs
30#       when a new volume is drawn (using the default transfer function) and
31#       then when the correct transfer function has been sent and linked to
32#       the volume. 
[1549]33#
[436]34option add *NanovisViewer.width 4i widgetDefault
[878]35option add *NanovisViewer*cursor crosshair widgetDefault
[436]36option add *NanovisViewer.height 4i widgetDefault
37option add *NanovisViewer.foreground black widgetDefault
38option add *NanovisViewer.controlBackground gray widgetDefault
39option add *NanovisViewer.controlDarkBackground #999999 widgetDefault
40option add *NanovisViewer.plotBackground black widgetDefault
41option add *NanovisViewer.plotForeground white widgetDefault
42option add *NanovisViewer.plotOutline gray widgetDefault
43option add *NanovisViewer.font \
[676]44    -*-helvetica-medium-r-normal-*-12-* widgetDefault
[436]45
[907]46# must use this name -- plugs into Rappture::resources::load
47proc NanovisViewer_init_resources {} {
48    Rappture::resources::register \
[1694]49        nanovis_server Rappture::NanovisViewer::SetServerList
[907]50}
51
[436]52itcl::class Rappture::NanovisViewer {
[907]53    inherit Rappture::VisViewer
[1254]54
[436]55    itk_option define -plotforeground plotForeground Foreground ""
56    itk_option define -plotbackground plotBackground Background ""
57    itk_option define -plotoutline plotOutline PlotOutline ""
[1254]58
59    constructor { hostlist args } {
[1694]60        Rappture::VisViewer::constructor $hostlist
[1254]61    } {
[1694]62        # defined below
[907]63    }
[1254]64    destructor {
[1694]65        # defined below
[907]66    }
[935]67    public proc SetServerList { namelist } {
[1694]68        Rappture::VisViewer::SetServerList "nanovis" $namelist
[935]69    }
[436]70    public method add {dataobj {settings ""}}
[1435]71    public method camera {option args}
[436]72    public method delete {args}
[1514]73    public method disconnect {}
[464]74    public method download {option args}
[1435]75    public method get {args}
76    public method isconnected {}
77    public method limits { tf }
78    public method overmarker { m x }
[1514]79    public method sendto { string }
[1435]80    public method parameters {title args} {
[1694]81        # do nothing
[907]82    }
[1435]83    public method rmdupmarker { m x }
84    public method scale {args}
[1376]85    public method updatetransferfuncs {}
[436]86
[907]87    protected method Connect {}
[1514]88    protected method CurrentVolumes {{what -all}}
[907]89    protected method Disconnect {}
[1435]90    protected method DoResize {}
91    protected method FixLegend {}
92    protected method FixSettings {what {value ""}}
93    protected method Pan {option x y}
94    protected method Rebuild {}
95    protected method ReceiveData { args }
96    protected method ReceiveImage { args }
97    protected method ReceiveLegend { tf vmin vmax size }
98    protected method Rotate {option x y}
[1376]99    protected method SendCmd {string}
100    protected method SendTransferFuncs {}
101    protected method Slice {option args}
102    protected method SlicerTip {axis}
[1435]103    protected method Zoom {option}
[459]104
[1254]105    # The following methods are only used by this class.
[1376]106    private method AddIsoMarker { x y }
[1435]107    private method BuildCameraTab {}
[1376]108    private method BuildCutplanesTab {}
109    private method BuildViewTab {}
110    private method BuildVolumeTab {}
[1435]111    private method ComputeTransferFunc { tf }
112    private method EventuallyResize { w h }
113    private method EventuallyResizeLegend { }
114    private method NameTransferFunc { dataobj comp }
[1376]115    private method PanCamera {}
[1435]116    private method ParseLevelsOption { tf levels }
117    private method ParseMarkersOption { tf markers }
[1535]118    private method volume { tag name }
119    private method GetVolumeInfo { w }
[3032]120    private method SetOrientation {}
[1376]121
[1435]122    private variable _outbuf       ;# buffer for outgoing commands
[456]123
[1435]124    private variable _dlist ""     ;# list of data objects
125    private variable _allDataObjs
126    private variable _obj2ovride   ;# maps dataobj => style override
[1549]127    private variable _serverVols   ;# contains all the dataobj-component
[1694]128                                   ;# to volumes in the server
[1549]129    private variable _serverTfs    ;# contains all the transfer functions
[1694]130                                   ;# in the server.
[1549]131    private variable _recvdVols    ;# list of data objs to send to server
132    private variable _vol2style    ;# maps dataobj-component to transfunc
133    private variable _style2vols   ;# maps tf back to list of
[1694]134                                    # dataobj-components using the tf.
[447]135
[3330]136    private variable _reset 1;          # Connection to server has been reset
[1435]137    private variable _click        ;# info used for rotate operations
138    private variable _limits       ;# autoscale min/max for all axes
139    private variable _view         ;# view params for 3D view
140    private variable _isomarkers    ;# array of isosurface level values 0..1
141    private common   _settings
[1549]142    # Array of transfer functions in server.  If 0 the transfer has been
143    # defined but not loaded.  If 1 the transfer function has been named
144    # and loaded.
145    private variable _activeTfs
[1435]146    private variable _first ""     ;# This is the topmost volume.
147    private variable _buffering 0
148
[1254]149    # This
150    # indicates which isomarkers and transfer
151    # function to use when changing markers,
152    # opacity, or thickness.
[1435]153    common _downloadPopup          ;# download options from popup
154    private common _hardcopy
155    private variable _width 0
156    private variable _height 0
[1448]157    private variable _resizePending 0
158    private variable _resizeLegendPending 0
[436]159}
160
161itk::usual NanovisViewer {
162    keep -background -foreground -cursor -font
163    keep -plotbackground -plotforeground
164}
165
166# ----------------------------------------------------------------------
167# CONSTRUCTOR
168# ----------------------------------------------------------------------
[462]169itcl::body Rappture::NanovisViewer::constructor {hostlist args} {
[2671]170    set _serverType "nanovis"
[907]171
172    # Draw legend event
[456]173    $_dispatcher register !legend
[1376]174    $_dispatcher dispatch $this !legend "[itcl::code $this FixLegend]; list"
[1435]175
[907]176    # Send transfer functions event
[972]177    $_dispatcher register !send_transfunc
178    $_dispatcher dispatch $this !send_transfunc \
[1694]179        "[itcl::code $this SendTransferFuncs]; list"
[1435]180
[907]181    # Rebuild event
182    $_dispatcher register !rebuild
[1376]183    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
[456]184
[1435]185    # Resize event
[1377]186    $_dispatcher register !resize
187    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
188
[1435]189    set _outbuf ""
[447]190
[436]191    #
[907]192    # Populate parser with commands handle incoming requests
[436]193    #
[1376]194    $_parser alias image [itcl::code $this ReceiveImage]
195    $_parser alias legend [itcl::code $this ReceiveLegend]
196    $_parser alias data [itcl::code $this ReceiveData]
[3330]197    $_parser alias viserror [itcl::code $this ReceiveError]
[436]198
[919]199    # Initialize the view to some default parameters.
[1435]200    array set _view {
[1694]201        theta   45
202        phi     45
203        psi     0
204        zoom    1.0
[2744]205        pan-x   0
206        pan-y   0
[919]207    }
[1435]208    set _limits(vmin) 0.0
209    set _limits(vmax) 1.0
[3330]210    set _reset 1
[436]211
[1435]212    array set _settings [subst {
[2744]213        $this-pan-x             $_view(pan-x)
214        $this-pan-y             $_view(pan-y)
215        $this-phi               $_view(phi)
216        $this-psi               $_view(psi)
217        $this-theta             $_view(theta)
218        $this-volume            1
219        $this-xcutplane         0
220        $this-xcutposition      0
221        $this-ycutplane         0
222        $this-ycutposition      0
223        $this-zcutplane         0
224        $this-zcutposition      0
225        $this-zoom              $_view(zoom)
[1435]226    }]
[436]227
[1545]228    itk_component add 3dview {
[2584]229        label $itk_component(plotarea).view -image $_image(plot) \
[1694]230            -highlightthickness 0 -borderwidth 0
[1545]231    } {
[1694]232        usual
233        ignore -highlightthickness -borderwidth  -background
[1545]234    }
[3330]235    bind $itk_component(3dview) <Control-F1> [itcl::code $this ToggleConsole]
[1545]236
[1376]237    set f [$itk_component(main) component controls]
[436]238    itk_component add reset {
[1376]239        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
[1391]240            -highlightthickness 0 \
[1376]241            -image [Rappture::icon reset-view] \
242            -command [itcl::code $this Zoom reset]
[1391]243    } {
244        usual
245        ignore -highlightthickness
[436]246    }
[1376]247    pack $itk_component(reset) -side top -padx 2 -pady 2
[436]248    Rappture::Tooltip::for $itk_component(reset) "Reset the view to the default zoom level"
249
250    itk_component add zoomin {
[1376]251        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
[1391]252            -highlightthickness 0 \
[1376]253            -image [Rappture::icon zoom-in] \
254            -command [itcl::code $this Zoom in]
[1391]255    } {
256        usual
257        ignore -highlightthickness
[436]258    }
[1376]259    pack $itk_component(zoomin) -side top -padx 2 -pady 2
[436]260    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
261
262    itk_component add zoomout {
[1376]263        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
[1391]264            -highlightthickness 0 \
[1376]265            -image [Rappture::icon zoom-out] \
266            -command [itcl::code $this Zoom out]
[1391]267    } {
268        usual
269        ignore -highlightthickness
[436]270    }
[1376]271    pack $itk_component(zoomout) -side top -padx 2 -pady 2
[436]272    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
273
[447]274    itk_component add volume {
[1435]275        Rappture::PushButton $f.volume \
[1694]276            -onimage [Rappture::icon volume-on] \
277            -offimage [Rappture::icon volume-off] \
278            -command [itcl::code $this FixSettings volume] \
279            -variable [itcl::scope _settings($this-volume)]
[447]280    }
[1435]281    $itk_component(volume) select
[447]282    Rappture::Tooltip::for $itk_component(volume) \
[1376]283        "Toggle the volume cloud on/off"
[1435]284    pack $itk_component(volume) -padx 2 -pady 2
[447]285
[1376]286    BuildViewTab
287    BuildVolumeTab
288    BuildCutplanesTab
289    BuildCameraTab
[447]290
[907]291    # Legend
[459]292
[456]293    set _image(legend) [image create photo]
294    itk_component add legend {
[1694]295        canvas $itk_component(plotarea).legend -height 50 -highlightthickness 0
[456]296    } {
[1694]297        usual
298        ignore -highlightthickness
299        rename -background -plotbackground plotBackground Background
[456]300    }
[1377]301    bind $itk_component(legend) <Configure> \
[1694]302        [itcl::code $this EventuallyResizeLegend]
[1377]303
[1376]304    # Hack around the Tk panewindow.  The problem is that the requested
305    # size of the 3d view isn't set until an image is retrieved from
306    # the server.  So the panewindow uses the tiny size.
[1435]307    set w 10000
[1376]308    pack forget $itk_component(3dview)
309    blt::table $itk_component(plotarea) \
[1694]310        0,0 $itk_component(3dview) -fill both -reqwidth $w \
311        1,0 $itk_component(legend) -fill x
[1376]312    blt::table configure $itk_component(plotarea) r1 -resize none
[456]313
[1228]314    # Bindings for rotation via mouse
[1215]315    bind $itk_component(3dview) <ButtonPress-1> \
[1694]316        [itcl::code $this Rotate click %x %y]
[459]317    bind $itk_component(3dview) <B1-Motion> \
[1694]318        [itcl::code $this Rotate drag %x %y]
[1215]319    bind $itk_component(3dview) <ButtonRelease-1> \
[1694]320        [itcl::code $this Rotate release %x %y]
[459]321    bind $itk_component(3dview) <Configure> \
[1694]322        [itcl::code $this EventuallyResize %w %h]
[436]323
[1228]324    # Bindings for panning via mouse
[1215]325    bind $itk_component(3dview) <ButtonPress-2> \
[1694]326        [itcl::code $this Pan click %x %y]
[1215]327    bind $itk_component(3dview) <B2-Motion> \
[1694]328        [itcl::code $this Pan drag %x %y]
[1215]329    bind $itk_component(3dview) <ButtonRelease-2> \
[1694]330        [itcl::code $this Pan release %x %y]
[1215]331
[1228]332    # Bindings for panning via keyboard
333    bind $itk_component(3dview) <KeyPress-Left> \
[1694]334        [itcl::code $this Pan set -10 0]
[1228]335    bind $itk_component(3dview) <KeyPress-Right> \
[1694]336        [itcl::code $this Pan set 10 0]
[1228]337    bind $itk_component(3dview) <KeyPress-Up> \
[1694]338        [itcl::code $this Pan set 0 -10]
[1228]339    bind $itk_component(3dview) <KeyPress-Down> \
[1694]340        [itcl::code $this Pan set 0 10]
[1228]341    bind $itk_component(3dview) <Shift-KeyPress-Left> \
[1694]342        [itcl::code $this Pan set -2 0]
[1228]343    bind $itk_component(3dview) <Shift-KeyPress-Right> \
[1694]344        [itcl::code $this Pan set 2 0]
[1228]345    bind $itk_component(3dview) <Shift-KeyPress-Up> \
[1694]346        [itcl::code $this Pan set 0 -2]
[1228]347    bind $itk_component(3dview) <Shift-KeyPress-Down> \
[1694]348        [itcl::code $this Pan set 0 2]
[1228]349
350    # Bindings for zoom via keyboard
351    bind $itk_component(3dview) <KeyPress-Prior> \
[1694]352        [itcl::code $this Zoom out]
[1228]353    bind $itk_component(3dview) <KeyPress-Next> \
[1694]354        [itcl::code $this Zoom in]
[1228]355
356    bind $itk_component(3dview) <Enter> "focus $itk_component(3dview)"
357
[1215]358    if {[string equal "x11" [tk windowingsystem]]} {
[1694]359        # Bindings for zoom via mouse
360        bind $itk_component(3dview) <4> [itcl::code $this Zoom out]
361        bind $itk_component(3dview) <5> [itcl::code $this Zoom in]
[1215]362    }
363
[459]364    set _image(download) [image create photo]
365
[436]366    eval itk_initialize $args
367
[907]368    Connect
[436]369}
370
371# ----------------------------------------------------------------------
372# DESTRUCTOR
373# ----------------------------------------------------------------------
374itcl::body Rappture::NanovisViewer::destructor {} {
[907]375    $_dispatcher cancel !rebuild
[972]376    $_dispatcher cancel !send_transfunc
[1448]377    $_dispatcher cancel !resize
[456]378    image delete $_image(plot)
379    image delete $_image(legend)
[459]380    image delete $_image(download)
[1435]381    array unset _settings $this-*
[436]382}
383
384# ----------------------------------------------------------------------
385# USAGE: add <dataobj> ?<settings>?
386#
387# Clients use this to add a data object to the plot.  The optional
388# <settings> are used to configure the plot.  Allowed settings are
389# -color, -brightness, -width, -linestyle, and -raise.
390# ----------------------------------------------------------------------
391itcl::body Rappture::NanovisViewer::add {dataobj {settings ""}} {
392    array set params {
[1694]393        -color auto
394        -width 1
395        -linestyle solid
396        -brightness 0
397        -raise 0
398        -description ""
399        -param ""
[436]400    }
401    foreach {opt val} $settings {
[1694]402        if {![info exists params($opt)]} {
403            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
404        }
405        set params($opt) $val
[436]406    }
407    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
[1694]408        # can't handle -autocolors yet
409        set params(-color) black
[436]410    }
[1435]411    set pos [lsearch -exact $dataobj $_dlist]
[436]412    if {$pos < 0} {
[1694]413        lappend _dlist $dataobj
414        set _allDataObjs($dataobj) 1
415        set _obj2ovride($dataobj-color) $params(-color)
416        set _obj2ovride($dataobj-width) $params(-width)
417        set _obj2ovride($dataobj-raise) $params(-raise)
418        $_dispatcher event -idle !rebuild
[436]419    }
420}
421
422# ----------------------------------------------------------------------
[572]423# USAGE: get ?-objects?
424# USAGE: get ?-image 3dview|legend?
[436]425#
426# Clients use this to query the list of objects being plotted, in
[572]427# order from bottom to top of this result.  The optional "-image"
428# flag can also request the internal images being shown.
[436]429# ----------------------------------------------------------------------
[572]430itcl::body Rappture::NanovisViewer::get {args} {
431    if {[llength $args] == 0} {
[1694]432        set args "-objects"
[572]433    }
434
435    set op [lindex $args 0]
436    switch -- $op {
437      -objects {
[1694]438        # put the dataobj list in order according to -raise options
439        set dlist $_dlist
440        foreach obj $dlist {
441            if {[info exists _obj2ovride($obj-raise)] && $_obj2ovride($obj-raise)} {
442                set i [lsearch -exact $dlist $obj]
443                if {$i >= 0} {
444                    set dlist [lreplace $dlist $i $i]
445                    lappend dlist $obj
446                }
447            }
448        }
449        return $dlist
[572]450      }
451      -image {
[1694]452        if {[llength $args] != 2} {
453            error "wrong # args: should be \"get -image 3dview|legend\""
454        }
455        switch -- [lindex $args end] {
456            3dview {
457                return $_image(plot)
458            }
459            legend {
460                return $_image(legend)
461            }
462            default {
463                error "bad image name \"[lindex $args end]\": should be 3dview or legend"
464            }
465        }
[572]466      }
467      default {
[1694]468        error "bad option \"$op\": should be -objects or -image"
[572]469      }
[436]470    }
471}
472
473# ----------------------------------------------------------------------
474# USAGE: delete ?<dataobj1> <dataobj2> ...?
475#
[1254]476#       Clients use this to delete a dataobj from the plot.  If no dataobjs
477#       are specified, then all dataobjs are deleted.  No data objects are
478#       deleted.  They are only removed from the display list.
[1141]479#
[436]480# ----------------------------------------------------------------------
481itcl::body Rappture::NanovisViewer::delete {args} {
482    if {[llength $args] == 0} {
[1694]483        set args $_dlist
[436]484    }
[1141]485    # Delete all specified dataobjs
[436]486    set changed 0
487    foreach dataobj $args {
[1694]488        set pos [lsearch -exact $_dlist $dataobj]
489        if { $pos >= 0 } {
490            set _dlist [lreplace $_dlist $pos $pos]
491            array unset _limits $dataobj*
492            array unset _obj2ovride $dataobj-*
493            array unset _vol2style $dataobj-*
494            set changed 1
495        }
[436]496    }
[1141]497    # If anything changed, then rebuild the plot
[436]498    if {$changed} {
[1694]499        $_dispatcher event -idle !rebuild
[436]500    }
501}
502
503# ----------------------------------------------------------------------
504# USAGE: scale ?<data1> <data2> ...?
505#
506# Sets the default limits for the overall plot according to the
507# limits of the data for all of the given <data> objects.  This
508# accounts for all objects--even those not showing on the screen.
509# Because of this, the limits are appropriate for all objects as
510# the user scans through data in the ResultSet viewer.
511# ----------------------------------------------------------------------
512itcl::body Rappture::NanovisViewer::scale {args} {
513    foreach val {xmin xmax ymin ymax zmin zmax vmin vmax} {
[1694]514        set _limits($val) ""
[436]515    }
516    foreach obj $args {
[1694]517        foreach axis {x y z v} {
[1013]518
[1694]519            foreach { min max } [$obj limits $axis] break
[1013]520
[1694]521            if {"" != $min && "" != $max} {
522                if {"" == $_limits(${axis}min)} {
523                    set _limits(${axis}min) $min
524                    set _limits(${axis}max) $max
525                } else {
526                    if {$min < $_limits(${axis}min)} {
527                        set _limits(${axis}min) $min
528                    }
529                    if {$max > $_limits(${axis}max)} {
530                        set _limits(${axis}max) $max
531                    }
532                }
533            }
534        }
[436]535    }
536}
537
538# ----------------------------------------------------------------------
539# USAGE: download coming
[464]540# USAGE: download controls <downloadCommand>
[436]541# USAGE: download now
542#
543# Clients use this method to create a downloadable representation
544# of the plot.  Returns a list of the form {ext string}, where
545# "ext" is the file extension (indicating the type of data) and
546# "string" is the data itself.
547# ----------------------------------------------------------------------
[464]548itcl::body Rappture::NanovisViewer::download {option args} {
[436]549    switch $option {
[1694]550        coming {
551            if {[catch {
552                blt::winop snap $itk_component(plotarea) $_image(download)
553            }]} {
554                $_image(download) configure -width 1 -height 1
555                $_image(download) put #000000
556            }
557        }
558        controls {
559            # no controls for this download yet
560            return ""
561        }
562        now {
563            # Get the image data (as base64) and decode it back to binary.
564            # This is better than writing to temporary files.  When we switch
565            # to the BLT picture image it won't be necessary to decode the
566            # image data.
567            if { [image width $_image(plot)] > 0 &&
568                 [image height $_image(plot)] > 0 } {
569                set bytes [$_image(plot) data -format "jpeg -quality 100"]
570                set bytes [Rappture::encoding::decode -as b64 $bytes]
571                return [list .jpg $bytes]
572            }
573            return ""
574        }
575        default {
576            error "bad option \"$option\": should be coming, controls, now"
577        }
[436]578    }
579}
580
581# ----------------------------------------------------------------------
[907]582# USAGE: Connect ?<host:port>,<host:port>...?
[436]583#
584# Clients use this method to establish a connection to a new
585# server, or to reestablish a connection to the previous server.
586# Any existing connection is automatically closed.
587# ----------------------------------------------------------------------
[907]588itcl::body Rappture::NanovisViewer::Connect {} {
589    set _hosts [GetServerList "nanovis"]
590    if { "" == $_hosts } {
[1694]591        return 0
[436]592    }
[3330]593    set _reset 1
[907]594    set result [VisViewer::Connect $_hosts]
[1141]595    if { $result } {
[1694]596        set w [winfo width $itk_component(3dview)]
597        set h [winfo height $itk_component(3dview)]
598        EventuallyResize $w $h
[1141]599    }
[907]600    return $result
601}
[436]602
[907]603#
[919]604# isconnected --
605#
[921]606#       Indicates if we are currently connected to the visualization server.
[919]607#
[907]608itcl::body Rappture::NanovisViewer::isconnected {} {
609    return [VisViewer::IsConnected]
[436]610}
611
612#
[1514]613# disconnect --
614#
615itcl::body Rappture::NanovisViewer::disconnect {} {
616    Disconnect
617}
618
619#
[919]620# Disconnect --
621#
[921]622#       Clients use this method to disconnect from the current rendering
623#       server.
[919]624#
[907]625itcl::body Rappture::NanovisViewer::Disconnect {} {
626    VisViewer::Disconnect
[447]627
[459]628    # disconnected -- no more data sitting on server
[1435]629    set _outbuf ""
[1549]630    array unset _serverVols
[436]631}
632
633#
[1514]634# sendto --
635#
636itcl::body Rappture::NanovisViewer::sendto { bytes } {
637    SendBytes "$bytes\n"
638}
639
640#
[1376]641# SendCmd
[919]642#
[921]643#       Send commands off to the rendering server.  If we're currently
644#       sending data objects to the server, buffer the commands to be
645#       sent later.
[919]646#
[1376]647itcl::body Rappture::NanovisViewer::SendCmd {string} {
[1435]648    if { $_buffering } {
[1694]649        append _outbuf $string "\n"
[919]650    } else {
[1694]651        foreach line [split $string \n] {
652            SendEcho >>line $line
653        }
654        SendBytes "$string\n"
[436]655    }
656}
657
[1435]658
[878]659# ----------------------------------------------------------------------
[1376]660# USAGE: SendTransferFuncs
[878]661# ----------------------------------------------------------------------
[1376]662itcl::body Rappture::NanovisViewer::SendTransferFuncs {} {
[1435]663    if { $_first == "" } {
[1694]664        puts stderr "first not set"
665        return
[1435]666    }
[3059]667    # Ensure that the global opacity and thickness settings (in the slider
[1549]668    # settings widgets) are used for the active transfer-function.  Update
669    # the values in the _settings varible.
670    set opacity [expr { double($_settings($this-opacity)) * 0.01 }]
[1013]671    # Scale values between 0.00001 and 0.01000
[1549]672    set thickness [expr {double($_settings($this-thickness)) * 0.0001}]
[1013]673
[1549]674    foreach vol [CurrentVolumes] {
[1694]675        if { ![info exists _serverVols($vol)] || !$_serverVols($vol) } {
676            # The volume hasn't reached the server yet.  How did we get
677            # here?
678            continue
679        }
680        if { ![info exists _vol2style($vol)] } {
681            puts stderr "unknown volume $vol"
682            continue;                        # How does this happen?
683        }
684        set tf $_vol2style($vol)
685        set _settings($this-$tf-opacity) $opacity
686        set _settings($this-$tf-thickness) $thickness
687        ComputeTransferFunc $tf
688        # FIXME: Need to the send information as to what transfer functions
[2744]689        #        to update so that we only update the transfer function
690        #        as necessary.  Right now, all transfer functions are
691        #        updated. This makes moving the isomarker slider chunky.
[1694]692        if { ![info exists _activeTfs($tf)] || !$_activeTfs($tf) } {
693            set _activeTfs($tf) 1
694        }
695        SendCmd "volume shading transfunc $tf $vol"
[1435]696    }
697    FixLegend
[447]698}
699
700# ----------------------------------------------------------------------
[1376]701# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
[436]702#
703# Invoked automatically whenever the "image" command comes in from
704# the rendering server.  Indicates that binary image data with the
705# specified <size> will follow.
706# ----------------------------------------------------------------------
[1376]707itcl::body Rappture::NanovisViewer::ReceiveImage { args } {
[1349]708    array set info {
[1694]709        -token "???"
710        -bytes 0
711        -type image
[1349]712    }
713    array set info $args
714    set bytes [ReceiveBytes $info(-bytes)]
[1435]715    ReceiveEcho <<line "<read $info(-bytes) bytes"
[1349]716    if { $info(-type) == "image" } {
[2744]717        ReceiveEcho "for [image width $_image(plot)]x[image height $_image(plot)] image>"       
[1694]718        $_image(plot) configure -data $bytes
[1349]719    } elseif { $info(type) == "print" } {
[1694]720        set tag $this-print-$info(-token)
721        set _hardcopy($tag) $bytes
[436]722    }
723}
724
[456]725#
[1376]726# ReceiveLegend --
[1013]727#
[1254]728#       The procedure is the response from the render server to each "legend"
729#       command.  The server sends back a "legend" command invoked our
730#       the slave interpreter.  The purpose is to collect data of the image
731#       representing the legend in the canvas.  In addition, the isomarkers
732#       of the active transfer function are displayed.
[1013]733#
[1254]734#       I don't know is this is the right place to display the isomarkers.
735#       I don't know all the different paths used to draw the plot. There's
[1376]736#       "Rebuild", "add", etc.
[1013]737#
[1376]738itcl::body Rappture::NanovisViewer::ReceiveLegend { tf vmin vmax size } {
[1013]739    if { ![isconnected] } {
[1694]740        return
[1013]741    }
742    set bytes [ReceiveBytes $size]
743    $_image(legend) configure -data $bytes
744    ReceiveEcho <<line "<read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
[1254]745
[1013]746    set c $itk_component(legend)
747    set w [winfo width $c]
748    set h [winfo height $c]
749    set lx 10
750    set ly [expr {$h - 1}]
751    if {"" == [$c find withtag transfunc]} {
[1694]752        $c create image 10 10 -anchor nw \
753            -image $_image(legend) -tags transfunc
754        $c create text $lx $ly -anchor sw \
755            -fill $itk_option(-plotforeground) -tags "limits vmin"
756        $c create text [expr {$w-$lx}] $ly -anchor se \
757            -fill $itk_option(-plotforeground) -tags "limits vmax"
758        $c lower transfunc
759        $c bind transfunc <ButtonRelease-1> \
760            [itcl::code $this AddIsoMarker %x %y]
[1013]761    }
[1248]762    # Display the markers used by the active transfer function.
[1247]763
[1376]764    array set limits [limits $tf]
[1013]765    $c itemconfigure vmin -text [format %.2g $limits(min)]
766    $c coords vmin $lx $ly
[1254]767
[1013]768    $c itemconfigure vmax -text [format %.2g $limits(max)]
769    $c coords vmax [expr {$w-$lx}] $ly
[456]770
[1435]771    if { [info exists _isomarkers($tf)] } {
[1694]772        foreach m $_isomarkers($tf) {
773            $m visible yes
774        }
[1013]775    }
[2733]776
777    # The colormap may have changed. Resync the slicers with the colormap.
778    set vols [CurrentVolumes -cutplanes]
779    SendCmd "volume data state $_settings($this-volume) $vols"
780
781    # Adjust the cutplane for only the first component in the topmost volume
782    # (i.e. the first volume designated in the field).
783    set vol [lindex $vols 0]
784    foreach axis {x y z} {
[2744]785        # Turn off cutplanes for all volumes
786        SendCmd "cutplane state 0 $axis"
787        if { $_settings($this-${axis}cutplane) } {
788            # Turn on cutplane for this particular volume and set the position
789            SendCmd "cutplane state 1 $axis $vol"
790            set pos [expr {0.01*$_settings($this-${axis}cutposition)}]
791            SendCmd "cutplane position $pos $axis $vol"
792        }
[2733]793    }
[1013]794}
[459]795
[1013]796#
[1376]797# ReceiveData --
[1013]798#
[1254]799#       The procedure is the response from the render server to each "data
800#       follows" command.  The server sends back a "data" command invoked our
801#       the slave interpreter.  The purpose is to collect the min/max of the
802#       volume sent to the render server.  Since the client (nanovisviewer)
803#       doesn't parse 3D data formats, we rely on the server (nanovis) to
804#       tell us what the limits are.  Once we've received the limits to all
[1549]805#       the data we've sent (tracked by _recvdVols) we can then determine
[1254]806#       what the transfer functions are for these volumes.
[1013]807#
808#
[1254]809#       Note: There is a considerable tradeoff in having the server report
810#             back what the data limits are.  It means that much of the code
811#             having to do with transfer-functions has to wait for the data
812#             to come back, since the isomarkers are calculated based upon
813#             the data limits.  The client code is much messier because of
814#             this.  The alternative is to parse any of the 3D formats on the
815#             client side.
[1013]816#
[1376]817itcl::body Rappture::NanovisViewer::ReceiveData { args } {
[1013]818    if { ![isconnected] } {
[1694]819        return
[1013]820    }
821    # Arguments from server are name value pairs. Stuff them in an array.
822    array set info $args
[968]823
[1435]824    set tag $info(tag)
825    set parts [split $tag -]
[1013]826
[1435]827    #
828    # Volumes don't exist until we're told about them.
829    #
830    set dataobj [lindex $parts 0]
[1549]831    set _serverVols($tag) 1
[1435]832    if { $_settings($this-volume) && $dataobj == $_first } {
[1694]833        SendCmd "volume state 1 $tag"
[1435]834    }
[1514]835    set _limits($tag-min) $info(min);  # Minimum value of the volume.
836    set _limits($tag-max) $info(max);  # Maximum value of the volume.
[1435]837    set _limits(vmin)      $info(vmin); # Overall minimum value.
838    set _limits(vmax)      $info(vmax); # Overall maximum value.
[1013]839
[1549]840    unset _recvdVols($tag)
841    if { [array size _recvdVols] == 0 } {
[1694]842        # The active transfer function is by default the first component of
843        # the first data object.  This assumes that the data is always
844        # successfully transferred.
845        updatetransferfuncs
[456]846    }
847}
848
849# ----------------------------------------------------------------------
[1376]850# USAGE: Rebuild
[436]851#
852# Called automatically whenever something changes that affects the
853# data in the widget.  Clears any existing data and rebuilds the
854# widget to display new data.
855# ----------------------------------------------------------------------
[1376]856itcl::body Rappture::NanovisViewer::Rebuild {} {
[1435]857
[1549]858    # Turn on buffering of commands to the server.  We don't want to
859    # be preempted by a server disconnect/reconnect (which automatically
860    # generates a new call to Rebuild).   
861    set _buffering 1
862
[1013]863    # Hide all the isomarkers. Can't remove them. Have to remember the
864    # settings since the user may have created/deleted/moved markers.
865
[1435]866    foreach tf [array names _isomarkers] {
[1694]867        foreach m $_isomarkers($tf) {
868            $m visible no
869        }
[1013]870    }
[447]871
[1448]872    set w [winfo width $itk_component(3dview)]
873    set h [winfo height $itk_component(3dview)]
874    EventuallyResize $w $h
875
[447]876    foreach dataobj [get] {
[1694]877        foreach comp [$dataobj components] {
878            set vol $dataobj-$comp
879            if { ![info exists _serverVols($vol)] } {
880                # Send the data as one huge base64-encoded mess -- yuck!
881                set data [$dataobj values $comp]
882                set nbytes [string length $data]
883                append _outbuf "volume data follows $nbytes $vol\n"
884                append _outbuf $data
885                set _recvdVols($vol) 1
886                set _serverVols($vol) 0
887            }
888            NameTransferFunc $dataobj $comp
889        }
[447]890    }
[3330]891    if { $_reset } {
892        #
893        # Reset the camera and other view parameters
894        #
895       
896        set _settings($this-theta) $_view(theta)
897        set _settings($this-phi)   $_view(phi)
898        set _settings($this-psi)   $_view(psi)
899        set _settings($this-pan-x) $_view(pan-x)
900        set _settings($this-pan-y) $_view(pan-y)
901        set _settings($this-zoom)  $_view(zoom)
902       
903        set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
904        SendCmd "camera angle $xyz"
905        PanCamera
906        SendCmd "camera zoom $_view(zoom)"
907        FixSettings light
908        FixSettings transp
909        FixSettings isosurface
910        FixSettings grid
911        FixSettings axes
912        FixSettings outline
913       
914        # nothing to send -- activate the proper ivol
915        SendCmd "volume state 0"
916        set _first [lindex [get] 0]
917        if {"" != $_first} {
918            set axis [$_first hints updir]
919            if { "" != $axis } {
920                SendCmd "up $axis"
921            }
922            set location [$_first hints camera]
923            if { $location != "" } {
924                array set _view $location
925            }
926            set vols [array names _serverVols $_first-*]
927            if { $vols != "" } {
928                SendCmd "volume state 1 $vols"
929            }
930            if 1 {
931                # Tell the server the name of the tool, the version, and dataset
932                # that we are rendering.  Have to do it here because we don't know
933                # what data objects are using the renderer until be get here.
934                global env
[1141]935
[3330]936                lappend out "hub" [exec hostname]
937                lappend out "viewer" "nanovisviewer"
938                if { [info exists env(USER)] } {
939                    lappend out "user" $env(USER)
940                }
941                if { [info exists env(SESSION)] } {
942                    lappend out "session" $env(SESSION)
943                }
944                lappend out "tool_id"      [$_first hints toolId]
945                lappend out "tool_name"    [$_first hints toolName]
946                lappend out "tool_version" [$_first hints toolRevision]
947                lappend out "tool_title"   [$_first hints toolTitle]
948                lappend out "tool_dataset" [$_first hints label]
949                SendCmd "clientinfo [list $out]"
950            }
[1141]951
[3330]952            foreach axis {x y z} {
953                # Turn off cutplanes for all volumes
954                SendCmd "cutplane state 0 $axis"
955            }
956           
957            # If the first volume already exists on the server, then make sure
958            # we display the proper transfer function in the legend.
959            set comp [lindex [$_first components] 0]
960            if { [info exists _serverVols($_first-$comp)] } {
961                updatetransferfuncs
962            }
963        }
[1013]964    }
[1694]965    set _buffering 0;                        # Turn off buffering.
[1435]966    # Actually write the commands to the server socket.  If it fails, we don't
967    # care.  We're finished here.
[1549]968    blt::busy hold $itk_component(hull)
[1694]969    SendBytes $_outbuf;                       
[1549]970    blt::busy release $itk_component(hull)
[3330]971    set _reset 0
[1694]972    set _outbuf "";                        # Clear the buffer.               
[436]973}
974
975# ----------------------------------------------------------------------
[1514]976# USAGE: CurrentVolumes ?-cutplanes?
[447]977#
978# Returns a list of volume server IDs for the current volume being
979# displayed.  This is normally a single ID, but it might be a list
980# of IDs if the current data object has multiple components.
981# ----------------------------------------------------------------------
[1514]982itcl::body Rappture::NanovisViewer::CurrentVolumes {{what -all}} {
[447]983    set rlist ""
[1435]984    if { $_first == "" } {
[1694]985        return
[1435]986    }
[1549]987    foreach comp [$_first components] {
[1694]988        set vol $_first-$comp
989        if { [info exists _serverVols($vol)] && $_serverVols($vol) } {
990            array set style {
991                -cutplanes 1
992            }
993            array set style [lindex [$_first components -style $comp] 0]
[2353]994            if { $what != "-cutplanes" || $style(-cutplanes) } {
[1694]995                lappend rlist $vol
996            }
997        }
[447]998    }
999    return $rlist
1000}
1001
1002# ----------------------------------------------------------------------
[1376]1003# USAGE: Zoom in
1004# USAGE: Zoom out
1005# USAGE: Zoom reset
[436]1006#
1007# Called automatically when the user clicks on one of the zoom
1008# controls for this widget.  Changes the zoom for the current view.
1009# ----------------------------------------------------------------------
[1376]1010itcl::body Rappture::NanovisViewer::Zoom {option} {
[436]1011    switch -- $option {
[1694]1012        "in" {
1013            set _view(zoom) [expr {$_view(zoom)*1.25}]
1014            set _settings($this-zoom) $_view(zoom)
1015        }
1016        "out" {
1017            set _view(zoom) [expr {$_view(zoom)*0.8}]
1018            set _settings($this-zoom) $_view(zoom)
1019        }
[1376]1020        "reset" {
[1694]1021            array set _view {
1022                theta   45
1023                phi     45
1024                psi     0
[2744]1025                zoom    1.0
1026                pan-x   0
1027                pan-y   0
[1694]1028            }
1029            if { $_first != "" } {
1030                set location [$_first hints camera]
1031                if { $location != "" } {
1032                    array set _view $location
1033                }
1034            }
[1435]1035            set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
[1376]1036            SendCmd "camera angle $xyz"
[1694]1037            PanCamera
1038            set _settings($this-theta) $_view(theta)
1039            set _settings($this-phi)   $_view(phi)
1040            set _settings($this-psi)   $_view(psi)
1041            set _settings($this-pan-x) $_view(pan-x)
1042            set _settings($this-pan-y) $_view(pan-y)
1043            set _settings($this-zoom)  $_view(zoom)
[1376]1044        }
[436]1045    }
[1435]1046    SendCmd "camera zoom $_view(zoom)"
[436]1047}
1048
[1376]1049itcl::body Rappture::NanovisViewer::PanCamera {} {
[1435]1050    #set x [expr ($_view(pan-x)) / $_limits(xrange)]
1051    #set y [expr ($_view(pan-y)) / $_limits(yrange)]
1052    set x $_view(pan-x)
1053    set y $_view(pan-y)
[1376]1054    SendCmd "camera pan $x $y"
1055}
1056
1057
[436]1058# ----------------------------------------------------------------------
[1376]1059# USAGE: Rotate click <x> <y>
1060# USAGE: Rotate drag <x> <y>
1061# USAGE: Rotate release <x> <y>
[436]1062#
1063# Called automatically when the user clicks/drags/releases in the
1064# plot area.  Moves the plot according to the user's actions.
1065# ----------------------------------------------------------------------
[1376]1066itcl::body Rappture::NanovisViewer::Rotate {option x y} {
[436]1067    switch -- $option {
[1376]1068        click {
1069            $itk_component(3dview) configure -cursor fleur
[1435]1070            set _click(x) $x
1071            set _click(y) $y
1072            set _click(theta) $_view(theta)
1073            set _click(phi) $_view(phi)
[1376]1074        }
1075        drag {
[1435]1076            if {[array size _click] == 0} {
[1376]1077                Rotate click $x $y
1078            } else {
1079                set w [winfo width $itk_component(3dview)]
1080                set h [winfo height $itk_component(3dview)]
1081                if {$w <= 0 || $h <= 0} {
1082                    return
1083                }
[436]1084
[1376]1085                if {[catch {
1086                    # this fails sometimes for no apparent reason
[1435]1087                    set dx [expr {double($x-$_click(x))/$w}]
1088                    set dy [expr {double($y-$_click(y))/$h}]
[1376]1089                }]} {
1090                    return
1091                }
[436]1092
[1376]1093                #
1094                # Rotate the camera in 3D
1095                #
[1435]1096                if {$_view(psi) > 90 || $_view(psi) < -90} {
[1376]1097                    # when psi is flipped around, theta moves backwards
1098                    set dy [expr {-$dy}]
1099                }
[1435]1100                set theta [expr {$_view(theta) - $dy*180}]
[1376]1101                while {$theta < 0} { set theta [expr {$theta+180}] }
1102                while {$theta > 180} { set theta [expr {$theta-180}] }
[436]1103
[1376]1104                if {abs($theta) >= 30 && abs($theta) <= 160} {
[1435]1105                    set phi [expr {$_view(phi) - $dx*360}]
[1376]1106                    while {$phi < 0} { set phi [expr {$phi+360}] }
1107                    while {$phi > 360} { set phi [expr {$phi-360}] }
[1435]1108                    set psi $_view(psi)
[1376]1109                } else {
[1435]1110                    set phi $_view(phi)
1111                    set psi [expr {$_view(psi) - $dx*360}]
[1376]1112                    while {$psi < -180} { set psi [expr {$psi+360}] }
1113                    while {$psi > 180} { set psi [expr {$psi-360}] }
1114                }
[436]1115
[1694]1116                set _view(theta)        $theta
1117                set _view(phi)          $phi
1118                set _view(psi)          $psi
[1376]1119                set xyz [Euler2XYZ $theta $phi $psi]
[1694]1120                set _settings($this-theta) $_view(theta)
1121                set _settings($this-phi)   $_view(phi)
1122                set _settings($this-psi)   $_view(psi)
[3059]1123                SendCmd "camera angle $xyz"
[1435]1124                set _click(x) $x
1125                set _click(y) $y
[1376]1126            }
1127        }
1128        release {
1129            Rotate drag $x $y
1130            $itk_component(3dview) configure -cursor ""
[1435]1131            catch {unset _click}
[1376]1132        }
1133        default {
1134            error "bad option \"$option\": should be click, drag, release"
1135        }
[436]1136    }
1137}
1138
1139# ----------------------------------------------------------------------
[1376]1140# USAGE: $this Pan click x y
1141#        $this Pan drag x y
1142#        $this Pan release x y
[1215]1143#
1144# Called automatically when the user clicks on one of the zoom
1145# controls for this widget.  Changes the zoom for the current view.
1146# ----------------------------------------------------------------------
[1376]1147itcl::body Rappture::NanovisViewer::Pan {option x y} {
[1220]1148    # Experimental stuff
[1242]1149    set w [winfo width $itk_component(3dview)]
1150    set h [winfo height $itk_component(3dview)]
[1228]1151    if { $option == "set" } {
[1376]1152        set x [expr $x / double($w)]
1153        set y [expr $y / double($h)]
[1435]1154        set _view(pan-x) [expr $_view(pan-x) + $x]
1155        set _view(pan-y) [expr $_view(pan-y) + $y]
[1376]1156        PanCamera
[1694]1157        set _settings($this-pan-x) $_view(pan-x)
1158        set _settings($this-pan-y) $_view(pan-y)
[1376]1159        return
[1228]1160    }
[1254]1161    if { $option == "click" } {
[1435]1162        set _click(x) $x
1163        set _click(y) $y
[1376]1164        $itk_component(3dview) configure -cursor hand1
[1215]1165    }
1166    if { $option == "drag" || $option == "release" } {
[1435]1167        set dx [expr ($_click(x) - $x)/double($w)]
1168        set dy [expr ($_click(y) - $y)/double($h)]
1169        set _click(x) $x
1170        set _click(y) $y
1171        set _view(pan-x) [expr $_view(pan-x) - $dx]
1172        set _view(pan-y) [expr $_view(pan-y) - $dy]
[1376]1173        PanCamera
[1694]1174        set _settings($this-pan-x) $_view(pan-x)
1175        set _settings($this-pan-y) $_view(pan-y)
[1215]1176    }
1177    if { $option == "release" } {
[1376]1178        $itk_component(3dview) configure -cursor ""
[1215]1179    }
1180}
1181
1182# ----------------------------------------------------------------------
[1376]1183# USAGE: FixSettings <what> ?<value>?
[447]1184#
1185# Used internally to update rendering settings whenever parameters
1186# change in the popup settings panel.  Sends the new settings off
1187# to the back end.
1188# ----------------------------------------------------------------------
[1376]1189itcl::body Rappture::NanovisViewer::FixSettings {what {value ""}} {
[447]1190    switch -- $what {
[1376]1191        light {
1192            if {[isconnected]} {
[1435]1193                set val $_settings($this-light)
[2876]1194                set sval [expr {0.01*$val}]
[1376]1195                SendCmd "volume shading diffuse $sval"
1196                set sval [expr {sqrt($val+1.0)}]
1197                SendCmd "volume shading specular $sval"
1198            }
1199        }
1200        transp {
1201            if {[isconnected]} {
[1435]1202                set val $_settings($this-transp)
[1376]1203                set sval [expr {0.2*$val+1}]
1204                SendCmd "volume shading opacity $sval"
1205            }
1206        }
1207        opacity {
[1549]1208            if {[isconnected] && [array size _activeTfs] > 0 } {
[1435]1209                set val $_settings($this-opacity)
[1376]1210                set sval [expr { 0.01 * double($val) }]
[1694]1211                foreach tf [array names _activeTfs] {
1212                    set _settings($this-$tf-opacity) $sval
1213                    set _activeTfs($tf) 0
1214                }
[1376]1215                updatetransferfuncs
1216            }
1217        }
[447]1218
[1376]1219        thickness {
[1549]1220            if {[isconnected] && [array names _activeTfs] > 0 } {
[1435]1221                set val $_settings($this-thickness)
[1376]1222                # Scale values between 0.00001 and 0.01000
1223                set sval [expr {0.0001*double($val)}]
[1694]1224                foreach tf [array names _activeTfs] {
1225                    set _settings($this-$tf-thickness) $sval
1226                    set _activeTfs($tf) 0
1227                }
[1376]1228                updatetransferfuncs
1229            }
1230        }
1231        "outline" {
1232            if {[isconnected]} {
[1435]1233                SendCmd "volume outline state $_settings($this-outline)"
[1376]1234            }
1235        }
1236        "isosurface" {
1237            if {[isconnected]} {
[1435]1238                SendCmd "volume shading isosurface $_settings($this-isosurface)"
[1376]1239            }
1240        }
1241        "grid" {
1242            if { [isconnected] } {
[1435]1243                SendCmd "grid visible $_settings($this-grid)"
[1376]1244            }
1245        }
1246        "axes" {
1247            if { [isconnected] } {
[1435]1248                SendCmd "axis visible $_settings($this-axes)"
[1376]1249            }
1250        }
[1694]1251        "legend" {
1252            if { $_settings($this-legend) } {
1253                blt::table $itk_component(plotarea) \
1254                    0,0 $itk_component(3dview) -fill both \
1255                    1,0 $itk_component(legend) -fill x
1256                blt::table configure $itk_component(plotarea) r1 -resize none
1257            } else {
1258                blt::table forget $itk_component(legend)
1259            }
1260        }
[1376]1261        "volume" {
[1435]1262            if { [isconnected] } {
[1694]1263                set vols [CurrentVolumes -cutplanes]
[1435]1264                SendCmd "volume data state $_settings($this-volume) $vols"
[1694]1265            }
[1376]1266        }
[1435]1267        "xcutplane" - "ycutplane" - "zcutplane" {
[1694]1268            set axis [string range $what 0 0]
1269            set bool $_settings($this-$what)
[1435]1270            if { [isconnected] } {
[1694]1271                set vols [CurrentVolumes -cutplanes]
[2744]1272                set vol [lindex $vols 0]
[2390]1273                SendCmd "cutplane state $bool $axis $vol"
[1694]1274            }
1275            if { $bool } {
1276                $itk_component(${axis}CutScale) configure -state normal \
1277                    -troughcolor white
[1435]1278            } else {
[1694]1279                $itk_component(${axis}CutScale) configure -state disabled \
1280                    -troughcolor grey82
[1435]1281            }
1282        }
[1376]1283        default {
1284            error "don't know how to fix $what"
1285        }
[447]1286    }
1287}
1288
1289# ----------------------------------------------------------------------
[1376]1290# USAGE: FixLegend
[456]1291#
1292# Used internally to update the legend area whenever it changes size
1293# or when the field changes.  Asks the server to send a new legend
1294# for the current field.
1295# ----------------------------------------------------------------------
[1376]1296itcl::body Rappture::NanovisViewer::FixLegend {} {
[1448]1297    set _resizeLegendPending 0
[459]1298    set lineht [font metrics $itk_option(-font) -linespace]
[1435]1299    set w [expr {$_width-20}]
[459]1300    set h [expr {[winfo height $itk_component(legend)]-20-$lineht}]
[1549]1301    if {$w > 0 && $h > 0 && [array names _activeTfs] > 0 && $_first != "" } {
[1694]1302        set vol [lindex [CurrentVolumes] 0]
1303        if { [info exists _vol2style($vol)] } {
1304            SendCmd "legend $_vol2style($vol) $w $h"
1305        }
[456]1306    } else {
[1694]1307        # Can't do this as this will remove the items associated with the
1308        # isomarkers.
1309       
1310        #$itk_component(legend) delete all
[456]1311    }
1312}
1313
[459]1314#
[1376]1315# NameTransferFunc --
[1013]1316#
[1254]1317#       Creates a transfer function name based on the <style> settings in the
1318#       library run.xml file. This placeholder will be used later to create
1319#       and send the actual transfer function once the data info has been sent
1320#       to us by the render server. [We won't know the volume limits until the
[1376]1321#       server parses the 3D data and sends back the limits via ReceiveData.]
[1013]1322#
[1254]1323#       FIXME: The current way we generate transfer-function names completely
1324#              ignores the -markers option.  The problem is that we are forced
1325#              to compute the name from an increasing complex set of values:
1326#              color, levels, marker, opacity.  I think we're stuck doing it
1327#              now.
[1013]1328#
[1435]1329itcl::body Rappture::NanovisViewer::NameTransferFunc { dataobj comp } {
[459]1330    array set style {
[1694]1331        -color rainbow
1332        -levels 6
1333        -opacity 1.0
[459]1334    }
1335    array set style [lindex [$dataobj components -style $comp] 0]
[1013]1336    set tf "$style(-color):$style(-levels):$style(-opacity)"
[1549]1337    set _vol2style($dataobj-$comp) $tf
1338    lappend _style2vols($tf) $dataobj-$comp
[1435]1339    return $tf
[1013]1340}
1341
1342#
[1376]1343# ComputeTransferFunc --
[1013]1344#
[1254]1345#   Computes and sends the transfer function to the render server.  It's
1346#   assumed that the volume data limits are known and that the global
1347#   transfer-functions slider values have be setup.  Both parts are
1348#   needed to compute the relative value (location) of the marker, and
1349#   the alpha map of the transfer function.
[1013]1350#
[1376]1351itcl::body Rappture::NanovisViewer::ComputeTransferFunc { tf } {
[1013]1352    array set style {
[1694]1353        -color rainbow
1354        -levels 6
1355        -opacity 1.0
[1013]1356    }
[2872]1357    foreach {dataobj comp} [split [lindex $_style2vols($tf) 0] -] break
[1013]1358    array set style [lindex [$dataobj components -style $comp] 0]
1359
1360
1361    # We have to parse the style attributes for a volume using this
1362    # transfer-function *once*.  This sets up the initial isomarkers for the
1363    # transfer function.  The user may add/delete markers, so we have to
1364    # maintain a list of markers for each transfer-function.  We use the one
1365    # of the volumes (the first in the list) using the transfer-function as a
[1254]1366    # reference.
[1013]1367    #
1368    # FIXME: The current way we generate transfer-function names completely
[1254]1369    #        ignores the -markers option.  The problem is that we are forced
[1013]1370    #        to compute the name from an increasing complex set of values:
1371    #        color, levels, marker, opacity.  I think the cow's out of the
[1254]1372    #        barn on this one.
[1013]1373
[1435]1374    if { ![info exists _isomarkers($tf)] } {
[1694]1375        # Have to defer creation of isomarkers until we have data limits
1376        if { [info exists style(-markers)] } {
1377            ParseMarkersOption $tf $style(-markers)
1378        } else {
1379            ParseLevelsOption $tf $style(-levels)
1380        }
[1254]1381    }
[459]1382    if {$style(-color) == "rainbow"} {
[1694]1383        set style(-color) "white:yellow:green:cyan:blue:magenta"
[459]1384    }
1385    set clist [split $style(-color) :]
[907]1386    set cmap "0.0 [Color2RGB white] "
[459]1387    for {set i 0} {$i < [llength $clist]} {incr i} {
[1694]1388        set x [expr {double($i+1)/([llength $clist]+1)}]
1389        set color [lindex $clist $i]
1390        append cmap "$x [Color2RGB $color] "
[459]1391    }
[907]1392    append cmap "1.0 [Color2RGB $color]"
[459]1393
[1013]1394    set tag $this-$tf
[1435]1395    if { ![info exists _settings($tag-opacity)] } {
[1694]1396        set _settings($tag-opacity) $style(-opacity)
[459]1397    }
[1435]1398    set max $_settings($tag-opacity)
[968]1399
[878]1400    set isovalues {}
[1435]1401    foreach m $_isomarkers($tf) {
[1694]1402        lappend isovalues [$m relval]
[878]1403    }
1404    # Sort the isovalues
1405    set isovalues [lsort -real $isovalues]
1406
[1435]1407    if { ![info exists _settings($tag-thickness)]} {
[1694]1408        set _settings($tag-thickness) 0.05
[885]1409    }
[1435]1410    set delta $_settings($tag-thickness)
[972]1411
[878]1412    set first [lindex $isovalues 0]
1413    set last [lindex $isovalues end]
1414    set wmap ""
1415    if { $first == "" || $first != 0.0 } {
[1694]1416        lappend wmap 0.0 0.0
[878]1417    }
1418    foreach x $isovalues {
[1694]1419        set x1 [expr {$x-$delta-0.00001}]
1420        set x2 [expr {$x-$delta}]
1421        set x3 [expr {$x+$delta}]
1422        set x4 [expr {$x+$delta+0.00001}]
1423        if { $x1 < 0.0 } {
1424            set x1 0.0
1425        } elseif { $x1 > 1.0 } {
1426            set x1 1.0
1427        }
1428        if { $x2 < 0.0 } {
1429            set x2 0.0
1430        } elseif { $x2 > 1.0 } {
1431            set x2 1.0
1432        }
1433        if { $x3 < 0.0 } {
1434            set x3 0.0
1435        } elseif { $x3 > 1.0 } {
1436            set x3 1.0
1437        }
1438        if { $x4 < 0.0 } {
1439            set x4 0.0
1440        } elseif { $x4 > 1.0 } {
1441            set x4 1.0
1442        }
1443        # add spikes in the middle
1444        lappend wmap $x1 0.0
1445        lappend wmap $x2 $max
1446        lappend wmap $x3 $max
1447        lappend wmap $x4 0.0
[878]1448    }
1449    if { $last == "" || $last != 1.0 } {
[1694]1450        lappend wmap 1.0 0.0
[878]1451    }
[1549]1452    SendCmd "transfunc define $tf { $cmap } { $wmap }"
[459]1453}
1454
1455# ----------------------------------------------------------------------
[436]1456# CONFIGURATION OPTION: -plotbackground
1457# ----------------------------------------------------------------------
1458itcl::configbody Rappture::NanovisViewer::plotbackground {
[907]1459    if { [isconnected] } {
[1694]1460        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1461        #fix this!
1462        #SendCmd "color background $r $g $b"
[907]1463    }
[436]1464}
1465
1466# ----------------------------------------------------------------------
1467# CONFIGURATION OPTION: -plotforeground
1468# ----------------------------------------------------------------------
1469itcl::configbody Rappture::NanovisViewer::plotforeground {
[907]1470    if { [isconnected] } {
[1694]1471        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1472        #fix this!
1473        #SendCmd "color background $r $g $b"
[907]1474    }
[436]1475}
1476
1477# ----------------------------------------------------------------------
1478# CONFIGURATION OPTION: -plotoutline
1479# ----------------------------------------------------------------------
1480itcl::configbody Rappture::NanovisViewer::plotoutline {
[907]1481    # Must check if we are connected because this routine is called from the
1482    # class body when the -plotoutline itk_option is defined.  At that point
1483    # the NanovisViewer class constructor hasn't been called, so we can't
1484    # start sending commands to visualization server.
1485    if { [isconnected] } {
[1694]1486        if {"" == $itk_option(-plotoutline)} {
1487            SendCmd "volume outline state off"
1488        } else {
1489            SendCmd "volume outline state on"
1490            SendCmd "volume outline color [Color2RGB $itk_option(-plotoutline)]"
1491        }
[436]1492    }
1493}
[878]1494
[965]1495#
1496# The -levels option takes a single value that represents the number
1497# of evenly distributed markers based on the current data range. Each
1498# marker is a relative value from 0.0 to 1.0.
1499#
[1435]1500itcl::body Rappture::NanovisViewer::ParseLevelsOption { tf levels } {
[965]1501    set c $itk_component(legend)
1502    regsub -all "," $levels " " levels
[972]1503    if {[string is int $levels]} {
[1694]1504        for {set i 1} { $i <= $levels } {incr i} {
1505            set x [expr {double($i)/($levels+1)}]
1506            set m [Rappture::IsoMarker \#auto $c $this $tf]
1507            $m relval $x
1508            lappend _isomarkers($tf) $m
1509        }
[972]1510    } else {
[1694]1511        foreach x $levels {
1512            set m [Rappture::IsoMarker \#auto $c $this $tf]
1513            $m relval $x
1514            lappend _isomarkers($tf) $m
1515        }
[878]1516    }
[965]1517}
1518
1519#
1520# The -markers option takes a list of zero or more values (the values
1521# may be separated either by spaces or commas) that have the following
1522# format:
1523#
[1254]1524#   N%  Percent of current total data range.  Converted to
1525#       to a relative value between 0.0 and 1.0.
1526#   N   Absolute value of marker.  If the marker is outside of
1527#       the current range, it will be displayed on the outer
1528#       edge of the legends, but it range it represents will
1529#       not be seen.
[965]1530#
[1435]1531itcl::body Rappture::NanovisViewer::ParseMarkersOption { tf markers } {
[878]1532    set c $itk_component(legend)
[965]1533    regsub -all "," $markers " " markers
1534    foreach marker $markers {
[1694]1535        set n [scan $marker "%g%s" value suffix]
1536        if { $n == 2 && $suffix == "%" } {
1537            # ${n}% : Set relative value.
1538            set value [expr {$value * 0.01}]
1539            set m [Rappture::IsoMarker \#auto $c $this $tf]
1540            $m relval $value
1541            lappend _isomarkers($tf) $m
1542        } else {
1543            # ${n} : Set absolute value.
1544            set m [Rappture::IsoMarker \#auto $c $this $tf]
1545            $m absval $value
1546            lappend _isomarkers($tf) $m
1547        }
[878]1548    }
1549}
1550
1551# ----------------------------------------------------------------------
[1435]1552# USAGE: UndateTransferFuncs
[878]1553# ----------------------------------------------------------------------
[1376]1554itcl::body Rappture::NanovisViewer::updatetransferfuncs {} {
[972]1555    $_dispatcher event -idle !send_transfunc
[878]1556}
1557
[1376]1558itcl::body Rappture::NanovisViewer::AddIsoMarker { x y } {
[1549]1559    if { $_first == "" } {
[1694]1560        error "active transfer function isn't set"
[975]1561    }
[1549]1562    set vol [lindex [CurrentVolumes] 0]
1563    set tf $_vol2style($vol)
[878]1564    set c $itk_component(legend)
[1435]1565    set m [Rappture::IsoMarker \#auto $c $this $tf]
[878]1566    set w [winfo width $c]
[1376]1567    $m relval [expr {double($x-10)/($w-20)}]
[1435]1568    lappend _isomarkers($tf) $m
[1376]1569    updatetransferfuncs
[878]1570    return 1
1571}
1572
[1376]1573itcl::body Rappture::NanovisViewer::rmdupmarker { marker x } {
1574    set tf [$marker transferfunc]
[885]1575    set bool 0
[1435]1576    if { [info exists _isomarkers($tf)] } {
[1694]1577        set list {}
1578        set marker [namespace tail $marker]
1579        foreach m $_isomarkers($tf) {
1580            set sx [$m screenpos]
1581            if { $m != $marker } {
1582                if { $x >= ($sx-3) && $x <= ($sx+3) } {
1583                    $marker relval [$m relval]
1584                    itcl::delete object $m
1585                    bell
1586                    set bool 1
1587                    continue
1588                }
1589            }
1590            lappend list $m
1591        }
1592        set _isomarkers($tf) $list
1593        updatetransferfuncs
[878]1594    }
1595    return $bool
1596}
[885]1597
[1376]1598itcl::body Rappture::NanovisViewer::overmarker { marker x } {
1599    set tf [$marker transferfunc]
[1435]1600    if { [info exists _isomarkers($tf)] } {
[1694]1601        set marker [namespace tail $marker]
1602        foreach m $_isomarkers($tf) {
1603            set sx [$m screenpos]
1604            if { $m != $marker } {
1605                set bool [expr { $x >= ($sx-3) && $x <= ($sx+3) }]
1606                $m activate $bool
1607            }
1608        }
[885]1609    }
1610    return ""
1611}
[1141]1612
[1376]1613itcl::body Rappture::NanovisViewer::limits { tf } {
[1435]1614    set _limits(min) 0.0
1615    set _limits(max) 1.0
[1549]1616    if { ![info exists _style2vols($tf)] } {
[1694]1617        return [array get _limits]
[1435]1618    }
1619    set min ""; set max ""
[1549]1620    foreach vol $_style2vols($tf) {
[1694]1621        if { ![info exists _serverVols($vol)] } {
1622            continue
1623        }
1624        if { ![info exists _limits($vol-min)] } {
1625            continue
1626        }
1627        if { $min == "" || $min > $_limits($vol-min) } {
1628            set min $_limits($vol-min)
1629        }
1630        if { $max == "" || $max < $_limits($vol-max) } {
1631            set max $_limits($vol-max)
1632        }
[1141]1633    }
[1435]1634    if { $min != "" } {
[1694]1635        set _limits(min) $min
[1435]1636    }
1637    if { $max != "" } {
[1694]1638        set _limits(max) $max
[1435]1639    }
1640    return [array get _limits]
[1141]1641}
[1376]1642
1643
1644itcl::body Rappture::NanovisViewer::BuildViewTab {} {
1645    foreach { key value } {
[2744]1646        grid            0
1647        axes            1
1648        outline         1
1649        volume          1
1650        legend          1
1651        particles       1
1652        lic             1
[1376]1653    } {
[1694]1654        set _settings($this-$key) $value
[1376]1655    }
1656
1657    set fg [option get $itk_component(hull) font Font]
1658    #set bfg [option get $itk_component(hull) boldFont Font]
1659
1660    set inner [$itk_component(main) insert end \
1661        -title "View Settings" \
1662        -icon [Rappture::icon wrench]]
1663    $inner configure -borderwidth 4
1664
[1435]1665    set ::Rappture::NanovisViewer::_settings($this-isosurface) 0
[1376]1666    checkbutton $inner.isosurface \
1667        -text "Isosurface shading" \
[1435]1668        -variable [itcl::scope _settings($this-isosurface)] \
[1376]1669        -command [itcl::code $this FixSettings isosurface] \
[1694]1670        -font "Arial 9"
[1376]1671
1672    checkbutton $inner.axes \
1673        -text "Axes" \
[1435]1674        -variable [itcl::scope _settings($this-axes)] \
[1376]1675        -command [itcl::code $this FixSettings axes] \
[1694]1676        -font "Arial 9"
[1376]1677
1678    checkbutton $inner.grid \
1679        -text "Grid" \
[1435]1680        -variable [itcl::scope _settings($this-grid)] \
[1376]1681        -command [itcl::code $this FixSettings grid] \
[1694]1682        -font "Arial 9"
[1376]1683
1684    checkbutton $inner.outline \
1685        -text "Outline" \
[1435]1686        -variable [itcl::scope _settings($this-outline)] \
[1376]1687        -command [itcl::code $this FixSettings outline] \
[1694]1688        -font "Arial 9"
[1376]1689
1690    checkbutton $inner.legend \
1691        -text "Legend" \
[1435]1692        -variable [itcl::scope _settings($this-legend)] \
[1376]1693        -command [itcl::code $this FixSettings legend] \
[1694]1694        -font "Arial 9"
[1376]1695
1696    checkbutton $inner.volume \
1697        -text "Volume" \
[1435]1698        -variable [itcl::scope _settings($this-volume)] \
[1376]1699        -command [itcl::code $this FixSettings volume] \
[1694]1700        -font "Arial 9"
[1376]1701
1702    blt::table $inner \
[1694]1703        0,0 $inner.axes  -columnspan 2 -anchor w \
1704        1,0 $inner.grid  -columnspan 2 -anchor w \
1705        2,0 $inner.outline  -columnspan 2 -anchor w \
1706        3,0 $inner.volume  -columnspan 2 -anchor w \
1707        4,0 $inner.legend  -columnspan 2 -anchor w
[1376]1708
[1536]1709    if 0 {
1710    bind $inner <Map> [itcl::code $this GetVolumeInfo $inner]
1711    }
[1376]1712    blt::table configure $inner r* -resize none
1713    blt::table configure $inner r5 -resize expand
1714}
1715
1716itcl::body Rappture::NanovisViewer::BuildVolumeTab {} {
1717    foreach { key value } {
[2744]1718        light           40
1719        transp          50
1720        opacity         100
1721        thickness       350
[1376]1722    } {
[1694]1723        set _settings($this-$key) $value
[1376]1724    }
1725
1726    set inner [$itk_component(main) insert end \
1727        -title "Volume Settings" \
[1391]1728        -icon [Rappture::icon volume-on]]
[1376]1729    $inner configure -borderwidth 4
1730
1731    set fg [option get $itk_component(hull) font Font]
1732    #set bfg [option get $itk_component(hull) boldFont Font]
1733
[1391]1734    checkbutton $inner.vol -text "Show volume" -font $fg \
[1435]1735        -variable [itcl::scope _settings($this-volume)] \
[1391]1736        -command [itcl::code $this FixSettings volume]
1737    label $inner.shading -text "Shading:" -font $fg
1738
[1376]1739    label $inner.dim -text "Dim" -font $fg
1740    ::scale $inner.light -from 0 -to 100 -orient horizontal \
[1435]1741        -variable [itcl::scope _settings($this-light)] \
[1694]1742        -width 10 \
[1376]1743        -showvalue off -command [itcl::code $this FixSettings light]
1744    label $inner.bright -text "Bright" -font $fg
1745
1746    label $inner.fog -text "Fog" -font $fg
1747    ::scale $inner.transp -from 0 -to 100 -orient horizontal \
[1435]1748        -variable [itcl::scope _settings($this-transp)] \
[1694]1749        -width 10 \
[1376]1750        -showvalue off -command [itcl::code $this FixSettings transp]
1751    label $inner.plastic -text "Plastic" -font $fg
1752
1753    label $inner.clear -text "Clear" -font $fg
1754    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
[1435]1755        -variable [itcl::scope _settings($this-opacity)] \
[1694]1756        -width 10 \
[1376]1757        -showvalue off -command [itcl::code $this FixSettings opacity]
1758    label $inner.opaque -text "Opaque" -font $fg
1759
1760    label $inner.thin -text "Thin" -font $fg
1761    ::scale $inner.thickness -from 0 -to 1000 -orient horizontal \
[1435]1762        -variable [itcl::scope _settings($this-thickness)] \
[1694]1763        -width 10 \
[1376]1764        -showvalue off -command [itcl::code $this FixSettings thickness]
1765    label $inner.thick -text "Thick" -font $fg
1766
1767    blt::table $inner \
[1694]1768        0,0 $inner.vol -columnspan 4 -anchor w -pady 2 \
1769        1,0 $inner.shading -columnspan 4 -anchor w -pady {10 2} \
1770        2,0 $inner.dim -anchor e -pady 2 \
1771        2,1 $inner.light -columnspan 2 -pady 2 -fill x \
1772        2,3 $inner.bright -anchor w -pady 2 \
1773        3,0 $inner.fog -anchor e -pady 2 \
1774        3,1 $inner.transp -columnspan 2 -pady 2 -fill x \
1775        3,3 $inner.plastic -anchor w -pady 2 \
1776        4,0 $inner.clear -anchor e -pady 2 \
1777        4,1 $inner.opacity -columnspan 2 -pady 2 -fill x\
1778        4,3 $inner.opaque -anchor w -pady 2 \
1779        5,0 $inner.thin -anchor e -pady 2 \
1780        5,1 $inner.thickness -columnspan 2 -pady 2 -fill x\
1781        5,3 $inner.thick -anchor w -pady 2
[1376]1782
[1390]1783    blt::table configure $inner c0 c1 c3 r* -resize none
[1391]1784    blt::table configure $inner r6 -resize expand
[1376]1785}
1786
1787itcl::body Rappture::NanovisViewer::BuildCutplanesTab {} {
1788    set inner [$itk_component(main) insert end \
1789        -title "Cutplane Settings" \
1790        -icon [Rappture::icon cutbutton]]
1791    $inner configure -borderwidth 4
1792
1793    # X-value slicer...
1794    itk_component add xCutButton {
[1435]1795        Rappture::PushButton $inner.xbutton \
[1694]1796            -onimage [Rappture::icon x-cutplane] \
1797            -offimage [Rappture::icon x-cutplane] \
1798            -command [itcl::code $this FixSettings xcutplane] \
1799            -variable [itcl::scope _settings($this-xcutplane)]
[1376]1800    }
1801    Rappture::Tooltip::for $itk_component(xCutButton) \
1802        "Toggle the X cut plane on/off"
1803
1804    itk_component add xCutScale {
1805        ::scale $inner.xval -from 100 -to 0 \
1806            -width 10 -orient vertical -showvalue off \
1807            -borderwidth 1 -highlightthickness 0 \
[1435]1808            -command [itcl::code $this Slice move x] \
[1694]1809            -variable [itcl::scope _settings($this-xcutposition)]
[1376]1810    } {
1811        usual
1812        ignore -borderwidth -highlightthickness
1813    }
1814    # Set the default cutplane value before disabling the scale.
1815    $itk_component(xCutScale) set 50
1816    $itk_component(xCutScale) configure -state disabled
1817    Rappture::Tooltip::for $itk_component(xCutScale) \
1818        "@[itcl::code $this SlicerTip x]"
1819
1820    # Y-value slicer...
1821    itk_component add yCutButton {
[1435]1822        Rappture::PushButton $inner.ybutton \
[1694]1823            -onimage [Rappture::icon y-cutplane] \
1824            -offimage [Rappture::icon y-cutplane] \
1825            -command [itcl::code $this FixSettings ycutplane] \
1826            -variable [itcl::scope _settings($this-ycutplane)]
[1376]1827    }
1828    Rappture::Tooltip::for $itk_component(yCutButton) \
1829        "Toggle the Y cut plane on/off"
1830
1831    itk_component add yCutScale {
1832        ::scale $inner.yval -from 100 -to 0 \
1833            -width 10 -orient vertical -showvalue off \
1834            -borderwidth 1 -highlightthickness 0 \
[1435]1835            -command [itcl::code $this Slice move y] \
[1694]1836            -variable [itcl::scope _settings($this-ycutposition)]
[1376]1837    } {
1838        usual
1839        ignore -borderwidth -highlightthickness
1840    }
1841    Rappture::Tooltip::for $itk_component(yCutScale) \
1842        "@[itcl::code $this SlicerTip y]"
1843    # Set the default cutplane value before disabling the scale.
1844    $itk_component(yCutScale) set 50
1845    $itk_component(yCutScale) configure -state disabled
1846
1847    # Z-value slicer...
1848    itk_component add zCutButton {
[1435]1849        Rappture::PushButton $inner.zbutton \
[1694]1850            -onimage [Rappture::icon z-cutplane] \
1851            -offimage [Rappture::icon z-cutplane] \
1852            -command [itcl::code $this FixSettings zcutplane] \
1853            -variable [itcl::scope _settings($this-zcutplane)]
[1376]1854    }
1855    Rappture::Tooltip::for $itk_component(zCutButton) \
1856        "Toggle the Z cut plane on/off"
1857
1858    itk_component add zCutScale {
1859        ::scale $inner.zval -from 100 -to 0 \
1860            -width 10 -orient vertical -showvalue off \
1861            -borderwidth 1 -highlightthickness 0 \
[1435]1862            -command [itcl::code $this Slice move z] \
[1694]1863            -variable [itcl::scope _settings($this-zcutposition)]
[1376]1864    } {
1865        usual
1866        ignore -borderwidth -highlightthickness
1867    }
1868    $itk_component(zCutScale) set 50
1869    $itk_component(zCutScale) configure -state disabled
1870    #$itk_component(zCutScale) configure -state disabled
1871    Rappture::Tooltip::for $itk_component(zCutScale) \
1872        "@[itcl::code $this SlicerTip z]"
1873
1874    blt::table $inner \
[1694]1875        1,1 $itk_component(xCutButton) \
1876        1,2 $itk_component(yCutButton) \
1877        1,3 $itk_component(zCutButton) \
1878        0,1 $itk_component(xCutScale) \
1879        0,2 $itk_component(yCutScale) \
[2743]1880        0,3 $itk_component(zCutScale)
[1376]1881
1882    blt::table configure $inner r0 r1 c* -resize none
1883    blt::table configure $inner r2 c4 -resize expand
1884    blt::table configure $inner c0 -width 2
1885    blt::table configure $inner c1 c2 c3 -padx 2
1886}
1887
1888itcl::body Rappture::NanovisViewer::BuildCameraTab {} {
1889    set inner [$itk_component(main) insert end \
1890        -title "Camera Settings" \
1891        -icon [Rappture::icon camera]]
1892    $inner configure -borderwidth 4
1893
1894    set labels { phi theta psi pan-x pan-y zoom }
1895    set row 0
1896    foreach tag $labels {
[1694]1897        label $inner.${tag}label -text $tag -font "Arial 9"
1898        entry $inner.${tag} -font "Arial 9"  -bg white \
1899            -textvariable [itcl::scope _settings($this-$tag)]
1900        bind $inner.${tag} <KeyPress-Return> \
1901            [itcl::code $this camera set ${tag}]
1902        blt::table $inner \
1903            $row,0 $inner.${tag}label -anchor e -pady 2 \
1904            $row,1 $inner.${tag} -anchor w -pady 2
[1376]1905        blt::table configure $inner r$row -resize none
[1694]1906        incr row
[1376]1907    }
[3032]1908
1909    itk_component add orientation {
1910        Rappture::Combobox $inner.orientation -width 10 -editable no
1911    }
[3060]1912    $inner.orientation choices insert end \
1913        "front"    "0 90 0" \
1914        "back"   "180 90 0" \
[3032]1915        "top"     "0 0 0"  \
1916        "bottom"     "0 180 0"  \
[3060]1917        "left"     "270 90 0 "  \
1918        "right"     "90 90 0"  \
[3032]1919        "default"  "45 45 0"
1920    $itk_component(orientation) value "default"
[3060]1921    bind $inner.orientation <<Value>> [itcl::code $this SetOrientation]
[3032]1922
[1376]1923    blt::table configure $inner c0 c1 -resize none
1924    blt::table configure $inner c2 -resize expand
1925    blt::table configure $inner r$row -resize expand
1926}
1927
1928
1929# ----------------------------------------------------------------------
1930# USAGE: Slice move x|y|z <newval>
1931#
1932# Called automatically when the user drags the slider to move the
1933# cut plane that slices 3D data.  Gets the current value from the
1934# slider and moves the cut plane to the appropriate point in the
1935# data set.
1936# ----------------------------------------------------------------------
1937itcl::body Rappture::NanovisViewer::Slice {option args} {
1938    switch -- $option {
1939        move {
1940            if {[llength $args] != 2} {
1941                error "wrong # args: should be \"Slice move x|y|z newval\""
1942            }
1943            set axis [lindex $args 0]
1944            set newval [lindex $args 1]
1945
1946            set newpos [expr {0.01*$newval}]
[1514]1947            set vols [CurrentVolumes -cutplanes]
[2744]1948            set vol [lindex $vols 0]
[2390]1949            SendCmd "cutplane position $newpos $axis $vol"
[1376]1950        }
1951        default {
1952            error "bad option \"$option\": should be axis, move, or volume"
1953        }
1954    }
1955}
1956
1957# ----------------------------------------------------------------------
1958# USAGE: SlicerTip <axis>
1959#
1960# Used internally to generate a tooltip for the x/y/z slicer controls.
1961# Returns a message that includes the current slicer value.
1962# ----------------------------------------------------------------------
1963itcl::body Rappture::NanovisViewer::SlicerTip {axis} {
1964    set val [$itk_component(${axis}CutScale) get]
1965#    set val [expr {0.01*($val-50)
[1435]1966#        *($_limits(${axis}max)-$_limits(${axis}min))
1967#          + 0.5*($_limits(${axis}max)+$_limits(${axis}min))}]
[1376]1968    return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
1969}
[1377]1970
1971
1972itcl::body Rappture::NanovisViewer::DoResize {} {
[1435]1973    SendCmd "screen $_width $_height"
[1448]1974    set _resizePending 0
[1377]1975}
1976
1977itcl::body Rappture::NanovisViewer::EventuallyResize { w h } {
[1448]1978    set _width $w
1979    set _height $h
1980    if { !$_resizePending } {
[1694]1981        $_dispatcher event -idle !resize
1982        set _resizePending 1
[1377]1983    }
1984}
1985
1986itcl::body Rappture::NanovisViewer::EventuallyResizeLegend {} {
[1448]1987    if { !$_resizeLegendPending } {
[1694]1988        $_dispatcher event -idle !legend
1989        set _resizeLegendPending 1
[1448]1990    }
[1377]1991}
[1423]1992
[1435]1993
[1423]1994#  camera --
1995#
1996itcl::body Rappture::NanovisViewer::camera {option args} {
1997    switch -- $option {
[1694]1998        "show" {
1999            puts [array get _view]
2000        }
2001        "set" {
2002            set who [lindex $args 0]
2003            set x $_settings($this-$who)
2004            set code [catch { string is double $x } result]
2005            if { $code != 0 || !$result } {
2006                set _settings($this-$who) $_view($who)
2007                return
2008            }
2009            switch -- $who {
2010                "pan-x" - "pan-y" {
2011                    set _view($who) $_settings($this-$who)
2012                    PanCamera
2013                }
2014                "phi" - "theta" - "psi" {
2015                    set _view($who) $_settings($this-$who)
2016                    set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
2017                    SendCmd "camera angle $xyz"
2018                }
2019                "zoom" {
2020                    set _view($who) $_settings($this-$who)
2021                    SendCmd "camera zoom $_view(zoom)"
2022                }
2023            }
2024        }
[1423]2025    }
2026}
[1535]2027
2028itcl::body Rappture::NanovisViewer::GetVolumeInfo { w } {
2029    set flowobj ""
2030    foreach key [array names _obj2flow] {
[1694]2031        set flowobj $_obj2flow($key)
2032        break
[1535]2033    }
2034    if { $flowobj == "" } {
[1694]2035        return
[1535]2036    }
2037    if { [winfo exists $w.frame] } {
[1694]2038        destroy $w.frame
[1535]2039    }
2040    set inner [frame $w.frame]
2041    blt::table $w \
[1694]2042        5,0 $inner -fill both -columnspan 2 -anchor nw
[1535]2043    array set hints [$dataobj hints]
2044
2045    label $inner.volumes -text "Volumes" -font "Arial 9 bold"
2046    blt::table $inner \
[1694]2047        1,0 $inner.volumes  -anchor w \
[1535]2048    blt::table configure $inner c0 c1 -resize none
2049    blt::table configure $inner c2 -resize expand
2050
2051    set row 3
2052    set volumes [get]
2053    if { [llength $volumes] > 0 } {
[1694]2054        blt::table $inner $row,0 $inner.volumes  -anchor w
2055        incr row
[1535]2056    }
2057    foreach vol $volumes {
[1694]2058        array unset info
2059        array set info $vol
2060        set name $info(name)
2061        if { ![info exists _settings($this-volume-$name)] } {
2062            set _settings($this-volume-$name) $info(hide)
2063        }
2064        checkbutton $inner.vol$row -text $info(label) \
2065            -variable [itcl::scope _settings($this-volume-$name)] \
2066            -onvalue 0 -offvalue 1 \
2067            -command [itcl::code $this volume $key $name] \
2068            -font "Arial 9"
2069        Rappture::Tooltip::for $inner.vol$row $info(description)
2070        blt::table $inner $row,0 $inner.vol$row -anchor w
2071        if { !$_settings($this-volume-$name) } {
2072            $inner.vol$row select
2073        }
2074        incr row
[1535]2075    }
2076    blt::table configure $inner r* -resize none
2077    blt::table configure $inner r$row -resize expand
2078    blt::table configure $inner c3 -resize expand
2079    event generate [winfo parent [winfo parent $w]] <Configure>
2080}
2081
2082itcl::body Rappture::NanovisViewer::volume { tag name } {
2083    set bool $_settings($this-volume-$name)
2084    SendCmd "volume statue $bool $name"
2085}
2086
[3032]2087
2088itcl::body Rappture::NanovisViewer::SetOrientation {} {
2089    set angles [$itk_component(orientation) value]
2090    foreach name { theta phi psi } angle $angles {
2091        set _view($name) $angle
2092    }
2093    set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
2094    SendCmd "camera angle $xyz"
2095}
Note: See TracBrowser for help on using the repository browser.