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

Last change on this file since 3339 was 3339, checked in by gah, 11 years ago

fix nanovis Rebuild (too much in reset)

File size: 72.6 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 CurrentDatasets {{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 _serverDatasets   ;# 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 _recvdDatasets    ;# list of data objs to send to server
132    private variable _vol2style    ;# maps dataobj-component to transfunc
133    private variable _style2datasets   ;# 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 variable  _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 _serverDatasets
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 tag [CurrentDatasets] {
675        if { ![info exists _serverDatasets($tag)] || !$_serverDatasets($tag) } {
676            # The volume hasn't reached the server yet.  How did we get
677            # here?
678            continue
679        }
680        if { ![info exists _vol2style($tag)] } {
681            puts stderr "unknown volume $tag"
682            continue;                        # How does this happen?
683        }
684        set tf $_vol2style($tag)
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 $tag"
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 datasets [CurrentDatasets -cutplanes]
779    SendCmd "volume data state $_settings($this-volume) $datasets"
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 tag [lindex $datasets 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 $tag"
790            set pos [expr {0.01*$_settings($this-${axis}cutposition)}]
791            SendCmd "cutplane position $pos $axis $tag"
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 _recvdDatasets) 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 _serverDatasets($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 _recvdDatasets($tag)
841    if { [array size _recvdDatasets] == 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 cname [$dataobj components] {
878            set tag $dataobj-$cname
879            if { ![info exists _serverDatasets($tag)] } {
880                # Send the data as one huge base64-encoded mess -- yuck!
881                set data [$dataobj values $cname]
882                set nbytes [string length $data]
883                append _outbuf "volume data follows $nbytes $tag\n"
884                append _outbuf $data
885                set _recvdDatasets($tag) 1
886                set _serverDatasets($tag) 0
887            }
888            NameTransferFunc $dataobj $cname
889        }
890    }
891    set _first [lindex [get] 0]
892    if { $_reset } {
893        #
894        # Reset the camera and other view parameters
895        #
896       
897        set _settings($this-theta) $_view(theta)
898        set _settings($this-phi)   $_view(phi)
899        set _settings($this-psi)   $_view(psi)
900        set _settings($this-pan-x) $_view(pan-x)
901        set _settings($this-pan-y) $_view(pan-y)
902        set _settings($this-zoom)  $_view(zoom)
903       
904        set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
905        SendCmd "camera angle $xyz"
906        PanCamera
907        SendCmd "camera zoom $_view(zoom)"
908        FixSettings light
909        FixSettings transp
910        FixSettings isosurface
911        FixSettings grid
912        FixSettings axes
913        FixSettings outline
914       
915        foreach axis {x y z} {
916            # Turn off cutplanes for all volumes
917            SendCmd "cutplane state 0 $axis"
918        }
919        if {"" != $_first} {
920            set axis [$_first hints updir]
921            if { "" != $axis } {
922                SendCmd "up $axis"
923            }
924            set location [$_first hints camera]
925            if { $location != "" } {
926                array set _view $location
927            }
928            if 1 {
929                # Tell the server the name of the tool, the version, and
930                # dataset that we are rendering.  Have to do it here because
931                # we don't know what data objects are using the renderer until
932                # be get here.
933                global env
934
935                lappend out "hub" [exec hostname]
936                lappend out "viewer" "nanovisviewer"
937                if { [info exists env(USER)] } {
938                    lappend out "user" $env(USER)
939                }
940                if { [info exists env(SESSION)] } {
941                    lappend out "session" $env(SESSION)
942                }
943                lappend out "tool_id"      [$_first hints toolId]
944                lappend out "tool_name"    [$_first hints toolName]
945                lappend out "tool_version" [$_first hints toolRevision]
946                lappend out "tool_title"   [$_first hints toolTitle]
947                lappend out "tool_dataset" [$_first hints label]
948                SendCmd "clientinfo [list $out]"
949            }
950        }
951    }
952    # nothing to send -- activate the proper ivol
953    SendCmd "volume state 0"
954    if {"" != $_first} {
955        set datasets [array names _serverDatasets $_first-*]
956        if { $datasets != "" } {
957            SendCmd "volume state 1 $datasets"
958        }
959        # If the first volume already exists on the server, then make sure
960        # we display the proper transfer function in the legend.
961        set cname [lindex [$_first components] 0]
962        if { [info exists _serverDatasets($_first-$cname)] } {
963            updatetransferfuncs
964        }
965    }
966    set _buffering 0;                        # Turn off buffering.
967    # Actually write the commands to the server socket.  If it fails, we don't
968    # care.  We're finished here.
969    blt::busy hold $itk_component(hull)
970    SendBytes $_outbuf;                       
971    blt::busy release $itk_component(hull)
972    set _reset 0
973    set _outbuf "";                        # Clear the buffer.               
974}
975
976# ----------------------------------------------------------------------
977# USAGE: CurrentDatasets ?-cutplanes?
978#
979# Returns a list of volume server IDs for the current volume being
980# displayed.  This is normally a single ID, but it might be a list
981# of IDs if the current data object has multiple components.
982# ----------------------------------------------------------------------
983itcl::body Rappture::NanovisViewer::CurrentDatasets {{what -all}} {
984    set rlist ""
985    if { $_first == "" } {
986        return
987    }
988    foreach cname [$_first components] {
989        set tag $_first-$cname
990        if { [info exists _serverDatasets($tag)] && $_serverDatasets($tag) } {
991            array set style {
992                -cutplanes 1
993            }
994            array set style [lindex [$_first components -style $cname] 0]
995            if { $what != "-cutplanes" || $style(-cutplanes) } {
996                lappend rlist $tag
997            }
998        }
999    }
1000    return $rlist
1001}
1002
1003# ----------------------------------------------------------------------
1004# USAGE: Zoom in
1005# USAGE: Zoom out
1006# USAGE: Zoom reset
1007#
1008# Called automatically when the user clicks on one of the zoom
1009# controls for this widget.  Changes the zoom for the current view.
1010# ----------------------------------------------------------------------
1011itcl::body Rappture::NanovisViewer::Zoom {option} {
1012    switch -- $option {
1013        "in" {
1014            set _view(zoom) [expr {$_view(zoom)*1.25}]
1015            set _settings($this-zoom) $_view(zoom)
1016        }
1017        "out" {
1018            set _view(zoom) [expr {$_view(zoom)*0.8}]
1019            set _settings($this-zoom) $_view(zoom)
1020        }
1021        "reset" {
1022            array set _view {
1023                theta   45
1024                phi     45
1025                psi     0
1026                zoom    1.0
1027                pan-x   0
1028                pan-y   0
1029            }
1030            if { $_first != "" } {
1031                set location [$_first hints camera]
1032                if { $location != "" } {
1033                    array set _view $location
1034                }
1035            }
1036            set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
1037            SendCmd "camera angle $xyz"
1038            PanCamera
1039            set _settings($this-theta) $_view(theta)
1040            set _settings($this-phi)   $_view(phi)
1041            set _settings($this-psi)   $_view(psi)
1042            set _settings($this-pan-x) $_view(pan-x)
1043            set _settings($this-pan-y) $_view(pan-y)
1044            set _settings($this-zoom)  $_view(zoom)
1045        }
1046    }
1047    SendCmd "camera zoom $_view(zoom)"
1048}
1049
1050itcl::body Rappture::NanovisViewer::PanCamera {} {
1051    #set x [expr ($_view(pan-x)) / $_limits(xrange)]
1052    #set y [expr ($_view(pan-y)) / $_limits(yrange)]
1053    set x $_view(pan-x)
1054    set y $_view(pan-y)
1055    SendCmd "camera pan $x $y"
1056}
1057
1058
1059# ----------------------------------------------------------------------
1060# USAGE: Rotate click <x> <y>
1061# USAGE: Rotate drag <x> <y>
1062# USAGE: Rotate release <x> <y>
1063#
1064# Called automatically when the user clicks/drags/releases in the
1065# plot area.  Moves the plot according to the user's actions.
1066# ----------------------------------------------------------------------
1067itcl::body Rappture::NanovisViewer::Rotate {option x y} {
1068    switch -- $option {
1069        click {
1070            $itk_component(3dview) configure -cursor fleur
1071            set _click(x) $x
1072            set _click(y) $y
1073            set _click(theta) $_view(theta)
1074            set _click(phi) $_view(phi)
1075        }
1076        drag {
1077            if {[array size _click] == 0} {
1078                Rotate click $x $y
1079            } else {
1080                set w [winfo width $itk_component(3dview)]
1081                set h [winfo height $itk_component(3dview)]
1082                if {$w <= 0 || $h <= 0} {
1083                    return
1084                }
1085
1086                if {[catch {
1087                    # this fails sometimes for no apparent reason
1088                    set dx [expr {double($x-$_click(x))/$w}]
1089                    set dy [expr {double($y-$_click(y))/$h}]
1090                }]} {
1091                    return
1092                }
1093
1094                #
1095                # Rotate the camera in 3D
1096                #
1097                if {$_view(psi) > 90 || $_view(psi) < -90} {
1098                    # when psi is flipped around, theta moves backwards
1099                    set dy [expr {-$dy}]
1100                }
1101                set theta [expr {$_view(theta) - $dy*180}]
1102                while {$theta < 0} { set theta [expr {$theta+180}] }
1103                while {$theta > 180} { set theta [expr {$theta-180}] }
1104
1105                if {abs($theta) >= 30 && abs($theta) <= 160} {
1106                    set phi [expr {$_view(phi) - $dx*360}]
1107                    while {$phi < 0} { set phi [expr {$phi+360}] }
1108                    while {$phi > 360} { set phi [expr {$phi-360}] }
1109                    set psi $_view(psi)
1110                } else {
1111                    set phi $_view(phi)
1112                    set psi [expr {$_view(psi) - $dx*360}]
1113                    while {$psi < -180} { set psi [expr {$psi+360}] }
1114                    while {$psi > 180} { set psi [expr {$psi-360}] }
1115                }
1116
1117                set _view(theta)        $theta
1118                set _view(phi)          $phi
1119                set _view(psi)          $psi
1120                set xyz [Euler2XYZ $theta $phi $psi]
1121                set _settings($this-theta) $_view(theta)
1122                set _settings($this-phi)   $_view(phi)
1123                set _settings($this-psi)   $_view(psi)
1124                SendCmd "camera angle $xyz"
1125                set _click(x) $x
1126                set _click(y) $y
1127            }
1128        }
1129        release {
1130            Rotate drag $x $y
1131            $itk_component(3dview) configure -cursor ""
1132            catch {unset _click}
1133        }
1134        default {
1135            error "bad option \"$option\": should be click, drag, release"
1136        }
1137    }
1138}
1139
1140# ----------------------------------------------------------------------
1141# USAGE: $this Pan click x y
1142#        $this Pan drag x y
1143#        $this Pan release x y
1144#
1145# Called automatically when the user clicks on one of the zoom
1146# controls for this widget.  Changes the zoom for the current view.
1147# ----------------------------------------------------------------------
1148itcl::body Rappture::NanovisViewer::Pan {option x y} {
1149    # Experimental stuff
1150    set w [winfo width $itk_component(3dview)]
1151    set h [winfo height $itk_component(3dview)]
1152    if { $option == "set" } {
1153        set x [expr $x / double($w)]
1154        set y [expr $y / double($h)]
1155        set _view(pan-x) [expr $_view(pan-x) + $x]
1156        set _view(pan-y) [expr $_view(pan-y) + $y]
1157        PanCamera
1158        set _settings($this-pan-x) $_view(pan-x)
1159        set _settings($this-pan-y) $_view(pan-y)
1160        return
1161    }
1162    if { $option == "click" } {
1163        set _click(x) $x
1164        set _click(y) $y
1165        $itk_component(3dview) configure -cursor hand1
1166    }
1167    if { $option == "drag" || $option == "release" } {
1168        set dx [expr ($_click(x) - $x)/double($w)]
1169        set dy [expr ($_click(y) - $y)/double($h)]
1170        set _click(x) $x
1171        set _click(y) $y
1172        set _view(pan-x) [expr $_view(pan-x) - $dx]
1173        set _view(pan-y) [expr $_view(pan-y) - $dy]
1174        PanCamera
1175        set _settings($this-pan-x) $_view(pan-x)
1176        set _settings($this-pan-y) $_view(pan-y)
1177    }
1178    if { $option == "release" } {
1179        $itk_component(3dview) configure -cursor ""
1180    }
1181}
1182
1183# ----------------------------------------------------------------------
1184# USAGE: FixSettings <what> ?<value>?
1185#
1186# Used internally to update rendering settings whenever parameters
1187# change in the popup settings panel.  Sends the new settings off
1188# to the back end.
1189# ----------------------------------------------------------------------
1190itcl::body Rappture::NanovisViewer::FixSettings {what {value ""}} {
1191    switch -- $what {
1192        light {
1193            if {[isconnected]} {
1194                set val $_settings($this-light)
1195                set sval [expr {0.01*$val}]
1196                SendCmd "volume shading diffuse $sval"
1197                set sval [expr {sqrt($val+1.0)}]
1198                SendCmd "volume shading specular $sval"
1199            }
1200        }
1201        transp {
1202            if {[isconnected]} {
1203                set val $_settings($this-transp)
1204                set sval [expr {0.2*$val+1}]
1205                SendCmd "volume shading opacity $sval"
1206            }
1207        }
1208        opacity {
1209            if {[isconnected] && [array size _activeTfs] > 0 } {
1210                set val $_settings($this-opacity)
1211                set sval [expr { 0.01 * double($val) }]
1212                foreach tf [array names _activeTfs] {
1213                    set _settings($this-$tf-opacity) $sval
1214                    set _activeTfs($tf) 0
1215                }
1216                updatetransferfuncs
1217            }
1218        }
1219
1220        thickness {
1221            if {[isconnected] && [array names _activeTfs] > 0 } {
1222                set val $_settings($this-thickness)
1223                # Scale values between 0.00001 and 0.01000
1224                set sval [expr {0.0001*double($val)}]
1225                foreach tf [array names _activeTfs] {
1226                    set _settings($this-$tf-thickness) $sval
1227                    set _activeTfs($tf) 0
1228                }
1229                updatetransferfuncs
1230            }
1231        }
1232        "outline" {
1233            if {[isconnected]} {
1234                SendCmd "volume outline state $_settings($this-outline)"
1235            }
1236        }
1237        "isosurface" {
1238            if {[isconnected]} {
1239                SendCmd "volume shading isosurface $_settings($this-isosurface)"
1240            }
1241        }
1242        "grid" {
1243            if { [isconnected] } {
1244                SendCmd "grid visible $_settings($this-grid)"
1245            }
1246        }
1247        "axes" {
1248            if { [isconnected] } {
1249                SendCmd "axis visible $_settings($this-axes)"
1250            }
1251        }
1252        "legend" {
1253            if { $_settings($this-legend) } {
1254                blt::table $itk_component(plotarea) \
1255                    0,0 $itk_component(3dview) -fill both \
1256                    1,0 $itk_component(legend) -fill x
1257                blt::table configure $itk_component(plotarea) r1 -resize none
1258            } else {
1259                blt::table forget $itk_component(legend)
1260            }
1261        }
1262        "volume" {
1263            if { [isconnected] } {
1264                set datasets [CurrentDatasets -cutplanes]
1265                SendCmd "volume data state $_settings($this-volume) $datasets"
1266            }
1267        }
1268        "xcutplane" - "ycutplane" - "zcutplane" {
1269            set axis [string range $what 0 0]
1270            set bool $_settings($this-$what)
1271            if { [isconnected] } {
1272                set datasets [CurrentDatasets -cutplanes]
1273                set tag [lindex $datasets 0]
1274                SendCmd "cutplane state $bool $axis $tag"
1275            }
1276            if { $bool } {
1277                $itk_component(${axis}CutScale) configure -state normal \
1278                    -troughcolor white
1279            } else {
1280                $itk_component(${axis}CutScale) configure -state disabled \
1281                    -troughcolor grey82
1282            }
1283        }
1284        default {
1285            error "don't know how to fix $what"
1286        }
1287    }
1288}
1289
1290# ----------------------------------------------------------------------
1291# USAGE: FixLegend
1292#
1293# Used internally to update the legend area whenever it changes size
1294# or when the field changes.  Asks the server to send a new legend
1295# for the current field.
1296# ----------------------------------------------------------------------
1297itcl::body Rappture::NanovisViewer::FixLegend {} {
1298    set _resizeLegendPending 0
1299    set lineht [font metrics $itk_option(-font) -linespace]
1300    set w [expr {$_width-20}]
1301    set h [expr {[winfo height $itk_component(legend)]-20-$lineht}]
1302    if {$w > 0 && $h > 0 && [array names _activeTfs] > 0 && $_first != "" } {
1303        set tag [lindex [CurrentDatasets] 0]
1304        if { [info exists _vol2style($tag)] } {
1305            SendCmd "legend $_vol2style($tag) $w $h"
1306        }
1307    } else {
1308        # Can't do this as this will remove the items associated with the
1309        # isomarkers.
1310       
1311        #$itk_component(legend) delete all
1312    }
1313}
1314
1315#
1316# NameTransferFunc --
1317#
1318#       Creates a transfer function name based on the <style> settings in the
1319#       library run.xml file. This placeholder will be used later to create
1320#       and send the actual transfer function once the data info has been sent
1321#       to us by the render server. [We won't know the volume limits until the
1322#       server parses the 3D data and sends back the limits via ReceiveData.]
1323#
1324#       FIXME: The current way we generate transfer-function names completely
1325#              ignores the -markers option.  The problem is that we are forced
1326#              to compute the name from an increasing complex set of values:
1327#              color, levels, marker, opacity.  I think we're stuck doing it
1328#              now.
1329#
1330itcl::body Rappture::NanovisViewer::NameTransferFunc { dataobj cname } {
1331    array set style {
1332        -color rainbow
1333        -levels 6
1334        -opacity 1.0
1335    }
1336    set tag $dataobj-$cname
1337    array set style [lindex [$dataobj components -style $cname] 0]
1338    set tf "$style(-color):$style(-levels):$style(-opacity)"
1339    set _vol2style($tag) $tf
1340    lappend _style2datasets($tf) $tag
1341    return $tf
1342}
1343
1344#
1345# ComputeTransferFunc --
1346#
1347#   Computes and sends the transfer function to the render server.  It's
1348#   assumed that the volume data limits are known and that the global
1349#   transfer-functions slider values have be setup.  Both parts are
1350#   needed to compute the relative value (location) of the marker, and
1351#   the alpha map of the transfer function.
1352#
1353itcl::body Rappture::NanovisViewer::ComputeTransferFunc { tf } {
1354    array set style {
1355        -color rainbow
1356        -levels 6
1357        -opacity 1.0
1358    }
1359    foreach {dataobj cname} [split [lindex $_style2datasets($tf) 0] -] break
1360    array set style [lindex [$dataobj components -style $cname] 0]
1361
1362
1363    # We have to parse the style attributes for a volume using this
1364    # transfer-function *once*.  This sets up the initial isomarkers for the
1365    # transfer function.  The user may add/delete markers, so we have to
1366    # maintain a list of markers for each transfer-function.  We use the one
1367    # of the volumes (the first in the list) using the transfer-function as a
1368    # reference.
1369    #
1370    # FIXME: The current way we generate transfer-function names completely
1371    #        ignores the -markers option.  The problem is that we are forced
1372    #        to compute the name from an increasing complex set of values:
1373    #        color, levels, marker, opacity.  I think the cow's out of the
1374    #        barn on this one.
1375
1376    if { ![info exists _isomarkers($tf)] } {
1377        # Have to defer creation of isomarkers until we have data limits
1378        if { [info exists style(-markers)] } {
1379            ParseMarkersOption $tf $style(-markers)
1380        } else {
1381            ParseLevelsOption $tf $style(-levels)
1382        }
1383    }
1384    if {$style(-color) == "rainbow"} {
1385        set style(-color) "white:yellow:green:cyan:blue:magenta"
1386    }
1387    set clist [split $style(-color) :]
1388    set cmap "0.0 [Color2RGB white] "
1389    for {set i 0} {$i < [llength $clist]} {incr i} {
1390        set x [expr {double($i+1)/([llength $clist]+1)}]
1391        set color [lindex $clist $i]
1392        append cmap "$x [Color2RGB $color] "
1393    }
1394    append cmap "1.0 [Color2RGB $color]"
1395
1396    set tag $this-$tf
1397    if { ![info exists _settings($tag-opacity)] } {
1398        set _settings($tag-opacity) $style(-opacity)
1399    }
1400    set max $_settings($tag-opacity)
1401
1402    set isovalues {}
1403    foreach m $_isomarkers($tf) {
1404        lappend isovalues [$m relval]
1405    }
1406    # Sort the isovalues
1407    set isovalues [lsort -real $isovalues]
1408
1409    if { ![info exists _settings($tag-thickness)]} {
1410        set _settings($tag-thickness) 0.05
1411    }
1412    set delta $_settings($tag-thickness)
1413
1414    set first [lindex $isovalues 0]
1415    set last [lindex $isovalues end]
1416    set wmap ""
1417    if { $first == "" || $first != 0.0 } {
1418        lappend wmap 0.0 0.0
1419    }
1420    foreach x $isovalues {
1421        set x1 [expr {$x-$delta-0.00001}]
1422        set x2 [expr {$x-$delta}]
1423        set x3 [expr {$x+$delta}]
1424        set x4 [expr {$x+$delta+0.00001}]
1425        if { $x1 < 0.0 } {
1426            set x1 0.0
1427        } elseif { $x1 > 1.0 } {
1428            set x1 1.0
1429        }
1430        if { $x2 < 0.0 } {
1431            set x2 0.0
1432        } elseif { $x2 > 1.0 } {
1433            set x2 1.0
1434        }
1435        if { $x3 < 0.0 } {
1436            set x3 0.0
1437        } elseif { $x3 > 1.0 } {
1438            set x3 1.0
1439        }
1440        if { $x4 < 0.0 } {
1441            set x4 0.0
1442        } elseif { $x4 > 1.0 } {
1443            set x4 1.0
1444        }
1445        # add spikes in the middle
1446        lappend wmap $x1 0.0
1447        lappend wmap $x2 $max
1448        lappend wmap $x3 $max
1449        lappend wmap $x4 0.0
1450    }
1451    if { $last == "" || $last != 1.0 } {
1452        lappend wmap 1.0 0.0
1453    }
1454    SendCmd "transfunc define $tf { $cmap } { $wmap }"
1455}
1456
1457# ----------------------------------------------------------------------
1458# CONFIGURATION OPTION: -plotbackground
1459# ----------------------------------------------------------------------
1460itcl::configbody Rappture::NanovisViewer::plotbackground {
1461    if { [isconnected] } {
1462        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1463        #fix this!
1464        #SendCmd "color background $r $g $b"
1465    }
1466}
1467
1468# ----------------------------------------------------------------------
1469# CONFIGURATION OPTION: -plotforeground
1470# ----------------------------------------------------------------------
1471itcl::configbody Rappture::NanovisViewer::plotforeground {
1472    if { [isconnected] } {
1473        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1474        #fix this!
1475        #SendCmd "color background $r $g $b"
1476    }
1477}
1478
1479# ----------------------------------------------------------------------
1480# CONFIGURATION OPTION: -plotoutline
1481# ----------------------------------------------------------------------
1482itcl::configbody Rappture::NanovisViewer::plotoutline {
1483    # Must check if we are connected because this routine is called from the
1484    # class body when the -plotoutline itk_option is defined.  At that point
1485    # the NanovisViewer class constructor hasn't been called, so we can't
1486    # start sending commands to visualization server.
1487    if { [isconnected] } {
1488        if {"" == $itk_option(-plotoutline)} {
1489            SendCmd "volume outline state off"
1490        } else {
1491            SendCmd "volume outline state on"
1492            SendCmd "volume outline color [Color2RGB $itk_option(-plotoutline)]"
1493        }
1494    }
1495}
1496
1497#
1498# The -levels option takes a single value that represents the number
1499# of evenly distributed markers based on the current data range. Each
1500# marker is a relative value from 0.0 to 1.0.
1501#
1502itcl::body Rappture::NanovisViewer::ParseLevelsOption { tf levels } {
1503    set c $itk_component(legend)
1504    regsub -all "," $levels " " levels
1505    if {[string is int $levels]} {
1506        for {set i 1} { $i <= $levels } {incr i} {
1507            set x [expr {double($i)/($levels+1)}]
1508            set m [Rappture::IsoMarker \#auto $c $this $tf]
1509            $m relval $x
1510            lappend _isomarkers($tf) $m
1511        }
1512    } else {
1513        foreach x $levels {
1514            set m [Rappture::IsoMarker \#auto $c $this $tf]
1515            $m relval $x
1516            lappend _isomarkers($tf) $m
1517        }
1518    }
1519}
1520
1521#
1522# The -markers option takes a list of zero or more values (the values
1523# may be separated either by spaces or commas) that have the following
1524# format:
1525#
1526#   N%  Percent of current total data range.  Converted to
1527#       to a relative value between 0.0 and 1.0.
1528#   N   Absolute value of marker.  If the marker is outside of
1529#       the current range, it will be displayed on the outer
1530#       edge of the legends, but it range it represents will
1531#       not be seen.
1532#
1533itcl::body Rappture::NanovisViewer::ParseMarkersOption { tf markers } {
1534    set c $itk_component(legend)
1535    regsub -all "," $markers " " markers
1536    foreach marker $markers {
1537        set n [scan $marker "%g%s" value suffix]
1538        if { $n == 2 && $suffix == "%" } {
1539            # ${n}% : Set relative value.
1540            set value [expr {$value * 0.01}]
1541            set m [Rappture::IsoMarker \#auto $c $this $tf]
1542            $m relval $value
1543            lappend _isomarkers($tf) $m
1544        } else {
1545            # ${n} : Set absolute value.
1546            set m [Rappture::IsoMarker \#auto $c $this $tf]
1547            $m absval $value
1548            lappend _isomarkers($tf) $m
1549        }
1550    }
1551}
1552
1553# ----------------------------------------------------------------------
1554# USAGE: UndateTransferFuncs
1555# ----------------------------------------------------------------------
1556itcl::body Rappture::NanovisViewer::updatetransferfuncs {} {
1557    $_dispatcher event -idle !send_transfunc
1558}
1559
1560itcl::body Rappture::NanovisViewer::AddIsoMarker { x y } {
1561    if { $_first == "" } {
1562        error "active transfer function isn't set"
1563    }
1564    set tag [lindex [CurrentDatasets] 0]
1565    set tf $_vol2style($tag)
1566    set c $itk_component(legend)
1567    set m [Rappture::IsoMarker \#auto $c $this $tf]
1568    set w [winfo width $c]
1569    $m relval [expr {double($x-10)/($w-20)}]
1570    lappend _isomarkers($tf) $m
1571    updatetransferfuncs
1572    return 1
1573}
1574
1575itcl::body Rappture::NanovisViewer::rmdupmarker { marker x } {
1576    set tf [$marker transferfunc]
1577    set bool 0
1578    if { [info exists _isomarkers($tf)] } {
1579        set list {}
1580        set marker [namespace tail $marker]
1581        foreach m $_isomarkers($tf) {
1582            set sx [$m screenpos]
1583            if { $m != $marker } {
1584                if { $x >= ($sx-3) && $x <= ($sx+3) } {
1585                    $marker relval [$m relval]
1586                    itcl::delete object $m
1587                    bell
1588                    set bool 1
1589                    continue
1590                }
1591            }
1592            lappend list $m
1593        }
1594        set _isomarkers($tf) $list
1595        updatetransferfuncs
1596    }
1597    return $bool
1598}
1599
1600itcl::body Rappture::NanovisViewer::overmarker { marker x } {
1601    set tf [$marker transferfunc]
1602    if { [info exists _isomarkers($tf)] } {
1603        set marker [namespace tail $marker]
1604        foreach m $_isomarkers($tf) {
1605            set sx [$m screenpos]
1606            if { $m != $marker } {
1607                set bool [expr { $x >= ($sx-3) && $x <= ($sx+3) }]
1608                $m activate $bool
1609            }
1610        }
1611    }
1612    return ""
1613}
1614
1615itcl::body Rappture::NanovisViewer::limits { tf } {
1616    set _limits(min) 0.0
1617    set _limits(max) 1.0
1618    if { ![info exists _style2datasets($tf)] } {
1619        return [array get _limits]
1620    }
1621    set min ""; set max ""
1622    foreach tag $_style2datasets($tf) {
1623        if { ![info exists _serverDatasets($tag)] } {
1624            continue
1625        }
1626        if { ![info exists _limits($tag-min)] } {
1627            continue
1628        }
1629        if { $min == "" || $min > $_limits($tag-min) } {
1630            set min $_limits($tag-min)
1631        }
1632        if { $max == "" || $max < $_limits($tag-max) } {
1633            set max $_limits($tag-max)
1634        }
1635    }
1636    if { $min != "" } {
1637        set _limits(min) $min
1638    }
1639    if { $max != "" } {
1640        set _limits(max) $max
1641    }
1642    return [array get _limits]
1643}
1644
1645
1646itcl::body Rappture::NanovisViewer::BuildViewTab {} {
1647    foreach { key value } {
1648        grid            0
1649        axes            1
1650        outline         1
1651        volume          1
1652        legend          1
1653        particles       1
1654        lic             1
1655    } {
1656        set _settings($this-$key) $value
1657    }
1658
1659    set fg [option get $itk_component(hull) font Font]
1660    #set bfg [option get $itk_component(hull) boldFont Font]
1661
1662    set inner [$itk_component(main) insert end \
1663        -title "View Settings" \
1664        -icon [Rappture::icon wrench]]
1665    $inner configure -borderwidth 4
1666
1667    set ::Rappture::NanovisViewer::_settings($this-isosurface) 0
1668    checkbutton $inner.isosurface \
1669        -text "Isosurface shading" \
1670        -variable [itcl::scope _settings($this-isosurface)] \
1671        -command [itcl::code $this FixSettings isosurface] \
1672        -font "Arial 9"
1673
1674    checkbutton $inner.axes \
1675        -text "Axes" \
1676        -variable [itcl::scope _settings($this-axes)] \
1677        -command [itcl::code $this FixSettings axes] \
1678        -font "Arial 9"
1679
1680    checkbutton $inner.grid \
1681        -text "Grid" \
1682        -variable [itcl::scope _settings($this-grid)] \
1683        -command [itcl::code $this FixSettings grid] \
1684        -font "Arial 9"
1685
1686    checkbutton $inner.outline \
1687        -text "Outline" \
1688        -variable [itcl::scope _settings($this-outline)] \
1689        -command [itcl::code $this FixSettings outline] \
1690        -font "Arial 9"
1691
1692    checkbutton $inner.legend \
1693        -text "Legend" \
1694        -variable [itcl::scope _settings($this-legend)] \
1695        -command [itcl::code $this FixSettings legend] \
1696        -font "Arial 9"
1697
1698    checkbutton $inner.volume \
1699        -text "Volume" \
1700        -variable [itcl::scope _settings($this-volume)] \
1701        -command [itcl::code $this FixSettings volume] \
1702        -font "Arial 9"
1703
1704    blt::table $inner \
1705        0,0 $inner.axes  -columnspan 2 -anchor w \
1706        1,0 $inner.grid  -columnspan 2 -anchor w \
1707        2,0 $inner.outline  -columnspan 2 -anchor w \
1708        3,0 $inner.volume  -columnspan 2 -anchor w \
1709        4,0 $inner.legend  -columnspan 2 -anchor w
1710
1711    if 0 {
1712    bind $inner <Map> [itcl::code $this GetVolumeInfo $inner]
1713    }
1714    blt::table configure $inner r* -resize none
1715    blt::table configure $inner r5 -resize expand
1716}
1717
1718itcl::body Rappture::NanovisViewer::BuildVolumeTab {} {
1719    foreach { key value } {
1720        light           40
1721        transp          50
1722        opacity         100
1723        thickness       350
1724    } {
1725        set _settings($this-$key) $value
1726    }
1727
1728    set inner [$itk_component(main) insert end \
1729        -title "Volume Settings" \
1730        -icon [Rappture::icon volume-on]]
1731    $inner configure -borderwidth 4
1732
1733    set fg [option get $itk_component(hull) font Font]
1734    #set bfg [option get $itk_component(hull) boldFont Font]
1735
1736    checkbutton $inner.vol -text "Show volume" -font $fg \
1737        -variable [itcl::scope _settings($this-volume)] \
1738        -command [itcl::code $this FixSettings volume]
1739    label $inner.shading -text "Shading:" -font $fg
1740
1741    label $inner.dim -text "Dim" -font $fg
1742    ::scale $inner.light -from 0 -to 100 -orient horizontal \
1743        -variable [itcl::scope _settings($this-light)] \
1744        -width 10 \
1745        -showvalue off -command [itcl::code $this FixSettings light]
1746    label $inner.bright -text "Bright" -font $fg
1747
1748    label $inner.fog -text "Fog" -font $fg
1749    ::scale $inner.transp -from 0 -to 100 -orient horizontal \
1750        -variable [itcl::scope _settings($this-transp)] \
1751        -width 10 \
1752        -showvalue off -command [itcl::code $this FixSettings transp]
1753    label $inner.plastic -text "Plastic" -font $fg
1754
1755    label $inner.clear -text "Clear" -font $fg
1756    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1757        -variable [itcl::scope _settings($this-opacity)] \
1758        -width 10 \
1759        -showvalue off -command [itcl::code $this FixSettings opacity]
1760    label $inner.opaque -text "Opaque" -font $fg
1761
1762    label $inner.thin -text "Thin" -font $fg
1763    ::scale $inner.thickness -from 0 -to 1000 -orient horizontal \
1764        -variable [itcl::scope _settings($this-thickness)] \
1765        -width 10 \
1766        -showvalue off -command [itcl::code $this FixSettings thickness]
1767    label $inner.thick -text "Thick" -font $fg
1768
1769    blt::table $inner \
1770        0,0 $inner.vol -columnspan 4 -anchor w -pady 2 \
1771        1,0 $inner.shading -columnspan 4 -anchor w -pady {10 2} \
1772        2,0 $inner.dim -anchor e -pady 2 \
1773        2,1 $inner.light -columnspan 2 -pady 2 -fill x \
1774        2,3 $inner.bright -anchor w -pady 2 \
1775        3,0 $inner.fog -anchor e -pady 2 \
1776        3,1 $inner.transp -columnspan 2 -pady 2 -fill x \
1777        3,3 $inner.plastic -anchor w -pady 2 \
1778        4,0 $inner.clear -anchor e -pady 2 \
1779        4,1 $inner.opacity -columnspan 2 -pady 2 -fill x\
1780        4,3 $inner.opaque -anchor w -pady 2 \
1781        5,0 $inner.thin -anchor e -pady 2 \
1782        5,1 $inner.thickness -columnspan 2 -pady 2 -fill x\
1783        5,3 $inner.thick -anchor w -pady 2
1784
1785    blt::table configure $inner c0 c1 c3 r* -resize none
1786    blt::table configure $inner r6 -resize expand
1787}
1788
1789itcl::body Rappture::NanovisViewer::BuildCutplanesTab {} {
1790    set inner [$itk_component(main) insert end \
1791        -title "Cutplane Settings" \
1792        -icon [Rappture::icon cutbutton]]
1793    $inner configure -borderwidth 4
1794
1795    # X-value slicer...
1796    itk_component add xCutButton {
1797        Rappture::PushButton $inner.xbutton \
1798            -onimage [Rappture::icon x-cutplane] \
1799            -offimage [Rappture::icon x-cutplane] \
1800            -command [itcl::code $this FixSettings xcutplane] \
1801            -variable [itcl::scope _settings($this-xcutplane)]
1802    }
1803    Rappture::Tooltip::for $itk_component(xCutButton) \
1804        "Toggle the X cut plane on/off"
1805
1806    itk_component add xCutScale {
1807        ::scale $inner.xval -from 100 -to 0 \
1808            -width 10 -orient vertical -showvalue off \
1809            -borderwidth 1 -highlightthickness 0 \
1810            -command [itcl::code $this Slice move x] \
1811            -variable [itcl::scope _settings($this-xcutposition)]
1812    } {
1813        usual
1814        ignore -borderwidth -highlightthickness
1815    }
1816    # Set the default cutplane value before disabling the scale.
1817    $itk_component(xCutScale) set 50
1818    $itk_component(xCutScale) configure -state disabled
1819    Rappture::Tooltip::for $itk_component(xCutScale) \
1820        "@[itcl::code $this SlicerTip x]"
1821
1822    # Y-value slicer...
1823    itk_component add yCutButton {
1824        Rappture::PushButton $inner.ybutton \
1825            -onimage [Rappture::icon y-cutplane] \
1826            -offimage [Rappture::icon y-cutplane] \
1827            -command [itcl::code $this FixSettings ycutplane] \
1828            -variable [itcl::scope _settings($this-ycutplane)]
1829    }
1830    Rappture::Tooltip::for $itk_component(yCutButton) \
1831        "Toggle the Y cut plane on/off"
1832
1833    itk_component add yCutScale {
1834        ::scale $inner.yval -from 100 -to 0 \
1835            -width 10 -orient vertical -showvalue off \
1836            -borderwidth 1 -highlightthickness 0 \
1837            -command [itcl::code $this Slice move y] \
1838            -variable [itcl::scope _settings($this-ycutposition)]
1839    } {
1840        usual
1841        ignore -borderwidth -highlightthickness
1842    }
1843    Rappture::Tooltip::for $itk_component(yCutScale) \
1844        "@[itcl::code $this SlicerTip y]"
1845    # Set the default cutplane value before disabling the scale.
1846    $itk_component(yCutScale) set 50
1847    $itk_component(yCutScale) configure -state disabled
1848
1849    # Z-value slicer...
1850    itk_component add zCutButton {
1851        Rappture::PushButton $inner.zbutton \
1852            -onimage [Rappture::icon z-cutplane] \
1853            -offimage [Rappture::icon z-cutplane] \
1854            -command [itcl::code $this FixSettings zcutplane] \
1855            -variable [itcl::scope _settings($this-zcutplane)]
1856    }
1857    Rappture::Tooltip::for $itk_component(zCutButton) \
1858        "Toggle the Z cut plane on/off"
1859
1860    itk_component add zCutScale {
1861        ::scale $inner.zval -from 100 -to 0 \
1862            -width 10 -orient vertical -showvalue off \
1863            -borderwidth 1 -highlightthickness 0 \
1864            -command [itcl::code $this Slice move z] \
1865            -variable [itcl::scope _settings($this-zcutposition)]
1866    } {
1867        usual
1868        ignore -borderwidth -highlightthickness
1869    }
1870    $itk_component(zCutScale) set 50
1871    $itk_component(zCutScale) configure -state disabled
1872    #$itk_component(zCutScale) configure -state disabled
1873    Rappture::Tooltip::for $itk_component(zCutScale) \
1874        "@[itcl::code $this SlicerTip z]"
1875
1876    blt::table $inner \
1877        1,1 $itk_component(xCutButton) \
1878        1,2 $itk_component(yCutButton) \
1879        1,3 $itk_component(zCutButton) \
1880        0,1 $itk_component(xCutScale) \
1881        0,2 $itk_component(yCutScale) \
1882        0,3 $itk_component(zCutScale)
1883
1884    blt::table configure $inner r0 r1 c* -resize none
1885    blt::table configure $inner r2 c4 -resize expand
1886    blt::table configure $inner c0 -width 2
1887    blt::table configure $inner c1 c2 c3 -padx 2
1888}
1889
1890itcl::body Rappture::NanovisViewer::BuildCameraTab {} {
1891    set inner [$itk_component(main) insert end \
1892        -title "Camera Settings" \
1893        -icon [Rappture::icon camera]]
1894    $inner configure -borderwidth 4
1895
1896    set labels { phi theta psi pan-x pan-y zoom }
1897    set row 0
1898    foreach tag $labels {
1899        label $inner.${tag}label -text $tag -font "Arial 9"
1900        entry $inner.${tag} -font "Arial 9"  -bg white \
1901            -textvariable [itcl::scope _settings($this-$tag)]
1902        bind $inner.${tag} <KeyPress-Return> \
1903            [itcl::code $this camera set ${tag}]
1904        blt::table $inner \
1905            $row,0 $inner.${tag}label -anchor e -pady 2 \
1906            $row,1 $inner.${tag} -anchor w -pady 2
1907        blt::table configure $inner r$row -resize none
1908        incr row
1909    }
1910
1911    itk_component add orientation {
1912        Rappture::Combobox $inner.orientation -width 10 -editable no
1913    }
1914    $inner.orientation choices insert end \
1915        "front"    "0 90 0" \
1916        "back"   "180 90 0" \
1917        "top"     "0 0 0"  \
1918        "bottom"     "0 180 0"  \
1919        "left"     "270 90 0 "  \
1920        "right"     "90 90 0"  \
1921        "default"  "45 45 0"
1922    $itk_component(orientation) value "default"
1923    bind $inner.orientation <<Value>> [itcl::code $this SetOrientation]
1924
1925    blt::table configure $inner c0 c1 -resize none
1926    blt::table configure $inner c2 -resize expand
1927    blt::table configure $inner r$row -resize expand
1928}
1929
1930
1931# ----------------------------------------------------------------------
1932# USAGE: Slice move x|y|z <newval>
1933#
1934# Called automatically when the user drags the slider to move the
1935# cut plane that slices 3D data.  Gets the current value from the
1936# slider and moves the cut plane to the appropriate point in the
1937# data set.
1938# ----------------------------------------------------------------------
1939itcl::body Rappture::NanovisViewer::Slice {option args} {
1940    switch -- $option {
1941        move {
1942            if {[llength $args] != 2} {
1943                error "wrong # args: should be \"Slice move x|y|z newval\""
1944            }
1945            set axis [lindex $args 0]
1946            set newval [lindex $args 1]
1947
1948            set newpos [expr {0.01*$newval}]
1949            set datasets [CurrentDatasets -cutplanes]
1950            set tag [lindex $datasets 0]
1951            SendCmd "cutplane position $newpos $axis $tag"
1952        }
1953        default {
1954            error "bad option \"$option\": should be axis, move, or volume"
1955        }
1956    }
1957}
1958
1959# ----------------------------------------------------------------------
1960# USAGE: SlicerTip <axis>
1961#
1962# Used internally to generate a tooltip for the x/y/z slicer controls.
1963# Returns a message that includes the current slicer value.
1964# ----------------------------------------------------------------------
1965itcl::body Rappture::NanovisViewer::SlicerTip {axis} {
1966    set val [$itk_component(${axis}CutScale) get]
1967#    set val [expr {0.01*($val-50)
1968#        *($_limits(${axis}max)-$_limits(${axis}min))
1969#          + 0.5*($_limits(${axis}max)+$_limits(${axis}min))}]
1970    return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
1971}
1972
1973
1974itcl::body Rappture::NanovisViewer::DoResize {} {
1975    SendCmd "screen $_width $_height"
1976    set _resizePending 0
1977}
1978
1979itcl::body Rappture::NanovisViewer::EventuallyResize { w h } {
1980    set _width $w
1981    set _height $h
1982    if { !$_resizePending } {
1983        $_dispatcher event -idle !resize
1984        set _resizePending 1
1985    }
1986}
1987
1988itcl::body Rappture::NanovisViewer::EventuallyResizeLegend {} {
1989    if { !$_resizeLegendPending } {
1990        $_dispatcher event -idle !legend
1991        set _resizeLegendPending 1
1992    }
1993}
1994
1995
1996#  camera --
1997#
1998itcl::body Rappture::NanovisViewer::camera {option args} {
1999    switch -- $option {
2000        "show" {
2001            puts [array get _view]
2002        }
2003        "set" {
2004            set who [lindex $args 0]
2005            set x $_settings($this-$who)
2006            set code [catch { string is double $x } result]
2007            if { $code != 0 || !$result } {
2008                set _settings($this-$who) $_view($who)
2009                return
2010            }
2011            switch -- $who {
2012                "pan-x" - "pan-y" {
2013                    set _view($who) $_settings($this-$who)
2014                    PanCamera
2015                }
2016                "phi" - "theta" - "psi" {
2017                    set _view($who) $_settings($this-$who)
2018                    set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
2019                    SendCmd "camera angle $xyz"
2020                }
2021                "zoom" {
2022                    set _view($who) $_settings($this-$who)
2023                    SendCmd "camera zoom $_view(zoom)"
2024                }
2025            }
2026        }
2027    }
2028}
2029
2030itcl::body Rappture::NanovisViewer::GetVolumeInfo { w } {
2031    set flowobj ""
2032    foreach key [array names _obj2flow] {
2033        set flowobj $_obj2flow($key)
2034        break
2035    }
2036    if { $flowobj == "" } {
2037        return
2038    }
2039    if { [winfo exists $w.frame] } {
2040        destroy $w.frame
2041    }
2042    set inner [frame $w.frame]
2043    blt::table $w \
2044        5,0 $inner -fill both -columnspan 2 -anchor nw
2045    array set hints [$dataobj hints]
2046
2047    label $inner.volumes -text "Volumes" -font "Arial 9 bold"
2048    blt::table $inner \
2049        1,0 $inner.volumes  -anchor w \
2050    blt::table configure $inner c0 c1 -resize none
2051    blt::table configure $inner c2 -resize expand
2052
2053    set row 3
2054    set volumes [get]
2055    if { [llength $volumes] > 0 } {
2056        blt::table $inner $row,0 $inner.volumes  -anchor w
2057        incr row
2058    }
2059    foreach vol $volumes {
2060        array unset info
2061        array set info $vol
2062        set name $info(name)
2063        if { ![info exists _settings($this-volume-$name)] } {
2064            set _settings($this-volume-$name) $info(hide)
2065        }
2066        checkbutton $inner.vol$row -text $info(label) \
2067            -variable [itcl::scope _settings($this-volume-$name)] \
2068            -onvalue 0 -offvalue 1 \
2069            -command [itcl::code $this volume $key $name] \
2070            -font "Arial 9"
2071        Rappture::Tooltip::for $inner.vol$row $info(description)
2072        blt::table $inner $row,0 $inner.vol$row -anchor w
2073        if { !$_settings($this-volume-$name) } {
2074            $inner.vol$row select
2075        }
2076        incr row
2077    }
2078    blt::table configure $inner r* -resize none
2079    blt::table configure $inner r$row -resize expand
2080    blt::table configure $inner c3 -resize expand
2081    event generate [winfo parent [winfo parent $w]] <Configure>
2082}
2083
2084itcl::body Rappture::NanovisViewer::volume { tag name } {
2085    set bool $_settings($this-volume-$name)
2086    SendCmd "volume statue $bool $name"
2087}
2088
2089
2090itcl::body Rappture::NanovisViewer::SetOrientation {} {
2091    set angles [$itk_component(orientation) value]
2092    foreach name { theta phi psi } angle $angles {
2093        set _view($name) $angle
2094    }
2095    set xyz [Euler2XYZ $_view(theta) $_view(phi) $_view(psi)]
2096    SendCmd "camera angle $xyz"
2097}
Note: See TracBrowser for help on using the repository browser.