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

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

Merge r5205,r5210:5211

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