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

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

Fix check for existing glyph settings tab

File size: 110.4 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 _settings($what) $colormap
1435            foreach dataset [CurrentDatasets -visible $_first] {
1436                foreach {dataobj comp} [split $dataset -] break
1437                set type [$dataobj type $comp]
1438                if { $type == "glyphs" } {
1439                    ChangeColormap $dataobj $comp $colormap
1440                    # FIXME: fill in current selected fieldname
1441                    #SendCmd "glyphs colormode scalar {} $dataset"
1442                }
1443            }
1444            set _legendPending 1
1445        }
1446        "polydata-opacity" {
1447            foreach dataset [CurrentDatasets -visible $_first] {
1448                foreach { dataobj comp } [split $dataset -] break
1449                if { [$dataobj type $comp] == "polydata" } {
1450                    SetOpacity $dataset
1451                }
1452            }
1453        }
1454        "polydata-outline" {
1455            set bool $_settings($what)
1456            foreach dataset [CurrentDatasets -visible $_first] {
1457                foreach { dataobj comp } [split $dataset -] break
1458                set type [$dataobj type $comp]
1459                if { $type == "polydata" } {
1460                    SendCmd "outline visible $bool $dataset"
1461                }
1462            }
1463        }
1464        "polydata-wireframe" {
1465            set bool $_settings($what)
1466            foreach dataset [CurrentDatasets -visible $_first] {
1467                foreach { dataobj comp } [split $dataset -] break
1468                set type [$dataobj type $comp]
1469                if { $type == "polydata" } {
1470                    SendCmd "$type wireframe $bool $dataset"
1471                }
1472            }
1473        }
1474        "polydata-visible" {
1475            set bool $_settings($what)
1476            foreach dataset [CurrentDatasets -visible $_first] {
1477                foreach { dataobj comp } [split $dataset -] break
1478                set type [$dataobj type $comp]
1479                if { $type == "polydata" } {
1480                    SendCmd "$type visible $bool $dataset"
1481                }
1482            }
1483        }
1484        "polydata-lighting" {
1485            set bool $_settings($what)
1486            foreach dataset [CurrentDatasets -visible $_first] {
1487                foreach { dataobj comp } [split $dataset -] break
1488                set type [$dataobj type $comp]
1489                if { $type == "polydata" } {
1490                    SendCmd "$type lighting $bool $dataset"
1491                }
1492            }
1493        }
1494        "polydata-edges" {
1495            set bool $_settings($what)
1496            foreach dataset [CurrentDatasets -visible $_first] {
1497                foreach { dataobj comp } [split $dataset -] break
1498                set type [$dataobj type $comp]
1499                if { $type == "polydata" } {
1500                    SendCmd "$type edges $bool $dataset"
1501                }
1502            }
1503        }
1504        "polydata-colormap" {
1505            set colormap [$itk_component(meshcolormap) value]
1506            set _settings($what) $colormap
1507            foreach dataset [CurrentDatasets -visible $_first] {
1508                foreach {dataobj comp} [split $dataset -] break
1509                set type [$dataobj type $comp]
1510                if { $type == "polydata" } {
1511                    ChangeColormap $dataobj $comp $colormap
1512                    # FIXME: fill in current selected fieldname
1513                    #SendCmd "polydata colormode scalar {} $dataset"
1514                }
1515            }
1516            set _legendPending 1
1517        }
1518        "molecule-opacity" {
1519            foreach dataset [CurrentDatasets -visible $_first] {
1520                foreach { dataobj comp } [split $dataset -] break
1521                if { [$dataobj type $comp] == "molecule" } {
1522                    SetOpacity $dataset
1523                }
1524            }
1525        }
1526        "molecule-outline" {
1527            set bool $_settings($what)
1528            foreach dataset [CurrentDatasets -visible $_first] {
1529                foreach { dataobj comp } [split $dataset -] break
1530                set type [$dataobj type $comp]
1531                if { $type == "molecule" } {
1532                    SendCmd "outline visible $bool $dataset"
1533                }
1534            }
1535        }
1536        "molecule-wireframe" {
1537            set bool $_settings($what)
1538            foreach dataset [CurrentDatasets -visible $_first] {
1539                foreach { dataobj comp } [split $dataset -] break
1540                set type [$dataobj type $comp]
1541                if { $type == "molecule" } {
1542                    SendCmd "molecule wireframe $bool $dataset"
1543                }
1544            }
1545        }
1546        "molecule-visible" {
1547            set bool $_settings($what)
1548            foreach dataset [CurrentDatasets -visible $_first] {
1549                foreach { dataobj comp } [split $dataset -] break
1550                set type [$dataobj type $comp]
1551                if { $type == "molecule" } {
1552                    SendCmd "molecule visible $bool $dataset"
1553                }
1554            }
1555        }
1556        "molecule-lighting" {
1557            set bool $_settings($what)
1558            foreach dataset [CurrentDatasets -visible $_first] {
1559                foreach { dataobj comp } [split $dataset -] break
1560                set type [$dataobj type $comp]
1561                if { $type == "molecule" } {
1562                    SendCmd "molecule lighting $bool $dataset"
1563                }
1564            }
1565        }
1566        "molecule-edges" {
1567            set bool $_settings($what)
1568            foreach dataset [CurrentDatasets -visible $_first] {
1569                foreach { dataobj comp } [split $dataset -] break
1570                set type [$dataobj type $comp]
1571                if { $type == "molecule" } {
1572                    SendCmd "molecule edges $bool $dataset"
1573                }
1574            }
1575        }
1576        "molecule-colormap" {
1577            set colormap [$itk_component(moleculecolormap) value]
1578            set _settings($what) $colormap
1579            foreach dataset [CurrentDatasets -visible $_first] {
1580                foreach {dataobj comp} [split $dataset -] break
1581                set type [$dataobj type $comp]
1582                if { $type == "molecule" } {
1583                    ChangeColormap $dataobj $comp $colormap
1584                    if { $colormap == "elementDefault" } {
1585                        SendCmd [list molecule colormode by_elements element $dataset]
1586                    } else {
1587                        # FIXME: Set the chosen scalar field name here
1588                        set _settings(molecule-colormode) scalar
1589                        set _settings(molecule-colorfield) ""
1590                        SendCmd [list molecule colormode $_settings(molecule-colormode) $_settings(molecule-colorfield) $dataset]
1591                    }
1592                }
1593            }
1594            set _legendPending 1
1595        }
1596        "molecule-representation" {
1597            set value [$itk_component(representation) value]
1598            set value [$itk_component(representation) translate $value]
1599            switch -- $value {
1600                "ballandstick" {
1601                    set _settings(molecule-rscale) covalent
1602                    set _settings(molecule-atoms-visible) 1
1603                    set _settings(molecule-bonds-visible) 1
1604                    set _settings(molecule-bondstyle) cylinder
1605                    set _settings(molecule-atomscale) 0.3
1606                    set _settings(molecule-bondscale) 0.075
1607                }
1608                "balls" - "spheres" {
1609                    set _settings(molecule-rscale) covalent
1610                    set _settings(molecule-atoms-visible) 1
1611                    set _settings(molecule-bonds-visible) 0
1612                    set _settings(molecule-bondstyle) cylinder
1613                    set _settings(molecule-atomscale) 0.3
1614                    set _settings(molecule-bondscale) 0.075
1615                }
1616                "sticks" {
1617                    set _settings(molecule-rscale) none
1618                    set _settings(molecule-atoms-visible) 1
1619                    set _settings(molecule-bonds-visible) 1
1620                    set _settings(molecule-bondstyle) cylinder
1621                    set _settings(molecule-atomscale) 0.075
1622                    set _settings(molecule-bondscale) 0.075
1623                }
1624                "spacefilling" {
1625                    set _settings(molecule-rscale) van_der_waals
1626                    set _settings(molecule-atoms-visible) 1
1627                    set _settings(molecule-bonds-visible) 0
1628                    set _settings(molecule-bondstyle) cylinder
1629                    set _settings(molecule-atomscale) 1.0
1630                    set _settings(molecule-bondscale) 0.075
1631                }
1632                "rods"  {
1633                    set _settings(molecule-rscale) none
1634                    set _settings(molecule-atoms-visible) 1
1635                    set _settings(molecule-bonds-visible) 1
1636                    set _settings(molecule-bondstyle) cylinder
1637                    set _settings(molecule-atomscale) 0.1
1638                    set _settings(molecule-bondscale) 0.1
1639                }
1640                "wireframe" - "lines" {
1641                    set _settings(molecule-rscale) none
1642                    set _settings(molecule-atoms-visible) 0
1643                    set _settings(molecule-bonds-visible) 1
1644                    set _settings(molecule-bondstyle) line
1645                    set _settings(molecule-atomscale) 1.0
1646                    set _settings(molecule-bondscale) 1.0
1647                }
1648                default {
1649                    error "unknown representation $value"
1650                }
1651            }
1652            $itk_component(rscale) value [$itk_component(rscale) label $_settings(molecule-rscale)]
1653            switch -- $value {
1654                "ballandstick" - "balls" - "spheres" {
1655                    $itk_component(rscale) configure -state normal
1656                }
1657                default {
1658                    $itk_component(rscale) configure -state disabled
1659                }
1660            }
1661            foreach dataset [CurrentDatasets -all] {
1662                foreach {dataobj comp} [split $dataset -] break
1663                set type [$dataobj type $comp]
1664                if { $type == "molecule" } {
1665                    StartBufferingCommands
1666                    SendCmd [subst {molecule rscale $_settings(molecule-rscale) $dataset}]
1667                    SendCmd [subst {molecule ascale $_settings(molecule-atomscale) $dataset}]
1668                    SendCmd [subst {molecule bscale $_settings(molecule-bondscale) $dataset}]
1669                    SendCmd [subst {molecule bstyle $_settings(molecule-bondstyle) $dataset}]
1670                    SendCmd [subst {molecule atoms $_settings(molecule-atoms-visible) $dataset}]
1671                    SendCmd [subst {molecule bonds $_settings(molecule-bonds-visible) $dataset}]
1672                    StopBufferingCommands
1673                }
1674            }
1675        }
1676        "molecule-rscale" {
1677            set value [$itk_component(rscale) value]
1678            set value [$itk_component(rscale) translate $value]
1679            set _settings($what) $value
1680            foreach dataset [CurrentDatasets -visible $_first] {
1681                foreach {dataobj comp} [split $dataset -] break
1682                set type [$dataobj type $comp]
1683                if { $type == "molecule" } {
1684                    SendCmd [subst {molecule rscale $_settings($what) $dataset}]
1685                }
1686            }
1687        }
1688        "molecule-labels" {
1689            set bool $_settings($what)
1690            foreach dataset [CurrentDatasets -visible $_first] {
1691               foreach { dataobj comp } [split $dataset -] break
1692               set type [$dataobj type $comp]
1693               if { $type == "molecule" } {
1694                   SendCmd "molecule labels $bool $dataset"
1695               }
1696            }
1697        }
1698        "axis-visible" {
1699            set bool $_axis(visible)
1700            SendCmd "axis visible all $bool"
1701        }
1702        "axis-labels" {
1703            set bool $_axis(labels)
1704            SendCmd "axis labels all $bool"
1705        }
1706        "axis-minorticks" {
1707            set bool $_axis(minorticks)
1708            SendCmd "axis minticks all $bool"
1709        }
1710        "axis-xgrid" {
1711            set bool $_axis(xgrid)
1712            SendCmd "axis grid x $bool"
1713        }
1714        "axis-ygrid" {
1715            set bool $_axis(ygrid)
1716            SendCmd "axis grid y $bool"
1717        }
1718        "axis-zgrid" {
1719            set bool $_axis(zgrid)
1720            SendCmd "axis grid z $bool"
1721        }
1722        "axis-mode" {
1723            set mode [$itk_component(axismode) value]
1724            set mode [$itk_component(axismode) translate $mode]
1725            SendCmd "axis flymode $mode"
1726        }
1727        "axis-xcutaway" - "axis-ycutaway" - "axis-zcutaway" {
1728            set axis [string range $what 5 5]
1729            set bool $_axis(${axis}cutaway)
1730            if { $bool } {
1731                set pos [expr $_axis(${axis}position) * 0.01]
1732                set dir $_axis(${axis}direction)
1733                $itk_component(${axis}CutScale) configure -state normal \
1734                    -troughcolor white
1735                SendCmd "renderer clipplane $axis $pos $dir"
1736            } else {
1737                $itk_component(${axis}CutScale) configure -state disabled \
1738                    -troughcolor grey82
1739                SendCmd "renderer clipplane $axis 1 -1"
1740            }
1741        }
1742        "axis-xposition" - "axis-yposition" - "axis-zposition" -
1743        "axis-xdirection" - "axis-ydirection" - "axis-zdirection" {
1744            set axis [string range $what 5 5]
1745            #set dir $_axis(${axis}direction)
1746            set pos [expr $_axis(${axis}position) * 0.01]
1747            SendCmd "renderer clipplane ${axis} $pos -1"
1748        }
1749        default {
1750            error "don't know how to fix $what"
1751        }
1752    }
1753}
1754
1755#
1756# RequestLegend --
1757#
1758# Request a new legend from the server.  The size of the legend
1759# is determined from the height of the canvas.  It will be rotated
1760# to be vertical when drawn.
1761#
1762itcl::body Rappture::VtkViewer::RequestLegend {} {
1763    set font "Arial 8"
1764    set lineht [font metrics $font -linespace]
1765    set w 12
1766    set h [expr {$_height - 2 * ($lineht + 2)}]
1767    if { $h < 1 } {
1768        return
1769    }
1770    # Set the legend on the first dataset.
1771    foreach dataset [CurrentDatasets -visible] {
1772        foreach {dataobj comp} [split $dataset -] break
1773        if { [info exists _dataset2style($dataset)] } {
1774            #SendCmd "legend $_dataset2style($dataset) vmag {} {} $w $h 0"
1775            SendCmd "legend2 $_dataset2style($dataset) $w $h"
1776            break;
1777        }
1778    }
1779}
1780
1781#
1782# ChangeColormap --
1783#
1784itcl::body Rappture::VtkViewer::ChangeColormap {dataobj comp color} {
1785    set tag $dataobj-$comp
1786    if { ![info exist _style($tag)] } {
1787        error "no initial colormap"
1788    }
1789    array set style $_style($tag)
1790    set style(-color) $color
1791    set _style($tag) [array get style]
1792    SetColormap $dataobj $comp
1793}
1794
1795#
1796# SetColormap --
1797#
1798itcl::body Rappture::VtkViewer::SetColormap { dataobj comp } {
1799    array set style {
1800        -color BCGYR
1801    }
1802    if {[$dataobj type $comp] == "molecule"} {
1803        set style(-color) elementDefault
1804    }
1805    set tag $dataobj-$comp
1806    if { ![info exists _initialStyle($tag)] } {
1807        # Save the initial component style.
1808        set _initialStyle($tag) [$dataobj style $comp]
1809    }
1810
1811    # Override defaults with initial style defined in xml.
1812    array set style $_initialStyle($tag)
1813
1814    if { ![info exists _style($tag)] } {
1815        set _style($tag) [array get style]
1816    }
1817    # Override initial style with current style.
1818    array set style $_style($tag)
1819
1820    set name "$style(-color)"
1821    if { ![info exists _colormaps($name)] } {
1822        BuildColormap $name
1823        set _colormaps($name) 1
1824    }
1825    if { ![info exists _dataset2style($tag)] ||
1826         $_dataset2style($tag) != $name } {
1827        set _dataset2style($tag) $name
1828        switch -- [$dataobj type $comp] {
1829            "polydata" {
1830                SendCmd "polydata colormap $name $tag"
1831            }
1832            "glyphs" {
1833                SendCmd "glyphs colormap $name $tag"
1834            }
1835            "molecule" {
1836                SendCmd "molecule colormap $name $tag"
1837            }
1838        }
1839    }
1840}
1841
1842#
1843# BuildColormap --
1844#
1845itcl::body Rappture::VtkViewer::BuildColormap { name } {
1846    if { $name == "elementDefault" } {
1847        return
1848    }
1849    set cmap [ColorsToColormap $name]
1850    if { [llength $cmap] == 0 } {
1851        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
1852    }
1853    set amap "0.0 1.0 1.0 1.0"
1854    SendCmd "colormap add $name { $cmap } { $amap }"
1855}
1856
1857# ----------------------------------------------------------------------
1858# CONFIGURATION OPTION: -plotbackground
1859# ----------------------------------------------------------------------
1860itcl::configbody Rappture::VtkViewer::plotbackground {
1861    if { [isconnected] } {
1862        set rgb [Color2RGB $itk_option(-plotbackground)]
1863        SendCmd "screen bgcolor $rgb"
1864    }
1865}
1866
1867# ----------------------------------------------------------------------
1868# CONFIGURATION OPTION: -plotforeground
1869# ----------------------------------------------------------------------
1870itcl::configbody Rappture::VtkViewer::plotforeground {
1871    if { [isconnected] } {
1872        set rgb [Color2RGB $itk_option(-plotforeground)]
1873        SendCmd "axis color all $rgb"
1874        SendCmd "outline color $rgb"
1875    }
1876}
1877
1878itcl::body Rappture::VtkViewer::limits { dataobj } {
1879    foreach cname [$dataobj components] {
1880        set tag $dataobj-$cname
1881        if { ![info exists _limits($tag)] } {
1882            set data [$dataobj data $cname]
1883            if { $data == "" } {
1884                continue
1885            }
1886            set tmpfile file[pid].vtk
1887            set f [open "$tmpfile" "w"]
1888            fconfigure $f -translation binary -encoding binary
1889            puts $f $data
1890            close $f
1891            set reader [vtkDataSetReader $tag-xvtkDataSetReader]
1892            $reader SetFileName $tmpfile
1893            $reader ReadAllNormalsOn
1894            $reader ReadAllTCoordsOn
1895            $reader ReadAllScalarsOn
1896            $reader ReadAllColorScalarsOn
1897            $reader ReadAllVectorsOn
1898            $reader ReadAllTensorsOn
1899            $reader ReadAllFieldsOn
1900            $reader Update
1901            file delete $tmpfile
1902            set dataset [$reader GetOutput]
1903            if { $dataset == "" } {
1904                # Invalid VTK file -- loader failed to parse
1905                continue
1906            }
1907            set _limits($tag) [$dataset GetBounds]
1908            set dataAttrs [$dataset GetPointData]
1909            if { $dataAttrs != ""} {
1910                set numArrays [$dataAttrs GetNumberOfArrays]
1911                for {set i 0} {$i < $numArrays} {incr i} {
1912                    set array [$dataAttrs GetArray $i]
1913                    set fname [$dataAttrs GetArrayName $i]
1914                    foreach {min max} [$array GetRange -1] break
1915                    lappend _fieldlimits($tag) $fname [list $min $max]
1916                    set _fieldComponents($tag-$fname) [$array GetNumberOfComponents]
1917                }
1918            }
1919            set dataAttrs [$dataset GetCellData]
1920            if { $dataAttrs != ""} {
1921                set numArrays [$dataAttrs GetNumberOfArrays]
1922                for {set i 0} {$i < $numArrays} {incr i} {
1923                    set array [$dataAttrs GetArray $i]
1924                    set fname [$dataAttrs GetArrayName $i]
1925                    foreach {min max} [$array GetRange -1] break
1926                    lappend _fieldlimits($tag) $fname [list $min $max]
1927                    set _fieldComponents($tag-$fname) [$array GetNumberOfComponents]
1928                }
1929            }
1930            set dataAttrs [$dataset GetFieldData]
1931            if { $dataAttrs != ""} {
1932                set numArrays [$dataAttrs GetNumberOfArrays]
1933                for {set i 0} {$i < $numArrays} {incr i} {
1934                    set array [$dataAttrs GetArray $i]
1935                    set fname [$dataAttrs GetArrayName $i]
1936                    foreach {min max} [$array GetRange -1] break
1937                    lappend _fieldlimits($tag) $fname [list $min $max]
1938                    set _fieldComponents($tag-$fname) [$array GetNumberOfComponents]
1939                }
1940            }
1941            rename $dataset ""
1942            rename $reader ""
1943        }
1944        foreach {xMin xMax yMin yMax zMin zMax} $_limits($tag) break
1945        if {![info exists limits(xmin)] || $limits(xmin) > $xMin} {
1946            set limits(xmin) $xMin
1947        }
1948        if {![info exists limits(xmax)] || $limits(xmax) < $xMax} {
1949            set limits(xmax) $xMax
1950        }
1951        if {![info exists limits(ymin)] || $limits(ymin) > $yMin} {
1952            set limits(ymin) $xMin
1953        }
1954        if {![info exists limits(ymax)] || $limits(ymax) < $yMax} {
1955            set limits(ymax) $yMax
1956        }
1957        if {![info exists limits(zmin)] || $limits(zmin) > $zMin} {
1958            set limits(zmin) $zMin
1959        }
1960        if {![info exists limits(zmax)] || $limits(zmax) < $zMax} {
1961            set limits(zmax) $zMax
1962        }
1963    }
1964    return [array get limits]
1965}
1966
1967itcl::body Rappture::VtkViewer::BuildGlyphsTab {} {
1968
1969    set fg [option get $itk_component(hull) font Font]
1970    #set bfg [option get $itk_component(hull) boldFont Font]
1971
1972    set inner [$itk_component(main) insert 0 \
1973        -title "Glyph Settings" \
1974        -icon [Rappture::icon volume-on]]
1975    $inner configure -borderwidth 4
1976
1977    checkbutton $inner.glyphs \
1978        -text "Show Glyphs" \
1979        -variable [itcl::scope _settings(glyphs-visible)] \
1980        -command [itcl::code $this AdjustSetting glyphs-visible] \
1981        -font "Arial 9" -anchor w
1982
1983    checkbutton $inner.outline \
1984        -text "Show Outline" \
1985        -variable [itcl::scope _settings(glyphs-outline)] \
1986        -command [itcl::code $this AdjustSetting glyphs-outline] \
1987        -font "Arial 9" -anchor w
1988
1989    checkbutton $inner.wireframe \
1990        -text "Show Wireframe" \
1991        -variable [itcl::scope _settings(glyphs-wireframe)] \
1992        -command [itcl::code $this AdjustSetting glyphs-wireframe] \
1993        -font "Arial 9" -anchor w
1994
1995    checkbutton $inner.lighting \
1996        -text "Enable Lighting" \
1997        -variable [itcl::scope _settings(glyphs-lighting)] \
1998        -command [itcl::code $this AdjustSetting glyphs-lighting] \
1999        -font "Arial 9" -anchor w
2000
2001    checkbutton $inner.edges \
2002        -text "Show Edges" \
2003        -variable [itcl::scope _settings(glyphs-edges)] \
2004        -command [itcl::code $this AdjustSetting glyphs-edges] \
2005        -font "Arial 9" -anchor w
2006
2007    label $inner.colormap_l -text "Colormap" -font "Arial 9" -anchor w
2008    itk_component add glyphscolormap {
2009        Rappture::Combobox $inner.colormap -width 10 -editable 0
2010    }
2011    $inner.colormap choices insert end [GetColormapList]
2012    $itk_component(glyphscolormap) value "BCGYR"
2013    bind $inner.colormap <<Value>> \
2014        [itcl::code $this AdjustSetting glyphs-colormap]
2015
2016    label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w
2017    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2018        -variable [itcl::scope _settings(glyphs-opacity)] \
2019        -width 10 \
2020        -showvalue off \
2021        -command [itcl::code $this EventuallySetGlyphsOpacity]
2022    $inner.opacity set $_settings(glyphs-opacity)
2023
2024    blt::table $inner \
2025        0,0 $inner.glyphs     -cspan 2  -anchor w -pady 2 \
2026        1,0 $inner.outline    -cspan 2  -anchor w -pady 2 \
2027        2,0 $inner.wireframe  -cspan 2  -anchor w -pady 2 \
2028        3,0 $inner.lighting   -cspan 2  -anchor w -pady 2 \
2029        4,0 $inner.edges      -cspan 2  -anchor w -pady 2 \
2030        5,0 $inner.opacity_l  -anchor w -pady 2 \
2031        5,1 $inner.opacity    -fill x   -pady 2 \
2032        6,0 $inner.colormap_l -anchor w -pady 2 \
2033        6,1 $inner.colormap   -fill x   -pady 2
2034
2035    blt::table configure $inner r* c* -resize none
2036    blt::table configure $inner r8 c1 -resize expand
2037}
2038
2039itcl::body Rappture::VtkViewer::BuildPolydataTab {} {
2040
2041    set fg [option get $itk_component(hull) font Font]
2042    #set bfg [option get $itk_component(hull) boldFont Font]
2043
2044    set inner [$itk_component(main) insert 0 \
2045        -title "Mesh Settings" \
2046        -icon [Rappture::icon mesh]]
2047    $inner configure -borderwidth 4
2048
2049    checkbutton $inner.mesh \
2050        -text "Show Mesh" \
2051        -variable [itcl::scope _settings(polydata-visible)] \
2052        -command [itcl::code $this AdjustSetting polydata-visible] \
2053        -font "Arial 9" -anchor w
2054
2055    checkbutton $inner.outline \
2056        -text "Show Outline" \
2057        -variable [itcl::scope _settings(polydata-outline)] \
2058        -command [itcl::code $this AdjustSetting polydata-outline] \
2059        -font "Arial 9" -anchor w
2060
2061    checkbutton $inner.wireframe \
2062        -text "Show Wireframe" \
2063        -variable [itcl::scope _settings(polydata-wireframe)] \
2064        -command [itcl::code $this AdjustSetting polydata-wireframe] \
2065        -font "Arial 9" -anchor w
2066
2067    checkbutton $inner.lighting \
2068        -text "Enable Lighting" \
2069        -variable [itcl::scope _settings(polydata-lighting)] \
2070        -command [itcl::code $this AdjustSetting polydata-lighting] \
2071        -font "Arial 9" -anchor w
2072
2073    checkbutton $inner.edges \
2074        -text "Show Edges" \
2075        -variable [itcl::scope _settings(polydata-edges)] \
2076        -command [itcl::code $this AdjustSetting polydata-edges] \
2077        -font "Arial 9" -anchor w
2078
2079    label $inner.colormap_l -text "Colormap" -font "Arial 9" -anchor w
2080    itk_component add meshcolormap {
2081        Rappture::Combobox $inner.colormap -width 10 -editable 0
2082    }
2083    $inner.colormap choices insert end [GetColormapList]
2084    $itk_component(meshcolormap) value "BCGYR"
2085    bind $inner.colormap <<Value>> \
2086        [itcl::code $this AdjustSetting polydata-colormap]
2087
2088    label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w
2089    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2090        -variable [itcl::scope _settings(polydata-opacity)] \
2091        -width 10 \
2092        -showvalue off \
2093        -command [itcl::code $this EventuallySetPolydataOpacity]
2094    $inner.opacity set $_settings(polydata-opacity)
2095
2096    blt::table $inner \
2097        0,0 $inner.mesh       -cspan 2  -anchor w -pady 2 \
2098        1,0 $inner.outline    -cspan 2  -anchor w -pady 2 \
2099        2,0 $inner.wireframe  -cspan 2  -anchor w -pady 2 \
2100        3,0 $inner.lighting   -cspan 2  -anchor w -pady 2 \
2101        4,0 $inner.edges      -cspan 2  -anchor w -pady 2 \
2102        5,0 $inner.opacity_l  -anchor w -pady 2 \
2103        5,1 $inner.opacity    -fill x   -pady 2 \
2104        6,0 $inner.colormap_l -anchor w -pady 2 \
2105        6,1 $inner.colormap   -fill x   -pady 2
2106
2107    blt::table configure $inner r* c* -resize none
2108    blt::table configure $inner r8 c1 -resize expand
2109}
2110
2111itcl::body Rappture::VtkViewer::BuildAxisTab {} {
2112
2113    set fg [option get $itk_component(hull) font Font]
2114    #set bfg [option get $itk_component(hull) boldFont Font]
2115
2116    set inner [$itk_component(main) insert end \
2117        -title "Axis Settings" \
2118        -icon [Rappture::icon axis2]]
2119    $inner configure -borderwidth 4
2120
2121    checkbutton $inner.visible \
2122        -text "Axes" \
2123        -variable [itcl::scope _axis(visible)] \
2124        -command [itcl::code $this AdjustSetting axis-visible] \
2125        -font "Arial 9"
2126
2127    checkbutton $inner.labels \
2128        -text "Axis Labels" \
2129        -variable [itcl::scope _axis(labels)] \
2130        -command [itcl::code $this AdjustSetting axis-labels] \
2131        -font "Arial 9"
2132    label $inner.grid_l -text "Grid" -font "Arial 9"
2133    checkbutton $inner.xgrid \
2134        -text "X" \
2135        -variable [itcl::scope _axis(xgrid)] \
2136        -command [itcl::code $this AdjustSetting axis-xgrid] \
2137        -font "Arial 9"
2138    checkbutton $inner.ygrid \
2139        -text "Y" \
2140        -variable [itcl::scope _axis(ygrid)] \
2141        -command [itcl::code $this AdjustSetting axis-ygrid] \
2142        -font "Arial 9"
2143    checkbutton $inner.zgrid \
2144        -text "Z" \
2145        -variable [itcl::scope _axis(zgrid)] \
2146        -command [itcl::code $this AdjustSetting axis-zgrid] \
2147        -font "Arial 9"
2148    checkbutton $inner.minorticks \
2149        -text "Minor Ticks" \
2150        -variable [itcl::scope _axis(minorticks)] \
2151        -command [itcl::code $this AdjustSetting axis-minorticks] \
2152        -font "Arial 9"
2153
2154    label $inner.mode_l -text "Mode" -font "Arial 9"
2155
2156    itk_component add axismode {
2157        Rappture::Combobox $inner.mode -width 10 -editable 0
2158    }
2159    $inner.mode choices insert end \
2160        "static_triad"    "static" \
2161        "closest_triad"   "closest" \
2162        "furthest_triad"  "farthest" \
2163        "outer_edges"     "outer"
2164    $itk_component(axismode) value "static"
2165    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting axis-mode]
2166
2167    blt::table $inner \
2168        0,0 $inner.visible -anchor w -cspan 4 \
2169        1,0 $inner.labels  -anchor w -cspan 4 \
2170        2,0 $inner.minorticks  -anchor w -cspan 4 \
2171        4,0 $inner.grid_l  -anchor w \
2172        4,1 $inner.xgrid   -anchor w \
2173        4,2 $inner.ygrid   -anchor w \
2174        4,3 $inner.zgrid   -anchor w \
2175        5,0 $inner.mode_l  -anchor w -padx { 2 0 } \
2176        5,1 $inner.mode    -fill x   -cspan 3
2177
2178    blt::table configure $inner r* c* -resize none
2179    blt::table configure $inner r7 c6 -resize expand
2180    blt::table configure $inner r3 -height 0.125i
2181}
2182
2183itcl::body Rappture::VtkViewer::BuildCameraTab {} {
2184    set inner [$itk_component(main) insert end \
2185        -title "Camera Settings" \
2186        -icon [Rappture::icon camera]]
2187    $inner configure -borderwidth 4
2188
2189    label $inner.view_l -text "view" -font "Arial 9"
2190    set f [frame $inner.view]
2191    foreach side { front back left right top bottom } {
2192        button $f.$side  -image [Rappture::icon view$side] \
2193            -command [itcl::code $this SetOrientation $side]
2194        Rappture::Tooltip::for $f.$side "Change the view to $side"
2195        pack $f.$side -side left
2196    }
2197
2198    blt::table $inner \
2199        0,0 $inner.view_l -anchor e -pady 2 \
2200        0,1 $inner.view -anchor w -pady 2
2201    blt::table configure $inner r0 -resize none
2202
2203    set labels { qx qy qz qw xpan ypan zoom }
2204    set row 1
2205    foreach tag $labels {
2206        label $inner.${tag}label -text $tag -font "Arial 9"
2207        entry $inner.${tag} -font "Arial 9"  -bg white \
2208            -textvariable [itcl::scope _view(-$tag)]
2209        bind $inner.${tag} <Return> \
2210            [itcl::code $this camera set -${tag}]
2211        bind $inner.${tag} <KP_Enter> \
2212            [itcl::code $this camera set -${tag}]
2213        blt::table $inner \
2214            $row,0 $inner.${tag}label -anchor e -pady 2 \
2215            $row,1 $inner.${tag} -anchor w -pady 2
2216        blt::table configure $inner r$row -resize none
2217        incr row
2218    }
2219    checkbutton $inner.ortho \
2220        -text "Orthographic Projection" \
2221        -variable [itcl::scope _view(-ortho)] \
2222        -command [itcl::code $this camera set -ortho] \
2223        -font "Arial 9"
2224    blt::table $inner \
2225            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
2226    blt::table configure $inner r$row -resize none
2227    incr row
2228
2229    blt::table configure $inner c* -resize none
2230    blt::table configure $inner c2 -resize expand
2231    blt::table configure $inner r$row -resize expand
2232}
2233
2234itcl::body Rappture::VtkViewer::BuildCutawayTab {} {
2235
2236    set fg [option get $itk_component(hull) font Font]
2237
2238    set inner [$itk_component(main) insert end \
2239        -title "Cutaway Along Axis" \
2240        -icon [Rappture::icon cutbutton]]
2241
2242    $inner configure -borderwidth 4
2243
2244    # X-value slicer...
2245    itk_component add xCutButton {
2246        Rappture::PushButton $inner.xbutton \
2247            -onimage [Rappture::icon x-cutplane] \
2248            -offimage [Rappture::icon x-cutplane] \
2249            -command [itcl::code $this AdjustSetting axis-xcutaway] \
2250            -variable [itcl::scope _axis(xcutaway)]
2251    }
2252    Rappture::Tooltip::for $itk_component(xCutButton) \
2253        "Toggle the X-axis cutaway on/off"
2254
2255    itk_component add xCutScale {
2256        ::scale $inner.xval -from 100 -to 0 \
2257            -width 10 -orient vertical -showvalue 1 \
2258            -borderwidth 1 -highlightthickness 0 \
2259            -command [itcl::code $this Slice move x] \
2260            -variable [itcl::scope _axis(xposition)]
2261    } {
2262        usual
2263        ignore -borderwidth -highlightthickness
2264    }
2265    # Set the default cutaway value before disabling the scale.
2266    $itk_component(xCutScale) set 100
2267    $itk_component(xCutScale) configure -state disabled
2268    Rappture::Tooltip::for $itk_component(xCutScale) \
2269        "@[itcl::code $this Slice tooltip x]"
2270
2271    itk_component add xDirButton {
2272        Rappture::PushButton $inner.xdir \
2273            -onimage [Rappture::icon arrow-down] \
2274            -onvalue -1 \
2275            -offimage [Rappture::icon arrow-up] \
2276            -offvalue 1 \
2277            -command [itcl::code $this AdjustSetting axis-xdirection] \
2278            -variable [itcl::scope _axis(xdirection)]
2279    }
2280    set _axis(xdirection) -1
2281    Rappture::Tooltip::for $itk_component(xDirButton) \
2282        "Toggle the direction of the X-axis cutaway"
2283
2284    # Y-value slicer...
2285    itk_component add yCutButton {
2286        Rappture::PushButton $inner.ybutton \
2287            -onimage [Rappture::icon y-cutplane] \
2288            -offimage [Rappture::icon y-cutplane] \
2289            -command [itcl::code $this AdjustSetting axis-ycutaway] \
2290            -variable [itcl::scope _axis(ycutaway)]
2291    }
2292    Rappture::Tooltip::for $itk_component(yCutButton) \
2293        "Toggle the Y-axis cutaway on/off"
2294
2295    itk_component add yCutScale {
2296        ::scale $inner.yval -from 100 -to 0 \
2297            -width 10 -orient vertical -showvalue 1 \
2298            -borderwidth 1 -highlightthickness 0 \
2299            -command [itcl::code $this Slice move y] \
2300            -variable [itcl::scope _axis(yposition)]
2301    } {
2302        usual
2303        ignore -borderwidth -highlightthickness
2304    }
2305    Rappture::Tooltip::for $itk_component(yCutScale) \
2306        "@[itcl::code $this Slice tooltip y]"
2307    # Set the default cutaway value before disabling the scale.
2308    $itk_component(yCutScale) set 100
2309    $itk_component(yCutScale) configure -state disabled
2310
2311    itk_component add yDirButton {
2312        Rappture::PushButton $inner.ydir \
2313            -onimage [Rappture::icon arrow-down] \
2314            -onvalue -1 \
2315            -offimage [Rappture::icon arrow-up] \
2316            -offvalue 1 \
2317            -command [itcl::code $this AdjustSetting axis-ydirection] \
2318            -variable [itcl::scope _axis(ydirection)]
2319    }
2320    Rappture::Tooltip::for $itk_component(yDirButton) \
2321        "Toggle the direction of the Y-axis cutaway"
2322    set _axis(ydirection) -1
2323
2324    # Z-value slicer...
2325    itk_component add zCutButton {
2326        Rappture::PushButton $inner.zbutton \
2327            -onimage [Rappture::icon z-cutplane] \
2328            -offimage [Rappture::icon z-cutplane] \
2329            -command [itcl::code $this AdjustSetting axis-zcutaway] \
2330            -variable [itcl::scope _axis(zcutaway)]
2331    }
2332    Rappture::Tooltip::for $itk_component(zCutButton) \
2333        "Toggle the Z-axis cutaway on/off"
2334
2335    itk_component add zCutScale {
2336        ::scale $inner.zval -from 100 -to 0 \
2337            -width 10 -orient vertical -showvalue 1 \
2338            -borderwidth 1 -highlightthickness 0 \
2339            -command [itcl::code $this Slice move z] \
2340            -variable [itcl::scope _axis(zposition)]
2341    } {
2342        usual
2343        ignore -borderwidth -highlightthickness
2344    }
2345    $itk_component(zCutScale) set 100
2346    $itk_component(zCutScale) configure -state disabled
2347    Rappture::Tooltip::for $itk_component(zCutScale) \
2348        "@[itcl::code $this Slice tooltip z]"
2349
2350    itk_component add zDirButton {
2351        Rappture::PushButton $inner.zdir \
2352            -onimage [Rappture::icon arrow-down] \
2353            -onvalue -1 \
2354            -offimage [Rappture::icon arrow-up] \
2355            -offvalue 1 \
2356            -command [itcl::code $this AdjustSetting axis-zdirection] \
2357            -variable [itcl::scope _axis(zdirection)]
2358    }
2359    set _axis(zdirection) -1
2360    Rappture::Tooltip::for $itk_component(zDirButton) \
2361        "Toggle the direction of the Z-axis cutaway"
2362
2363    blt::table $inner \
2364        0,0 $itk_component(xCutButton)  -anchor e -padx 2 -pady 2 \
2365        1,0 $itk_component(xCutScale)   -fill y \
2366        0,1 $itk_component(yCutButton)  -anchor e -padx 2 -pady 2 \
2367        1,1 $itk_component(yCutScale)   -fill y \
2368        0,2 $itk_component(zCutButton)  -anchor e -padx 2 -pady 2 \
2369        1,2 $itk_component(zCutScale)   -fill y \
2370
2371    blt::table configure $inner r* c* -resize none
2372    blt::table configure $inner r1 c3 -resize expand
2373}
2374
2375itcl::body Rappture::VtkViewer::BuildMoleculeTab {} {
2376    set fg [option get $itk_component(hull) font Font]
2377
2378    set inner [$itk_component(main) insert 0 \
2379        -title "Molecule Settings" \
2380        -icon [Rappture::icon molecule]]
2381    $inner configure -borderwidth 4
2382
2383    checkbutton $inner.molecule \
2384        -text "Show Molecule" \
2385        -variable [itcl::scope _settings(molecule-visible)] \
2386        -command [itcl::code $this AdjustSetting molecule-visible] \
2387        -font "Arial 9"
2388
2389    checkbutton $inner.outline \
2390        -text "Show Outline" \
2391        -variable [itcl::scope _settings(molecule-outline)] \
2392        -command [itcl::code $this AdjustSetting molecule-outline] \
2393        -font "Arial 9"
2394
2395    checkbutton $inner.label \
2396        -text "Show Atom Labels" \
2397        -variable [itcl::scope _settings(molecule-labels)] \
2398        -command [itcl::code $this AdjustSetting molecule-labels] \
2399        -font "Arial 9"
2400
2401    checkbutton $inner.wireframe \
2402        -text "Show Wireframe" \
2403        -variable [itcl::scope _settings(molecule-wireframe)] \
2404        -command [itcl::code $this AdjustSetting molecule-wireframe] \
2405        -font "Arial 9"
2406
2407    checkbutton $inner.lighting \
2408        -text "Enable Lighting" \
2409        -variable [itcl::scope _settings(molecule-lighting)] \
2410        -command [itcl::code $this AdjustSetting molecule-lighting] \
2411        -font "Arial 9"
2412
2413    checkbutton $inner.edges \
2414        -text "Show Edges" \
2415        -variable [itcl::scope _settings(molecule-edges)] \
2416        -command [itcl::code $this AdjustSetting molecule-edges] \
2417        -font "Arial 9"
2418
2419    label $inner.rep_l -text "Molecule Representation" \
2420        -font "Arial 9"
2421
2422    itk_component add representation {
2423        Rappture::Combobox $inner.rep -width 20 -editable 0
2424    }
2425    $inner.rep choices insert end \
2426        "ballandstick"  "Ball and Stick" \
2427        "spheres"       "Spheres"        \
2428        "sticks"        "Sticks"         \
2429        "rods"          "Rods"           \
2430        "wireframe"     "Wireframe"      \
2431        "spacefilling"  "Space Filling"
2432
2433    bind $inner.rep <<Value>> \
2434        [itcl::code $this AdjustSetting molecule-representation]
2435    $inner.rep value "Ball and Stick"
2436
2437    label $inner.rscale_l -text "Atom Radii" \
2438        -font "Arial 9"
2439
2440    itk_component add rscale {
2441        Rappture::Combobox $inner.rscale -width 20 -editable 0
2442    }
2443    $inner.rscale choices insert end \
2444        "atomic"        "Atomic"   \
2445        "covalent"      "Covalent" \
2446        "van_der_waals" "VDW"      \
2447        "none"          "Constant"
2448
2449    bind $inner.rscale <<Value>> \
2450        [itcl::code $this AdjustSetting molecule-rscale]
2451    $inner.rscale value "Covalent"
2452
2453    label $inner.colormap_l -text "Colormap" -font "Arial 9"
2454    itk_component add moleculecolormap {
2455        Rappture::Combobox $inner.colormap -width 10 -editable 0
2456    }
2457    $inner.colormap choices insert end [GetColormapList -includeElementDefault]
2458    $itk_component(moleculecolormap) value "elementDefault"
2459    bind $inner.colormap <<Value>> \
2460        [itcl::code $this AdjustSetting molecule-colormap]
2461
2462    label $inner.atomscale_l -text "Atom Scale" -font "Arial 9"
2463    ::scale $inner.atomscale -width 15 -font "Arial 7" \
2464        -from 0.025 -to 2.0 -resolution 0.025 -label "" \
2465        -showvalue true -orient horizontal \
2466        -command [itcl::code $this EventuallySetAtomScale] \
2467        -variable [itcl::scope _settings(molecule-atomscale)]
2468    $inner.atomscale set $_settings(molecule-atomscale)
2469    Rappture::Tooltip::for $inner.atomscale \
2470        "Adjust relative scale of atoms (spheres or balls)."
2471
2472    label $inner.bondscale_l -text "Bond Scale" -font "Arial 9"
2473    ::scale $inner.bondscale -width 15 -font "Arial 7" \
2474        -from 0.005 -to 0.3 -resolution 0.005 -label "" \
2475        -showvalue true -orient horizontal \
2476        -command [itcl::code $this EventuallySetBondScale] \
2477        -variable [itcl::scope _settings(molecule-bondscale)]
2478    Rappture::Tooltip::for $inner.bondscale \
2479        "Adjust scale of bonds (sticks)."
2480    $inner.bondscale set $_settings(molecule-bondscale)
2481
2482    label $inner.opacity_l -text "Opacity" -font "Arial 9"
2483    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2484        -variable [itcl::scope _settings(molecule-opacity)] \
2485        -width 15 -font "Arial 7" \
2486        -showvalue on \
2487        -command [itcl::code $this EventuallySetMoleculeOpacity]
2488
2489    label $inner.quality_l -text "Quality" -font "Arial 9"
2490    ::scale $inner.quality -width 15 -font "Arial 7" \
2491        -from 0.0 -to 10.0 -resolution 0.1 -label "" \
2492        -showvalue true -orient horizontal \
2493        -command [itcl::code $this EventuallySetMoleculeQuality] \
2494        -variable [itcl::scope _settings(molecule-quality)]
2495    Rappture::Tooltip::for $inner.quality \
2496        "Adjust tesselation quality"
2497    $inner.quality set $_settings(molecule-quality)
2498
2499    blt::table $inner \
2500        0,0 $inner.molecule     -anchor w -pady {1 0} \
2501        1,0 $inner.outline      -anchor w -pady {1 0} \
2502        2,0 $inner.label        -anchor w -pady {1 0} \
2503        3,0 $inner.edges        -anchor w -pady {1 0} \
2504        4,0 $inner.rep_l        -anchor w -pady { 2 0 } \
2505        5,0 $inner.rep          -fill x    -pady 2 \
2506        6,0 $inner.rscale_l     -anchor w -pady { 2 0 } \
2507        7,0 $inner.rscale       -fill x    -pady 2 \
2508        8,0 $inner.colormap_l    -anchor w  -pady 0 \
2509        9,0 $inner.colormap      -fill x    -padx 2 \
2510        10,0 $inner.atomscale_l  -anchor w -pady {3 0} \
2511        11,0 $inner.atomscale   -fill x    -padx 2 \
2512        12,0 $inner.bondscale_l -anchor w -pady {3 0} \
2513        13,0 $inner.bondscale   -fill x   -padx 2 \
2514        14,0 $inner.opacity_l   -anchor w -pady {3 0} \
2515        15,0 $inner.opacity     -fill x    -padx 2 \
2516        16,0 $inner.quality_l   -anchor w -pady {3 0} \
2517        17,0 $inner.quality     -fill x    -padx 2
2518
2519    blt::table configure $inner r* -resize none
2520    blt::table configure $inner r18 -resize expand
2521}
2522
2523#
2524# camera --
2525#
2526itcl::body Rappture::VtkViewer::camera {option args} {
2527    switch -- $option {
2528        "show" {
2529            puts [array get _view]
2530        }
2531        "set" {
2532            set what [lindex $args 0]
2533            set x $_view($what)
2534            set code [catch { string is double $x } result]
2535            if { $code != 0 || !$result } {
2536                return
2537            }
2538            switch -- $what {
2539                "-ortho" {
2540                    if {$_view($what)} {
2541                        SendCmd "camera mode ortho"
2542                    } else {
2543                        SendCmd "camera mode persp"
2544                    }
2545                }
2546                "-xpan" - "-ypan" {
2547                    PanCamera
2548                }
2549                "-qx" - "-qy" - "-qz" - "-qw" {
2550                    set q [ViewToQuaternion]
2551                    $_arcball quaternion $q
2552                    EventuallyRotate $q
2553                }
2554                "-zoom" {
2555                    SendCmd "camera zoom $_view($what)"
2556                }
2557            }
2558        }
2559    }
2560}
2561
2562itcl::body Rappture::VtkViewer::GetVtkData { args } {
2563    set bytes ""
2564    foreach dataobj [get] {
2565        foreach comp [$dataobj components] {
2566            set tag $dataobj-$comp
2567            set contents [$dataobj data $comp]
2568            append bytes "$contents\n"
2569        }
2570    }
2571    return [list .vtk $bytes]
2572}
2573
2574itcl::body Rappture::VtkViewer::GetImage { args } {
2575    if { [image width $_image(download)] > 0 &&
2576         [image height $_image(download)] > 0 } {
2577        set bytes [$_image(download) data -format "jpeg -quality 100"]
2578        set bytes [Rappture::encoding::decode -as b64 $bytes]
2579        return [list .jpg $bytes]
2580    }
2581    return ""
2582}
2583
2584itcl::body Rappture::VtkViewer::BuildDownloadPopup { popup command } {
2585    Rappture::Balloon $popup \
2586        -title "[Rappture::filexfer::label downloadWord] as..."
2587    set inner [$popup component inner]
2588    label $inner.summary -text "" -anchor w
2589    radiobutton $inner.vtk_button -text "VTK data file" \
2590        -variable [itcl::scope _downloadPopup(format)] \
2591        -font "Arial 9" \
2592        -value vtk
2593    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2594    radiobutton $inner.image_button -text "Image File" \
2595        -variable [itcl::scope _downloadPopup(format)] \
2596        -value image
2597    Rappture::Tooltip::for $inner.image_button \
2598        "Save as digital image."
2599
2600    button $inner.ok -text "Save" \
2601        -highlightthickness 0 -pady 2 -padx 3 \
2602        -command $command \
2603        -compound left \
2604        -image [Rappture::icon download]
2605
2606    button $inner.cancel -text "Cancel" \
2607        -highlightthickness 0 -pady 2 -padx 3 \
2608        -command [list $popup deactivate] \
2609        -compound left \
2610        -image [Rappture::icon cancel]
2611
2612    blt::table $inner \
2613        0,0 $inner.summary -cspan 2  \
2614        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2615        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2616        4,1 $inner.cancel -width .9i -fill y \
2617        4,0 $inner.ok -padx 2 -width .9i -fill y
2618    blt::table configure $inner r3 -height 4
2619    blt::table configure $inner r4 -pady 4
2620    raise $inner.image_button
2621    $inner.vtk_button invoke
2622    return $inner
2623}
2624
2625itcl::body Rappture::VtkViewer::SetObjectStyle { dataobj comp } {
2626    # Parse style string.
2627    set tag $dataobj-$comp
2628    set type [$dataobj type $comp]
2629    set style [$dataobj style $comp]
2630    switch -- $type {
2631        "glyphs" {
2632            array set settings {
2633                -color white
2634                -edgecolor black
2635                -edges 0
2636                -gscale 1
2637                -lighting 1
2638                -linewidth 1.0
2639                -normscale 0
2640                -opacity 1.0
2641                -orientglyphs 0
2642                -outline 0
2643                -ptsize 1.0
2644                -quality 1
2645                -scalemode "vcomp"
2646                -shape "sphere"
2647                -visible 1
2648                -wireframe 0
2649            }
2650            array set settings $style
2651            set shape [$dataobj shape $comp]
2652            if {$shape != ""} {
2653                set settings(-shape) $shape
2654            }
2655            # Backwards compat with camel case style option
2656            if { [info exists settings(-orientGlyphs)] } {
2657                set settings(-orientglyphs) $settings(-orientGlyphs)
2658                array unset settings -orientGlyphs
2659            }
2660            # Backwards compat with camel case style option
2661            if { [info exists settings(-scaleMode)] } {
2662                set settings(-scalemode) $settings(-scaleMode)
2663                array unset settings -scaleMode
2664            }
2665            SendCmd "outline add $tag"
2666            SendCmd "outline color [Color2RGB $settings(-color)] $tag"
2667            SendCmd "outline visible $settings(-outline) $tag"
2668            set _settings(glyphs-outline) $settings(-outline)
2669
2670            SendCmd "glyphs add $settings(-shape) $tag"
2671            SendCmd "glyphs normscale $settings(-normscale) $tag"
2672            SendCmd "glyphs gscale $settings(-gscale) $tag"
2673            SendCmd "glyphs wireframe $settings(-wireframe) $tag"
2674            SendCmd "glyphs color [Color2RGB $settings(-color)] $tag"
2675            #SendCmd "glyphs colormode constant {} $tag"
2676            # Omitting field name for gorient and smode commands
2677            # defaults to active scalars or vectors depending on mode
2678            SendCmd "glyphs gorient $settings(-orientglyphs) {} $tag"
2679            SendCmd "glyphs smode $settings(-scalemode) {} $tag"
2680            SendCmd "glyphs edges $settings(-edges) $tag"
2681            SendCmd "glyphs linecolor [Color2RGB $settings(-edgecolor)] $tag"
2682            SendCmd "glyphs linewidth $settings(-linewidth) $tag"
2683            SendCmd "glyphs ptsize $settings(-ptsize) $tag"
2684            SendCmd "glyphs quality $settings(-quality) $tag"
2685            SendCmd "glyphs lighting $settings(-lighting) $tag"
2686            SendCmd "glyphs opacity $settings(-opacity) $tag"
2687            set _settings(glyphs-opacity) [expr 100.0 * $settings(-opacity)]
2688            SendCmd "glyphs visible $settings(-visible) $tag"
2689            set _settings(glyphs-wireframe) $settings(-wireframe)
2690        }
2691        "molecule" {
2692            array set settings {
2693                -atomscale 0.3
2694                -atomsvisible 1
2695                -bondconstcolor white
2696                -bondcolormode "by_elements"
2697                -bondscale 0.075
2698                -bondstyle "cylinder"
2699                -bondsvisible 1
2700                -color "elementDefault"
2701                -colormode "by_elements"
2702                -colorfield "element"
2703                -constcolor white
2704                -edgecolor black
2705                -edges 0
2706                -labels 0
2707                -lighting 1
2708                -linewidth 1.0
2709                -opacity 1.0
2710                -outline 0
2711                -quality 1.0
2712                -representation ""
2713                -rscale "covalent"
2714                -visible 1
2715                -wireframe 0
2716            }
2717            array set settings $style
2718
2719            SendCmd "outline add $tag"
2720            SendCmd "outline color [Color2RGB white] $tag"
2721            SendCmd "outline visible $settings(-outline) $tag"
2722            set _settings(molecule-outline) $settings(-outline)
2723
2724            SendCmd "molecule add $tag"
2725            if {$settings(-representation) != ""} {
2726                switch -- $settings(-representation) {
2727                    "ballandstick" {
2728                        set _settings(molecule-rscale) covalent
2729                        set _settings(molecule-atoms-visible) 1
2730                        set _settings(molecule-bonds-visible) 1
2731                        set _settings(molecule-bondstyle) cylinder
2732                        set _settings(molecule-atomscale) 0.3
2733                        set _settings(molecule-bondscale) 0.075
2734                    }
2735                    "balls" - "spheres" {
2736                        set _settings(molecule-rscale) covalent
2737                        set _settings(molecule-atoms-visible) 1
2738                        set _settings(molecule-bonds-visible) 0
2739                        set _settings(molecule-bondstyle) cylinder
2740                        set _settings(molecule-atomscale) 0.3
2741                        set _settings(molecule-bondscale) 0.075
2742                    }
2743                    "sticks" {
2744                        set _settings(molecule-rscale) none
2745                        set _settings(molecule-atoms-visible) 1
2746                        set _settings(molecule-bonds-visible) 1
2747                        set _settings(molecule-bondstyle) cylinder
2748                        set _settings(molecule-atomscale) 0.075
2749                        set _settings(molecule-bondscale) 0.075
2750                    }
2751                    "spacefilling" {
2752                        set _settings(molecule-rscale) van_der_waals
2753                        set _settings(molecule-atoms-visible) 1
2754                        set _settings(molecule-bonds-visible) 0
2755                        set _settings(molecule-bondstyle) cylinder
2756                        set _settings(molecule-atomscale) 1.0
2757                        set _settings(molecule-bondscale) 0.075
2758                    }
2759                    "rods"  {
2760                        set _settings(molecule-rscale) none
2761                        set _settings(molecule-atoms-visible) 1
2762                        set _settings(molecule-bonds-visible) 1
2763                        set _settings(molecule-bondstyle) cylinder
2764                        set _settings(molecule-atomscale) 0.1
2765                        set _settings(molecule-bondscale) 0.1
2766                    }
2767                    "wireframe" - "lines" {
2768                        set _settings(molecule-rscale) none
2769                        set _settings(molecule-atoms-visible) 0
2770                        set _settings(molecule-bonds-visible) 1
2771                        set _settings(molecule-bondstyle) line
2772                        set _settings(molecule-atomscale) 1.0
2773                        set _settings(molecule-bondscale) 1.0
2774                    }
2775                    default {
2776                        error "unknown representation $value"
2777                    }
2778                }
2779                SendCmd "molecule rscale $_settings(molecule-rscale) $tag"
2780                SendCmd "molecule atoms $_settings(molecule-atoms-visible) $tag"
2781                SendCmd "molecule bonds $_settings(molecule-bonds-visible) $tag"
2782                SendCmd "molecule bstyle $_settings(molecule-bondstyle) $tag"
2783                SendCmd "molecule ascale $_settings(molecule-atomscale) $tag"
2784                SendCmd "molecule bscale $_settings(molecule-bondscale) $tag"
2785                $itk_component(representation) value [$itk_component(representation) label $settings(-representation)]
2786                $itk_component(rscale) value [$itk_component(rscale) label $_settings(molecule-rscale)]
2787                switch -- $settings(-representation) {
2788                    "ballandstick" - "balls" - "spheres" {
2789                        $itk_component(rscale) configure -state normal
2790                    }
2791                    default {
2792                        $itk_component(rscale) configure -state disabled
2793                    }
2794                }
2795            } else {
2796                SendCmd "molecule rscale $settings(-rscale) $tag"
2797                set _settings(molecule-rscale) $settings(-rscale)
2798                SendCmd "molecule atoms $settings(-atomsvisible) $tag"
2799                set _settings(molecule-atoms-visible) $settings(-atomsvisible)
2800                SendCmd "molecule bonds $settings(-bondsvisible) $tag"
2801                set _settings(molecule-bonds-visible) $settings(-bondsvisible)
2802                SendCmd "molecule bstyle $settings(-bondstyle) $tag"
2803                set _settings(molecule-bondstyle) $settings(-bondstyle)
2804                SendCmd "molecule ascale $settings(-atomscale) $tag"
2805                set _settings(molecule-atomscale) $settings(-atomscale)
2806                SendCmd "molecule bscale $settings(-bondscale) $tag"
2807                set _settings(molecule-bondscale) $settings(-bondscale)
2808            }
2809            SendCmd "molecule labels $settings(-labels) $tag"
2810            set _settings(molecule-labels) $settings(-labels)
2811            SendCmd "molecule color [Color2RGB $settings(-constcolor)] $tag"
2812            SendCmd "molecule bcmode $settings(-bondcolormode) $tag"
2813            SendCmd "molecule bcolor [Color2RGB $settings(-bondconstcolor)] $tag"
2814            # Colormap is set by call to SetColormap below
2815            set _settings(molecule-colormap) $settings(-color)
2816            $itk_component(moleculecolormap) value $settings(-color)
2817            SendCmd [list molecule colormode $settings(-colormode) $settings(-colorfield) $tag]
2818            set _settings(molecule-colormode) $settings(-colormode)
2819            set _settings(molecule-colorfield) $settings(-colorfield)
2820            SendCmd "molecule linecolor [Color2RGB $settings(-edgecolor)] $tag"
2821            SendCmd "molecule linewidth $settings(-linewidth) $tag"
2822            SendCmd "molecule edges $settings(-edges) $tag"
2823            set _settings(molecule-edges) $settings(-edges)
2824            SendCmd "molecule lighting $settings(-lighting) $tag"
2825            set _settings(molecule-lighting) $settings(-lighting)
2826            SendCmd "molecule aquality $settings(-quality) $tag"
2827            SendCmd "molecule bquality $settings(-quality) $tag"
2828            set _settings(molecule-quality) $settings(-quality)
2829            SendCmd "molecule visible $settings(-visible) $tag"
2830            set _settings(molecule-visible) $settings(-visible)
2831            set _haveMolecules 1
2832        }
2833        "polydata" {
2834            array set settings {
2835                -cloudstyle "mesh"
2836                -color white
2837                -edgecolor black
2838                -edges 1
2839                -lighting 1
2840                -linewidth 1.0
2841                -opacity 1.0
2842                -outline 0
2843                -visible 1
2844                -wireframe 0
2845            }
2846            array set settings $style
2847
2848            SendCmd "outline add $tag"
2849            SendCmd "outline color [Color2RGB $settings(-color)] $tag"
2850            SendCmd "outline visible $settings(-outline) $tag"
2851            set _settings(polydata-outline) $settings(-outline)
2852
2853            SendCmd "polydata add $tag"
2854            SendCmd "polydata visible $settings(-visible) $tag"
2855            set _settings(polydata-visible) $settings(-visible)
2856            SendCmd "polydata edges $settings(-edges) $tag"
2857            set _settings(polydata-edges) $settings(-edges)
2858            SendCmd "polydata cloudstyle $settings(-cloudstyle) $tag"
2859            SendCmd "polydata color [Color2RGB $settings(-color)] $tag"
2860            #SendCmd "polydata colormode constant {} $tag"
2861            SendCmd "polydata lighting $settings(-lighting) $tag"
2862            set _settings(polydata-lighting) $settings(-lighting)
2863            SendCmd "polydata linecolor [Color2RGB $settings(-edgecolor)] $tag"
2864            SendCmd "polydata linewidth $settings(-linewidth) $tag"
2865            SendCmd "polydata opacity $settings(-opacity) $tag"
2866            set _settings(polydata-opacity) [expr 100.0 * $settings(-opacity)]
2867            SendCmd "polydata wireframe $settings(-wireframe) $tag"
2868            set _settings(polydata-wireframe) $settings(-wireframe)
2869            set havePolyData 1
2870        }
2871    }
2872    SetColormap $dataobj $comp
2873}
2874
2875itcl::body Rappture::VtkViewer::IsValidObject { dataobj } {
2876    if {[catch {$dataobj isa Rappture::Drawing} valid] != 0 || !$valid} {
2877        return 0
2878    }
2879    return 1
2880}
2881
2882# ----------------------------------------------------------------------
2883# USAGE: ReceiveLegend <colormap> <title> <min> <max> <size>
2884#
2885# Invoked automatically whenever the "legend" command comes in from
2886# the rendering server.  Indicates that binary image data with the
2887# specified <size> will follow.
2888# ----------------------------------------------------------------------
2889itcl::body Rappture::VtkViewer::ReceiveLegend { colormap title min max size } {
2890    set _limits(vmin) $min
2891    set _limits(vmax) $max
2892    set _title $title
2893    if { [IsConnected] } {
2894        set bytes [ReceiveBytes $size]
2895        if { ![info exists _image(legend)] } {
2896            set _image(legend) [image create photo]
2897        }
2898        $_image(legend) configure -data $bytes
2899        DrawLegend
2900    }
2901}
2902
2903#
2904# DrawLegend --
2905#
2906# Draws the legend in it's own canvas which resides to the right
2907# of the contour plot area.
2908#
2909itcl::body Rappture::VtkViewer::DrawLegend {} {
2910    set c $itk_component(view)
2911    set w [winfo width $c]
2912    set h [winfo height $c]
2913    set font "Arial 8"
2914    set lineht [font metrics $font -linespace]
2915
2916    if { $_settings(legend) } {
2917        set x [expr $w - 2]
2918        if { [$c find withtag "legend"] == "" } {
2919            $c create image $x [expr {$lineht+2}] \
2920                -anchor ne \
2921                -image $_image(legend) -tags "colormap legend"
2922            $c create text $x 2 \
2923                -anchor ne \
2924                -fill $itk_option(-plotforeground) -tags "vmax legend" \
2925                -font $font
2926            $c create text $x [expr {$h-2}] \
2927                -anchor se \
2928                -fill $itk_option(-plotforeground) -tags "vmin legend" \
2929                -font $font
2930            #$c bind colormap <Enter> [itcl::code $this EnterLegend %x %y]
2931            $c bind colormap <Leave> [itcl::code $this LeaveLegend]
2932            $c bind colormap <Motion> [itcl::code $this MotionLegend %x %y]
2933        }
2934        # Reset the item coordinates according the current size of the plot.
2935        $c coords colormap $x [expr {$lineht+2}]
2936        if { $_limits(vmin) != "" } {
2937            $c itemconfigure vmin -text [format %g $_limits(vmin)]
2938        }
2939        if { $_limits(vmax) != "" } {
2940            $c itemconfigure vmax -text [format %g $_limits(vmax)]
2941        }
2942        $c coords vmin $x [expr {$h-2}]
2943        $c coords vmax $x 2
2944    }
2945}
2946
2947#
2948# EnterLegend --
2949#
2950itcl::body Rappture::VtkViewer::EnterLegend { x y } {
2951    SetLegendTip $x $y
2952}
2953
2954#
2955# MotionLegend --
2956#
2957itcl::body Rappture::VtkViewer::MotionLegend { x y } {
2958    Rappture::Tooltip::tooltip cancel
2959    set c $itk_component(view)
2960    SetLegendTip $x $y
2961}
2962
2963#
2964# LeaveLegend --
2965#
2966itcl::body Rappture::VtkViewer::LeaveLegend { } {
2967    Rappture::Tooltip::tooltip cancel
2968    .rappturetooltip configure -icon ""
2969}
2970
2971#
2972# SetLegendTip --
2973#
2974itcl::body Rappture::VtkViewer::SetLegendTip { x y } {
2975    set c $itk_component(view)
2976    set w [winfo width $c]
2977    set h [winfo height $c]
2978    set font "Arial 8"
2979    set lineht [font metrics $font -linespace]
2980
2981    set imgHeight [image height $_image(legend)]
2982    set coords [$c coords colormap]
2983    set imgX [expr $w - [image width $_image(legend)] - 2]
2984    set imgY [expr $y - $lineht - 2]
2985
2986    # Make a swatch of the selected color
2987    if { [catch { $_image(legend) get 10 $imgY } pixel] != 0 } {
2988        return
2989    }
2990    if { ![info exists _image(swatch)] } {
2991        set _image(swatch) [image create photo -width 24 -height 24]
2992    }
2993    set color [eval format "\#%02x%02x%02x" $pixel]
2994    $_image(swatch) put black  -to 0 0 23 23
2995    $_image(swatch) put $color -to 1 1 22 22
2996    .rappturetooltip configure -icon $_image(swatch)
2997
2998    # Compute the value of the point
2999    set t [expr 1.0 - (double($imgY) / double($imgHeight-1))]
3000    set value [expr $t * ($_limits(vmax) - $_limits(vmin)) + $_limits(vmin)]
3001    set tipx [expr $x + 15]
3002    set tipy [expr $y - 5]
3003    Rappture::Tooltip::text $c "$_title $value"
3004    Rappture::Tooltip::tooltip show $c +$tipx,+$tipy
3005}
3006
3007# ----------------------------------------------------------------------
3008# USAGE: Slice move x|y|z <newval>
3009#
3010# Called automatically when the user drags the slider to move the
3011# cut plane that slices 3D data.  Gets the current value from the
3012# slider and moves the cut plane to the appropriate point in the
3013# data set.
3014# ----------------------------------------------------------------------
3015itcl::body Rappture::VtkViewer::Slice {option args} {
3016    switch -- $option {
3017        "move" {
3018            set axis [lindex $args 0]
3019            set newval [lindex $args 1]
3020            if {[llength $args] != 2} {
3021                error "wrong # args: should be \"Slice move x|y|z newval\""
3022            }
3023            set newpos [expr {0.01*$newval}]
3024            SendCmd "renderer clipplane $axis $newpos -1"
3025        }
3026        "tooltip" {
3027            set axis [lindex $args 0]
3028            set val [$itk_component(${axis}CutScale) get]
3029            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
3030        }
3031        default {
3032            error "bad option \"$option\": should be axis, move, or tooltip"
3033        }
3034    }
3035}
3036
3037itcl::body Rappture::VtkViewer::SetOrientation { side } {
3038    array set positions {
3039        front "1 0 0 0"
3040        back  "0 0 1 0"
3041        left  "0.707107 0 -0.707107 0"
3042        right "0.707107 0 0.707107 0"
3043        top   "0.707107 -0.707107 0 0"
3044        bottom "0.707107 0.707107 0 0"
3045    }
3046    foreach name { -qw -qx -qy -qz } value $positions($side) {
3047        set _view($name) $value
3048    }
3049    set q [ViewToQuaternion]
3050    $_arcball quaternion $q
3051    SendCmd "camera orient $q"
3052    SendCmd "camera reset"
3053    set _view(-xpan) 0
3054    set _view(-ypan) 0
3055    set _view(-zoom) 1.0
3056}
3057
3058itcl::body Rappture::VtkViewer::SetOpacity { dataset } {
3059    foreach {dataobj comp} [split $dataset -] break
3060    set type [$dataobj type $comp]
3061    set val $_settings($type-opacity)
3062    set sval [expr { 0.01 * double($val) }]
3063    if { !$_obj2ovride($dataobj-raise) } {
3064        # This is wrong.  Need to figure out why raise isn't set with 1
3065        #set sval [expr $sval * .6]
3066    }
3067    SendCmd "$type opacity $sval $dataset"
3068}
Note: See TracBrowser for help on using the repository browser.