source: trunk/gui/scripts/nanovisviewer.tcl @ 3330

Last change on this file since 3330 was 3330, checked in by gah, 12 years ago

merge (by hand) with Rappture1.2 branch

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