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

Last change on this file since 3899 was 3899, checked in by ldelgass, 8 years ago

Add cutplane visibility button to nanovis viewers.

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