source: branches/blt4/gui/scripts/nanovisviewer.tcl @ 1923

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