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

Last change on this file since 4766 was 4766, checked in by ldelgass, 6 years ago

merge r4765 from trunk

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