source: branches/1.6/gui/scripts/vtkviewer.tcl @ 6363

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

merge viewer fixes from trunk

File size: 109.2 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: vtkviewer - Vtk drawing object viewer
4#
5#  It connects to the Vtkvis server running on a rendering farm,
6#  transmits data, and displays the results.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2016  HUBzero Foundation, LLC
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15package require BLT
16#package require Img
17
18option add *VtkViewer.width 4i widgetDefault
19option add *VtkViewer*cursor crosshair widgetDefault
20option add *VtkViewer.height 4i widgetDefault
21option add *VtkViewer.foreground black widgetDefault
22option add *VtkViewer.controlBackground gray widgetDefault
23option add *VtkViewer.controlDarkBackground #999999 widgetDefault
24option add *VtkViewer.plotBackground black widgetDefault
25option add *VtkViewer.plotForeground white widgetDefault
26option add *VtkViewer.font \
27    -*-helvetica-medium-r-normal-*-12-* widgetDefault
28
29# must use this name -- plugs into Rappture::resources::load
30proc VtkViewer_init_resources {} {
31    Rappture::resources::register \
32        vtkvis_server Rappture::VtkViewer::SetServerList
33}
34
35itcl::class Rappture::VtkViewer {
36    inherit Rappture::VisViewer
37
38    itk_option define -plotforeground plotForeground Foreground ""
39    itk_option define -plotbackground plotBackground Background ""
40
41    constructor { args } {
42        Rappture::VisViewer::constructor
43    } {
44        # defined below
45    }
46    destructor {
47        # defined below
48    }
49    public proc SetServerList { namelist } {
50        Rappture::VisViewer::SetServerList "vtkvis" $namelist
51    }
52    public method add {dataobj {settings ""}}
53    public method camera {option args}
54    public method delete {args}
55    public method disconnect {}
56    public method download {option args}
57    public method get {args}
58    public method isconnected {}
59    public method limits { dataobj }
60    public method parameters {title args} {
61        # do nothing
62    }
63    public method scale {args}
64
65    # The following methods are only used by this class.
66    private method AdjustSetting {what {value ""}}
67    private method BuildAxisTab {}
68    private method BuildCameraTab {}
69    private method BuildColormap { name }
70    private method BuildCutawayTab {}
71    private method BuildDownloadPopup { widget command }
72    private method BuildGlyphsTab {}
73    private method BuildMoleculeTab {}
74    private method BuildPolydataTab {}
75    private method ChangeColormap { dataobj comp color }
76    private method Connect {}
77    private method CurrentDatasets {args}
78    private method Disconnect {}
79    private method DoResize {}
80    private method DoRotate {}
81    private method DrawLegend {}
82    private method EnterLegend { x y }
83    private method EventuallyResize { w h }
84    private method EventuallyRotate { q }
85    private method EventuallySetAtomScale { args }
86    private method EventuallySetBondScale { args }
87    private method EventuallySetGlyphsOpacity { args }
88    private method EventuallySetMoleculeOpacity { args }
89    private method EventuallySetMoleculeQuality { args }
90    private method EventuallySetPolydataOpacity { args }
91    private method GetImage { args }
92    private method GetVtkData { args }
93    private method InitSettings { args }
94    private method IsValidObject { dataobj }
95    private method LeaveLegend {}
96    private method MotionLegend { x y }
97    private method Pan {option x y}
98    private method PanCamera {}
99    private method Pick {x y}
100    private method QuaternionToView { q } {
101        foreach { _view(-qw) _view(-qx) _view(-qy) _view(-qz) } $q break
102    }
103    private method Rebuild {}
104    private method ReceiveDataset { args }
105    private method ReceiveImage { args }
106    private method ReceiveLegend { colormap title min max size }
107    private method RequestLegend {}
108    private method Rotate {option x y}
109    private method SetAtomScale {}
110    private method SetBondScale {}
111    private method SetColormap { dataobj comp }
112    private method SetGlyphsOpacity {}
113    private method SetLegendTip { x y }
114    private method SetMoleculeOpacity {}
115    private method SetMoleculeQuality {}
116    private method SetObjectStyle { dataobj comp }
117    private method SetOpacity { dataset }
118    private method SetOrientation { side }
119    private method SetPolydataOpacity {}
120    private method Slice {option args}
121    private method ViewToQuaternion {} {
122        return [list $_view(-qw) $_view(-qx) $_view(-qy) $_view(-qz)]
123    }
124    private method Zoom {option}
125
126    private variable _arcball ""
127    private variable _dlist "";         # list of data objects
128    private variable _obj2ovride;       # maps dataobj => style override
129    private variable _datasets;         # contains all the dataobj-component
130                                        # datasets in the server
131    private variable _colormaps;        # contains all the colormaps
132                                        # in the server.
133    private variable _dataset2style;    # maps dataobj-component to transfunc
134    private variable _click;            # info used for rotate operations
135    private variable _limits;           # autoscale min/max for all axes
136    private variable _view;             # view params for 3D view
137    private variable _settings
138    private variable _style;            # Array of current component styles.
139    private variable _initialStyle;     # Array of initial component styles.
140    private variable _axis
141    private variable _reset 1;          # Connection to server has been reset.
142    private variable _haveGlyphs 0
143    private variable _haveMolecules 0
144    private variable _havePolydata 0
145
146    private variable _first "";         # This is the topmost dataset.
147    private variable _start 0
148    private variable _title ""
149    private variable _width 0
150    private variable _height 0
151    private variable _resizePending 0
152    private variable _rotatePending 0
153    private variable _atomScalePending 0
154    private variable _bondScalePending 0
155    private variable _moleculeOpacityPending 0
156    private variable _moleculeQualityPending 0
157    private variable _polydataOpacityPending 0
158    private variable _glyphsOpacityPending 0
159    private variable _rotateDelay 150
160    private variable _scaleDelay 100
161
162    private common _downloadPopup;      # download options from popup
163    private common _hardcopy
164}
165
166itk::usual VtkViewer {
167    keep -background -foreground -cursor -font
168    keep -plotbackground -plotforeground
169}
170
171# ----------------------------------------------------------------------
172# CONSTRUCTOR
173# ----------------------------------------------------------------------
174itcl::body Rappture::VtkViewer::constructor {args} {
175    package require vtk
176    set _serverType "vtkvis"
177
178    #DebugOn
179    EnableWaitDialog 900
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    # Rotate event
190    $_dispatcher register !rotate
191    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
192
193    # Atom scale event
194    $_dispatcher register !atomscale
195    $_dispatcher dispatch $this !atomscale \
196        "[itcl::code $this SetAtomScale]; list"
197
198    # Bond scale event
199    $_dispatcher register !bondscale
200    $_dispatcher dispatch $this !bondscale \
201        "[itcl::code $this SetBondScale]; list"
202
203    # Molecule opacity event
204    $_dispatcher register !moleculeOpacity
205    $_dispatcher dispatch $this !moleculeOpacity \
206        "[itcl::code $this SetMoleculeOpacity]; list"
207
208    # Molecule quality event
209    $_dispatcher register !moleculeQuality
210    $_dispatcher dispatch $this !moleculeQuality \
211        "[itcl::code $this SetMoleculeQuality]; list"
212
213    # Polydata opacity event
214    $_dispatcher register !polydataOpacity
215    $_dispatcher dispatch $this !polydataOpacity \
216        "[itcl::code $this SetPolydataOpacity]; list"
217
218    # Glyphs opacity event
219    $_dispatcher register !glyphsOpacity
220    $_dispatcher dispatch $this !glyphsOpacity \
221        "[itcl::code $this SetGlyphsOpacity]; list"
222
223    #
224    # Populate parser with commands handle incoming requests
225    #
226    $_parser alias image [itcl::code $this ReceiveImage]
227    $_parser alias legend [itcl::code $this ReceiveLegend]
228    $_parser alias dataset [itcl::code $this ReceiveDataset]
229
230    # Initialize the view to some default parameters.
231    array set _view {
232        -ortho    0
233        -qw       0.853553
234        -qx       -0.353553
235        -qy       0.353553
236        -qz       0.146447
237        -xpan     0
238        -ypan     0
239        -zoom     1.0
240    }
241    set _arcball [blt::arcball create 100 100]
242    $_arcball quaternion [ViewToQuaternion]
243
244    set _limits(zmin) 0.0
245    set _limits(zmax) 1.0
246
247    array set _axis [subst {
248        labels          1
249        minorticks      1
250        visible         1
251        xgrid           0
252        ygrid           0
253        zgrid           0
254        xcutaway        0
255        ycutaway        0
256        zcutaway        0
257        xposition       0
258        yposition       0
259        zposition       0
260        xdirection      -1
261        ydirection      -1
262        zdirection      -1
263    }]
264    array set _settings [subst {
265        glyphs-edges            0
266        glyphs-lighting         1
267        glyphs-opacity          100
268        glyphs-outline          0
269        glyphs-palette          BCGYR
270        glyphs-visible          1
271        glyphs-wireframe        0
272        legend                  1
273        molecule-atoms-visible  1
274        molecule-atomscale      0.3
275        molecule-bonds-visible  1
276        molecule-bondscale      0.075
277        molecule-bondstyle      "cylinder"
278        molecule-edges          0
279        molecule-labels         0
280        molecule-lighting       1
281        molecule-opacity        100
282        molecule-outline        0
283        molecule-palette        elementDefault
284        molecule-quality        1.0
285        molecule-representation "Ball and Stick"
286        molecule-rscale         "covalent"
287        molecule-visible        1
288        molecule-wireframe      0
289        polydata-edges          0
290        polydata-lighting       1
291        polydata-opacity        100
292        polydata-outline        0
293        polydata-palette        BCGYR
294        polydata-visible        1
295        polydata-wireframe      0
296    }]
297    itk_component add view {
298        canvas $itk_component(plotarea).view \
299            -highlightthickness 0 -borderwidth 0
300    } {
301        usual
302        ignore -highlightthickness -borderwidth -background
303    }
304
305    set c $itk_component(view)
306    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
307    bind $c <Control-F1> [itcl::code $this ToggleConsole]
308
309    # Fix the scrollregion in case we go off screen
310    $c configure -scrollregion [$c bbox all]
311
312    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
313    set _map(cwidth) -1
314    set _map(cheight) -1
315    set _map(zoom) 1.0
316    set _map(original) ""
317
318    set f [$itk_component(main) component controls]
319    itk_component add reset {
320        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
321            -highlightthickness 0 \
322            -image [Rappture::icon reset-view] \
323            -command [itcl::code $this Zoom reset]
324    } {
325        usual
326        ignore -highlightthickness
327    }
328    pack $itk_component(reset) -side top -padx 2 -pady 2
329    Rappture::Tooltip::for $itk_component(reset) \
330        "Reset the view to the default zoom level"
331
332    itk_component add zoomin {
333        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
334            -highlightthickness 0 \
335            -image [Rappture::icon zoom-in] \
336            -command [itcl::code $this Zoom in]
337    } {
338        usual
339        ignore -highlightthickness
340    }
341    pack $itk_component(zoomin) -side top -padx 2 -pady 2
342    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
343
344    itk_component add zoomout {
345        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
346            -highlightthickness 0 \
347            -image [Rappture::icon zoom-out] \
348            -command [itcl::code $this Zoom out]
349    } {
350        usual
351        ignore -highlightthickness
352    }
353    pack $itk_component(zoomout) -side top -padx 2 -pady 2
354    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
355
356    if { [catch {
357        BuildAxisTab
358        #BuildCutawayTab
359        BuildCameraTab
360    } errs] != 0 } {
361        puts stderr errs=$errs
362    }
363
364    # Legend
365    set _image(legend) [image create photo]
366    itk_component add legend {
367        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
368    } {
369        usual
370        ignore -highlightthickness
371        rename -background -plotbackground plotBackground Background
372    }
373
374    # Hack around the Tk panewindow.  The problem is that the requested
375    # size of the 3d view isn't set until an image is retrieved from
376    # the server.  So the panewindow uses the tiny size.
377    set w 10000
378    pack forget $itk_component(view)
379    blt::table $itk_component(plotarea) \
380        0,0 $itk_component(view) -fill both -reqwidth $w
381    blt::table configure $itk_component(plotarea) c1 -resize none
382
383    # Bindings for rotation via mouse
384    bind $itk_component(view) <ButtonPress-1> \
385        [itcl::code $this Rotate click %x %y]
386    bind $itk_component(view) <B1-Motion> \
387        [itcl::code $this Rotate drag %x %y]
388    bind $itk_component(view) <ButtonRelease-1> \
389        [itcl::code $this Rotate release %x %y]
390
391    # Bindings for panning via mouse
392    bind $itk_component(view) <ButtonPress-2> \
393        [itcl::code $this Pan click %x %y]
394    bind $itk_component(view) <B2-Motion> \
395        [itcl::code $this Pan drag %x %y]
396    bind $itk_component(view) <ButtonRelease-2> \
397        [itcl::code $this Pan release %x %y]
398
399    #bind $itk_component(view) <ButtonRelease-3> \
400    #    [itcl::code $this Pick %x %y]
401
402    # Bindings for panning via keyboard
403    bind $itk_component(view) <KeyPress-Left> \
404        [itcl::code $this Pan set -10 0]
405    bind $itk_component(view) <KeyPress-Right> \
406        [itcl::code $this Pan set 10 0]
407    bind $itk_component(view) <KeyPress-Up> \
408        [itcl::code $this Pan set 0 -10]
409    bind $itk_component(view) <KeyPress-Down> \
410        [itcl::code $this Pan set 0 10]
411    bind $itk_component(view) <Shift-KeyPress-Left> \
412        [itcl::code $this Pan set -2 0]
413    bind $itk_component(view) <Shift-KeyPress-Right> \
414        [itcl::code $this Pan set 2 0]
415    bind $itk_component(view) <Shift-KeyPress-Up> \
416        [itcl::code $this Pan set 0 -2]
417    bind $itk_component(view) <Shift-KeyPress-Down> \
418        [itcl::code $this Pan set 0 2]
419
420    # Bindings for zoom via keyboard
421    bind $itk_component(view) <KeyPress-Prior> \
422        [itcl::code $this Zoom out]
423    bind $itk_component(view) <KeyPress-Next> \
424        [itcl::code $this Zoom in]
425
426    bind $itk_component(view) <Enter> "focus $itk_component(view)"
427
428    if {[string equal "x11" [tk windowingsystem]]} {
429        # Bindings for zoom via mouse
430        bind $itk_component(view) <4> [itcl::code $this Zoom out]
431        bind $itk_component(view) <5> [itcl::code $this Zoom in]
432    }
433
434    set _image(download) [image create photo]
435
436    eval itk_initialize $args
437
438    Connect
439}
440
441# ----------------------------------------------------------------------
442# DESTRUCTOR
443# ----------------------------------------------------------------------
444itcl::body Rappture::VtkViewer::destructor {} {
445    Disconnect
446    $_dispatcher cancel !rebuild
447    $_dispatcher cancel !resize
448    $_dispatcher cancel !rotate
449    image delete $_image(plot)
450    image delete $_image(download)
451    catch { blt::arcball destroy $_arcball }
452}
453
454itcl::body Rappture::VtkViewer::DoResize {} {
455    if { $_width < 2 } {
456        set _width 500
457    }
458    if { $_height < 2 } {
459        set _height 500
460    }
461    set _start [clock clicks -milliseconds]
462    SendCmd "screen size $_width $_height"
463
464    set _resizePending 0
465}
466
467itcl::body Rappture::VtkViewer::DoRotate {} {
468    SendCmd "camera orient [ViewToQuaternion]"
469    set _rotatePending 0
470}
471
472itcl::body Rappture::VtkViewer::EventuallyResize { w h } {
473    set _width $w
474    set _height $h
475    $_arcball resize $w $h
476    if { !$_resizePending } {
477        set _resizePending 1
478        $_dispatcher event -after 200 !resize
479    }
480}
481
482itcl::body Rappture::VtkViewer::EventuallyRotate { q } {
483    QuaternionToView $q
484    if { !$_rotatePending } {
485        set _rotatePending 1
486        $_dispatcher event -after $_rotateDelay !rotate
487    }
488}
489
490itcl::body Rappture::VtkViewer::SetAtomScale {} {
491    SendCmd "molecule ascale $_settings(molecule-atomscale)"
492    set _atomScalePending 0
493}
494
495itcl::body Rappture::VtkViewer::SetBondScale {} {
496    SendCmd "molecule bscale $_settings(molecule-bondscale)"
497    set _bondScalePending 0
498}
499
500itcl::body Rappture::VtkViewer::SetMoleculeOpacity {} {
501    set _moleculeOpacityPending 0
502    foreach dataset [CurrentDatasets -visible $_first] {
503        foreach { dataobj comp } [split $dataset -] break
504        if { [$dataobj type $comp] == "molecule" } {
505            SetOpacity $dataset
506        }
507    }
508}
509
510itcl::body Rappture::VtkViewer::SetMoleculeQuality {} {
511    SendCmd [subst {molecule aquality $_settings(molecule-quality)
512molecule bquality $_settings(molecule-quality)}]
513    set _moleculeQualityPending 0
514}
515
516itcl::body Rappture::VtkViewer::SetGlyphsOpacity {} {
517    set _glyphsOpacityPending 0
518    foreach dataset [CurrentDatasets -visible $_first] {
519        foreach { dataobj comp } [split $dataset -] break
520        if { [$dataobj type $comp] == "glyphs" } {
521            SetOpacity $dataset
522        }
523    }
524}
525
526itcl::body Rappture::VtkViewer::SetPolydataOpacity {} {
527    set _polydataOpacityPending 0
528    foreach dataset [CurrentDatasets -visible $_first] {
529        foreach { dataobj comp } [split $dataset -] break
530        if { [$dataobj type $comp] == "polydata" } {
531            SetOpacity $dataset
532        }
533    }
534}
535
536itcl::body Rappture::VtkViewer::EventuallySetAtomScale { args } {
537    if { !$_atomScalePending } {
538        set _atomScalePending 1
539        $_dispatcher event -after $_scaleDelay !atomscale
540    }
541}
542
543itcl::body Rappture::VtkViewer::EventuallySetBondScale { args } {
544    if { !$_bondScalePending } {
545        set _bondScalePending 1
546        $_dispatcher event -after $_scaleDelay !bondscale
547    }
548}
549
550itcl::body Rappture::VtkViewer::EventuallySetMoleculeOpacity { args } {
551    if { !$_moleculeOpacityPending } {
552        set _moleculeOpacityPending 1
553        $_dispatcher event -after $_scaleDelay !moleculeOpacity
554    }
555}
556
557itcl::body Rappture::VtkViewer::EventuallySetMoleculeQuality { args } {
558    if { !$_moleculeQualityPending } {
559        set _moleculeQualityPending 1
560        $_dispatcher event -after $_scaleDelay !moleculeQuality
561    }
562}
563
564itcl::body Rappture::VtkViewer::EventuallySetPolydataOpacity { args } {
565    if { !$_polydataOpacityPending } {
566        set _polydataOpacityPending 1
567        $_dispatcher event -after $_scaleDelay !polydataOpacity
568    }
569}
570
571itcl::body Rappture::VtkViewer::EventuallySetGlyphsOpacity { args } {
572    if { !$_glyphsOpacityPending } {
573        set _glyphsOpacityPending 1
574        $_dispatcher event -after $_scaleDelay !glyphsOpacity
575    }
576}
577
578# ----------------------------------------------------------------------
579# USAGE: add <dataobj> ?<settings>?
580#
581# Clients use this to add a data object to the plot.  The optional
582# <settings> are used to configure the plot.  Allowed settings are
583# -color, -brightness, -width, -linestyle, and -raise.
584# ----------------------------------------------------------------------
585itcl::body Rappture::VtkViewer::add {dataobj {settings ""}} {
586    array set params {
587        -color auto
588        -width 1
589        -linestyle solid
590        -brightness 0
591        -raise 0
592        -description ""
593        -param ""
594        -type ""
595    }
596    array set params $settings
597
598    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
599        # can't handle -autocolors yet
600        set params(-color) black
601    }
602    set pos [lsearch -exact $_dlist $dataobj]
603    if {$pos < 0} {
604        lappend _dlist $dataobj
605    }
606    set _obj2ovride($dataobj-color) $params(-color)
607    set _obj2ovride($dataobj-width) $params(-width)
608    set _obj2ovride($dataobj-raise) $params(-raise)
609    $_dispatcher event -idle !rebuild
610}
611
612# ----------------------------------------------------------------------
613# USAGE: delete ?<dataobj1> <dataobj2> ...?
614#
615# Clients use this to delete a dataobj from the plot.  If no dataobjs
616# are specified, then all dataobjs are deleted.  No data objects are
617# deleted.  They are only removed from the display list.
618# ----------------------------------------------------------------------
619itcl::body Rappture::VtkViewer::delete {args} {
620    if { [llength $args] == 0} {
621        set args $_dlist
622    }
623    # Delete all specified dataobjs
624    set changed 0
625    foreach dataobj $args {
626        set pos [lsearch -exact $_dlist $dataobj]
627        if { $pos < 0 } {
628            continue;                   # Don't know anything about it.
629        }
630        # Remove it from the dataobj list.
631        set _dlist [lreplace $_dlist $pos $pos]
632        array unset _obj2ovride $dataobj-*
633        set changed 1
634    }
635    # If anything changed, then rebuild the plot
636    if { $changed } {
637        $_dispatcher event -idle !rebuild
638    }
639}
640
641# ----------------------------------------------------------------------
642# USAGE: get ?-objects?
643# USAGE: get ?-visible?
644# USAGE: get ?-image view?
645#
646# Clients use this to query the list of objects being plotted, in
647# order from bottom to top of this result.  The optional "-image"
648# flag can also request the internal images being shown.
649# ----------------------------------------------------------------------
650itcl::body Rappture::VtkViewer::get {args} {
651    if {[llength $args] == 0} {
652        set args "-objects"
653    }
654
655    set op [lindex $args 0]
656    switch -- $op {
657        "-objects" {
658            # put the dataobj list in order according to -raise options
659            set dlist {}
660            foreach dataobj $_dlist {
661                if { ![IsValidObject $dataobj] } {
662                    continue
663                }
664                if {[info exists _obj2ovride($dataobj-raise)] &&
665                    $_obj2ovride($dataobj-raise)} {
666                    set dlist [linsert $dlist 0 $dataobj]
667                } else {
668                    lappend dlist $dataobj
669                }
670            }
671            return $dlist
672        }
673        "-visible" {
674            set dlist {}
675            foreach dataobj $_dlist {
676                if { ![IsValidObject $dataobj] } {
677                    continue
678                }
679                if { ![info exists _obj2ovride($dataobj-raise)] } {
680                    # No setting indicates that the object isn't visible.
681                    continue
682                }
683                # Otherwise use the -raise parameter to put the object to
684                # the front of the list.
685                if { $_obj2ovride($dataobj-raise) } {
686                    set dlist [linsert $dlist 0 $dataobj]
687                } else {
688                    lappend dlist $dataobj
689                }
690            }
691            return $dlist
692        }
693        "-image" {
694            if {[llength $args] != 2} {
695                error "wrong # args: should be \"get -image view\""
696            }
697            switch -- [lindex $args end] {
698                view {
699                    return $_image(plot)
700                }
701                default {
702                    error "bad image name \"[lindex $args end]\": should be view"
703                }
704            }
705        }
706        default {
707            error "bad option \"$op\": should be -objects, -visible or -image"
708        }
709    }
710}
711
712# ----------------------------------------------------------------------
713# USAGE: scale ?<data1> <data2> ...?
714#
715# Sets the default limits for the overall plot according to the
716# limits of the data for all of the given <data> objects.  This
717# accounts for all objects--even those not showing on the screen.
718# Because of this, the limits are appropriate for all objects as
719# the user scans through data in the ResultSet viewer.
720# ----------------------------------------------------------------------
721itcl::body Rappture::VtkViewer::scale {args} {
722    foreach dataobj $args {
723        foreach comp [$dataobj components] {
724            set type [$dataobj type $comp]
725            switch -- $type {
726                "polydata" {
727                    set _havePolydata 1
728                }
729                "glyphs" {
730                    set _haveGlyphs 1
731                }
732                "molecule" {
733                    set _haveMolecules 1
734                }
735            }
736        }
737        array set bounds [limits $dataobj]
738        if {[info exists bounds(xmin)] && (![info exists _limits(xmin)] || $_limits(xmin) > $bounds(xmin))} {
739            set _limits(xmin) $bounds(xmin)
740        }
741        if {[info exists bounds(xmax)] && (![info exists _limits(xmax)] || $_limits(xmax) < $bounds(xmax))} {
742            set _limits(xmax) $bounds(xmax)
743        }
744
745        if {[info exists bounds(ymin)] && (![info exists _limits(ymin)] || $_limits(ymin) > $bounds(ymin))} {
746            set _limits(ymin) $bounds(ymin)
747        }
748        if {[info exists bounds(ymax)] && (![info exists _limits(ymax)] || $_limits(ymax) < $bounds(ymax))} {
749            set _limits(ymax) $bounds(ymax)
750        }
751
752        if {[info exists bounds(zmin)] && (![info exists _limits(zmin)] || $_limits(zmin) > $bounds(zmin))} {
753            set _limits(zmin) $bounds(zmin)
754        }
755        if {[info exists bounds(zmax)] && (![info exists _limits(zmax)] || $_limits(zmax) < $bounds(zmax))} {
756            set _limits(zmax) $bounds(zmax)
757        }
758    }
759    if { $_haveGlyphs } {
760        if { ![$itk_component(main) exists "Glyphs Settings"] } {
761            if { [catch { BuildGlyphsTab } errs ]  != 0 } {
762                puts stderr "errs=$errs"
763            }
764        }
765    }
766    if { $_havePolydata } {
767        if { ![$itk_component(main) exists "Mesh Settings"] } {
768            if { [catch { BuildPolydataTab } errs ]  != 0 } {
769                puts stderr "errs=$errs"
770            }
771        }
772    }
773    if { $_haveMolecules } {
774        if { ![$itk_component(main) exists "Molecule Settings"]} {
775            if { [catch { BuildMoleculeTab } errs ]  != 0 } {
776                global errorInfo
777                puts stderr "errs=$errs\nerrorInfo=$errorInfo"
778            }
779        }
780    }
781}
782
783# ----------------------------------------------------------------------
784# USAGE: download coming
785# USAGE: download controls <downloadCommand>
786# USAGE: download now
787#
788# Clients use this method to create a downloadable representation
789# of the plot.  Returns a list of the form {ext string}, where
790# "ext" is the file extension (indicating the type of data) and
791# "string" is the data itself.
792# ----------------------------------------------------------------------
793itcl::body Rappture::VtkViewer::download {option args} {
794    switch $option {
795        coming {
796            if {[catch {
797                blt::winop snap $itk_component(plotarea) $_image(download)
798            }]} {
799                $_image(download) configure -width 1 -height 1
800                $_image(download) put #000000
801            }
802        }
803        controls {
804            set popup .vtkviewerdownload
805            if { ![winfo exists .vtkviewerdownload] } {
806                set inner [BuildDownloadPopup $popup [lindex $args 0]]
807            } else {
808                set inner [$popup component inner]
809            }
810            set _downloadPopup(image_controls) $inner.image_frame
811            set num [llength [get]]
812            set num [expr {($num == 1) ? "1 result" : "$num results"}]
813            set word [Rappture::filexfer::label downloadWord]
814            $inner.summary configure -text "$word $num in the following format:"
815            update idletasks            ;# Fix initial sizes
816            return $popup
817        }
818        now {
819            set popup .vtkviewerdownload
820            if {[winfo exists .vtkviewerdownload]} {
821                $popup deactivate
822            }
823            switch -- $_downloadPopup(format) {
824                "image" {
825                    return [$this GetImage [lindex $args 0]]
826                }
827                "vtk" {
828                    return [$this GetVtkData [lindex $args 0]]
829                }
830            }
831            return ""
832        }
833        default {
834            error "bad option \"$option\": should be coming, controls, now"
835        }
836    }
837}
838
839# ----------------------------------------------------------------------
840# USAGE: Connect ?<host:port>,<host:port>...?
841#
842# Clients use this method to establish a connection to a new
843# server, or to reestablish a connection to the previous server.
844# Any existing connection is automatically closed.
845# ----------------------------------------------------------------------
846itcl::body Rappture::VtkViewer::Connect {} {
847    global readyForNextFrame
848    set readyForNextFrame 1
849    set _hosts [GetServerList "vtkvis"]
850    if { "" == $_hosts } {
851        return 0
852    }
853    set _reset 1
854    set result [VisViewer::Connect $_hosts]
855    if { $result } {
856        if { $_reportClientInfo }  {
857            # Tell the server the viewer, hub, user and session.
858            # Do this immediately on connect before buffering any commands
859            global env
860
861            set info {}
862            set user "???"
863            if { [info exists env(USER)] } {
864                set user $env(USER)
865            }
866            set session "???"
867            if { [info exists env(SESSION)] } {
868                set session $env(SESSION)
869            }
870            lappend info "version" "$Rappture::version"
871            lappend info "build" "$Rappture::build"
872            lappend info "svnurl" "$Rappture::svnurl"
873            lappend info "installdir" "$Rappture::installdir"
874            lappend info "hub" [exec hostname]
875            lappend info "client" "vtkviewer"
876            lappend info "user" $user
877            lappend info "session" $session
878            SendCmd "clientinfo [list $info]"
879        }
880
881        set w [winfo width $itk_component(view)]
882        set h [winfo height $itk_component(view)]
883        EventuallyResize $w $h
884    }
885    return $result
886}
887
888#
889# isconnected --
890#
891# Indicates if we are currently connected to the visualization server.
892#
893itcl::body Rappture::VtkViewer::isconnected {} {
894    return [VisViewer::IsConnected]
895}
896
897#
898# disconnect --
899#
900itcl::body Rappture::VtkViewer::disconnect {} {
901    Disconnect
902    set _reset 1
903}
904
905#
906# Disconnect --
907#
908# Clients use this method to disconnect from the current rendering server.
909#
910itcl::body Rappture::VtkViewer::Disconnect {} {
911    VisViewer::Disconnect
912
913    # disconnected -- no more data sitting on server
914    array unset _datasets
915    array unset _colormaps
916    global readyForNextFrame
917    set readyForNextFrame 1
918}
919
920# ----------------------------------------------------------------------
921# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
922#
923# Invoked automatically whenever the "image" command comes in from
924# the rendering server.  Indicates that binary image data with the
925# specified <size> will follow.
926# ----------------------------------------------------------------------
927itcl::body Rappture::VtkViewer::ReceiveImage { args } {
928    global readyForNextFrame
929    set readyForNextFrame 1
930    array set info {
931        -token "???"
932        -bytes 0
933        -type image
934    }
935    array set info $args
936    set bytes [ReceiveBytes $info(-bytes)]
937    if { $info(-type) == "image" } {
938        if 0 {
939            set f [open "last.ppm" "w"]
940            fconfigure $f -encoding binary
941            puts -nonewline $f $bytes
942            close $f
943        }
944        $_image(plot) configure -data $bytes
945        set time [clock seconds]
946        set date [clock format $time]
947        if { $_start > 0 } {
948            set finish [clock clicks -milliseconds]
949            set _start 0
950        }
951    } elseif { $info(type) == "print" } {
952        set tag $this-print-$info(-token)
953        set _hardcopy($tag) $bytes
954    }
955}
956
957#
958# ReceiveDataset --
959#
960itcl::body Rappture::VtkViewer::ReceiveDataset { args } {
961    if { ![isconnected] } {
962        return
963    }
964    set option [lindex $args 0]
965    switch -- $option {
966        "scalar" {
967            set option [lindex $args 1]
968            switch -- $option {
969                "world" {
970                    foreach { x y z value tag } [lrange $args 2 end] break
971                }
972                "pixel" {
973                    foreach { x y value tag } [lrange $args 2 end] break
974                }
975            }
976        }
977        "vector" {
978            set option [lindex $args 1]
979            switch -- $option {
980                "world" {
981                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
982                }
983                "pixel" {
984                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
985                }
986            }
987        }
988        "names" {
989            foreach { name } [lindex $args 1] {
990                #puts stderr "Dataset: $name"
991            }
992        }
993        default {
994            error "unknown dataset option \"$option\" from server"
995        }
996    }
997}
998
999# ----------------------------------------------------------------------
1000# USAGE: Rebuild
1001#
1002# Called automatically whenever something changes that affects the
1003# data in the widget.  Clears any existing data and rebuilds the
1004# widget to display new data.
1005# ----------------------------------------------------------------------
1006itcl::body Rappture::VtkViewer::Rebuild {} {
1007    set w [winfo width $itk_component(view)]
1008    set h [winfo height $itk_component(view)]
1009    if { $w < 2 || $h < 2 } {
1010        update
1011        $_dispatcher event -idle !rebuild
1012        return
1013    }
1014
1015    # Turn on buffering of commands to the server.  We don't want to
1016    # be preempted by a server disconnect/reconnect (which automatically
1017    # generates a new call to Rebuild).
1018    StartBufferingCommands
1019
1020    if { $_reset } {
1021        set _width $w
1022        set _height $h
1023        $_arcball resize $w $h
1024        DoResize
1025        InitSettings axis-xgrid axis-ygrid axis-zgrid axis-mode \
1026            axis-visible axis-labels axis-minorticks
1027
1028        StopBufferingCommands
1029        SendCmd "imgflush"
1030        StartBufferingCommands
1031    }
1032
1033    set _limits(zmin) ""
1034    set _limits(zmax) ""
1035    set _first ""
1036    SendCmd "dataset visible 0"
1037    set count 0
1038    foreach dataobj [get -objects] {
1039        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
1040            set _first $dataobj
1041        }
1042        foreach comp [$dataobj components] {
1043            set tag $dataobj-$comp
1044            if { ![info exists _datasets($tag)] } {
1045                set bytes [$dataobj data $comp]
1046                if { $bytes == "" } {
1047                    continue
1048                }
1049                if 0 {
1050                    set f [open /tmp/vtkviewer.vtk "w"]
1051                    fconfigure $f -translation binary -encoding binary
1052                    puts -nonewline $f $bytes
1053                    close $f
1054                }
1055                set length [string length $bytes]
1056                if { $_reportClientInfo }  {
1057                    set info {}
1058                    lappend info "tool_id"       [$dataobj hints toolid]
1059                    lappend info "tool_name"     [$dataobj hints toolname]
1060                    lappend info "tool_title"    [$dataobj hints tooltitle]
1061                    lappend info "tool_command"  [$dataobj hints toolcommand]
1062                    lappend info "tool_revision" [$dataobj hints toolrevision]
1063                    lappend info "dataset_label" [$dataobj hints label]
1064                    lappend info "dataset_size"  $length
1065                    lappend info "dataset_tag"   $tag
1066                    SendCmd "clientinfo [list $info]"
1067                }
1068                SendCmd "dataset add $tag data follows $length"
1069                SendData $bytes
1070                set _datasets($tag) 1
1071                SetObjectStyle $dataobj $comp
1072            }
1073            set type [$dataobj type $comp]
1074            if { [info exists _obj2ovride($dataobj-raise)] } {
1075                SendCmd "$type visible 1 $tag"
1076                SetOpacity $tag
1077            }
1078        }
1079    }
1080    if {"" != $_first} {
1081        foreach axis { x y z } {
1082            set label [$_first hints ${axis}label]
1083            if { $label != "" } {
1084                SendCmd [list axis name $axis $label]
1085            }
1086            set units [$_first hints ${axis}units]
1087            if { $units != "" } {
1088                SendCmd [list axis units $axis $units]
1089            }
1090        }
1091    }
1092    if { $_haveGlyphs } {
1093        InitSettings glyphs-outline
1094    }
1095    if { $_haveMolecules } {
1096        InitSettings molecule-outline
1097    }
1098    if { $_havePolydata } {
1099        InitSettings polydata-outline
1100    }
1101    if { $_reset } {
1102        if { $_haveGlyphs } {
1103            InitSettings glyphs-edges glyphs-lighting glyphs-opacity \
1104                glyphs-visible glyphs-wireframe
1105        }
1106        if { $_havePolydata } {
1107            InitSettings polydata-edges polydata-lighting polydata-opacity \
1108                polydata-visible polydata-wireframe
1109        }
1110        if { $_haveMolecules } {
1111            InitSettings molecule-edges molecule-lighting molecule-opacity \
1112                molecule-visible molecule-wireframe molecule-labels
1113        }
1114
1115        $_arcball quaternion [ViewToQuaternion]
1116        SendCmd "camera reset"
1117        if { $_view(-ortho)} {
1118            SendCmd "camera mode ortho"
1119        } else {
1120            SendCmd "camera mode persp"
1121        }
1122        DoRotate
1123        PanCamera
1124        Zoom reset
1125    }
1126
1127    if { $_haveMolecules } {
1128        #InitSettings molecule-representation
1129    }
1130    set _reset 0
1131    global readyForNextFrame
1132    set readyForNextFrame 0;            # Don't advance to the next frame
1133                                        # until we get an image.
1134
1135    # Actually write the commands to the server socket.  If it fails, we don't
1136    # care.  We're finished here.
1137    blt::busy hold $itk_component(hull)
1138    StopBufferingCommands
1139    blt::busy release $itk_component(hull)
1140}
1141
1142# ----------------------------------------------------------------------
1143# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
1144#
1145# Returns a list of server IDs for the current datasets being displayed.  This
1146# is normally a single ID, but it might be a list of IDs if the current data
1147# object has multiple components.
1148# ----------------------------------------------------------------------
1149itcl::body Rappture::VtkViewer::CurrentDatasets {args} {
1150    set flag [lindex $args 0]
1151    switch -- $flag {
1152        "-all" {
1153            if { [llength $args] > 1 } {
1154                error "CurrentDatasets: can't specify dataobj after \"-all\""
1155            }
1156            set dlist [get -objects]
1157        }
1158        "-visible" {
1159            if { [llength $args] > 1 } {
1160                set dlist {}
1161                set args [lrange $args 1 end]
1162                foreach dataobj $args {
1163                    if { [info exists _obj2ovride($dataobj-raise)] } {
1164                        lappend dlist $dataobj
1165                    }
1166                }
1167            } else {
1168                set dlist [get -visible]
1169            }
1170        }
1171        default {
1172            set dlist $args
1173        }
1174    }
1175    set rlist ""
1176    foreach dataobj $dlist {
1177        foreach comp [$dataobj components] {
1178            set tag $dataobj-$comp
1179            if { [info exists _datasets($tag)] && $_datasets($tag) } {
1180                lappend rlist $tag
1181            }
1182        }
1183    }
1184    return $rlist
1185}
1186
1187# ----------------------------------------------------------------------
1188# USAGE: Zoom in
1189# USAGE: Zoom out
1190# USAGE: Zoom reset
1191#
1192# Called automatically when the user clicks on one of the zoom
1193# controls for this widget.  Changes the zoom for the current view.
1194# ----------------------------------------------------------------------
1195itcl::body Rappture::VtkViewer::Zoom {option} {
1196    switch -- $option {
1197        "in" {
1198            set _view(-zoom) [expr {$_view(-zoom)*1.25}]
1199            SendCmd "camera zoom $_view(-zoom)"
1200        }
1201        "out" {
1202            set _view(-zoom) [expr {$_view(-zoom)*0.8}]
1203            SendCmd "camera zoom $_view(-zoom)"
1204        }
1205        "reset" {
1206            array set _view {
1207                -qw      0.853553
1208                -qx      -0.353553
1209                -qy      0.353553
1210                -qz      0.146447
1211                -xpan    0
1212                -ypan    0
1213                -zoom    1.0
1214            }
1215            if { $_first != "" } {
1216                set location [$_first hints camera]
1217                if { $location != "" } {
1218                    array set _view $location
1219                }
1220            }
1221            $_arcball quaternion [ViewToQuaternion]
1222            DoRotate
1223            SendCmd "camera reset"
1224        }
1225    }
1226}
1227
1228itcl::body Rappture::VtkViewer::PanCamera {} {
1229    set x $_view(-xpan)
1230    set y $_view(-ypan)
1231    SendCmd "camera pan $x $y"
1232}
1233
1234# ----------------------------------------------------------------------
1235# USAGE: Rotate click <x> <y>
1236# USAGE: Rotate drag <x> <y>
1237# USAGE: Rotate release <x> <y>
1238#
1239# Called automatically when the user clicks/drags/releases in the
1240# plot area.  Moves the plot according to the user's actions.
1241# ----------------------------------------------------------------------
1242itcl::body Rappture::VtkViewer::Rotate {option x y} {
1243    switch -- $option {
1244        "click" {
1245            $itk_component(view) configure -cursor fleur
1246            set _click(x) $x
1247            set _click(y) $y
1248        }
1249        "drag" {
1250            if {[array size _click] == 0} {
1251                Rotate click $x $y
1252            } else {
1253                set w [winfo width $itk_component(view)]
1254                set h [winfo height $itk_component(view)]
1255                if {$w <= 0 || $h <= 0} {
1256                    return
1257                }
1258
1259                if {[catch {
1260                    # this fails sometimes for no apparent reason
1261                    set dx [expr {double($x-$_click(x))/$w}]
1262                    set dy [expr {double($y-$_click(y))/$h}]
1263                }]} {
1264                    return
1265                }
1266                if { $dx == 0 && $dy == 0 } {
1267                    return
1268                }
1269                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1270                EventuallyRotate $q
1271                set _click(x) $x
1272                set _click(y) $y
1273            }
1274        }
1275        "release" {
1276            Rotate drag $x $y
1277            $itk_component(view) configure -cursor ""
1278            catch {unset _click}
1279        }
1280        default {
1281            error "bad option \"$option\": should be click, drag, release"
1282        }
1283    }
1284}
1285
1286itcl::body Rappture::VtkViewer::Pick {x y} {
1287    foreach tag [CurrentDatasets -visible] {
1288        SendCmd "dataset getscalar pixel $x $y $tag"
1289    }
1290}
1291
1292# ----------------------------------------------------------------------
1293# USAGE: $this Pan click x y
1294#        $this Pan drag x y
1295#        $this Pan release x y
1296#
1297# Called automatically when the user clicks on one of the zoom
1298# controls for this widget.  Changes the zoom for the current view.
1299# ----------------------------------------------------------------------
1300itcl::body Rappture::VtkViewer::Pan {option x y} {
1301    switch -- $option {
1302        "set" {
1303            set w [winfo width $itk_component(view)]
1304            set h [winfo height $itk_component(view)]
1305            set x [expr $x / double($w)]
1306            set y [expr $y / double($h)]
1307            set _view(-xpan) [expr $_view(-xpan) + $x]
1308            set _view(-ypan) [expr $_view(-ypan) + $y]
1309            PanCamera
1310            return
1311        }
1312        "click" {
1313            set _click(x) $x
1314            set _click(y) $y
1315            $itk_component(view) configure -cursor hand1
1316        }
1317        "drag" {
1318            if { ![info exists _click(x)] } {
1319                set _click(x) $x
1320            }
1321            if { ![info exists _click(y)] } {
1322                set _click(y) $y
1323            }
1324            set w [winfo width $itk_component(view)]
1325            set h [winfo height $itk_component(view)]
1326            set dx [expr ($_click(x) - $x)/double($w)]
1327            set dy [expr ($_click(y) - $y)/double($h)]
1328            set _click(x) $x
1329            set _click(y) $y
1330            set _view(-xpan) [expr $_view(-xpan) - $dx]
1331            set _view(-ypan) [expr $_view(-ypan) - $dy]
1332            PanCamera
1333        }
1334        "release" {
1335            Pan drag $x $y
1336            $itk_component(view) configure -cursor ""
1337        }
1338        default {
1339            error "unknown option \"$option\": should set, click, drag, or release"
1340        }
1341    }
1342}
1343
1344# ----------------------------------------------------------------------
1345# USAGE: InitSettings <what> ?<value>?
1346#
1347# Used internally to update rendering settings whenever parameters
1348# change in the popup settings panel.  Sends the new settings off
1349# to the back end.
1350# ----------------------------------------------------------------------
1351itcl::body Rappture::VtkViewer::InitSettings { args } {
1352    foreach setting $args {
1353        AdjustSetting $setting
1354    }
1355}
1356
1357#
1358# AdjustSetting --
1359#
1360# Changes/updates a specific setting in the widget.  There are
1361# usually user-setable option.  Commands are sent to the render
1362# server.
1363#
1364itcl::body Rappture::VtkViewer::AdjustSetting {what {value ""}} {
1365    if { ![isconnected] } {
1366        return
1367    }
1368    switch -- $what {
1369        "glyphs-opacity" {
1370            foreach dataset [CurrentDatasets -visible $_first] {
1371                foreach { dataobj comp } [split $dataset -] break
1372                if { [$dataobj type $comp] == "glyphs" } {
1373                    SetOpacity $dataset
1374                }
1375            }
1376        }
1377        "glyphs-outline" {
1378            set bool $_settings($what)
1379            foreach dataset [CurrentDatasets -visible $_first] {
1380                foreach { dataobj comp } [split $dataset -] break
1381                set type [$dataobj type $comp]
1382                if { $type == "glyphs" } {
1383                    SendCmd "outline visible $bool $dataset"
1384                }
1385            }
1386        }
1387        "glyphs-wireframe" {
1388            set bool $_settings($what)
1389            foreach dataset [CurrentDatasets -visible $_first] {
1390                foreach { dataobj comp } [split $dataset -] break
1391                set type [$dataobj type $comp]
1392                if { $type == "glyphs" } {
1393                    SendCmd "$type wireframe $bool $dataset"
1394                }
1395            }
1396        }
1397        "glyphs-visible" {
1398            set bool $_settings($what)
1399            foreach dataset [CurrentDatasets -visible $_first] {
1400                foreach { dataobj comp } [split $dataset -] break
1401                set type [$dataobj type $comp]
1402                if { $type == "glyphs" } {
1403                    SendCmd "$type visible $bool $dataset"
1404                }
1405            }
1406        }
1407        "glyphs-lighting" {
1408            set bool $_settings($what)
1409            foreach dataset [CurrentDatasets -visible $_first] {
1410                foreach { dataobj comp } [split $dataset -] break
1411                set type [$dataobj type $comp]
1412                if { $type == "glyphs" } {
1413                    SendCmd "$type lighting $bool $dataset"
1414                }
1415            }
1416        }
1417        "glyphs-edges" {
1418            set bool $_settings($what)
1419            foreach dataset [CurrentDatasets -visible $_first] {
1420                foreach { dataobj comp } [split $dataset -] break
1421                set type [$dataobj type $comp]
1422                if { $type == "glyphs" } {
1423                    SendCmd "$type edges $bool $dataset"
1424                }
1425            }
1426        }
1427        "glyphs-palette" {
1428            set palette [$itk_component(glyphspalette) value]
1429            set _settings($what) $palette
1430            foreach dataset [CurrentDatasets -visible $_first] {
1431                foreach {dataobj comp} [split $dataset -] break
1432                set type [$dataobj type $comp]
1433                if { $type == "glyphs" } {
1434                    ChangeColormap $dataobj $comp $palette
1435                    # FIXME: fill in current selected fieldname
1436                    #SendCmd "glyphs colormode scalar {} $dataset"
1437                }
1438            }
1439            set _legendPending 1
1440        }
1441        "polydata-opacity" {
1442            foreach dataset [CurrentDatasets -visible $_first] {
1443                foreach { dataobj comp } [split $dataset -] break
1444                if { [$dataobj type $comp] == "polydata" } {
1445                    SetOpacity $dataset
1446                }
1447            }
1448        }
1449        "polydata-outline" {
1450            set bool $_settings($what)
1451            foreach dataset [CurrentDatasets -visible $_first] {
1452                foreach { dataobj comp } [split $dataset -] break
1453                set type [$dataobj type $comp]
1454                if { $type == "polydata" } {
1455                    SendCmd "outline visible $bool $dataset"
1456                }
1457            }
1458        }
1459        "polydata-wireframe" {
1460            set bool $_settings($what)
1461            foreach dataset [CurrentDatasets -visible $_first] {
1462                foreach { dataobj comp } [split $dataset -] break
1463                set type [$dataobj type $comp]
1464                if { $type == "polydata" } {
1465                    SendCmd "$type wireframe $bool $dataset"
1466                }
1467            }
1468        }
1469        "polydata-visible" {
1470            set bool $_settings($what)
1471            foreach dataset [CurrentDatasets -visible $_first] {
1472                foreach { dataobj comp } [split $dataset -] break
1473                set type [$dataobj type $comp]
1474                if { $type == "polydata" } {
1475                    SendCmd "$type visible $bool $dataset"
1476                }
1477            }
1478        }
1479        "polydata-lighting" {
1480            set bool $_settings($what)
1481            foreach dataset [CurrentDatasets -visible $_first] {
1482                foreach { dataobj comp } [split $dataset -] break
1483                set type [$dataobj type $comp]
1484                if { $type == "polydata" } {
1485                    SendCmd "$type lighting $bool $dataset"
1486                }
1487            }
1488        }
1489        "polydata-edges" {
1490            set bool $_settings($what)
1491            foreach dataset [CurrentDatasets -visible $_first] {
1492                foreach { dataobj comp } [split $dataset -] break
1493                set type [$dataobj type $comp]
1494                if { $type == "polydata" } {
1495                    SendCmd "$type edges $bool $dataset"
1496                }
1497            }
1498        }
1499        "polydata-palette" {
1500            set palette [$itk_component(meshpalette) value]
1501            set _settings($what) $palette
1502            foreach dataset [CurrentDatasets -visible $_first] {
1503                foreach {dataobj comp} [split $dataset -] break
1504                set type [$dataobj type $comp]
1505                if { $type == "polydata" } {
1506                    ChangeColormap $dataobj $comp $palette
1507                    # FIXME: fill in current selected fieldname
1508                    #SendCmd "polydata colormode scalar {} $dataset"
1509                }
1510            }
1511            set _legendPending 1
1512        }
1513        "molecule-opacity" {
1514            foreach dataset [CurrentDatasets -visible $_first] {
1515                foreach { dataobj comp } [split $dataset -] break
1516                if { [$dataobj type $comp] == "molecule" } {
1517                    SetOpacity $dataset
1518                }
1519            }
1520        }
1521        "molecule-outline" {
1522            set bool $_settings($what)
1523            foreach dataset [CurrentDatasets -visible $_first] {
1524                foreach { dataobj comp } [split $dataset -] break
1525                set type [$dataobj type $comp]
1526                if { $type == "molecule" } {
1527                    SendCmd "outline visible $bool $dataset"
1528                }
1529            }
1530        }
1531        "molecule-wireframe" {
1532            set bool $_settings($what)
1533            foreach dataset [CurrentDatasets -visible $_first] {
1534                foreach { dataobj comp } [split $dataset -] break
1535                set type [$dataobj type $comp]
1536                if { $type == "molecule" } {
1537                    SendCmd "molecule wireframe $bool $dataset"
1538                }
1539            }
1540        }
1541        "molecule-visible" {
1542            set bool $_settings($what)
1543            foreach dataset [CurrentDatasets -visible $_first] {
1544                foreach { dataobj comp } [split $dataset -] break
1545                set type [$dataobj type $comp]
1546                if { $type == "molecule" } {
1547                    SendCmd "molecule visible $bool $dataset"
1548                }
1549            }
1550        }
1551        "molecule-lighting" {
1552            set bool $_settings($what)
1553            foreach dataset [CurrentDatasets -visible $_first] {
1554                foreach { dataobj comp } [split $dataset -] break
1555                set type [$dataobj type $comp]
1556                if { $type == "molecule" } {
1557                    SendCmd "molecule lighting $bool $dataset"
1558                }
1559            }
1560        }
1561        "molecule-edges" {
1562            set bool $_settings($what)
1563            foreach dataset [CurrentDatasets -visible $_first] {
1564                foreach { dataobj comp } [split $dataset -] break
1565                set type [$dataobj type $comp]
1566                if { $type == "molecule" } {
1567                    SendCmd "molecule edges $bool $dataset"
1568                }
1569            }
1570        }
1571        "molecule-palette" {
1572            set palette [$itk_component(moleculepalette) value]
1573            set _settings($what) $palette
1574            foreach dataset [CurrentDatasets -visible $_first] {
1575                foreach {dataobj comp} [split $dataset -] break
1576                set type [$dataobj type $comp]
1577                if { $type == "molecule" } {
1578                    ChangeColormap $dataobj $comp $palette
1579                    if { $palette == "elementDefault" } {
1580                        SendCmd "molecule colormode by_elements element $dataset"
1581                    } else {
1582                        # FIXME: Set the chosen scalar field name here
1583                        SendCmd "molecule colormode scalar {} $dataset"
1584                    }
1585                }
1586            }
1587            set _legendPending 1
1588        }
1589        "molecule-representation" {
1590            set value [$itk_component(representation) value]
1591            set value [$itk_component(representation) translate $value]
1592            switch -- $value {
1593                "ballandstick" {
1594                    set _settings(molecule-rscale) covalent
1595                    set _settings(molecule-atoms-visible) 1
1596                    set _settings(molecule-bonds-visible) 1
1597                    set _settings(molecule-bondstyle) cylinder
1598                    set _settings(molecule-atomscale) 0.3
1599                    set _settings(molecule-bondscale) 0.075
1600                }
1601                "balls" - "spheres" {
1602                    set _settings(molecule-rscale) covalent
1603                    set _settings(molecule-atoms-visible) 1
1604                    set _settings(molecule-bonds-visible) 0
1605                    set _settings(molecule-bondstyle) cylinder
1606                    set _settings(molecule-atomscale) 0.3
1607                    set _settings(molecule-bondscale) 0.075
1608                }
1609                "sticks" {
1610                    set _settings(molecule-rscale) none
1611                    set _settings(molecule-atoms-visible) 1
1612                    set _settings(molecule-bonds-visible) 1
1613                    set _settings(molecule-bondstyle) cylinder
1614                    set _settings(molecule-atomscale) 0.075
1615                    set _settings(molecule-bondscale) 0.075
1616                }
1617                "spacefilling" {
1618                    set _settings(molecule-rscale) van_der_waals
1619                    set _settings(molecule-atoms-visible) 1
1620                    set _settings(molecule-bonds-visible) 0
1621                    set _settings(molecule-bondstyle) cylinder
1622                    set _settings(molecule-atomscale) 1.0
1623                    set _settings(molecule-bondscale) 0.075
1624                }
1625                "rods"  {
1626                    set _settings(molecule-rscale) none
1627                    set _settings(molecule-atoms-visible) 1
1628                    set _settings(molecule-bonds-visible) 1
1629                    set _settings(molecule-bondstyle) cylinder
1630                    set _settings(molecule-atomscale) 0.1
1631                    set _settings(molecule-bondscale) 0.1
1632                }
1633                "wireframe" - "lines" {
1634                    set _settings(molecule-rscale) none
1635                    set _settings(molecule-atoms-visible) 0
1636                    set _settings(molecule-bonds-visible) 1
1637                    set _settings(molecule-bondstyle) line
1638                    set _settings(molecule-atomscale) 1.0
1639                    set _settings(molecule-bondscale) 1.0
1640                }
1641                default {
1642                    error "unknown representation $value"
1643                }
1644            }
1645            $itk_component(rscale) value [$itk_component(rscale) label $_settings(molecule-rscale)]
1646            switch -- $value {
1647                "ballandstick" - "balls" - "spheres" {
1648                    $itk_component(rscale) configure -state normal
1649                }
1650                default {
1651                    $itk_component(rscale) configure -state disabled
1652                }
1653            }
1654            foreach dataset [CurrentDatasets -all] {
1655                foreach {dataobj comp} [split $dataset -] break
1656                set type [$dataobj type $comp]
1657                if { $type == "molecule" } {
1658                    StartBufferingCommands
1659                    SendCmd [subst {molecule rscale $_settings(molecule-rscale) $dataset}]
1660                    SendCmd [subst {molecule ascale $_settings(molecule-atomscale) $dataset}]
1661                    SendCmd [subst {molecule bscale $_settings(molecule-bondscale) $dataset}]
1662                    SendCmd [subst {molecule bstyle $_settings(molecule-bondstyle) $dataset}]
1663                    SendCmd [subst {molecule atoms $_settings(molecule-atoms-visible) $dataset}]
1664                    SendCmd [subst {molecule bonds $_settings(molecule-bonds-visible) $dataset}]
1665                    StopBufferingCommands
1666                }
1667            }
1668        }
1669        "molecule-rscale" {
1670            set value [$itk_component(rscale) value]
1671            set value [$itk_component(rscale) translate $value]
1672            set _settings($what) $value
1673            foreach dataset [CurrentDatasets -visible $_first] {
1674                foreach {dataobj comp} [split $dataset -] break
1675                set type [$dataobj type $comp]
1676                if { $type == "molecule" } {
1677                    SendCmd [subst {molecule rscale $_settings($what) $dataset}]
1678                }
1679            }
1680        }
1681        "molecule-labels" {
1682            set bool $_settings($what)
1683            foreach dataset [CurrentDatasets -visible $_first] {
1684               foreach { dataobj comp } [split $dataset -] break
1685               set type [$dataobj type $comp]
1686               if { $type == "molecule" } {
1687                   SendCmd "molecule labels $bool $dataset"
1688               }
1689            }
1690        }
1691        "axis-visible" {
1692            set bool $_axis(visible)
1693            SendCmd "axis visible all $bool"
1694        }
1695        "axis-labels" {
1696            set bool $_axis(labels)
1697            SendCmd "axis labels all $bool"
1698        }
1699        "axis-minorticks" {
1700            set bool $_axis(minorticks)
1701            SendCmd "axis minticks all $bool"
1702        }
1703        "axis-xgrid" {
1704            set bool $_axis(xgrid)
1705            SendCmd "axis grid x $bool"
1706        }
1707        "axis-ygrid" {
1708            set bool $_axis(ygrid)
1709            SendCmd "axis grid y $bool"
1710        }
1711        "axis-zgrid" {
1712            set bool $_axis(zgrid)
1713            SendCmd "axis grid z $bool"
1714        }
1715        "axis-mode" {
1716            set mode [$itk_component(axismode) value]
1717            set mode [$itk_component(axismode) translate $mode]
1718            SendCmd "axis flymode $mode"
1719        }
1720        "axis-xcutaway" - "axis-ycutaway" - "axis-zcutaway" {
1721            set axis [string range $what 5 5]
1722            set bool $_axis(${axis}cutaway)
1723            if { $bool } {
1724                set pos [expr $_axis(${axis}position) * 0.01]
1725                set dir $_axis(${axis}direction)
1726                $itk_component(${axis}CutScale) configure -state normal \
1727                    -troughcolor white
1728                SendCmd "renderer clipplane $axis $pos $dir"
1729            } else {
1730                $itk_component(${axis}CutScale) configure -state disabled \
1731                    -troughcolor grey82
1732                SendCmd "renderer clipplane $axis 1 -1"
1733            }
1734        }
1735        "axis-xposition" - "axis-yposition" - "axis-zposition" -
1736        "axis-xdirection" - "axis-ydirection" - "axis-zdirection" {
1737            set axis [string range $what 5 5]
1738            #set dir $_axis(${axis}direction)
1739            set pos [expr $_axis(${axis}position) * 0.01]
1740            SendCmd "renderer clipplane ${axis} $pos -1"
1741        }
1742        default {
1743            error "don't know how to fix $what"
1744        }
1745    }
1746}
1747
1748#
1749# RequestLegend --
1750#
1751# Request a new legend from the server.  The size of the legend
1752# is determined from the height of the canvas.  It will be rotated
1753# to be vertical when drawn.
1754#
1755itcl::body Rappture::VtkViewer::RequestLegend {} {
1756    set font "Arial 8"
1757    set lineht [font metrics $font -linespace]
1758    set w 12
1759    set h [expr {$_height - 2 * ($lineht + 2)}]
1760    if { $h < 1 } {
1761        return
1762    }
1763    # Set the legend on the first dataset.
1764    foreach dataset [CurrentDatasets -visible] {
1765        foreach {dataobj comp} [split $dataset -] break
1766        if { [info exists _dataset2style($dataset)] } {
1767            #SendCmd "legend $_dataset2style($dataset) vmag {} {} $w $h 0"
1768            SendCmd "legend2 $_dataset2style($dataset) $w $h"
1769            break;
1770        }
1771    }
1772}
1773
1774#
1775# ChangeColormap --
1776#
1777itcl::body Rappture::VtkViewer::ChangeColormap {dataobj comp color} {
1778    set tag $dataobj-$comp
1779    if { ![info exist _style($tag)] } {
1780        error "no initial colormap"
1781    }
1782    array set style $_style($tag)
1783    set style(-color) $color
1784    set _style($tag) [array get style]
1785    SetColormap $dataobj $comp
1786}
1787
1788#
1789# SetColormap --
1790#
1791itcl::body Rappture::VtkViewer::SetColormap { dataobj comp } {
1792    array set style {
1793        -color BCGYR
1794    }
1795    if {[$dataobj type $comp] == "molecule"} {
1796        set style(-color) elementDefault
1797    }
1798    set tag $dataobj-$comp
1799    if { ![info exists _initialStyle($tag)] } {
1800        # Save the initial component style.
1801        set _initialStyle($tag) [$dataobj style $comp]
1802    }
1803
1804    # Override defaults with initial style defined in xml.
1805    array set style $_initialStyle($tag)
1806
1807    if { ![info exists _style($tag)] } {
1808        set _style($tag) [array get style]
1809    }
1810    # Override initial style with current style.
1811    array set style $_style($tag)
1812
1813    set name "$style(-color)"
1814    if { ![info exists _colormaps($name)] } {
1815        BuildColormap $name
1816        set _colormaps($name) 1
1817    }
1818    if { ![info exists _dataset2style($tag)] ||
1819         $_dataset2style($tag) != $name } {
1820        set _dataset2style($tag) $name
1821        switch -- [$dataobj type $comp] {
1822            "polydata" {
1823                SendCmd "polydata colormap $name $tag"
1824            }
1825            "glyphs" {
1826                SendCmd "glyphs colormap $name $tag"
1827            }
1828            "molecule" {
1829                SendCmd "molecule colormap $name $tag"
1830            }
1831        }
1832    }
1833}
1834
1835#
1836# BuildColormap --
1837#
1838itcl::body Rappture::VtkViewer::BuildColormap { name } {
1839    if { $name == "elementDefault" } {
1840        return
1841    }
1842    set cmap [ColorsToColormap $name]
1843    if { [llength $cmap] == 0 } {
1844        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
1845    }
1846    set amap "0.0 1.0 1.0 1.0"
1847    SendCmd "colormap add $name { $cmap } { $amap }"
1848}
1849
1850# ----------------------------------------------------------------------
1851# CONFIGURATION OPTION: -plotbackground
1852# ----------------------------------------------------------------------
1853itcl::configbody Rappture::VtkViewer::plotbackground {
1854    if { [isconnected] } {
1855        set rgb [Color2RGB $itk_option(-plotbackground)]
1856        SendCmd "screen bgcolor $rgb"
1857    }
1858}
1859
1860# ----------------------------------------------------------------------
1861# CONFIGURATION OPTION: -plotforeground
1862# ----------------------------------------------------------------------
1863itcl::configbody Rappture::VtkViewer::plotforeground {
1864    if { [isconnected] } {
1865        set rgb [Color2RGB $itk_option(-plotforeground)]
1866        SendCmd "axis color all $rgb"
1867        SendCmd "outline color $rgb"
1868    }
1869}
1870
1871itcl::body Rappture::VtkViewer::limits { dataobj } {
1872    foreach comp [$dataobj components] {
1873        set tag $dataobj-$comp
1874        if { ![info exists _limits($tag)] } {
1875            set data [$dataobj data $comp]
1876            if { $data == "" } {
1877                continue
1878            }
1879            set tmpfile file[pid].vtk
1880            set f [open "$tmpfile" "w"]
1881            fconfigure $f -translation binary -encoding binary
1882            puts $f $data
1883            close $f
1884            set reader [vtkDataSetReader $tag-xvtkDataSetReader]
1885            $reader SetFileName $tmpfile
1886set debug 0
1887            if {$debug} {
1888                # Only needed for debug output below
1889                $reader ReadAllNormalsOn
1890                $reader ReadAllTCoordsOn
1891                $reader ReadAllScalarsOn
1892                $reader ReadAllColorScalarsOn
1893                $reader ReadAllVectorsOn
1894                $reader ReadAllTensorsOn
1895                $reader ReadAllFieldsOn
1896            }
1897            $reader Update
1898            file delete $tmpfile
1899            set output [$reader GetOutput]
1900            if { $output == "" } {
1901                # Invalid VTK file -- loader failed to parse
1902                continue
1903            }
1904            set _limits($tag) [$output GetBounds]
1905            if {$debug} {
1906                puts stderr "\#scalars=[$reader GetNumberOfScalarsInFile]"
1907                puts stderr "\#vectors=[$reader GetNumberOfVectorsInFile]"
1908                puts stderr "\#tensors=[$reader GetNumberOfTensorsInFile]"
1909                puts stderr "\#normals=[$reader GetNumberOfNormalsInFile]"
1910                puts stderr "\#tcoords=[$reader GetNumberOfTCoordsInFile]"
1911                puts stderr "\#fielddata=[$reader GetNumberOfFieldDataInFile]"
1912                puts stderr "fielddataname=[$reader GetFieldDataNameInFile 0]"
1913                set pointData [$output GetPointData]
1914                if { $pointData != ""} {
1915                    puts stderr "point \#arrays=[$pointData GetNumberOfArrays]"
1916                    puts stderr "point \#components=[$pointData GetNumberOfComponents]"
1917                    puts stderr "point \#tuples=[$pointData GetNumberOfTuples]"
1918                    puts stderr "point scalars=[$pointData GetScalars]"
1919                    puts stderr "point vectors=[$pointData GetVectors]"
1920                }
1921                set cellData [$output GetCellData]
1922                if { $cellData != ""} {
1923                    puts stderr "cell \#arrays=[$cellData GetNumberOfArrays]"
1924                    puts stderr "cell \#components=[$cellData GetNumberOfComponents]"
1925                    puts stderr "cell \#tuples=[$cellData GetNumberOfTuples]"
1926                    puts stderr "cell scalars=[$cellData GetScalars]"
1927                    puts stderr "cell vectors=[$cellData GetVectors]"
1928                }
1929                set fieldData [$output GetFieldData]
1930                if { $fieldData != ""} {
1931                    puts stderr "field \#arrays=[$fieldData GetNumberOfArrays]"
1932                    puts stderr "field \#components=[$fieldData GetNumberOfComponents]"
1933                    puts stderr "field \#tuples=[$fieldData GetNumberOfTuples]"
1934                }
1935            }
1936            rename $output ""
1937            rename $reader ""
1938        }
1939        foreach { xMin xMax yMin yMax zMin zMax} $_limits($tag) break
1940        if {![info exists limits(xmin)] || $limits(xmin) > $xMin} {
1941            set limits(xmin) $xMin
1942        }
1943        if {![info exists limits(xmax)] || $limits(xmax) < $xMax} {
1944            set limits(xmax) $xMax
1945        }
1946        if {![info exists limits(ymin)] || $limits(ymin) > $yMin} {
1947            set limits(ymin) $xMin
1948        }
1949        if {![info exists limits(ymax)] || $limits(ymax) < $yMax} {
1950            set limits(ymax) $yMax
1951        }
1952        if {![info exists limits(zmin)] || $limits(zmin) > $zMin} {
1953            set limits(zmin) $zMin
1954        }
1955        if {![info exists limits(zmax)] || $limits(zmax) < $zMax} {
1956            set limits(zmax) $zMax
1957        }
1958    }
1959    return [array get limits]
1960}
1961
1962itcl::body Rappture::VtkViewer::BuildGlyphsTab {} {
1963
1964    set fg [option get $itk_component(hull) font Font]
1965    #set bfg [option get $itk_component(hull) boldFont Font]
1966
1967    set inner [$itk_component(main) insert 0 \
1968        -title "Glyph Settings" \
1969        -icon [Rappture::icon volume-on]]
1970    $inner configure -borderwidth 4
1971
1972    checkbutton $inner.glyphs \
1973        -text "Show Glyphs" \
1974        -variable [itcl::scope _settings(glyphs-visible)] \
1975        -command [itcl::code $this AdjustSetting glyphs-visible] \
1976        -font "Arial 9" -anchor w
1977
1978    checkbutton $inner.outline \
1979        -text "Show Outline" \
1980        -variable [itcl::scope _settings(glyphs-outline)] \
1981        -command [itcl::code $this AdjustSetting glyphs-outline] \
1982        -font "Arial 9" -anchor w
1983
1984    checkbutton $inner.wireframe \
1985        -text "Show Wireframe" \
1986        -variable [itcl::scope _settings(glyphs-wireframe)] \
1987        -command [itcl::code $this AdjustSetting glyphs-wireframe] \
1988        -font "Arial 9" -anchor w
1989
1990    checkbutton $inner.lighting \
1991        -text "Enable Lighting" \
1992        -variable [itcl::scope _settings(glyphs-lighting)] \
1993        -command [itcl::code $this AdjustSetting glyphs-lighting] \
1994        -font "Arial 9" -anchor w
1995
1996    checkbutton $inner.edges \
1997        -text "Show Edges" \
1998        -variable [itcl::scope _settings(glyphs-edges)] \
1999        -command [itcl::code $this AdjustSetting glyphs-edges] \
2000        -font "Arial 9" -anchor w
2001
2002    label $inner.palette_l -text "Palette" -font "Arial 9" -anchor w
2003    itk_component add glyphspalette {
2004        Rappture::Combobox $inner.palette -width 10 -editable 0
2005    }
2006    $inner.palette choices insert end [GetColormapList]
2007    $itk_component(glyphspalette) value "BCGYR"
2008    bind $inner.palette <<Value>> \
2009        [itcl::code $this AdjustSetting glyphs-palette]
2010
2011    label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w
2012    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2013        -variable [itcl::scope _settings(glyphs-opacity)] \
2014        -width 10 \
2015        -showvalue off \
2016        -command [itcl::code $this EventuallySetGlyphsOpacity]
2017    $inner.opacity set $_settings(glyphs-opacity)
2018
2019    blt::table $inner \
2020        0,0 $inner.glyphs    -cspan 2  -anchor w -pady 2 \
2021        1,0 $inner.outline   -cspan 2  -anchor w -pady 2 \
2022        2,0 $inner.wireframe -cspan 2  -anchor w -pady 2 \
2023        3,0 $inner.lighting  -cspan 2  -anchor w -pady 2 \
2024        4,0 $inner.edges     -cspan 2  -anchor w -pady 2 \
2025        5,0 $inner.opacity_l -anchor w -pady 2 \
2026        5,1 $inner.opacity   -fill x   -pady 2 \
2027        6,0 $inner.palette_l -anchor w -pady 2 \
2028        6,1 $inner.palette   -fill x   -pady 2
2029
2030    blt::table configure $inner r* c* -resize none
2031    blt::table configure $inner r8 c1 -resize expand
2032}
2033
2034itcl::body Rappture::VtkViewer::BuildPolydataTab {} {
2035
2036    set fg [option get $itk_component(hull) font Font]
2037    #set bfg [option get $itk_component(hull) boldFont Font]
2038
2039    set inner [$itk_component(main) insert 0 \
2040        -title "Mesh Settings" \
2041        -icon [Rappture::icon mesh]]
2042    $inner configure -borderwidth 4
2043
2044    checkbutton $inner.mesh \
2045        -text "Show Mesh" \
2046        -variable [itcl::scope _settings(polydata-visible)] \
2047        -command [itcl::code $this AdjustSetting polydata-visible] \
2048        -font "Arial 9" -anchor w
2049
2050    checkbutton $inner.outline \
2051        -text "Show Outline" \
2052        -variable [itcl::scope _settings(polydata-outline)] \
2053        -command [itcl::code $this AdjustSetting polydata-outline] \
2054        -font "Arial 9" -anchor w
2055
2056    checkbutton $inner.wireframe \
2057        -text "Show Wireframe" \
2058        -variable [itcl::scope _settings(polydata-wireframe)] \
2059        -command [itcl::code $this AdjustSetting polydata-wireframe] \
2060        -font "Arial 9" -anchor w
2061
2062    checkbutton $inner.lighting \
2063        -text "Enable Lighting" \
2064        -variable [itcl::scope _settings(polydata-lighting)] \
2065        -command [itcl::code $this AdjustSetting polydata-lighting] \
2066        -font "Arial 9" -anchor w
2067
2068    checkbutton $inner.edges \
2069        -text "Show Edges" \
2070        -variable [itcl::scope _settings(polydata-edges)] \
2071        -command [itcl::code $this AdjustSetting polydata-edges] \
2072        -font "Arial 9" -anchor w
2073
2074    label $inner.palette_l -text "Palette" -font "Arial 9" -anchor w
2075    itk_component add meshpalette {
2076        Rappture::Combobox $inner.palette -width 10 -editable 0
2077    }
2078    $inner.palette choices insert end [GetColormapList]
2079    $itk_component(meshpalette) value "BCGYR"
2080    bind $inner.palette <<Value>> \
2081        [itcl::code $this AdjustSetting polydata-palette]
2082
2083    label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w
2084    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2085        -variable [itcl::scope _settings(polydata-opacity)] \
2086        -width 10 \
2087        -showvalue off \
2088        -command [itcl::code $this EventuallySetPolydataOpacity]
2089    $inner.opacity set $_settings(polydata-opacity)
2090
2091    blt::table $inner \
2092        0,0 $inner.mesh      -cspan 2  -anchor w -pady 2 \
2093        1,0 $inner.outline   -cspan 2  -anchor w -pady 2 \
2094        2,0 $inner.wireframe -cspan 2  -anchor w -pady 2 \
2095        3,0 $inner.lighting  -cspan 2  -anchor w -pady 2 \
2096        4,0 $inner.edges     -cspan 2  -anchor w -pady 2 \
2097        5,0 $inner.opacity_l -anchor w -pady 2 \
2098        5,1 $inner.opacity   -fill x   -pady 2 \
2099        6,0 $inner.palette_l -anchor w -pady 2 \
2100        6,1 $inner.palette   -fill x   -pady 2
2101
2102    blt::table configure $inner r* c* -resize none
2103    blt::table configure $inner r8 c1 -resize expand
2104}
2105
2106itcl::body Rappture::VtkViewer::BuildAxisTab {} {
2107
2108    set fg [option get $itk_component(hull) font Font]
2109    #set bfg [option get $itk_component(hull) boldFont Font]
2110
2111    set inner [$itk_component(main) insert end \
2112        -title "Axis Settings" \
2113        -icon [Rappture::icon axis2]]
2114    $inner configure -borderwidth 4
2115
2116    checkbutton $inner.visible \
2117        -text "Axes" \
2118        -variable [itcl::scope _axis(visible)] \
2119        -command [itcl::code $this AdjustSetting axis-visible] \
2120        -font "Arial 9"
2121
2122    checkbutton $inner.labels \
2123        -text "Axis Labels" \
2124        -variable [itcl::scope _axis(labels)] \
2125        -command [itcl::code $this AdjustSetting axis-labels] \
2126        -font "Arial 9"
2127    label $inner.grid_l -text "Grid" -font "Arial 9"
2128    checkbutton $inner.xgrid \
2129        -text "X" \
2130        -variable [itcl::scope _axis(xgrid)] \
2131        -command [itcl::code $this AdjustSetting axis-xgrid] \
2132        -font "Arial 9"
2133    checkbutton $inner.ygrid \
2134        -text "Y" \
2135        -variable [itcl::scope _axis(ygrid)] \
2136        -command [itcl::code $this AdjustSetting axis-ygrid] \
2137        -font "Arial 9"
2138    checkbutton $inner.zgrid \
2139        -text "Z" \
2140        -variable [itcl::scope _axis(zgrid)] \
2141        -command [itcl::code $this AdjustSetting axis-zgrid] \
2142        -font "Arial 9"
2143    checkbutton $inner.minorticks \
2144        -text "Minor Ticks" \
2145        -variable [itcl::scope _axis(minorticks)] \
2146        -command [itcl::code $this AdjustSetting axis-minorticks] \
2147        -font "Arial 9"
2148
2149    label $inner.mode_l -text "Mode" -font "Arial 9"
2150
2151    itk_component add axismode {
2152        Rappture::Combobox $inner.mode -width 10 -editable 0
2153    }
2154    $inner.mode choices insert end \
2155        "static_triad"    "static" \
2156        "closest_triad"   "closest" \
2157        "furthest_triad"  "farthest" \
2158        "outer_edges"     "outer"
2159    $itk_component(axismode) value "static"
2160    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting axis-mode]
2161
2162    blt::table $inner \
2163        0,0 $inner.visible -anchor w -cspan 4 \
2164        1,0 $inner.labels  -anchor w -cspan 4 \
2165        2,0 $inner.minorticks  -anchor w -cspan 4 \
2166        4,0 $inner.grid_l  -anchor w \
2167        4,1 $inner.xgrid   -anchor w \
2168        4,2 $inner.ygrid   -anchor w \
2169        4,3 $inner.zgrid   -anchor w \
2170        5,0 $inner.mode_l  -anchor w -padx { 2 0 } \
2171        5,1 $inner.mode    -fill x   -cspan 3
2172
2173    blt::table configure $inner r* c* -resize none
2174    blt::table configure $inner r7 c6 -resize expand
2175    blt::table configure $inner r3 -height 0.125i
2176}
2177
2178itcl::body Rappture::VtkViewer::BuildCameraTab {} {
2179    set inner [$itk_component(main) insert end \
2180        -title "Camera Settings" \
2181        -icon [Rappture::icon camera]]
2182    $inner configure -borderwidth 4
2183
2184    label $inner.view_l -text "view" -font "Arial 9"
2185    set f [frame $inner.view]
2186    foreach side { front back left right top bottom } {
2187        button $f.$side  -image [Rappture::icon view$side] \
2188            -command [itcl::code $this SetOrientation $side]
2189        Rappture::Tooltip::for $f.$side "Change the view to $side"
2190        pack $f.$side -side left
2191    }
2192
2193    blt::table $inner \
2194        0,0 $inner.view_l -anchor e -pady 2 \
2195        0,1 $inner.view -anchor w -pady 2
2196    blt::table configure $inner r0 -resize none
2197
2198    set labels { qx qy qz qw xpan ypan zoom }
2199    set row 1
2200    foreach tag $labels {
2201        label $inner.${tag}label -text $tag -font "Arial 9"
2202        entry $inner.${tag} -font "Arial 9"  -bg white \
2203            -textvariable [itcl::scope _view(-$tag)]
2204        bind $inner.${tag} <Return> \
2205            [itcl::code $this camera set -${tag}]
2206        bind $inner.${tag} <KP_Enter> \
2207            [itcl::code $this camera set -${tag}]
2208        blt::table $inner \
2209            $row,0 $inner.${tag}label -anchor e -pady 2 \
2210            $row,1 $inner.${tag} -anchor w -pady 2
2211        blt::table configure $inner r$row -resize none
2212        incr row
2213    }
2214    checkbutton $inner.ortho \
2215        -text "Orthographic Projection" \
2216        -variable [itcl::scope _view(-ortho)] \
2217        -command [itcl::code $this camera set -ortho] \
2218        -font "Arial 9"
2219    blt::table $inner \
2220            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
2221    blt::table configure $inner r$row -resize none
2222    incr row
2223
2224    blt::table configure $inner c* -resize none
2225    blt::table configure $inner c2 -resize expand
2226    blt::table configure $inner r$row -resize expand
2227}
2228
2229itcl::body Rappture::VtkViewer::BuildCutawayTab {} {
2230
2231    set fg [option get $itk_component(hull) font Font]
2232
2233    set inner [$itk_component(main) insert end \
2234        -title "Cutaway Along Axis" \
2235        -icon [Rappture::icon cutbutton]]
2236
2237    $inner configure -borderwidth 4
2238
2239    # X-value slicer...
2240    itk_component add xCutButton {
2241        Rappture::PushButton $inner.xbutton \
2242            -onimage [Rappture::icon x-cutplane] \
2243            -offimage [Rappture::icon x-cutplane] \
2244            -command [itcl::code $this AdjustSetting axis-xcutaway] \
2245            -variable [itcl::scope _axis(xcutaway)]
2246    }
2247    Rappture::Tooltip::for $itk_component(xCutButton) \
2248        "Toggle the X-axis cutaway on/off"
2249
2250    itk_component add xCutScale {
2251        ::scale $inner.xval -from 100 -to 0 \
2252            -width 10 -orient vertical -showvalue 1 \
2253            -borderwidth 1 -highlightthickness 0 \
2254            -command [itcl::code $this Slice move x] \
2255            -variable [itcl::scope _axis(xposition)]
2256    } {
2257        usual
2258        ignore -borderwidth -highlightthickness
2259    }
2260    # Set the default cutaway value before disabling the scale.
2261    $itk_component(xCutScale) set 100
2262    $itk_component(xCutScale) configure -state disabled
2263    Rappture::Tooltip::for $itk_component(xCutScale) \
2264        "@[itcl::code $this Slice tooltip x]"
2265
2266    itk_component add xDirButton {
2267        Rappture::PushButton $inner.xdir \
2268            -onimage [Rappture::icon arrow-down] \
2269            -onvalue -1 \
2270            -offimage [Rappture::icon arrow-up] \
2271            -offvalue 1 \
2272            -command [itcl::code $this AdjustSetting axis-xdirection] \
2273            -variable [itcl::scope _axis(xdirection)]
2274    }
2275    set _axis(xdirection) -1
2276    Rappture::Tooltip::for $itk_component(xDirButton) \
2277        "Toggle the direction of the X-axis cutaway"
2278
2279    # Y-value slicer...
2280    itk_component add yCutButton {
2281        Rappture::PushButton $inner.ybutton \
2282            -onimage [Rappture::icon y-cutplane] \
2283            -offimage [Rappture::icon y-cutplane] \
2284            -command [itcl::code $this AdjustSetting axis-ycutaway] \
2285            -variable [itcl::scope _axis(ycutaway)]
2286    }
2287    Rappture::Tooltip::for $itk_component(yCutButton) \
2288        "Toggle the Y-axis cutaway on/off"
2289
2290    itk_component add yCutScale {
2291        ::scale $inner.yval -from 100 -to 0 \
2292            -width 10 -orient vertical -showvalue 1 \
2293            -borderwidth 1 -highlightthickness 0 \
2294            -command [itcl::code $this Slice move y] \
2295            -variable [itcl::scope _axis(yposition)]
2296    } {
2297        usual
2298        ignore -borderwidth -highlightthickness
2299    }
2300    Rappture::Tooltip::for $itk_component(yCutScale) \
2301        "@[itcl::code $this Slice tooltip y]"
2302    # Set the default cutaway value before disabling the scale.
2303    $itk_component(yCutScale) set 100
2304    $itk_component(yCutScale) configure -state disabled
2305
2306    itk_component add yDirButton {
2307        Rappture::PushButton $inner.ydir \
2308            -onimage [Rappture::icon arrow-down] \
2309            -onvalue -1 \
2310            -offimage [Rappture::icon arrow-up] \
2311            -offvalue 1 \
2312            -command [itcl::code $this AdjustSetting axis-ydirection] \
2313            -variable [itcl::scope _axis(ydirection)]
2314    }
2315    Rappture::Tooltip::for $itk_component(yDirButton) \
2316        "Toggle the direction of the Y-axis cutaway"
2317    set _axis(ydirection) -1
2318
2319    # Z-value slicer...
2320    itk_component add zCutButton {
2321        Rappture::PushButton $inner.zbutton \
2322            -onimage [Rappture::icon z-cutplane] \
2323            -offimage [Rappture::icon z-cutplane] \
2324            -command [itcl::code $this AdjustSetting axis-zcutaway] \
2325            -variable [itcl::scope _axis(zcutaway)]
2326    }
2327    Rappture::Tooltip::for $itk_component(zCutButton) \
2328        "Toggle the Z-axis cutaway on/off"
2329
2330    itk_component add zCutScale {
2331        ::scale $inner.zval -from 100 -to 0 \
2332            -width 10 -orient vertical -showvalue 1 \
2333            -borderwidth 1 -highlightthickness 0 \
2334            -command [itcl::code $this Slice move z] \
2335            -variable [itcl::scope _axis(zposition)]
2336    } {
2337        usual
2338        ignore -borderwidth -highlightthickness
2339    }
2340    $itk_component(zCutScale) set 100
2341    $itk_component(zCutScale) configure -state disabled
2342    Rappture::Tooltip::for $itk_component(zCutScale) \
2343        "@[itcl::code $this Slice tooltip z]"
2344
2345    itk_component add zDirButton {
2346        Rappture::PushButton $inner.zdir \
2347            -onimage [Rappture::icon arrow-down] \
2348            -onvalue -1 \
2349            -offimage [Rappture::icon arrow-up] \
2350            -offvalue 1 \
2351            -command [itcl::code $this AdjustSetting axis-zdirection] \
2352            -variable [itcl::scope _axis(zdirection)]
2353    }
2354    set _axis(zdirection) -1
2355    Rappture::Tooltip::for $itk_component(zDirButton) \
2356        "Toggle the direction of the Z-axis cutaway"
2357
2358    blt::table $inner \
2359        0,0 $itk_component(xCutButton)  -anchor e -padx 2 -pady 2 \
2360        1,0 $itk_component(xCutScale)   -fill y \
2361        0,1 $itk_component(yCutButton)  -anchor e -padx 2 -pady 2 \
2362        1,1 $itk_component(yCutScale)   -fill y \
2363        0,2 $itk_component(zCutButton)  -anchor e -padx 2 -pady 2 \
2364        1,2 $itk_component(zCutScale)   -fill y \
2365
2366    blt::table configure $inner r* c* -resize none
2367    blt::table configure $inner r1 c3 -resize expand
2368}
2369
2370itcl::body Rappture::VtkViewer::BuildMoleculeTab {} {
2371    set fg [option get $itk_component(hull) font Font]
2372
2373    set inner [$itk_component(main) insert 0 \
2374        -title "Molecule Settings" \
2375        -icon [Rappture::icon molecule]]
2376    $inner configure -borderwidth 4
2377
2378    checkbutton $inner.molecule \
2379        -text "Show Molecule" \
2380        -variable [itcl::scope _settings(molecule-visible)] \
2381        -command [itcl::code $this AdjustSetting molecule-visible] \
2382        -font "Arial 9"
2383
2384    checkbutton $inner.outline \
2385        -text "Show Outline" \
2386        -variable [itcl::scope _settings(molecule-outline)] \
2387        -command [itcl::code $this AdjustSetting molecule-outline] \
2388        -font "Arial 9"
2389
2390    checkbutton $inner.label \
2391        -text "Show Atom Labels" \
2392        -variable [itcl::scope _settings(molecule-labels)] \
2393        -command [itcl::code $this AdjustSetting molecule-labels] \
2394        -font "Arial 9"
2395
2396    checkbutton $inner.wireframe \
2397        -text "Show Wireframe" \
2398        -variable [itcl::scope _settings(molecule-wireframe)] \
2399        -command [itcl::code $this AdjustSetting molecule-wireframe] \
2400        -font "Arial 9"
2401
2402    checkbutton $inner.lighting \
2403        -text "Enable Lighting" \
2404        -variable [itcl::scope _settings(molecule-lighting)] \
2405        -command [itcl::code $this AdjustSetting molecule-lighting] \
2406        -font "Arial 9"
2407
2408    checkbutton $inner.edges \
2409        -text "Show Edges" \
2410        -variable [itcl::scope _settings(molecule-edges)] \
2411        -command [itcl::code $this AdjustSetting molecule-edges] \
2412        -font "Arial 9"
2413
2414    label $inner.rep_l -text "Molecule Representation" \
2415        -font "Arial 9"
2416
2417    itk_component add representation {
2418        Rappture::Combobox $inner.rep -width 20 -editable 0
2419    }
2420    $inner.rep choices insert end \
2421        "ballandstick"  "Ball and Stick" \
2422        "spheres"       "Spheres"        \
2423        "sticks"        "Sticks"         \
2424        "rods"          "Rods"           \
2425        "wireframe"     "Wireframe"      \
2426        "spacefilling"  "Space Filling"
2427
2428    bind $inner.rep <<Value>> \
2429        [itcl::code $this AdjustSetting molecule-representation]
2430    $inner.rep value "Ball and Stick"
2431
2432    label $inner.rscale_l -text "Atom Radii" \
2433        -font "Arial 9"
2434
2435    itk_component add rscale {
2436        Rappture::Combobox $inner.rscale -width 20 -editable 0
2437    }
2438    $inner.rscale choices insert end \
2439        "atomic"        "Atomic"   \
2440        "covalent"      "Covalent" \
2441        "van_der_waals" "VDW"      \
2442        "none"          "Constant"
2443
2444    bind $inner.rscale <<Value>> \
2445        [itcl::code $this AdjustSetting molecule-rscale]
2446    $inner.rscale value "Covalent"
2447
2448    label $inner.palette_l -text "Palette" -font "Arial 9"
2449    itk_component add moleculepalette {
2450        Rappture::Combobox $inner.palette -width 10 -editable 0
2451    }
2452    $inner.palette choices insert end [GetColormapList -includeElementDefault]
2453    $itk_component(moleculepalette) value "elementDefault"
2454    bind $inner.palette <<Value>> \
2455        [itcl::code $this AdjustSetting molecule-palette]
2456
2457    label $inner.atomscale_l -text "Atom Scale" -font "Arial 9"
2458    ::scale $inner.atomscale -width 15 -font "Arial 7" \
2459        -from 0.025 -to 2.0 -resolution 0.025 -label "" \
2460        -showvalue true -orient horizontal \
2461        -command [itcl::code $this EventuallySetAtomScale] \
2462        -variable [itcl::scope _settings(molecule-atomscale)]
2463    $inner.atomscale set $_settings(molecule-atomscale)
2464    Rappture::Tooltip::for $inner.atomscale \
2465        "Adjust relative scale of atoms (spheres or balls)."
2466
2467    label $inner.bondscale_l -text "Bond Scale" -font "Arial 9"
2468    ::scale $inner.bondscale -width 15 -font "Arial 7" \
2469        -from 0.005 -to 0.3 -resolution 0.005 -label "" \
2470        -showvalue true -orient horizontal \
2471        -command [itcl::code $this EventuallySetBondScale] \
2472        -variable [itcl::scope _settings(molecule-bondscale)]
2473    Rappture::Tooltip::for $inner.bondscale \
2474        "Adjust scale of bonds (sticks)."
2475    $inner.bondscale set $_settings(molecule-bondscale)
2476
2477    label $inner.opacity_l -text "Opacity" -font "Arial 9"
2478    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2479        -variable [itcl::scope _settings(molecule-opacity)] \
2480        -width 15 -font "Arial 7" \
2481        -showvalue on \
2482        -command [itcl::code $this EventuallySetMoleculeOpacity]
2483
2484    label $inner.quality_l -text "Quality" -font "Arial 9"
2485    ::scale $inner.quality -width 15 -font "Arial 7" \
2486        -from 0.0 -to 10.0 -resolution 0.1 -label "" \
2487        -showvalue true -orient horizontal \
2488        -command [itcl::code $this EventuallySetMoleculeQuality] \
2489        -variable [itcl::scope _settings(molecule-quality)]
2490    Rappture::Tooltip::for $inner.quality \
2491        "Adjust tesselation quality"
2492    $inner.quality set $_settings(molecule-quality)
2493
2494    blt::table $inner \
2495        0,0 $inner.molecule     -anchor w -pady {1 0} \
2496        1,0 $inner.outline      -anchor w -pady {1 0} \
2497        2,0 $inner.label        -anchor w -pady {1 0} \
2498        3,0 $inner.edges        -anchor w -pady {1 0} \
2499        4,0 $inner.rep_l        -anchor w -pady { 2 0 } \
2500        5,0 $inner.rep          -fill x    -pady 2 \
2501        6,0 $inner.rscale_l     -anchor w -pady { 2 0 } \
2502        7,0 $inner.rscale       -fill x    -pady 2 \
2503        8,0 $inner.palette_l    -anchor w  -pady 0 \
2504        9,0 $inner.palette      -fill x    -padx 2 \
2505        10,0 $inner.atomscale_l  -anchor w -pady {3 0} \
2506        11,0 $inner.atomscale   -fill x    -padx 2 \
2507        12,0 $inner.bondscale_l -anchor w -pady {3 0} \
2508        13,0 $inner.bondscale   -fill x   -padx 2 \
2509        14,0 $inner.opacity_l   -anchor w -pady {3 0} \
2510        15,0 $inner.opacity     -fill x    -padx 2 \
2511        16,0 $inner.quality_l   -anchor w -pady {3 0} \
2512        17,0 $inner.quality     -fill x    -padx 2
2513
2514    blt::table configure $inner r* -resize none
2515    blt::table configure $inner r18 -resize expand
2516}
2517
2518#
2519# camera --
2520#
2521itcl::body Rappture::VtkViewer::camera {option args} {
2522    switch -- $option {
2523        "show" {
2524            puts [array get _view]
2525        }
2526        "set" {
2527            set what [lindex $args 0]
2528            set x $_view($what)
2529            set code [catch { string is double $x } result]
2530            if { $code != 0 || !$result } {
2531                return
2532            }
2533            switch -- $what {
2534                "-ortho" {
2535                    if {$_view($what)} {
2536                        SendCmd "camera mode ortho"
2537                    } else {
2538                        SendCmd "camera mode persp"
2539                    }
2540                }
2541                "-xpan" - "-ypan" {
2542                    PanCamera
2543                }
2544                "-qx" - "-qy" - "-qz" - "-qw" {
2545                    set q [ViewToQuaternion]
2546                    $_arcball quaternion $q
2547                    EventuallyRotate $q
2548                }
2549                "-zoom" {
2550                    SendCmd "camera zoom $_view($what)"
2551                }
2552            }
2553        }
2554    }
2555}
2556
2557itcl::body Rappture::VtkViewer::GetVtkData { args } {
2558    set bytes ""
2559    foreach dataobj [get] {
2560        foreach comp [$dataobj components] {
2561            set tag $dataobj-$comp
2562            set contents [$dataobj data $comp]
2563            append bytes "$contents\n"
2564        }
2565    }
2566    return [list .vtk $bytes]
2567}
2568
2569itcl::body Rappture::VtkViewer::GetImage { args } {
2570    if { [image width $_image(download)] > 0 &&
2571         [image height $_image(download)] > 0 } {
2572        set bytes [$_image(download) data -format "jpeg -quality 100"]
2573        set bytes [Rappture::encoding::decode -as b64 $bytes]
2574        return [list .jpg $bytes]
2575    }
2576    return ""
2577}
2578
2579itcl::body Rappture::VtkViewer::BuildDownloadPopup { popup command } {
2580    Rappture::Balloon $popup \
2581        -title "[Rappture::filexfer::label downloadWord] as..."
2582    set inner [$popup component inner]
2583    label $inner.summary -text "" -anchor w
2584    radiobutton $inner.vtk_button -text "VTK data file" \
2585        -variable [itcl::scope _downloadPopup(format)] \
2586        -font "Arial 9" \
2587        -value vtk
2588    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2589    radiobutton $inner.image_button -text "Image File" \
2590        -variable [itcl::scope _downloadPopup(format)] \
2591        -value image
2592    Rappture::Tooltip::for $inner.image_button \
2593        "Save as digital image."
2594
2595    button $inner.ok -text "Save" \
2596        -highlightthickness 0 -pady 2 -padx 3 \
2597        -command $command \
2598        -compound left \
2599        -image [Rappture::icon download]
2600
2601    button $inner.cancel -text "Cancel" \
2602        -highlightthickness 0 -pady 2 -padx 3 \
2603        -command [list $popup deactivate] \
2604        -compound left \
2605        -image [Rappture::icon cancel]
2606
2607    blt::table $inner \
2608        0,0 $inner.summary -cspan 2  \
2609        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2610        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2611        4,1 $inner.cancel -width .9i -fill y \
2612        4,0 $inner.ok -padx 2 -width .9i -fill y
2613    blt::table configure $inner r3 -height 4
2614    blt::table configure $inner r4 -pady 4
2615    raise $inner.image_button
2616    $inner.vtk_button invoke
2617    return $inner
2618}
2619
2620itcl::body Rappture::VtkViewer::SetObjectStyle { dataobj comp } {
2621    # Parse style string.
2622    set tag $dataobj-$comp
2623    set type [$dataobj type $comp]
2624    set style [$dataobj style $comp]
2625    switch -- $type {
2626        "glyphs" {
2627            array set settings {
2628                -color white
2629                -edgecolor black
2630                -edges 0
2631                -gscale 1
2632                -lighting 1
2633                -linewidth 1.0
2634                -normscale 0
2635                -opacity 1.0
2636                -orientglyphs 0
2637                -outline 0
2638                -ptsize 1.0
2639                -quality 1
2640                -scalemode "vcomp"
2641                -shape "sphere"
2642                -visible 1
2643                -wireframe 0
2644            }
2645            array set settings $style
2646            set shape [$dataobj shape $comp]
2647            if {$shape != ""} {
2648                set settings(-shape) $shape
2649            }
2650            # Backwards compat with camel case style option
2651            if { [info exists settings(-orientGlyphs)] } {
2652                set settings(-orientglyphs) $settings(-orientGlyphs)
2653                array unset settings -orientGlyphs
2654            }
2655            # Backwards compat with camel case style option
2656            if { [info exists settings(-scaleMode)] } {
2657                set settings(-scalemode) $settings(-scaleMode)
2658                array unset settings -scaleMode
2659            }
2660            SendCmd "outline add $tag"
2661            SendCmd "outline color [Color2RGB $settings(-color)] $tag"
2662            SendCmd "outline visible $settings(-outline) $tag"
2663            set _settings(glyphs-outline) $settings(-outline)
2664
2665            SendCmd "glyphs add $settings(-shape) $tag"
2666            SendCmd "glyphs normscale $settings(-normscale) $tag"
2667            SendCmd "glyphs gscale $settings(-gscale) $tag"
2668            SendCmd "glyphs wireframe $settings(-wireframe) $tag"
2669            SendCmd "glyphs color [Color2RGB $settings(-color)] $tag"
2670            #SendCmd "glyphs colormode constant {} $tag"
2671            # Omitting field name for gorient and smode commands
2672            # defaults to active scalars or vectors depending on mode
2673            SendCmd "glyphs gorient $settings(-orientglyphs) {} $tag"
2674            SendCmd "glyphs smode $settings(-scalemode) {} $tag"
2675            SendCmd "glyphs edges $settings(-edges) $tag"
2676            SendCmd "glyphs linecolor [Color2RGB $settings(-edgecolor)] $tag"
2677            SendCmd "glyphs linewidth $settings(-linewidth) $tag"
2678            SendCmd "glyphs ptsize $settings(-ptsize) $tag"
2679            SendCmd "glyphs quality $settings(-quality) $tag"
2680            SendCmd "glyphs lighting $settings(-lighting) $tag"
2681            SendCmd "glyphs opacity $settings(-opacity) $tag"
2682            set _settings(glyphs-opacity) [expr 100.0 * $settings(-opacity)]
2683            SendCmd "glyphs visible $settings(-visible) $tag"
2684            set _settings(glyphs-wireframe) $settings(-wireframe)
2685        }
2686        "molecule" {
2687            array set settings {
2688                -atomscale 0.3
2689                -atomsvisible 1
2690                -bondscale 0.075
2691                -bondstyle "cylinder"
2692                -bondsvisible 1
2693                -color "elementDefault"
2694                -edgecolor black
2695                -edges 0
2696                -labels 0
2697                -lighting 1
2698                -linewidth 1.0
2699                -opacity 1.0
2700                -outline 0
2701                -quality 1.0
2702                -representation ""
2703                -rscale "covalent"
2704                -visible 1
2705                -wireframe 0
2706            }
2707            array set settings $style
2708
2709            SendCmd "outline add $tag"
2710            SendCmd "outline color [Color2RGB white] $tag"
2711            SendCmd "outline visible $settings(-outline) $tag"
2712            set _settings(molecule-outline) $settings(-outline)
2713
2714            SendCmd "molecule add $tag"
2715            if {$settings(-representation) != ""} {
2716                switch -- $settings(-representation) {
2717                    "ballandstick" {
2718                        set _settings(molecule-rscale) covalent
2719                        set _settings(molecule-atoms-visible) 1
2720                        set _settings(molecule-bonds-visible) 1
2721                        set _settings(molecule-bondstyle) cylinder
2722                        set _settings(molecule-atomscale) 0.3
2723                        set _settings(molecule-bondscale) 0.075
2724                    }
2725                    "balls" - "spheres" {
2726                        set _settings(molecule-rscale) covalent
2727                        set _settings(molecule-atoms-visible) 1
2728                        set _settings(molecule-bonds-visible) 0
2729                        set _settings(molecule-bondstyle) cylinder
2730                        set _settings(molecule-atomscale) 0.3
2731                        set _settings(molecule-bondscale) 0.075
2732                    }
2733                    "sticks" {
2734                        set _settings(molecule-rscale) none
2735                        set _settings(molecule-atoms-visible) 1
2736                        set _settings(molecule-bonds-visible) 1
2737                        set _settings(molecule-bondstyle) cylinder
2738                        set _settings(molecule-atomscale) 0.075
2739                        set _settings(molecule-bondscale) 0.075
2740                    }
2741                    "spacefilling" {
2742                        set _settings(molecule-rscale) van_der_waals
2743                        set _settings(molecule-atoms-visible) 1
2744                        set _settings(molecule-bonds-visible) 0
2745                        set _settings(molecule-bondstyle) cylinder
2746                        set _settings(molecule-atomscale) 1.0
2747                        set _settings(molecule-bondscale) 0.075
2748                    }
2749                    "rods"  {
2750                        set _settings(molecule-rscale) none
2751                        set _settings(molecule-atoms-visible) 1
2752                        set _settings(molecule-bonds-visible) 1
2753                        set _settings(molecule-bondstyle) cylinder
2754                        set _settings(molecule-atomscale) 0.1
2755                        set _settings(molecule-bondscale) 0.1
2756                    }
2757                    "wireframe" - "lines" {
2758                        set _settings(molecule-rscale) none
2759                        set _settings(molecule-atoms-visible) 0
2760                        set _settings(molecule-bonds-visible) 1
2761                        set _settings(molecule-bondstyle) line
2762                        set _settings(molecule-atomscale) 1.0
2763                        set _settings(molecule-bondscale) 1.0
2764                    }
2765                    default {
2766                        error "unknown representation $value"
2767                    }
2768                }
2769                SendCmd "molecule rscale $_settings(molecule-rscale) $tag"
2770                SendCmd "molecule atoms $_settings(molecule-atoms-visible) $tag"
2771                SendCmd "molecule bonds $_settings(molecule-bonds-visible) $tag"
2772                SendCmd "molecule bstyle $_settings(molecule-bondstyle) $tag"
2773                SendCmd "molecule ascale $_settings(molecule-atomscale) $tag"
2774                SendCmd "molecule bscale $_settings(molecule-bondscale) $tag"
2775                $itk_component(representation) value [$itk_component(representation) label $settings(-representation)]
2776                $itk_component(rscale) value [$itk_component(rscale) label $_settings(molecule-rscale)]
2777                switch -- $settings(-representation) {
2778                    "ballandstick" - "balls" - "spheres" {
2779                        $itk_component(rscale) configure -state normal
2780                    }
2781                    default {
2782                        $itk_component(rscale) configure -state disabled
2783                    }
2784                }
2785            } else {
2786                SendCmd "molecule rscale $settings(-rscale) $tag"
2787                set _settings(molecule-rscale) $settings(-rscale)
2788                SendCmd "molecule atoms $settings(-atomsvisible) $tag"
2789                set _settings(molecule-atoms-visible) $settings(-atomsvisible)
2790                SendCmd "molecule bonds $settings(-bondsvisible) $tag"
2791                set _settings(molecule-bonds-visible) $settings(-bondsvisible)
2792                SendCmd "molecule bstyle $settings(-bondstyle) $tag"
2793                set _settings(molecule-bondstyle) $settings(-bondstyle)
2794                SendCmd "molecule ascale $settings(-atomscale) $tag"
2795                set _settings(molecule-atomscale) $settings(-atomscale)
2796                SendCmd "molecule bscale $settings(-bondscale) $tag"
2797                set _settings(molecule-bondscale) $settings(-bondscale)
2798            }
2799            SendCmd "molecule labels $settings(-labels) $tag"
2800            set _settings(molecule-labels) $settings(-labels)
2801            SendCmd "molecule linecolor [Color2RGB $settings(-edgecolor)] $tag"
2802            SendCmd "molecule linewidth $settings(-linewidth) $tag"
2803            SendCmd "molecule edges $settings(-edges) $tag"
2804            set _settings(molecule-edges) $settings(-edges)
2805            SendCmd "molecule lighting $settings(-lighting) $tag"
2806            set _settings(molecule-lighting) $settings(-lighting)
2807            SendCmd "molecule aquality $settings(-quality) $tag"
2808            SendCmd "molecule bquality $settings(-quality) $tag"
2809            set _settings(molecule-quality) $settings(-quality)
2810            SendCmd "molecule visible $settings(-visible) $tag"
2811            set _settings(molecule-visible) $settings(-visible)
2812            set _haveMolecules 1
2813        }
2814        "polydata" {
2815            array set settings {
2816                -cloudstyle "mesh"
2817                -color white
2818                -edgecolor black
2819                -edges 1
2820                -lighting 1
2821                -linewidth 1.0
2822                -opacity 1.0
2823                -outline 0
2824                -visible 1
2825                -wireframe 0
2826            }
2827            array set settings $style
2828
2829            SendCmd "outline add $tag"
2830            SendCmd "outline color [Color2RGB $settings(-color)] $tag"
2831            SendCmd "outline visible $settings(-outline) $tag"
2832            set _settings(polydata-outline) $settings(-outline)
2833
2834            SendCmd "polydata add $tag"
2835            SendCmd "polydata visible $settings(-visible) $tag"
2836            set _settings(polydata-visible) $settings(-visible)
2837            SendCmd "polydata edges $settings(-edges) $tag"
2838            set _settings(polydata-edges) $settings(-edges)
2839            SendCmd "polydata cloudstyle $settings(-cloudstyle) $tag"
2840            SendCmd "polydata color [Color2RGB $settings(-color)] $tag"
2841            #SendCmd "polydata colormode constant {} $tag"
2842            SendCmd "polydata lighting $settings(-lighting) $tag"
2843            set _settings(polydata-lighting) $settings(-lighting)
2844            SendCmd "polydata linecolor [Color2RGB $settings(-edgecolor)] $tag"
2845            SendCmd "polydata linewidth $settings(-linewidth) $tag"
2846            SendCmd "polydata opacity $settings(-opacity) $tag"
2847            set _settings(polydata-opacity) [expr 100.0 * $settings(-opacity)]
2848            SendCmd "polydata wireframe $settings(-wireframe) $tag"
2849            set _settings(polydata-wireframe) $settings(-wireframe)
2850            set havePolyData 1
2851        }
2852    }
2853    SetColormap $dataobj $comp
2854}
2855
2856itcl::body Rappture::VtkViewer::IsValidObject { dataobj } {
2857    if {[catch {$dataobj isa Rappture::Drawing} valid] != 0 || !$valid} {
2858        return 0
2859    }
2860    return 1
2861}
2862
2863# ----------------------------------------------------------------------
2864# USAGE: ReceiveLegend <colormap> <title> <min> <max> <size>
2865#
2866# Invoked automatically whenever the "legend" command comes in from
2867# the rendering server.  Indicates that binary image data with the
2868# specified <size> will follow.
2869# ----------------------------------------------------------------------
2870itcl::body Rappture::VtkViewer::ReceiveLegend { colormap title min max size } {
2871    set _limits(vmin) $min
2872    set _limits(vmax) $max
2873    set _title $title
2874    if { [IsConnected] } {
2875        set bytes [ReceiveBytes $size]
2876        if { ![info exists _image(legend)] } {
2877            set _image(legend) [image create photo]
2878        }
2879        $_image(legend) configure -data $bytes
2880        DrawLegend
2881    }
2882}
2883
2884#
2885# DrawLegend --
2886#
2887# Draws the legend in it's own canvas which resides to the right
2888# of the contour plot area.
2889#
2890itcl::body Rappture::VtkViewer::DrawLegend {} {
2891    set c $itk_component(view)
2892    set w [winfo width $c]
2893    set h [winfo height $c]
2894    set font "Arial 8"
2895    set lineht [font metrics $font -linespace]
2896
2897    if { $_settings(legend) } {
2898        set x [expr $w - 2]
2899        if { [$c find withtag "legend"] == "" } {
2900            $c create image $x [expr {$lineht+2}] \
2901                -anchor ne \
2902                -image $_image(legend) -tags "colormap legend"
2903            $c create text $x 2 \
2904                -anchor ne \
2905                -fill $itk_option(-plotforeground) -tags "vmax legend" \
2906                -font $font
2907            $c create text $x [expr {$h-2}] \
2908                -anchor se \
2909                -fill $itk_option(-plotforeground) -tags "vmin legend" \
2910                -font $font
2911            #$c bind colormap <Enter> [itcl::code $this EnterLegend %x %y]
2912            $c bind colormap <Leave> [itcl::code $this LeaveLegend]
2913            $c bind colormap <Motion> [itcl::code $this MotionLegend %x %y]
2914        }
2915        # Reset the item coordinates according the current size of the plot.
2916        $c coords colormap $x [expr {$lineht+2}]
2917        if { $_limits(vmin) != "" } {
2918            $c itemconfigure vmin -text [format %g $_limits(vmin)]
2919        }
2920        if { $_limits(vmax) != "" } {
2921            $c itemconfigure vmax -text [format %g $_limits(vmax)]
2922        }
2923        $c coords vmin $x [expr {$h-2}]
2924        $c coords vmax $x 2
2925    }
2926}
2927
2928#
2929# EnterLegend --
2930#
2931itcl::body Rappture::VtkViewer::EnterLegend { x y } {
2932    SetLegendTip $x $y
2933}
2934
2935#
2936# MotionLegend --
2937#
2938itcl::body Rappture::VtkViewer::MotionLegend { x y } {
2939    Rappture::Tooltip::tooltip cancel
2940    set c $itk_component(view)
2941    SetLegendTip $x $y
2942}
2943
2944#
2945# LeaveLegend --
2946#
2947itcl::body Rappture::VtkViewer::LeaveLegend { } {
2948    Rappture::Tooltip::tooltip cancel
2949    .rappturetooltip configure -icon ""
2950}
2951
2952#
2953# SetLegendTip --
2954#
2955itcl::body Rappture::VtkViewer::SetLegendTip { x y } {
2956    set c $itk_component(view)
2957    set w [winfo width $c]
2958    set h [winfo height $c]
2959    set font "Arial 8"
2960    set lineht [font metrics $font -linespace]
2961
2962    set imgHeight [image height $_image(legend)]
2963    set coords [$c coords colormap]
2964    set imgX [expr $w - [image width $_image(legend)] - 2]
2965    set imgY [expr $y - $lineht - 2]
2966
2967    # Make a swatch of the selected color
2968    if { [catch { $_image(legend) get 10 $imgY } pixel] != 0 } {
2969        return
2970    }
2971    if { ![info exists _image(swatch)] } {
2972        set _image(swatch) [image create photo -width 24 -height 24]
2973    }
2974    set color [eval format "\#%02x%02x%02x" $pixel]
2975    $_image(swatch) put black  -to 0 0 23 23
2976    $_image(swatch) put $color -to 1 1 22 22
2977    .rappturetooltip configure -icon $_image(swatch)
2978
2979    # Compute the value of the point
2980    set t [expr 1.0 - (double($imgY) / double($imgHeight-1))]
2981    set value [expr $t * ($_limits(vmax) - $_limits(vmin)) + $_limits(vmin)]
2982    set tipx [expr $x + 15]
2983    set tipy [expr $y - 5]
2984    Rappture::Tooltip::text $c "$_title $value"
2985    Rappture::Tooltip::tooltip show $c +$tipx,+$tipy
2986}
2987
2988# ----------------------------------------------------------------------
2989# USAGE: Slice move x|y|z <newval>
2990#
2991# Called automatically when the user drags the slider to move the
2992# cut plane that slices 3D data.  Gets the current value from the
2993# slider and moves the cut plane to the appropriate point in the
2994# data set.
2995# ----------------------------------------------------------------------
2996itcl::body Rappture::VtkViewer::Slice {option args} {
2997    switch -- $option {
2998        "move" {
2999            set axis [lindex $args 0]
3000            set newval [lindex $args 1]
3001            if {[llength $args] != 2} {
3002                error "wrong # args: should be \"Slice move x|y|z newval\""
3003            }
3004            set newpos [expr {0.01*$newval}]
3005            SendCmd "renderer clipplane $axis $newpos -1"
3006        }
3007        "tooltip" {
3008            set axis [lindex $args 0]
3009            set val [$itk_component(${axis}CutScale) get]
3010            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
3011        }
3012        default {
3013            error "bad option \"$option\": should be axis, move, or tooltip"
3014        }
3015    }
3016}
3017
3018itcl::body Rappture::VtkViewer::SetOrientation { side } {
3019    array set positions {
3020        front "1 0 0 0"
3021        back  "0 0 1 0"
3022        left  "0.707107 0 -0.707107 0"
3023        right "0.707107 0 0.707107 0"
3024        top   "0.707107 -0.707107 0 0"
3025        bottom "0.707107 0.707107 0 0"
3026    }
3027    foreach name { -qw -qx -qy -qz } value $positions($side) {
3028        set _view($name) $value
3029    }
3030    set q [ViewToQuaternion]
3031    $_arcball quaternion $q
3032    SendCmd "camera orient $q"
3033    SendCmd "camera reset"
3034    set _view(-xpan) 0
3035    set _view(-ypan) 0
3036    set _view(-zoom) 1.0
3037}
3038
3039itcl::body Rappture::VtkViewer::SetOpacity { dataset } {
3040    foreach {dataobj comp} [split $dataset -] break
3041    set type [$dataobj type $comp]
3042    set val $_settings($type-opacity)
3043    set sval [expr { 0.01 * double($val) }]
3044    if { !$_obj2ovride($dataobj-raise) } {
3045        # This is wrong.  Need to figure out why raise isn't set with 1
3046        #set sval [expr $sval * .6]
3047    }
3048    SendCmd "$type opacity $sval $dataset"
3049}
Note: See TracBrowser for help on using the repository browser.