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

Last change on this file since 6142 was 6142, checked in by ldelgass, 9 years ago

minor cleanups

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