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

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

Split out DrawLegend? method

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