source: trunk/gui/scripts/vtkviewer.tcl @ 5094

Last change on this file since 5094 was 5094, checked in by ldelgass, 5 years ago

Enable the wait animation in all nanovis and vtkvis viewers

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