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

Last change on this file since 2048 was 2048, checked in by gah, 14 years ago

tool.xml

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