source: branches/1.3/gui/scripts/vtkviewer.tcl @ 4557

Last change on this file since 4557 was 4528, checked in by ldelgass, 10 years ago

Merge r4506 from trunk

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