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

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

bring IsoMarker? closer in line with version in trunk/1.4 branch

File size: 74.5 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: nanovisviewer - 3D volume rendering
4#
5#  This widget performs volume rendering on 3D scalar/vector datasets.
6#  It connects to the Nanovis server running on a rendering farm,
7#  transmits data, and displays the results.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itk
16package require BLT
17package require Img
18
19#
20# FIXME:
21#       Need to Add DX readers this client to examine the data before
22#       it's sent to the server.  This will eliminate 90% of the insanity in
23#       computing the limits of all the volumes.  I can rip out all the
24#       "receive data" "send transfer function" event crap.
25#
26#       This means we can compute the transfer function (relative values) and
27#       draw the legend min/max values without waiting for the information to
28#       come from the server.  This will also prevent the flashing that occurs
29#       when a new volume is drawn (using the default transfer function) and
30#       then when the correct transfer function has been sent and linked to
31#       the volume.
32#
33option add *NanovisViewer.width 4i widgetDefault
34option add *NanovisViewer*cursor crosshair widgetDefault
35option add *NanovisViewer.height 4i widgetDefault
36option add *NanovisViewer.foreground black widgetDefault
37option add *NanovisViewer.controlBackground gray widgetDefault
38option add *NanovisViewer.controlDarkBackground #999999 widgetDefault
39option add *NanovisViewer.plotBackground black widgetDefault
40option add *NanovisViewer.plotForeground white widgetDefault
41option add *NanovisViewer.plotOutline gray widgetDefault
42option add *NanovisViewer.font \
43    -*-helvetica-medium-r-normal-*-12-* widgetDefault
44
45# must use this name -- plugs into Rappture::resources::load
46proc NanovisViewer_init_resources {} {
47    Rappture::resources::register \
48        nanovis_server Rappture::NanovisViewer::SetServerList
49}
50
51itcl::class Rappture::NanovisViewer {
52    inherit Rappture::VisViewer
53
54    itk_option define -plotforeground plotForeground Foreground ""
55    itk_option define -plotbackground plotBackground Background ""
56    itk_option define -plotoutline plotOutline PlotOutline ""
57
58    constructor { hostlist args } {
59        Rappture::VisViewer::constructor $hostlist
60    } {
61        # defined below
62    }
63    destructor {
64        # defined below
65    }
66    public proc SetServerList { namelist } {
67        Rappture::VisViewer::SetServerList "nanovis" $namelist
68    }
69    public method add {dataobj {settings ""}}
70    public method camera {option args}
71    public method delete {args}
72    public method disconnect {}
73    public method download {option args}
74    public method get {args}
75    public method isconnected {}
76    public method limits { tf }
77    public method overMarker { m x }
78    public method parameters {title args} {
79        # do nothing
80    }
81    public method removeDuplicateMarker { m x }
82    public method scale {args}
83    public method updateTransferFunctions {}
84
85    protected method Connect {}
86    protected method CurrentDatasets {{what -all}}
87    protected method Disconnect {}
88    protected method DoResize {}
89    protected method FixLegend {}
90    protected method AdjustSetting {what {value ""}}
91    protected method InitSettings { args }
92    protected method Pan {option x y}
93    protected method Rebuild {}
94    protected method ReceiveData { args }
95    protected method ReceiveImage { args }
96    protected method ReceiveLegend { tf vmin vmax size }
97    protected method Rotate {option x y}
98    protected method SendTransferFuncs {}
99    protected method Slice {option args}
100    protected method SlicerTip {axis}
101    protected method Zoom {option}
102
103    # The following methods are only used by this class.
104    private method AddIsoMarker { x y }
105    private method BuildCameraTab {}
106    private method BuildCutplanesTab {}
107    private method BuildViewTab {}
108    private method BuildVolumeTab {}
109    private method ResetColormap { color }
110    private method ComputeTransferFunc { tf }
111    private method EventuallyResize { w h }
112    private method EventuallyResizeLegend { }
113    private method NameTransferFunc { dataobj comp }
114    private method PanCamera {}
115    private method ParseLevelsOption { tf levels }
116    private method ParseMarkersOption { tf markers }
117    private method volume { tag name }
118    private method GetVolumeInfo { w }
119    private method SetOrientation { side }
120
121    private variable _arcball ""
122
123    private variable _dlist ""     ;# list of data objects
124    private variable _allDataObjs
125    private variable _obj2ovride   ;# maps dataobj => style override
126    private variable _serverDatasets   ;# contains all the dataobj-component
127                                   ;# to volumes in the server
128    private variable _serverTfs    ;# contains all the transfer functions
129                                   ;# in the server.
130    private variable _recvdDatasets    ;# list of data objs to send to server
131    private variable _dataset2style    ;# maps dataobj-component to transfunc
132    private variable _style2datasets   ;# maps tf back to list of
133                                        # dataobj-components using the tf.
134
135    private variable _reset 1      ;# Connection to server has been reset
136    private variable _click        ;# info used for rotate operations
137    private variable _limits       ;# autoscale min/max for all axes
138    private variable _view         ;# view params for 3D view
139    private variable _isomarkers   ;# array of isosurface level values 0..1
140    private variable  _settings
141    # Array of transfer functions in server.  If 0 the transfer has been
142    # defined but not loaded.  If 1 the transfer function has been named
143    # and loaded.
144    private variable _activeTfs
145    private variable _first ""     ;# This is the topmost volume.
146
147    common _downloadPopup          ;# download options from popup
148    private common _hardcopy
149    private variable _width 0
150    private variable _height 0
151    private variable _resizePending 0
152    private variable _resizeLegendPending 0
153}
154
155itk::usual NanovisViewer {
156    keep -background -foreground -cursor -font
157    keep -plotbackground -plotforeground
158}
159
160# ----------------------------------------------------------------------
161# CONSTRUCTOR
162# ----------------------------------------------------------------------
163itcl::body Rappture::NanovisViewer::constructor {hostlist args} {
164    set _serverType "nanovis"
165
166    # Draw legend event
167    $_dispatcher register !legend
168    $_dispatcher dispatch $this !legend "[itcl::code $this FixLegend]; list"
169
170    # Send transfer functions event
171    $_dispatcher register !send_transfunc
172    $_dispatcher dispatch $this !send_transfunc \
173        "[itcl::code $this SendTransferFuncs]; list"
174
175    # Rebuild event
176    $_dispatcher register !rebuild
177    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
178
179    # Resize event
180    $_dispatcher register !resize
181    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
182
183    #
184    # Populate parser with commands handle incoming requests
185    #
186    $_parser alias image [itcl::code $this ReceiveImage]
187    $_parser alias legend [itcl::code $this ReceiveLegend]
188    $_parser alias data [itcl::code $this ReceiveData]
189
190    # Initialize the view to some default parameters.
191    array set _view {
192        qw      0.853553
193        qx      -0.353553
194        qy      0.353553
195        qz      0.146447
196        zoom    1.0
197        xpan    0
198        ypan    0
199    }
200    set _arcball [blt::arcball create 100 100]
201    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
202    $_arcball quaternion $q
203
204    set _limits(vmin) 0.0
205    set _limits(vmax) 1.0
206    set _reset 1
207
208    array set _settings [subst {
209        $this-qw                $_view(qw)
210        $this-qx                $_view(qx)
211        $this-qy                $_view(qy)
212        $this-qz                $_view(qz)
213        $this-zoom              $_view(zoom)
214        $this-xpan              $_view(xpan)
215        $this-ypan              $_view(ypan)
216        $this-volume            1
217        $this-xcutplane         0
218        $this-xcutposition      0
219        $this-ycutplane         0
220        $this-ycutposition      0
221        $this-zcutplane         0
222        $this-zcutposition      0
223    }]
224
225    itk_component add 3dview {
226        label $itk_component(plotarea).view -image $_image(plot) \
227            -highlightthickness 0 -borderwidth 0
228    } {
229        usual
230        ignore -highlightthickness -borderwidth  -background
231    }
232    bind $itk_component(3dview) <Control-F1> [itcl::code $this ToggleConsole]
233
234    set f [$itk_component(main) component controls]
235    itk_component add reset {
236        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
237            -highlightthickness 0 \
238            -image [Rappture::icon reset-view] \
239            -command [itcl::code $this Zoom reset]
240    } {
241        usual
242        ignore -highlightthickness
243    }
244    pack $itk_component(reset) -side top -padx 2 -pady 2
245    Rappture::Tooltip::for $itk_component(reset) "Reset the view to the default zoom level"
246
247    itk_component add zoomin {
248        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
249            -highlightthickness 0 \
250            -image [Rappture::icon zoom-in] \
251            -command [itcl::code $this Zoom in]
252    } {
253        usual
254        ignore -highlightthickness
255    }
256    pack $itk_component(zoomin) -side top -padx 2 -pady 2
257    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
258
259    itk_component add zoomout {
260        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
261            -highlightthickness 0 \
262            -image [Rappture::icon zoom-out] \
263            -command [itcl::code $this Zoom out]
264    } {
265        usual
266        ignore -highlightthickness
267    }
268    pack $itk_component(zoomout) -side top -padx 2 -pady 2
269    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
270
271    itk_component add volume {
272        Rappture::PushButton $f.volume \
273            -onimage [Rappture::icon volume-on] \
274            -offimage [Rappture::icon volume-off] \
275            -command [itcl::code $this AdjustSetting volume] \
276            -variable [itcl::scope _settings($this-volume)]
277    }
278    $itk_component(volume) select
279    Rappture::Tooltip::for $itk_component(volume) \
280        "Toggle the volume cloud on/off"
281    pack $itk_component(volume) -padx 2 -pady 2
282
283    if { [catch {
284        BuildViewTab
285        BuildVolumeTab
286        BuildCutplanesTab
287        BuildCameraTab
288    } errs] != 0 } {
289        global errorInfo
290        puts stderr "errs=$errs errorInfo=$errorInfo"
291    }
292
293    # Legend
294
295    set _image(legend) [image create photo]
296    itk_component add legend {
297        canvas $itk_component(plotarea).legend -height 50 -highlightthickness 0
298    } {
299        usual
300        ignore -highlightthickness
301        rename -background -plotbackground plotBackground Background
302    }
303    bind $itk_component(legend) <Configure> \
304        [itcl::code $this EventuallyResizeLegend]
305
306    # Hack around the Tk panewindow.  The problem is that the requested
307    # size of the 3d view isn't set until an image is retrieved from
308    # the server.  So the panewindow uses the tiny size.
309    set w 10000
310    pack forget $itk_component(3dview)
311    blt::table $itk_component(plotarea) \
312        0,0 $itk_component(3dview) -fill both -reqwidth $w \
313        1,0 $itk_component(legend) -fill x
314    blt::table configure $itk_component(plotarea) r1 -resize none
315
316    # Bindings for rotation via mouse
317    bind $itk_component(3dview) <ButtonPress-1> \
318        [itcl::code $this Rotate click %x %y]
319    bind $itk_component(3dview) <B1-Motion> \
320        [itcl::code $this Rotate drag %x %y]
321    bind $itk_component(3dview) <ButtonRelease-1> \
322        [itcl::code $this Rotate release %x %y]
323    bind $itk_component(3dview) <Configure> \
324        [itcl::code $this EventuallyResize %w %h]
325
326    # Bindings for panning via mouse
327    bind $itk_component(3dview) <ButtonPress-2> \
328        [itcl::code $this Pan click %x %y]
329    bind $itk_component(3dview) <B2-Motion> \
330        [itcl::code $this Pan drag %x %y]
331    bind $itk_component(3dview) <ButtonRelease-2> \
332        [itcl::code $this Pan release %x %y]
333
334    # Bindings for panning via keyboard
335    bind $itk_component(3dview) <KeyPress-Left> \
336        [itcl::code $this Pan set -10 0]
337    bind $itk_component(3dview) <KeyPress-Right> \
338        [itcl::code $this Pan set 10 0]
339    bind $itk_component(3dview) <KeyPress-Up> \
340        [itcl::code $this Pan set 0 -10]
341    bind $itk_component(3dview) <KeyPress-Down> \
342        [itcl::code $this Pan set 0 10]
343    bind $itk_component(3dview) <Shift-KeyPress-Left> \
344        [itcl::code $this Pan set -2 0]
345    bind $itk_component(3dview) <Shift-KeyPress-Right> \
346        [itcl::code $this Pan set 2 0]
347    bind $itk_component(3dview) <Shift-KeyPress-Up> \
348        [itcl::code $this Pan set 0 -2]
349    bind $itk_component(3dview) <Shift-KeyPress-Down> \
350        [itcl::code $this Pan set 0 2]
351
352    # Bindings for zoom via keyboard
353    bind $itk_component(3dview) <KeyPress-Prior> \
354        [itcl::code $this Zoom out]
355    bind $itk_component(3dview) <KeyPress-Next> \
356        [itcl::code $this Zoom in]
357
358    bind $itk_component(3dview) <Enter> "focus $itk_component(3dview)"
359
360    if {[string equal "x11" [tk windowingsystem]]} {
361        # Bindings for zoom via mouse
362        bind $itk_component(3dview) <4> [itcl::code $this Zoom out]
363        bind $itk_component(3dview) <5> [itcl::code $this Zoom in]
364    }
365
366    set _image(download) [image create photo]
367
368    eval itk_initialize $args
369
370    EnableWaitDialog 900
371    Connect
372}
373
374# ----------------------------------------------------------------------
375# DESTRUCTOR
376# ----------------------------------------------------------------------
377itcl::body Rappture::NanovisViewer::destructor {} {
378    $_dispatcher cancel !rebuild
379    $_dispatcher cancel !send_transfunc
380    $_dispatcher cancel !resize
381    image delete $_image(plot)
382    image delete $_image(legend)
383    image delete $_image(download)
384    catch { blt::arcball destroy $_arcball }
385    array unset _settings $this-*
386}
387
388# ----------------------------------------------------------------------
389# USAGE: add <dataobj> ?<settings>?
390#
391# Clients use this to add a data object to the plot.  The optional
392# <settings> are used to configure the plot.  Allowed settings are
393# -color, -brightness, -width, -linestyle, and -raise.
394# ----------------------------------------------------------------------
395itcl::body Rappture::NanovisViewer::add {dataobj {settings ""}} {
396    if { ![$dataobj isvalid] } {
397        return;                         # Object doesn't contain valid data.
398    }
399    array set params {
400        -color auto
401        -width 1
402        -linestyle solid
403        -brightness 0
404        -raise 0
405        -description ""
406        -param ""
407    }
408    array set params $settings
409
410    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
411        # can't handle -autocolors yet
412        set params(-color) black
413    }
414    set pos [lsearch -exact $_dlist $dataobj]
415    if {$pos < 0} {
416        lappend _dlist $dataobj
417        set _allDataObjs($dataobj) 1
418        set _obj2ovride($dataobj-color) $params(-color)
419        set _obj2ovride($dataobj-width) $params(-width)
420        set _obj2ovride($dataobj-raise) $params(-raise)
421        $_dispatcher event -idle !rebuild
422    }
423}
424
425# ----------------------------------------------------------------------
426# USAGE: get ?-objects?
427# USAGE: get ?-image 3dview|legend?
428#
429# Clients use this to query the list of objects being plotted, in
430# order from bottom to top of this result.  The optional "-image"
431# flag can also request the internal images being shown.
432# ----------------------------------------------------------------------
433itcl::body Rappture::NanovisViewer::get {args} {
434    if {[llength $args] == 0} {
435        set args "-objects"
436    }
437
438    set op [lindex $args 0]
439    switch -- $op {
440        -objects {
441            # put the dataobj list in order according to -raise options
442            set dlist $_dlist
443            foreach obj $dlist {
444                if {[info exists _obj2ovride($obj-raise)] &&
445                    $_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 buffering 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        updateTransferFunctions
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                SendData $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        # Turn off cutplanes for all volumes
950        foreach axis {x y z} {
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            updateTransferFunctions
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            updateTransferFunctions
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                updateTransferFunctions
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 amap ""
1411    if { $first == "" || $first != 0.0 } {
1412        lappend amap 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 amap $x1 0.0
1441        lappend amap $x2 $max
1442        lappend amap $x3 $max
1443        lappend amap $x4 0.0
1444    }
1445    if { $last == "" || $last != 1.0 } {
1446        lappend amap 1.0 0.0
1447    }
1448    SendCmd "transfunc define $tf { $cmap } { $amap }"
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::updateTransferFunctions {} {
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    updateTransferFunctions
1566    return 1
1567}
1568
1569itcl::body Rappture::NanovisViewer::removeDuplicateMarker { 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        updateTransferFunctions
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 [GetColormapList -includeNone]
1774    $itk_component(colormap) value "BCGYR"
1775    bind $inner.colormap <<Value>> \
1776        [itcl::code $this AdjustSetting colormap]
1777
1778    blt::table $inner \
1779        0,0 $inner.vol -cspan 4 -anchor w -pady 2 \
1780        1,0 $inner.shading -cspan 4 -anchor w -pady {10 2} \
1781        2,0 $inner.light2side -cspan 4 -anchor w -pady 2 \
1782        3,0 $inner.dim -anchor e -pady 2 \
1783        3,1 $inner.light -cspan 2 -pady 2 -fill x \
1784        3,3 $inner.bright -anchor w -pady 2 \
1785        4,0 $inner.fog -anchor e -pady 2 \
1786        4,1 $inner.transp -cspan 2 -pady 2 -fill x \
1787        4,3 $inner.plastic -anchor w -pady 2 \
1788        5,0 $inner.thin -anchor e -pady 2 \
1789        5,1 $inner.thickness -cspan 2 -pady 2 -fill x\
1790        5,3 $inner.thick -anchor w -pady 2
1791
1792    blt::table configure $inner c0 c1 c3 r* -resize none
1793    blt::table configure $inner r6 -resize expand
1794}
1795
1796itcl::body Rappture::NanovisViewer::BuildCutplanesTab {} {
1797    set inner [$itk_component(main) insert end \
1798        -title "Cutplane Settings" \
1799        -icon [Rappture::icon cutbutton]]
1800    $inner configure -borderwidth 4
1801
1802    # X-value slicer...
1803    itk_component add xCutButton {
1804        Rappture::PushButton $inner.xbutton \
1805            -onimage [Rappture::icon x-cutplane] \
1806            -offimage [Rappture::icon x-cutplane] \
1807            -command [itcl::code $this AdjustSetting xcutplane] \
1808            -variable [itcl::scope _settings($this-xcutplane)]
1809    }
1810    Rappture::Tooltip::for $itk_component(xCutButton) \
1811        "Toggle the X cut plane on/off"
1812
1813    itk_component add xCutScale {
1814        ::scale $inner.xval -from 100 -to 0 \
1815            -width 10 -orient vertical -showvalue off \
1816            -borderwidth 1 -highlightthickness 0 \
1817            -command [itcl::code $this Slice move x] \
1818            -variable [itcl::scope _settings($this-xcutposition)]
1819    } {
1820        usual
1821        ignore -borderwidth -highlightthickness
1822    }
1823    # Set the default cutplane value before disabling the scale.
1824    $itk_component(xCutScale) set 50
1825    $itk_component(xCutScale) configure -state disabled
1826    Rappture::Tooltip::for $itk_component(xCutScale) \
1827        "@[itcl::code $this SlicerTip x]"
1828
1829    # Y-value slicer...
1830    itk_component add yCutButton {
1831        Rappture::PushButton $inner.ybutton \
1832            -onimage [Rappture::icon y-cutplane] \
1833            -offimage [Rappture::icon y-cutplane] \
1834            -command [itcl::code $this AdjustSetting ycutplane] \
1835            -variable [itcl::scope _settings($this-ycutplane)]
1836    }
1837    Rappture::Tooltip::for $itk_component(yCutButton) \
1838        "Toggle the Y cut plane on/off"
1839
1840    itk_component add yCutScale {
1841        ::scale $inner.yval -from 100 -to 0 \
1842            -width 10 -orient vertical -showvalue off \
1843            -borderwidth 1 -highlightthickness 0 \
1844            -command [itcl::code $this Slice move y] \
1845            -variable [itcl::scope _settings($this-ycutposition)]
1846    } {
1847        usual
1848        ignore -borderwidth -highlightthickness
1849    }
1850    Rappture::Tooltip::for $itk_component(yCutScale) \
1851        "@[itcl::code $this SlicerTip y]"
1852    # Set the default cutplane value before disabling the scale.
1853    $itk_component(yCutScale) set 50
1854    $itk_component(yCutScale) configure -state disabled
1855
1856    # Z-value slicer...
1857    itk_component add zCutButton {
1858        Rappture::PushButton $inner.zbutton \
1859            -onimage [Rappture::icon z-cutplane] \
1860            -offimage [Rappture::icon z-cutplane] \
1861            -command [itcl::code $this AdjustSetting zcutplane] \
1862            -variable [itcl::scope _settings($this-zcutplane)]
1863    }
1864    Rappture::Tooltip::for $itk_component(zCutButton) \
1865        "Toggle the Z cut plane on/off"
1866
1867    itk_component add zCutScale {
1868        ::scale $inner.zval -from 100 -to 0 \
1869            -width 10 -orient vertical -showvalue off \
1870            -borderwidth 1 -highlightthickness 0 \
1871            -command [itcl::code $this Slice move z] \
1872            -variable [itcl::scope _settings($this-zcutposition)]
1873    } {
1874        usual
1875        ignore -borderwidth -highlightthickness
1876    }
1877    $itk_component(zCutScale) set 50
1878    $itk_component(zCutScale) configure -state disabled
1879    #$itk_component(zCutScale) configure -state disabled
1880    Rappture::Tooltip::for $itk_component(zCutScale) \
1881        "@[itcl::code $this SlicerTip z]"
1882
1883    blt::table $inner \
1884        1,1 $itk_component(xCutButton) \
1885        1,2 $itk_component(yCutButton) \
1886        1,3 $itk_component(zCutButton) \
1887        0,1 $itk_component(xCutScale) \
1888        0,2 $itk_component(yCutScale) \
1889        0,3 $itk_component(zCutScale)
1890
1891    blt::table configure $inner r0 r1 c* -resize none
1892    blt::table configure $inner r2 c4 -resize expand
1893    blt::table configure $inner c0 -width 2
1894    blt::table configure $inner c1 c2 c3 -padx 2
1895}
1896
1897itcl::body Rappture::NanovisViewer::BuildCameraTab {} {
1898    set inner [$itk_component(main) insert end \
1899        -title "Camera Settings" \
1900        -icon [Rappture::icon camera]]
1901    $inner configure -borderwidth 4
1902
1903    label $inner.view_l -text "view" -font "Arial 9"
1904    set f [frame $inner.view]
1905    foreach side { front back left right top bottom } {
1906        button $f.$side  -image [Rappture::icon view$side] \
1907            -command [itcl::code $this SetOrientation $side]
1908        Rappture::Tooltip::for $f.$side "Change the view to $side"
1909        pack $f.$side -side left
1910    }
1911
1912    blt::table $inner \
1913        0,0 $inner.view_l -anchor e -pady 2 \
1914        0,1 $inner.view -anchor w -pady 2
1915
1916    set row 1
1917    set labels { qw qx qy qz xpan ypan zoom }
1918    foreach tag $labels {
1919        label $inner.${tag}label -text $tag -font "Arial 9"
1920        entry $inner.${tag} -font "Arial 9"  -bg white \
1921            -textvariable [itcl::scope _settings($this-$tag)]
1922        bind $inner.${tag} <Return> \
1923            [itcl::code $this camera set ${tag}]
1924        bind $inner.${tag} <KP_Enter> \
1925            [itcl::code $this camera set ${tag}]
1926        blt::table $inner \
1927            $row,0 $inner.${tag}label -anchor e -pady 2 \
1928            $row,1 $inner.${tag} -anchor w -pady 2
1929        blt::table configure $inner r$row -resize none
1930        incr row
1931    }
1932
1933    blt::table configure $inner c* r* -resize none
1934    blt::table configure $inner c2 -resize expand
1935    blt::table configure $inner r$row -resize expand
1936}
1937
1938# ----------------------------------------------------------------------
1939# USAGE: Slice move x|y|z <newval>
1940#
1941# Called automatically when the user drags the slider to move the
1942# cut plane that slices 3D data.  Gets the current value from the
1943# slider and moves the cut plane to the appropriate point in the
1944# data set.
1945# ----------------------------------------------------------------------
1946itcl::body Rappture::NanovisViewer::Slice {option args} {
1947    switch -- $option {
1948        move {
1949            if {[llength $args] != 2} {
1950                error "wrong # args: should be \"Slice move x|y|z newval\""
1951            }
1952            set axis [lindex $args 0]
1953            set newval [lindex $args 1]
1954
1955            set newpos [expr {0.01*$newval}]
1956            set datasets [CurrentDatasets -cutplanes]
1957            set tag [lindex $datasets 0]
1958            SendCmd "cutplane position $newpos $axis $tag"
1959        }
1960        default {
1961            error "bad option \"$option\": should be axis, move, or volume"
1962        }
1963    }
1964}
1965
1966# ----------------------------------------------------------------------
1967# USAGE: SlicerTip <axis>
1968#
1969# Used internally to generate a tooltip for the x/y/z slicer controls.
1970# Returns a message that includes the current slicer value.
1971# ----------------------------------------------------------------------
1972itcl::body Rappture::NanovisViewer::SlicerTip {axis} {
1973    set val [$itk_component(${axis}CutScale) get]
1974#    set val [expr {0.01*($val-50)
1975#        *($_limits(${axis}max)-$_limits(${axis}min))
1976#          + 0.5*($_limits(${axis}max)+$_limits(${axis}min))}]
1977    return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
1978}
1979
1980
1981itcl::body Rappture::NanovisViewer::DoResize {} {
1982    $_arcball resize $_width $_height
1983    SendCmd "screen size $_width $_height"
1984    set _resizePending 0
1985}
1986
1987itcl::body Rappture::NanovisViewer::EventuallyResize { w h } {
1988    set _width $w
1989    set _height $h
1990    $_arcball resize $w $h
1991    if { !$_resizePending } {
1992        $_dispatcher event -idle !resize
1993        set _resizePending 1
1994    }
1995}
1996
1997itcl::body Rappture::NanovisViewer::EventuallyResizeLegend {} {
1998    if { !$_resizeLegendPending } {
1999        $_dispatcher event -idle !legend
2000        set _resizeLegendPending 1
2001    }
2002}
2003
2004#  camera --
2005#
2006itcl::body Rappture::NanovisViewer::camera {option args} {
2007    switch -- $option {
2008        "show" {
2009            puts [array get _view]
2010        }
2011        "set" {
2012            set who [lindex $args 0]
2013            set x $_settings($this-$who)
2014            set code [catch { string is double $x } result]
2015            if { $code != 0 || !$result } {
2016                set _settings($this-$who) $_view($who)
2017                return
2018            }
2019            switch -- $who {
2020                "xpan" - "ypan" {
2021                    set _view($who) $_settings($this-$who)
2022                    PanCamera
2023                }
2024                "qx" - "qy" - "qz" - "qw" {
2025                    set _view($who) $_settings($this-$who)
2026                    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
2027                    $_arcball quaternion $q
2028                    SendCmd "camera orient $q"
2029                }
2030                "zoom" {
2031                    set _view($who) $_settings($this-$who)
2032                    SendCmd "camera zoom $_view(zoom)"
2033                }
2034            }
2035        }
2036    }
2037}
2038
2039itcl::body Rappture::NanovisViewer::GetVolumeInfo { w } {
2040    set flowobj ""
2041    foreach key [array names _obj2flow] {
2042        set flowobj $_obj2flow($key)
2043        break
2044    }
2045    if { $flowobj == "" } {
2046        return
2047    }
2048    if { [winfo exists $w.frame] } {
2049        destroy $w.frame
2050    }
2051    set inner [frame $w.frame]
2052    blt::table $w \
2053        5,0 $inner -fill both -cspan 2 -anchor nw
2054    array set hints [$dataobj hints]
2055
2056    label $inner.volumes -text "Volumes" -font "Arial 9 bold"
2057    blt::table $inner \
2058        1,0 $inner.volumes  -anchor w \
2059    blt::table configure $inner c0 c1 -resize none
2060    blt::table configure $inner c2 -resize expand
2061
2062    set row 3
2063    set volumes [get]
2064    if { [llength $volumes] > 0 } {
2065        blt::table $inner $row,0 $inner.volumes  -anchor w
2066        incr row
2067    }
2068    foreach vol $volumes {
2069        array unset info
2070        array set info $vol
2071        set name $info(name)
2072        if { ![info exists _settings($this-volume-$name)] } {
2073            set _settings($this-volume-$name) $info(hide)
2074        }
2075        checkbutton $inner.vol$row -text $info(label) \
2076            -variable [itcl::scope _settings($this-volume-$name)] \
2077            -onvalue 0 -offvalue 1 \
2078            -command [itcl::code $this volume $key $name] \
2079            -font "Arial 9"
2080        Rappture::Tooltip::for $inner.vol$row $info(description)
2081        blt::table $inner $row,0 $inner.vol$row -anchor w
2082        if { !$_settings($this-volume-$name) } {
2083            $inner.vol$row select
2084        }
2085        incr row
2086    }
2087    blt::table configure $inner r* -resize none
2088    blt::table configure $inner r$row -resize expand
2089    blt::table configure $inner c3 -resize expand
2090    event generate [winfo parent [winfo parent $w]] <Configure>
2091}
2092
2093itcl::body Rappture::NanovisViewer::volume { tag name } {
2094    set bool $_settings($this-volume-$name)
2095    SendCmd "volume statue $bool $name"
2096}
2097
2098itcl::body Rappture::NanovisViewer::SetOrientation { side } {
2099    array set positions {
2100        front "1 0 0 0"
2101        back  "0 0 1 0"
2102        left  "0.707107 0 -0.707107 0"
2103        right "0.707107 0 0.707107 0"
2104        top   "0.707107 -0.707107 0 0"
2105        bottom "0.707107 0.707107 0 0"
2106    }
2107    foreach name { qw qx qy qz } value $positions($side) {
2108        set _view($name) $value
2109    }
2110    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
2111    $_arcball quaternion $q
2112    SendCmd "camera orient $q"
2113    SendCmd "camera reset"
2114    set _view(xpan) 0
2115    set _view(ypan) 0
2116    set _view(zoom) 1.0
2117    set _settings($this-xpan) $_view(xpan)
2118    set _settings($this-ypan) $_view(ypan)
2119    set _settings($this-zoom) $_view(zoom)
2120}
2121
Note: See TracBrowser for help on using the repository browser.