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

Last change on this file since 4482 was 4478, checked in by ldelgass, 10 years ago

merge r4465 from trunk

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