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

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

Split out DrawLegend? method

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