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

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

revert viewers to 1.3.5 tag versions

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