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

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

Add build info to clientinfo command

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