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

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

merge fixes from trunk

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