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

Last change on this file since 5115 was 5115, checked in by ldelgass, 6 years ago

whitespace

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