source: branches/1.7/gui/scripts/vtkviewer.tcl @ 6448

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

merge r6447 from trunk to 1.7 branch (init colormap dropdown)

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