source: branches/1.3/gui/scripts/nanovisviewer.tcl @ 5110

Last change on this file since 5110 was 5110, checked in by ldelgass, 9 years ago

merge r5092:r5097 from trunk (includes fix for viewer initial canvas size)

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