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

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

Code style change: use style as array name in SetObjectStyle? like other viewers.

File size: 111.9 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: vtkviewer - Vtk drawing object viewer
4#
5#  It connects to the Vtkvis server running on a rendering farm,
6#  transmits data, and displays the results.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2016  HUBzero Foundation, LLC
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15package require BLT
16#package require Img
17
18option add *VtkViewer.width 4i widgetDefault
19option add *VtkViewer*cursor crosshair widgetDefault
20option add *VtkViewer.height 4i widgetDefault
21option add *VtkViewer.foreground black widgetDefault
22option add *VtkViewer.controlBackground gray widgetDefault
23option add *VtkViewer.controlDarkBackground #999999 widgetDefault
24option add *VtkViewer.plotBackground black widgetDefault
25option add *VtkViewer.plotForeground white widgetDefault
26option add *VtkViewer.font \
27    -*-helvetica-medium-r-normal-*-12-* widgetDefault
28
29# must use this name -- plugs into Rappture::resources::load
30proc VtkViewer_init_resources {} {
31    Rappture::resources::register \
32        vtkvis_server Rappture::VtkViewer::SetServerList
33}
34
35itcl::class Rappture::VtkViewer {
36    inherit Rappture::VisViewer
37
38    itk_option define -plotforeground plotForeground Foreground ""
39    itk_option define -plotbackground plotBackground Background ""
40
41    constructor { args } {
42        Rappture::VisViewer::constructor
43    } {
44        # defined below
45    }
46    destructor {
47        # defined below
48    }
49    public proc SetServerList { namelist } {
50        Rappture::VisViewer::SetServerList "vtkvis" $namelist
51    }
52    public method add {dataobj {settings ""}}
53    public method camera {option args}
54    public method delete {args}
55    public method disconnect {}
56    public method download {option args}
57    public method get {args}
58    public method isconnected {}
59    public method limits { dataobj }
60    public method parameters {title args} {
61        # do nothing
62    }
63    public method scale {args}
64
65    # The following methods are only used by this class.
66    private method AdjustSetting {what {value ""}}
67    private method BuildAxisTab {}
68    private method BuildCameraTab {}
69    private method BuildColormap { name }
70    private method BuildCutawayTab {}
71    private method BuildDownloadPopup { widget command }
72    private method BuildGlyphsTab {}
73    private method BuildMoleculeTab {}
74    private method BuildPolydataTab {}
75    private method ChangeColormap { dataobj comp color }
76    private method Connect {}
77    private method CurrentDatasets {args}
78    private method Disconnect {}
79    private method DoResize {}
80    private method DoRotate {}
81    private method DrawLegend {}
82    private method EnterLegend { x y }
83    private method EventuallyResize { w h }
84    private method EventuallyRotate { q }
85    private method EventuallySetAtomScale { args }
86    private method EventuallySetBondScale { args }
87    private method EventuallySetGlyphsOpacity { args }
88    private method EventuallySetMoleculeOpacity { args }
89    private method EventuallySetMoleculeQuality { args }
90    private method EventuallySetPolydataOpacity { args }
91    private method GetImage { args }
92    private method GetVtkData { args }
93    private method InitSettings { args }
94    private method IsValidObject { dataobj }
95    private method LeaveLegend {}
96    private method MotionLegend { x y }
97    private method Pan {option x y}
98    private method PanCamera {}
99    private method Pick {x y}
100    private method QuaternionToView { q } {
101        foreach { _view(-qw) _view(-qx) _view(-qy) _view(-qz) } $q break
102    }
103    private method Rebuild {}
104    private method ReceiveDataset { args }
105    private method ReceiveImage { args }
106    private method ReceiveLegend { colormap title min max size }
107    private method RequestLegend {}
108    private method Rotate {option x y}
109    private method SetAtomScale {}
110    private method SetBondScale {}
111    private method SetColormap { dataobj comp }
112    private method SetGlyphsOpacity {}
113    private method SetLegendTip { x y }
114    private method SetMoleculeOpacity {}
115    private method SetMoleculeQuality {}
116    private method SetObjectStyle { dataobj comp }
117    private method SetOpacity { dataset }
118    private method SetOrientation { side }
119    private method SetPolydataOpacity {}
120    private method Slice {option args}
121    private method ViewToQuaternion {} {
122        return [list $_view(-qw) $_view(-qx) $_view(-qy) $_view(-qz)]
123    }
124    private method Zoom {option}
125
126    private variable _arcball ""
127    private variable _dlist "";         # list of data objects
128    private variable _obj2ovride;       # maps dataobj => style override
129    private variable _datasets;         # contains all the dataobj-component
130                                        # datasets in the server
131    private variable _colormaps;        # contains all the colormaps
132                                        # in the server.
133    private variable _dataset2style;    # maps dataobj-component to transfunc
134    private variable _click;            # info used for rotate operations
135    private variable _limits;           # autoscale min/max for all axes
136    private variable _fieldlimits;      # maps dataobj-comp to field limits
137    private variable _fieldComponents;  # maps dataobj-comp-fname to number of
138                                        # components
139    private variable _view;             # view params for 3D view
140    private variable _settings
141    private variable _style;            # Array of current component styles.
142    private variable _initialStyle;     # Array of initial component styles.
143    private variable _axis
144    private variable _reset 1;          # Connection to server has been reset.
145    private variable _haveGlyphs 0
146    private variable _haveMolecules 0
147    private variable _havePolydata 0
148
149    private variable _first "";         # This is the topmost dataset.
150    private variable _start 0
151    private variable _title ""
152    private variable _width 0
153    private variable _height 0
154    private variable _resizePending 0
155    private variable _rotatePending 0
156    private variable _atomScalePending 0
157    private variable _bondScalePending 0
158    private variable _moleculeOpacityPending 0
159    private variable _moleculeQualityPending 0
160    private variable _polydataOpacityPending 0
161    private variable _glyphsOpacityPending 0
162    private variable _rotateDelay 150
163    private variable _scaleDelay 100
164
165    private common _downloadPopup;      # download options from popup
166    private common _hardcopy
167}
168
169itk::usual VtkViewer {
170    keep -background -foreground -cursor -font
171    keep -plotbackground -plotforeground
172}
173
174# ----------------------------------------------------------------------
175# CONSTRUCTOR
176# ----------------------------------------------------------------------
177itcl::body Rappture::VtkViewer::constructor {args} {
178    package require vtk
179    set _serverType "vtkvis"
180
181    #DebugOn
182    EnableWaitDialog 900
183
184    # Rebuild event
185    $_dispatcher register !rebuild
186    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
187
188    # Resize event
189    $_dispatcher register !resize
190    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
191
192    # Rotate event
193    $_dispatcher register !rotate
194    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
195
196    # Atom scale event
197    $_dispatcher register !atomscale
198    $_dispatcher dispatch $this !atomscale \
199        "[itcl::code $this SetAtomScale]; list"
200
201    # Bond scale event
202    $_dispatcher register !bondscale
203    $_dispatcher dispatch $this !bondscale \
204        "[itcl::code $this SetBondScale]; list"
205
206    # Molecule opacity event
207    $_dispatcher register !moleculeOpacity
208    $_dispatcher dispatch $this !moleculeOpacity \
209        "[itcl::code $this SetMoleculeOpacity]; list"
210
211    # Molecule quality event
212    $_dispatcher register !moleculeQuality
213    $_dispatcher dispatch $this !moleculeQuality \
214        "[itcl::code $this SetMoleculeQuality]; list"
215
216    # Polydata opacity event
217    $_dispatcher register !polydataOpacity
218    $_dispatcher dispatch $this !polydataOpacity \
219        "[itcl::code $this SetPolydataOpacity]; list"
220
221    # Glyphs opacity event
222    $_dispatcher register !glyphsOpacity
223    $_dispatcher dispatch $this !glyphsOpacity \
224        "[itcl::code $this SetGlyphsOpacity]; list"
225
226    #
227    # Populate parser with commands handle incoming requests
228    #
229    $_parser alias image [itcl::code $this ReceiveImage]
230    $_parser alias legend [itcl::code $this ReceiveLegend]
231    $_parser alias dataset [itcl::code $this ReceiveDataset]
232
233    # Initialize the view to some default parameters.
234    array set _view {
235        -ortho    0
236        -qw       0.853553
237        -qx       -0.353553
238        -qy       0.353553
239        -qz       0.146447
240        -xpan     0
241        -ypan     0
242        -zoom     1.0
243    }
244    set _arcball [blt::arcball create 100 100]
245    $_arcball quaternion [ViewToQuaternion]
246
247    array set _axis [subst {
248        labels          1
249        minorticks      1
250        visible         1
251        xgrid           0
252        ygrid           0
253        zgrid           0
254        xcutaway        0
255        ycutaway        0
256        zcutaway        0
257        xposition       0
258        yposition       0
259        zposition       0
260        xdirection      -1
261        ydirection      -1
262        zdirection      -1
263    }]
264    array set _settings [subst {
265        glyphs-colormap            BCGYR
266        glyphs-edges               0
267        glyphs-lighting            1
268        glyphs-opacity             100
269        glyphs-outline             0
270        glyphs-visible             1
271        glyphs-wireframe           0
272        legend                     1
273        molecule-atoms-visible     1
274        molecule-atomscale         0.3
275        molecule-bonds-visible     1
276        molecule-bondscale         0.075
277        molecule-bondstyle         "cylinder"
278        molecule-colorfield        "element"
279        molecule-colormap          elementDefault
280        molecule-colormode         "by_elements"
281        molecule-edges             0
282        molecule-labels            0
283        molecule-lighting          1
284        molecule-opacity           100
285        molecule-outline           0
286        molecule-quality           1.0
287        molecule-representation    "Ball and Stick"
288        molecule-rscale            "covalent"
289        molecule-visible           1
290        molecule-wireframe         0
291        polydata-colormap          BCGYR
292        polydata-edges             0
293        polydata-lighting          1
294        polydata-opacity           100
295        polydata-outline           0
296        polydata-visible           1
297        polydata-wireframe         0
298    }]
299    itk_component add view {
300        canvas $itk_component(plotarea).view \
301            -highlightthickness 0 -borderwidth 0
302    } {
303        usual
304        ignore -highlightthickness -borderwidth -background
305    }
306
307    set c $itk_component(view)
308    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
309    bind $c <Control-F1> [itcl::code $this ToggleConsole]
310
311    # Fix the scrollregion in case we go off screen
312    $c configure -scrollregion [$c bbox all]
313
314    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
315    set _map(cwidth) -1
316    set _map(cheight) -1
317    set _map(zoom) 1.0
318    set _map(original) ""
319
320    set f [$itk_component(main) component controls]
321    itk_component add reset {
322        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
323            -highlightthickness 0 \
324            -image [Rappture::icon reset-view] \
325            -command [itcl::code $this Zoom reset]
326    } {
327        usual
328        ignore -highlightthickness
329    }
330    pack $itk_component(reset) -side top -padx 2 -pady 2
331    Rappture::Tooltip::for $itk_component(reset) \
332        "Reset the view to the default zoom level"
333
334    itk_component add zoomin {
335        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
336            -highlightthickness 0 \
337            -image [Rappture::icon zoom-in] \
338            -command [itcl::code $this Zoom in]
339    } {
340        usual
341        ignore -highlightthickness
342    }
343    pack $itk_component(zoomin) -side top -padx 2 -pady 2
344    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
345
346    itk_component add zoomout {
347        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
348            -highlightthickness 0 \
349            -image [Rappture::icon zoom-out] \
350            -command [itcl::code $this Zoom out]
351    } {
352        usual
353        ignore -highlightthickness
354    }
355    pack $itk_component(zoomout) -side top -padx 2 -pady 2
356    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
357
358    if { [catch {
359        BuildAxisTab
360        #BuildCutawayTab
361        BuildCameraTab
362    } errs] != 0 } {
363        puts stderr errs=$errs
364    }
365
366    # Legend
367    set _image(legend) [image create photo]
368    itk_component add legend {
369        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
370    } {
371        usual
372        ignore -highlightthickness
373        rename -background -plotbackground plotBackground Background
374    }
375
376    # Hack around the Tk panewindow.  The problem is that the requested
377    # size of the 3d view isn't set until an image is retrieved from
378    # the server.  So the panewindow uses the tiny size.
379    set w 10000
380    pack forget $itk_component(view)
381    blt::table $itk_component(plotarea) \
382        0,0 $itk_component(view) -fill both -reqwidth $w
383    blt::table configure $itk_component(plotarea) c1 -resize none
384
385    # Bindings for rotation via mouse
386    bind $itk_component(view) <ButtonPress-1> \
387        [itcl::code $this Rotate click %x %y]
388    bind $itk_component(view) <B1-Motion> \
389        [itcl::code $this Rotate drag %x %y]
390    bind $itk_component(view) <ButtonRelease-1> \
391        [itcl::code $this Rotate release %x %y]
392
393    # Bindings for panning via mouse
394    bind $itk_component(view) <ButtonPress-2> \
395        [itcl::code $this Pan click %x %y]
396    bind $itk_component(view) <B2-Motion> \
397        [itcl::code $this Pan drag %x %y]
398    bind $itk_component(view) <ButtonRelease-2> \
399        [itcl::code $this Pan release %x %y]
400
401    #bind $itk_component(view) <ButtonRelease-3> \
402    #    [itcl::code $this Pick %x %y]
403
404    # Bindings for panning via keyboard
405    bind $itk_component(view) <KeyPress-Left> \
406        [itcl::code $this Pan set -10 0]
407    bind $itk_component(view) <KeyPress-Right> \
408        [itcl::code $this Pan set 10 0]
409    bind $itk_component(view) <KeyPress-Up> \
410        [itcl::code $this Pan set 0 -10]
411    bind $itk_component(view) <KeyPress-Down> \
412        [itcl::code $this Pan set 0 10]
413    bind $itk_component(view) <Shift-KeyPress-Left> \
414        [itcl::code $this Pan set -2 0]
415    bind $itk_component(view) <Shift-KeyPress-Right> \
416        [itcl::code $this Pan set 2 0]
417    bind $itk_component(view) <Shift-KeyPress-Up> \
418        [itcl::code $this Pan set 0 -2]
419    bind $itk_component(view) <Shift-KeyPress-Down> \
420        [itcl::code $this Pan set 0 2]
421
422    # Bindings for zoom via keyboard
423    bind $itk_component(view) <KeyPress-Prior> \
424        [itcl::code $this Zoom out]
425    bind $itk_component(view) <KeyPress-Next> \
426        [itcl::code $this Zoom in]
427
428    bind $itk_component(view) <Enter> "focus $itk_component(view)"
429
430    if {[string equal "x11" [tk windowingsystem]]} {
431        # Bindings for zoom via mouse
432        bind $itk_component(view) <4> [itcl::code $this Zoom out]
433        bind $itk_component(view) <5> [itcl::code $this Zoom in]
434    }
435
436    set _image(download) [image create photo]
437
438    eval itk_initialize $args
439
440    Connect
441}
442
443# ----------------------------------------------------------------------
444# DESTRUCTOR
445# ----------------------------------------------------------------------
446itcl::body Rappture::VtkViewer::destructor {} {
447    Disconnect
448    $_dispatcher cancel !rebuild
449    $_dispatcher cancel !resize
450    $_dispatcher cancel !rotate
451    image delete $_image(plot)
452    image delete $_image(download)
453    catch { blt::arcball destroy $_arcball }
454}
455
456itcl::body Rappture::VtkViewer::DoResize {} {
457    if { $_width < 2 } {
458        set _width 500
459    }
460    if { $_height < 2 } {
461        set _height 500
462    }
463    set _start [clock clicks -milliseconds]
464    SendCmd "screen size $_width $_height"
465
466    set _resizePending 0
467}
468
469itcl::body Rappture::VtkViewer::DoRotate {} {
470    SendCmd "camera orient [ViewToQuaternion]"
471    set _rotatePending 0
472}
473
474itcl::body Rappture::VtkViewer::EventuallyResize { w h } {
475    set _width $w
476    set _height $h
477    $_arcball resize $w $h
478    if { !$_resizePending } {
479        set _resizePending 1
480        $_dispatcher event -after 200 !resize
481    }
482}
483
484itcl::body Rappture::VtkViewer::EventuallyRotate { q } {
485    QuaternionToView $q
486    if { !$_rotatePending } {
487        set _rotatePending 1
488        $_dispatcher event -after $_rotateDelay !rotate
489    }
490}
491
492itcl::body Rappture::VtkViewer::SetAtomScale {} {
493    SendCmd "molecule ascale $_settings(molecule-atomscale)"
494    set _atomScalePending 0
495}
496
497itcl::body Rappture::VtkViewer::SetBondScale {} {
498    SendCmd "molecule bscale $_settings(molecule-bondscale)"
499    set _bondScalePending 0
500}
501
502itcl::body Rappture::VtkViewer::SetMoleculeOpacity {} {
503    set _moleculeOpacityPending 0
504    foreach dataset [CurrentDatasets -visible $_first] {
505        foreach { dataobj comp } [split $dataset -] break
506        if { [$dataobj type $comp] == "molecule" } {
507            SetOpacity $dataset
508        }
509    }
510}
511
512itcl::body Rappture::VtkViewer::SetMoleculeQuality {} {
513    SendCmd [subst {molecule aquality $_settings(molecule-quality)
514molecule bquality $_settings(molecule-quality)}]
515    set _moleculeQualityPending 0
516}
517
518itcl::body Rappture::VtkViewer::SetGlyphsOpacity {} {
519    set _glyphsOpacityPending 0
520    foreach dataset [CurrentDatasets -visible $_first] {
521        foreach { dataobj comp } [split $dataset -] break
522        if { [$dataobj type $comp] == "glyphs" } {
523            SetOpacity $dataset
524        }
525    }
526}
527
528itcl::body Rappture::VtkViewer::SetPolydataOpacity {} {
529    set _polydataOpacityPending 0
530    foreach dataset [CurrentDatasets -visible $_first] {
531        foreach { dataobj comp } [split $dataset -] break
532        if { [$dataobj type $comp] == "polydata" } {
533            SetOpacity $dataset
534        }
535    }
536}
537
538itcl::body Rappture::VtkViewer::EventuallySetAtomScale { args } {
539    if { !$_atomScalePending } {
540        set _atomScalePending 1
541        $_dispatcher event -after $_scaleDelay !atomscale
542    }
543}
544
545itcl::body Rappture::VtkViewer::EventuallySetBondScale { args } {
546    if { !$_bondScalePending } {
547        set _bondScalePending 1
548        $_dispatcher event -after $_scaleDelay !bondscale
549    }
550}
551
552itcl::body Rappture::VtkViewer::EventuallySetMoleculeOpacity { args } {
553    if { !$_moleculeOpacityPending } {
554        set _moleculeOpacityPending 1
555        $_dispatcher event -after $_scaleDelay !moleculeOpacity
556    }
557}
558
559itcl::body Rappture::VtkViewer::EventuallySetMoleculeQuality { args } {
560    if { !$_moleculeQualityPending } {
561        set _moleculeQualityPending 1
562        $_dispatcher event -after $_scaleDelay !moleculeQuality
563    }
564}
565
566itcl::body Rappture::VtkViewer::EventuallySetPolydataOpacity { args } {
567    if { !$_polydataOpacityPending } {
568        set _polydataOpacityPending 1
569        $_dispatcher event -after $_scaleDelay !polydataOpacity
570    }
571}
572
573itcl::body Rappture::VtkViewer::EventuallySetGlyphsOpacity { args } {
574    if { !$_glyphsOpacityPending } {
575        set _glyphsOpacityPending 1
576        $_dispatcher event -after $_scaleDelay !glyphsOpacity
577    }
578}
579
580# ----------------------------------------------------------------------
581# USAGE: add <dataobj> ?<settings>?
582#
583# Clients use this to add a data object to the plot.  The optional
584# <settings> are used to configure the plot.  Allowed settings are
585# -color, -brightness, -width, -linestyle, and -raise.
586# ----------------------------------------------------------------------
587itcl::body Rappture::VtkViewer::add {dataobj {settings ""}} {
588    array set params {
589        -color auto
590        -width 1
591        -linestyle solid
592        -brightness 0
593        -raise 0
594        -description ""
595        -param ""
596        -type ""
597    }
598    array set params $settings
599
600    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
601        # can't handle -autocolors yet
602        set params(-color) black
603    }
604    set pos [lsearch -exact $_dlist $dataobj]
605    if {$pos < 0} {
606        lappend _dlist $dataobj
607    }
608    set _obj2ovride($dataobj-color) $params(-color)
609    set _obj2ovride($dataobj-width) $params(-width)
610    set _obj2ovride($dataobj-raise) $params(-raise)
611    $_dispatcher event -idle !rebuild
612}
613
614# ----------------------------------------------------------------------
615# USAGE: delete ?<dataobj1> <dataobj2> ...?
616#
617# Clients use this to delete a dataobj from the plot.  If no dataobjs
618# are specified, then all dataobjs are deleted.  No data objects are
619# deleted.  They are only removed from the display list.
620# ----------------------------------------------------------------------
621itcl::body Rappture::VtkViewer::delete {args} {
622    if { [llength $args] == 0} {
623        set args $_dlist
624    }
625    # Delete all specified dataobjs
626    set changed 0
627    foreach dataobj $args {
628        set pos [lsearch -exact $_dlist $dataobj]
629        if { $pos < 0 } {
630            continue;                   # Don't know anything about it.
631        }
632        # Remove it from the dataobj list.
633        set _dlist [lreplace $_dlist $pos $pos]
634        array unset _obj2ovride $dataobj-*
635        set changed 1
636    }
637    # If anything changed, then rebuild the plot
638    if { $changed } {
639        $_dispatcher event -idle !rebuild
640    }
641}
642
643# ----------------------------------------------------------------------
644# USAGE: get ?-objects?
645# USAGE: get ?-visible?
646# USAGE: get ?-image view?
647#
648# Clients use this to query the list of objects being plotted, in
649# order from bottom to top of this result.  The optional "-image"
650# flag can also request the internal images being shown.
651# ----------------------------------------------------------------------
652itcl::body Rappture::VtkViewer::get {args} {
653    if {[llength $args] == 0} {
654        set args "-objects"
655    }
656
657    set op [lindex $args 0]
658    switch -- $op {
659        "-objects" {
660            # put the dataobj list in order according to -raise options
661            set dlist {}
662            foreach dataobj $_dlist {
663                if { ![IsValidObject $dataobj] } {
664                    continue
665                }
666                if {[info exists _obj2ovride($dataobj-raise)] &&
667                    $_obj2ovride($dataobj-raise)} {
668                    set dlist [linsert $dlist 0 $dataobj]
669                } else {
670                    lappend dlist $dataobj
671                }
672            }
673            return $dlist
674        }
675        "-visible" {
676            set dlist {}
677            foreach dataobj $_dlist {
678                if { ![IsValidObject $dataobj] } {
679                    continue
680                }
681                if { ![info exists _obj2ovride($dataobj-raise)] } {
682                    # No setting indicates that the object isn't visible.
683                    continue
684                }
685                # Otherwise use the -raise parameter to put the object to
686                # the front of the list.
687                if { $_obj2ovride($dataobj-raise) } {
688                    set dlist [linsert $dlist 0 $dataobj]
689                } else {
690                    lappend dlist $dataobj
691                }
692            }
693            return $dlist
694        }
695        "-image" {
696            if {[llength $args] != 2} {
697                error "wrong # args: should be \"get -image view\""
698            }
699            switch -- [lindex $args end] {
700                view {
701                    return $_image(plot)
702                }
703                default {
704                    error "bad image name \"[lindex $args end]\": should be view"
705                }
706            }
707        }
708        default {
709            error "bad option \"$op\": should be -objects, -visible or -image"
710        }
711    }
712}
713
714# ----------------------------------------------------------------------
715# USAGE: scale ?<data1> <data2> ...?
716#
717# Sets the default limits for the overall plot according to the
718# limits of the data for all of the given <data> objects.  This
719# accounts for all objects--even those not showing on the screen.
720# Because of this, the limits are appropriate for all objects as
721# the user scans through data in the ResultSet viewer.
722# ----------------------------------------------------------------------
723itcl::body Rappture::VtkViewer::scale {args} {
724    foreach dataobj $args {
725        foreach comp [$dataobj components] {
726            set type [$dataobj type $comp]
727            switch -- $type {
728                "polydata" {
729                    set _havePolydata 1
730                }
731                "glyphs" {
732                    set _haveGlyphs 1
733                }
734                "molecule" {
735                    set _haveMolecules 1
736                }
737            }
738        }
739        array set bounds [limits $dataobj]
740        if {[info exists bounds(xmin)] && (![info exists _limits(xmin)] || $_limits(xmin) > $bounds(xmin))} {
741            set _limits(xmin) $bounds(xmin)
742        }
743        if {[info exists bounds(xmax)] && (![info exists _limits(xmax)] || $_limits(xmax) < $bounds(xmax))} {
744            set _limits(xmax) $bounds(xmax)
745        }
746
747        if {[info exists bounds(ymin)] && (![info exists _limits(ymin)] || $_limits(ymin) > $bounds(ymin))} {
748            set _limits(ymin) $bounds(ymin)
749        }
750        if {[info exists bounds(ymax)] && (![info exists _limits(ymax)] || $_limits(ymax) < $bounds(ymax))} {
751            set _limits(ymax) $bounds(ymax)
752        }
753
754        if {[info exists bounds(zmin)] && (![info exists _limits(zmin)] || $_limits(zmin) > $bounds(zmin))} {
755            set _limits(zmin) $bounds(zmin)
756        }
757        if {[info exists bounds(zmax)] && (![info exists _limits(zmax)] || $_limits(zmax) < $bounds(zmax))} {
758            set _limits(zmax) $bounds(zmax)
759        }
760    }
761    if { $_haveGlyphs } {
762        if { ![$itk_component(main) exists "Glyph Settings"] } {
763            if { [catch { BuildGlyphsTab } errs ]  != 0 } {
764                global errorInfo
765                puts stderr "errs=$errs\nerrorInfo=$errorInfo"
766            }
767        }
768    }
769    if { $_havePolydata } {
770        if { ![$itk_component(main) exists "Mesh Settings"] } {
771            if { [catch { BuildPolydataTab } errs ]  != 0 } {
772                global errorInfo
773                puts stderr "errs=$errs\nerrorInfo=$errorInfo"
774            }
775        }
776    }
777    if { $_haveMolecules } {
778        if { ![$itk_component(main) exists "Molecule Settings"]} {
779            if { [catch { BuildMoleculeTab } errs ]  != 0 } {
780                global errorInfo
781                puts stderr "errs=$errs\nerrorInfo=$errorInfo"
782            }
783        }
784    }
785}
786
787# ----------------------------------------------------------------------
788# USAGE: download coming
789# USAGE: download controls <downloadCommand>
790# USAGE: download now
791#
792# Clients use this method to create a downloadable representation
793# of the plot.  Returns a list of the form {ext string}, where
794# "ext" is the file extension (indicating the type of data) and
795# "string" is the data itself.
796# ----------------------------------------------------------------------
797itcl::body Rappture::VtkViewer::download {option args} {
798    switch $option {
799        coming {
800            if {[catch {
801                blt::winop snap $itk_component(plotarea) $_image(download)
802            }]} {
803                $_image(download) configure -width 1 -height 1
804                $_image(download) put #000000
805            }
806        }
807        controls {
808            set popup .vtkviewerdownload
809            if { ![winfo exists .vtkviewerdownload] } {
810                set inner [BuildDownloadPopup $popup [lindex $args 0]]
811            } else {
812                set inner [$popup component inner]
813            }
814            set _downloadPopup(image_controls) $inner.image_frame
815            set num [llength [get]]
816            set num [expr {($num == 1) ? "1 result" : "$num results"}]
817            set word [Rappture::filexfer::label downloadWord]
818            $inner.summary configure -text "$word $num in the following format:"
819            update idletasks            ;# Fix initial sizes
820            return $popup
821        }
822        now {
823            set popup .vtkviewerdownload
824            if {[winfo exists .vtkviewerdownload]} {
825                $popup deactivate
826            }
827            switch -- $_downloadPopup(format) {
828                "image" {
829                    return [$this GetImage [lindex $args 0]]
830                }
831                "vtk" {
832                    return [$this GetVtkData [lindex $args 0]]
833                }
834            }
835            return ""
836        }
837        default {
838            error "bad option \"$option\": should be coming, controls, now"
839        }
840    }
841}
842
843# ----------------------------------------------------------------------
844# USAGE: Connect ?<host:port>,<host:port>...?
845#
846# Clients use this method to establish a connection to a new
847# server, or to reestablish a connection to the previous server.
848# Any existing connection is automatically closed.
849# ----------------------------------------------------------------------
850itcl::body Rappture::VtkViewer::Connect {} {
851    global readyForNextFrame
852    set readyForNextFrame 1
853    set _hosts [GetServerList "vtkvis"]
854    if { "" == $_hosts } {
855        return 0
856    }
857    set _reset 1
858    set result [VisViewer::Connect $_hosts]
859    if { $result } {
860        if { $_reportClientInfo }  {
861            # Tell the server the viewer, hub, user and session.
862            # Do this immediately on connect before buffering any commands
863            global env
864
865            set info {}
866            set user "???"
867            if { [info exists env(USER)] } {
868                set user $env(USER)
869            }
870            set session "???"
871            if { [info exists env(SESSION)] } {
872                set session $env(SESSION)
873            }
874            lappend info "version" "$Rappture::version"
875            lappend info "build" "$Rappture::build"
876            lappend info "svnurl" "$Rappture::svnurl"
877            lappend info "installdir" "$Rappture::installdir"
878            lappend info "hub" [exec hostname]
879            lappend info "client" "vtkviewer"
880            lappend info "user" $user
881            lappend info "session" $session
882            SendCmd "clientinfo [list $info]"
883        }
884
885        set w [winfo width $itk_component(view)]
886        set h [winfo height $itk_component(view)]
887        EventuallyResize $w $h
888    }
889    return $result
890}
891
892#
893# isconnected --
894#
895# Indicates if we are currently connected to the visualization server.
896#
897itcl::body Rappture::VtkViewer::isconnected {} {
898    return [VisViewer::IsConnected]
899}
900
901#
902# disconnect --
903#
904itcl::body Rappture::VtkViewer::disconnect {} {
905    Disconnect
906    set _reset 1
907}
908
909#
910# Disconnect --
911#
912# Clients use this method to disconnect from the current rendering server.
913#
914itcl::body Rappture::VtkViewer::Disconnect {} {
915    VisViewer::Disconnect
916
917    # disconnected -- no more data sitting on server
918    array unset _datasets
919    array unset _colormaps
920    array unset _dataset2style
921
922    global readyForNextFrame
923    set readyForNextFrame 1
924}
925
926# ----------------------------------------------------------------------
927# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
928#
929# Invoked automatically whenever the "image" command comes in from
930# the rendering server.  Indicates that binary image data with the
931# specified <size> will follow.
932# ----------------------------------------------------------------------
933itcl::body Rappture::VtkViewer::ReceiveImage { args } {
934    global readyForNextFrame
935    set readyForNextFrame 1
936    array set info {
937        -token "???"
938        -bytes 0
939        -type image
940    }
941    array set info $args
942    set bytes [ReceiveBytes $info(-bytes)]
943    if { $info(-type) == "image" } {
944        if 0 {
945            set f [open "last.ppm" "w"]
946            fconfigure $f -encoding binary
947            puts -nonewline $f $bytes
948            close $f
949        }
950        $_image(plot) configure -data $bytes
951        set time [clock seconds]
952        set date [clock format $time]
953        if { $_start > 0 } {
954            set finish [clock clicks -milliseconds]
955            set _start 0
956        }
957    } elseif { $info(type) == "print" } {
958        set tag $this-print-$info(-token)
959        set _hardcopy($tag) $bytes
960    }
961}
962
963#
964# ReceiveDataset --
965#
966itcl::body Rappture::VtkViewer::ReceiveDataset { args } {
967    if { ![isconnected] } {
968        return
969    }
970    set option [lindex $args 0]
971    switch -- $option {
972        "scalar" {
973            set option [lindex $args 1]
974            switch -- $option {
975                "world" {
976                    foreach { x y z value tag } [lrange $args 2 end] break
977                }
978                "pixel" {
979                    foreach { x y value tag } [lrange $args 2 end] break
980                }
981            }
982        }
983        "vector" {
984            set option [lindex $args 1]
985            switch -- $option {
986                "world" {
987                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
988                }
989                "pixel" {
990                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
991                }
992            }
993        }
994        "names" {
995            foreach { name } [lindex $args 1] {
996                #puts stderr "Dataset: $name"
997            }
998        }
999        default {
1000            error "unknown dataset option \"$option\" from server"
1001        }
1002    }
1003}
1004
1005# ----------------------------------------------------------------------
1006# USAGE: Rebuild
1007#
1008# Called automatically whenever something changes that affects the
1009# data in the widget.  Clears any existing data and rebuilds the
1010# widget to display new data.
1011# ----------------------------------------------------------------------
1012itcl::body Rappture::VtkViewer::Rebuild {} {
1013    set w [winfo width $itk_component(view)]
1014    set h [winfo height $itk_component(view)]
1015    if { $w < 2 || $h < 2 } {
1016        update
1017        $_dispatcher event -idle !rebuild
1018        return
1019    }
1020
1021    # Turn on buffering of commands to the server.  We don't want to
1022    # be preempted by a server disconnect/reconnect (which automatically
1023    # generates a new call to Rebuild).
1024    StartBufferingCommands
1025
1026    if { $_reset } {
1027        set _width $w
1028        set _height $h
1029        $_arcball resize $w $h
1030        DoResize
1031        InitSettings axis-xgrid axis-ygrid axis-zgrid axis-mode \
1032            axis-visible axis-labels axis-minorticks
1033
1034        StopBufferingCommands
1035        SendCmd "imgflush"
1036        StartBufferingCommands
1037    }
1038
1039    set _first ""
1040    SendCmd "dataset visible 0"
1041    set count 0
1042    eval scale $_dlist
1043    foreach dataobj [get -objects] {
1044        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
1045            set _first $dataobj
1046        }
1047        foreach comp [$dataobj components] {
1048            set tag $dataobj-$comp
1049            if { ![info exists _datasets($tag)] } {
1050                set bytes [$dataobj data $comp]
1051                if { $bytes == "" } {
1052                    continue
1053                }
1054                if 0 {
1055                    set f [open /tmp/vtkviewer.vtk "w"]
1056                    fconfigure $f -translation binary -encoding binary
1057                    puts -nonewline $f $bytes
1058                    close $f
1059                }
1060                set length [string length $bytes]
1061                if { $_reportClientInfo }  {
1062                    set info {}
1063                    lappend info "tool_id"       [$dataobj hints toolid]
1064                    lappend info "tool_name"     [$dataobj hints toolname]
1065                    lappend info "tool_title"    [$dataobj hints tooltitle]
1066                    lappend info "tool_command"  [$dataobj hints toolcommand]
1067                    lappend info "tool_revision" [$dataobj hints toolrevision]
1068                    lappend info "dataset_label" [$dataobj hints label]
1069                    lappend info "dataset_size"  $length
1070                    lappend info "dataset_tag"   $tag
1071                    SendCmd "clientinfo [list $info]"
1072                }
1073                SendCmd "dataset add $tag data follows $length"
1074                SendData $bytes
1075                set _datasets($tag) 1
1076                SetObjectStyle $dataobj $comp
1077            }
1078            set type [$dataobj type $comp]
1079            if { [info exists _obj2ovride($dataobj-raise)] } {
1080                SendCmd "$type visible 1 $tag"
1081                SetOpacity $tag
1082            }
1083        }
1084    }
1085    if {"" != $_first} {
1086        foreach axis { x y z } {
1087            set label [$_first hints ${axis}label]
1088            if { $label != "" } {
1089                SendCmd [list axis name $axis $label]
1090            }
1091            set units [$_first hints ${axis}units]
1092            if { $units != "" } {
1093                SendCmd [list axis units $axis $units]
1094            }
1095        }
1096    }
1097    if { $_haveGlyphs } {
1098        InitSettings glyphs-outline
1099    }
1100    if { $_haveMolecules } {
1101        InitSettings molecule-outline
1102    }
1103    if { $_havePolydata } {
1104        InitSettings polydata-outline
1105    }
1106    if { $_reset } {
1107        if { $_haveGlyphs } {
1108            InitSettings glyphs-edges glyphs-lighting glyphs-opacity \
1109                glyphs-visible glyphs-wireframe
1110        }
1111        if { $_havePolydata } {
1112            InitSettings polydata-edges polydata-lighting polydata-opacity \
1113                polydata-visible polydata-wireframe
1114        }
1115        if { $_haveMolecules } {
1116            InitSettings molecule-edges molecule-lighting molecule-opacity \
1117                molecule-visible molecule-wireframe molecule-labels
1118        }
1119
1120        $_arcball quaternion [ViewToQuaternion]
1121        SendCmd "camera reset"
1122        if { $_view(-ortho)} {
1123            SendCmd "camera mode ortho"
1124        } else {
1125            SendCmd "camera mode persp"
1126        }
1127        DoRotate
1128        PanCamera
1129        Zoom reset
1130    }
1131
1132    if { $_haveMolecules } {
1133        #InitSettings molecule-representation
1134    }
1135    set _reset 0
1136    global readyForNextFrame
1137    set readyForNextFrame 0;            # Don't advance to the next frame
1138                                        # until we get an image.
1139
1140    # Actually write the commands to the server socket.  If it fails, we don't
1141    # care.  We're finished here.
1142    blt::busy hold $itk_component(hull)
1143    StopBufferingCommands
1144    blt::busy release $itk_component(hull)
1145}
1146
1147# ----------------------------------------------------------------------
1148# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
1149#
1150# Returns a list of server IDs for the current datasets being displayed.  This
1151# is normally a single ID, but it might be a list of IDs if the current data
1152# object has multiple components.
1153# ----------------------------------------------------------------------
1154itcl::body Rappture::VtkViewer::CurrentDatasets {args} {
1155    set flag [lindex $args 0]
1156    switch -- $flag {
1157        "-all" {
1158            if { [llength $args] > 1 } {
1159                error "CurrentDatasets: can't specify dataobj after \"-all\""
1160            }
1161            set dlist [get -objects]
1162        }
1163        "-visible" {
1164            if { [llength $args] > 1 } {
1165                set dlist {}
1166                set args [lrange $args 1 end]
1167                foreach dataobj $args {
1168                    if { [info exists _obj2ovride($dataobj-raise)] } {
1169                        lappend dlist $dataobj
1170                    }
1171                }
1172            } else {
1173                set dlist [get -visible]
1174            }
1175        }
1176        default {
1177            set dlist $args
1178        }
1179    }
1180    set rlist ""
1181    foreach dataobj $dlist {
1182        foreach comp [$dataobj components] {
1183            set tag $dataobj-$comp
1184            if { [info exists _datasets($tag)] && $_datasets($tag) } {
1185                lappend rlist $tag
1186            }
1187        }
1188    }
1189    return $rlist
1190}
1191
1192# ----------------------------------------------------------------------
1193# USAGE: Zoom in
1194# USAGE: Zoom out
1195# USAGE: Zoom reset
1196#
1197# Called automatically when the user clicks on one of the zoom
1198# controls for this widget.  Changes the zoom for the current view.
1199# ----------------------------------------------------------------------
1200itcl::body Rappture::VtkViewer::Zoom {option} {
1201    switch -- $option {
1202        "in" {
1203            set _view(-zoom) [expr {$_view(-zoom)*1.25}]
1204            SendCmd "camera zoom $_view(-zoom)"
1205        }
1206        "out" {
1207            set _view(-zoom) [expr {$_view(-zoom)*0.8}]
1208            SendCmd "camera zoom $_view(-zoom)"
1209        }
1210        "reset" {
1211            array set _view {
1212                -qw      0.853553
1213                -qx      -0.353553
1214                -qy      0.353553
1215                -qz      0.146447
1216                -xpan    0
1217                -ypan    0
1218                -zoom    1.0
1219            }
1220            if { $_first != "" } {
1221                set location [$_first hints camera]
1222                if { $location != "" } {
1223                    array set _view $location
1224                }
1225            }
1226            $_arcball quaternion [ViewToQuaternion]
1227            DoRotate
1228            SendCmd "camera reset"
1229        }
1230    }
1231}
1232
1233itcl::body Rappture::VtkViewer::PanCamera {} {
1234    set x $_view(-xpan)
1235    set y $_view(-ypan)
1236    SendCmd "camera pan $x $y"
1237}
1238
1239# ----------------------------------------------------------------------
1240# USAGE: Rotate click <x> <y>
1241# USAGE: Rotate drag <x> <y>
1242# USAGE: Rotate release <x> <y>
1243#
1244# Called automatically when the user clicks/drags/releases in the
1245# plot area.  Moves the plot according to the user's actions.
1246# ----------------------------------------------------------------------
1247itcl::body Rappture::VtkViewer::Rotate {option x y} {
1248    switch -- $option {
1249        "click" {
1250            $itk_component(view) configure -cursor fleur
1251            set _click(x) $x
1252            set _click(y) $y
1253        }
1254        "drag" {
1255            if {[array size _click] == 0} {
1256                Rotate click $x $y
1257            } else {
1258                set w [winfo width $itk_component(view)]
1259                set h [winfo height $itk_component(view)]
1260                if {$w <= 0 || $h <= 0} {
1261                    return
1262                }
1263
1264                if {[catch {
1265                    # this fails sometimes for no apparent reason
1266                    set dx [expr {double($x-$_click(x))/$w}]
1267                    set dy [expr {double($y-$_click(y))/$h}]
1268                }]} {
1269                    return
1270                }
1271                if { $dx == 0 && $dy == 0 } {
1272                    return
1273                }
1274                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1275                EventuallyRotate $q
1276                set _click(x) $x
1277                set _click(y) $y
1278            }
1279        }
1280        "release" {
1281            Rotate drag $x $y
1282            $itk_component(view) configure -cursor ""
1283            catch {unset _click}
1284        }
1285        default {
1286            error "bad option \"$option\": should be click, drag, release"
1287        }
1288    }
1289}
1290
1291itcl::body Rappture::VtkViewer::Pick {x y} {
1292    foreach tag [CurrentDatasets -visible] {
1293        SendCmd "dataset getscalar pixel $x $y $tag"
1294    }
1295}
1296
1297# ----------------------------------------------------------------------
1298# USAGE: $this Pan click x y
1299#        $this Pan drag x y
1300#        $this Pan release x y
1301#
1302# Called automatically when the user clicks on one of the zoom
1303# controls for this widget.  Changes the zoom for the current view.
1304# ----------------------------------------------------------------------
1305itcl::body Rappture::VtkViewer::Pan {option x y} {
1306    switch -- $option {
1307        "set" {
1308            set w [winfo width $itk_component(view)]
1309            set h [winfo height $itk_component(view)]
1310            set x [expr $x / double($w)]
1311            set y [expr $y / double($h)]
1312            set _view(-xpan) [expr $_view(-xpan) + $x]
1313            set _view(-ypan) [expr $_view(-ypan) + $y]
1314            PanCamera
1315            return
1316        }
1317        "click" {
1318            set _click(x) $x
1319            set _click(y) $y
1320            $itk_component(view) configure -cursor hand1
1321        }
1322        "drag" {
1323            if { ![info exists _click(x)] } {
1324                set _click(x) $x
1325            }
1326            if { ![info exists _click(y)] } {
1327                set _click(y) $y
1328            }
1329            set w [winfo width $itk_component(view)]
1330            set h [winfo height $itk_component(view)]
1331            set dx [expr ($_click(x) - $x)/double($w)]
1332            set dy [expr ($_click(y) - $y)/double($h)]
1333            set _click(x) $x
1334            set _click(y) $y
1335            set _view(-xpan) [expr $_view(-xpan) - $dx]
1336            set _view(-ypan) [expr $_view(-ypan) - $dy]
1337            PanCamera
1338        }
1339        "release" {
1340            Pan drag $x $y
1341            $itk_component(view) configure -cursor ""
1342        }
1343        default {
1344            error "unknown option \"$option\": should set, click, drag, or release"
1345        }
1346    }
1347}
1348
1349# ----------------------------------------------------------------------
1350# USAGE: InitSettings <what> ?<value>?
1351#
1352# Used internally to update rendering settings whenever parameters
1353# change in the popup settings panel.  Sends the new settings off
1354# to the back end.
1355# ----------------------------------------------------------------------
1356itcl::body Rappture::VtkViewer::InitSettings { args } {
1357    foreach setting $args {
1358        AdjustSetting $setting
1359    }
1360}
1361
1362#
1363# AdjustSetting --
1364#
1365# Changes/updates a specific setting in the widget.  There are
1366# usually user-setable option.  Commands are sent to the render
1367# server.
1368#
1369itcl::body Rappture::VtkViewer::AdjustSetting {what {value ""}} {
1370    if { ![isconnected] } {
1371        return
1372    }
1373    switch -- $what {
1374        "glyphs-opacity" {
1375            foreach dataset [CurrentDatasets -visible $_first] {
1376                foreach { dataobj comp } [split $dataset -] break
1377                if { [$dataobj type $comp] == "glyphs" } {
1378                    SetOpacity $dataset
1379                }
1380            }
1381        }
1382        "glyphs-outline" {
1383            set bool $_settings($what)
1384            foreach dataset [CurrentDatasets -visible $_first] {
1385                foreach { dataobj comp } [split $dataset -] break
1386                set type [$dataobj type $comp]
1387                if { $type == "glyphs" } {
1388                    SendCmd "outline visible $bool $dataset"
1389                }
1390            }
1391        }
1392        "glyphs-wireframe" {
1393            set bool $_settings($what)
1394            foreach dataset [CurrentDatasets -visible $_first] {
1395                foreach { dataobj comp } [split $dataset -] break
1396                set type [$dataobj type $comp]
1397                if { $type == "glyphs" } {
1398                    SendCmd "$type wireframe $bool $dataset"
1399                }
1400            }
1401        }
1402        "glyphs-visible" {
1403            set bool $_settings($what)
1404            foreach dataset [CurrentDatasets -visible $_first] {
1405                foreach { dataobj comp } [split $dataset -] break
1406                set type [$dataobj type $comp]
1407                if { $type == "glyphs" } {
1408                    SendCmd "$type visible $bool $dataset"
1409                }
1410            }
1411        }
1412        "glyphs-lighting" {
1413            set bool $_settings($what)
1414            foreach dataset [CurrentDatasets -visible $_first] {
1415                foreach { dataobj comp } [split $dataset -] break
1416                set type [$dataobj type $comp]
1417                if { $type == "glyphs" } {
1418                    SendCmd "$type lighting $bool $dataset"
1419                }
1420            }
1421        }
1422        "glyphs-edges" {
1423            set bool $_settings($what)
1424            foreach dataset [CurrentDatasets -visible $_first] {
1425                foreach { dataobj comp } [split $dataset -] break
1426                set type [$dataobj type $comp]
1427                if { $type == "glyphs" } {
1428                    SendCmd "$type edges $bool $dataset"
1429                }
1430            }
1431        }
1432        "glyphs-colormap" {
1433            set colormap [$itk_component(glyphscolormap) value]
1434            set colormap [$itk_component(glyphscolormap) translate $colormap]
1435            set _settings($what) $colormap
1436            foreach dataset [CurrentDatasets -visible $_first] {
1437                foreach {dataobj comp} [split $dataset -] break
1438                set type [$dataobj type $comp]
1439                if { $type == "glyphs" } {
1440                    ChangeColormap $dataobj $comp $colormap
1441                    # FIXME: fill in current selected fieldname
1442                    #SendCmd "glyphs colormode scalar {} $dataset"
1443                }
1444            }
1445            set _legendPending 1
1446        }
1447        "polydata-opacity" {
1448            foreach dataset [CurrentDatasets -visible $_first] {
1449                foreach { dataobj comp } [split $dataset -] break
1450                if { [$dataobj type $comp] == "polydata" } {
1451                    SetOpacity $dataset
1452                }
1453            }
1454        }
1455        "polydata-outline" {
1456            set bool $_settings($what)
1457            foreach dataset [CurrentDatasets -visible $_first] {
1458                foreach { dataobj comp } [split $dataset -] break
1459                set type [$dataobj type $comp]
1460                if { $type == "polydata" } {
1461                    SendCmd "outline visible $bool $dataset"
1462                }
1463            }
1464        }
1465        "polydata-wireframe" {
1466            set bool $_settings($what)
1467            foreach dataset [CurrentDatasets -visible $_first] {
1468                foreach { dataobj comp } [split $dataset -] break
1469                set type [$dataobj type $comp]
1470                if { $type == "polydata" } {
1471                    SendCmd "$type wireframe $bool $dataset"
1472                }
1473            }
1474        }
1475        "polydata-visible" {
1476            set bool $_settings($what)
1477            foreach dataset [CurrentDatasets -visible $_first] {
1478                foreach { dataobj comp } [split $dataset -] break
1479                set type [$dataobj type $comp]
1480                if { $type == "polydata" } {
1481                    SendCmd "$type visible $bool $dataset"
1482                }
1483            }
1484        }
1485        "polydata-lighting" {
1486            set bool $_settings($what)
1487            foreach dataset [CurrentDatasets -visible $_first] {
1488                foreach { dataobj comp } [split $dataset -] break
1489                set type [$dataobj type $comp]
1490                if { $type == "polydata" } {
1491                    SendCmd "$type lighting $bool $dataset"
1492                }
1493            }
1494        }
1495        "polydata-edges" {
1496            set bool $_settings($what)
1497            foreach dataset [CurrentDatasets -visible $_first] {
1498                foreach { dataobj comp } [split $dataset -] break
1499                set type [$dataobj type $comp]
1500                if { $type == "polydata" } {
1501                    SendCmd "$type edges $bool $dataset"
1502                }
1503            }
1504        }
1505        "polydata-colormap" {
1506            set colormap [$itk_component(polydatacolormap) value]
1507            set colormap [$itk_component(polydatacolormap) translate $colormap]
1508            set _settings($what) $colormap
1509            foreach dataset [CurrentDatasets -visible $_first] {
1510                foreach {dataobj comp} [split $dataset -] break
1511                set type [$dataobj type $comp]
1512                if { $type == "polydata" } {
1513                    ChangeColormap $dataobj $comp $colormap
1514                    # FIXME: fill in current selected fieldname
1515                    #SendCmd "polydata colormode scalar {} $dataset"
1516                }
1517            }
1518            set _legendPending 1
1519        }
1520        "molecule-opacity" {
1521            foreach dataset [CurrentDatasets -visible $_first] {
1522                foreach { dataobj comp } [split $dataset -] break
1523                if { [$dataobj type $comp] == "molecule" } {
1524                    SetOpacity $dataset
1525                }
1526            }
1527        }
1528        "molecule-outline" {
1529            set bool $_settings($what)
1530            foreach dataset [CurrentDatasets -visible $_first] {
1531                foreach { dataobj comp } [split $dataset -] break
1532                set type [$dataobj type $comp]
1533                if { $type == "molecule" } {
1534                    SendCmd "outline visible $bool $dataset"
1535                }
1536            }
1537        }
1538        "molecule-wireframe" {
1539            set bool $_settings($what)
1540            foreach dataset [CurrentDatasets -visible $_first] {
1541                foreach { dataobj comp } [split $dataset -] break
1542                set type [$dataobj type $comp]
1543                if { $type == "molecule" } {
1544                    SendCmd "molecule wireframe $bool $dataset"
1545                }
1546            }
1547        }
1548        "molecule-visible" {
1549            set bool $_settings($what)
1550            foreach dataset [CurrentDatasets -visible $_first] {
1551                foreach { dataobj comp } [split $dataset -] break
1552                set type [$dataobj type $comp]
1553                if { $type == "molecule" } {
1554                    SendCmd "molecule visible $bool $dataset"
1555                }
1556            }
1557        }
1558        "molecule-lighting" {
1559            set bool $_settings($what)
1560            foreach dataset [CurrentDatasets -visible $_first] {
1561                foreach { dataobj comp } [split $dataset -] break
1562                set type [$dataobj type $comp]
1563                if { $type == "molecule" } {
1564                    SendCmd "molecule lighting $bool $dataset"
1565                }
1566            }
1567        }
1568        "molecule-edges" {
1569            set bool $_settings($what)
1570            foreach dataset [CurrentDatasets -visible $_first] {
1571                foreach { dataobj comp } [split $dataset -] break
1572                set type [$dataobj type $comp]
1573                if { $type == "molecule" } {
1574                    SendCmd "molecule edges $bool $dataset"
1575                }
1576            }
1577        }
1578        "molecule-colormap" {
1579            set colormap [$itk_component(moleculecolormap) value]
1580            set colormap [$itk_component(moleculecolormap) translate $colormap]
1581            set _settings($what) $colormap
1582            foreach dataset [CurrentDatasets -visible $_first] {
1583                foreach {dataobj comp} [split $dataset -] break
1584                set type [$dataobj type $comp]
1585                if { $type == "molecule" } {
1586                    ChangeColormap $dataobj $comp $colormap
1587                    if { $colormap == "elementDefault" } {
1588                        SendCmd [list molecule colormode by_elements element $dataset]
1589                    } else {
1590                        # FIXME: Set the chosen scalar field name here
1591                        set _settings(molecule-colormode) scalar
1592                        set _settings(molecule-colorfield) ""
1593                        SendCmd [list molecule colormode $_settings(molecule-colormode) $_settings(molecule-colorfield) $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 { $name != "none" && ![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        if { $name != "none" } {
1832            switch -- [$dataobj type $comp] {
1833                "polydata" {
1834                    SendCmd "polydata colormap $name $tag"
1835                }
1836                "glyphs" {
1837                    SendCmd "glyphs colormap $name $tag"
1838                }
1839                "molecule" {
1840                    SendCmd "molecule colormap $name $tag"
1841                }
1842            }
1843        }
1844    }
1845}
1846
1847#
1848# BuildColormap --
1849#
1850itcl::body Rappture::VtkViewer::BuildColormap { name } {
1851    if { $name == "elementDefault" } {
1852        return
1853    }
1854    set cmap [ColorsToColormap $name]
1855    if { [llength $cmap] == 0 } {
1856        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
1857    }
1858    set amap "0.0 1.0 1.0 1.0"
1859    SendCmd "colormap add $name { $cmap } { $amap }"
1860}
1861
1862# ----------------------------------------------------------------------
1863# CONFIGURATION OPTION: -plotbackground
1864# ----------------------------------------------------------------------
1865itcl::configbody Rappture::VtkViewer::plotbackground {
1866    if { [isconnected] } {
1867        set rgb [Color2RGB $itk_option(-plotbackground)]
1868        SendCmd "screen bgcolor $rgb"
1869    }
1870}
1871
1872# ----------------------------------------------------------------------
1873# CONFIGURATION OPTION: -plotforeground
1874# ----------------------------------------------------------------------
1875itcl::configbody Rappture::VtkViewer::plotforeground {
1876    if { [isconnected] } {
1877        set rgb [Color2RGB $itk_option(-plotforeground)]
1878        SendCmd "axis color all $rgb"
1879        SendCmd "outline color $rgb"
1880    }
1881}
1882
1883itcl::body Rappture::VtkViewer::limits { dataobj } {
1884    foreach cname [$dataobj components] {
1885        set tag $dataobj-$cname
1886        if { ![info exists _limits($tag)] } {
1887            set data [$dataobj data $cname]
1888            if { $data == "" } {
1889                continue
1890            }
1891            set tmpfile file[pid].vtk
1892            set f [open "$tmpfile" "w"]
1893            fconfigure $f -translation binary -encoding binary
1894            puts $f $data
1895            close $f
1896            set reader [vtkDataSetReader $tag-xvtkDataSetReader]
1897            $reader SetFileName $tmpfile
1898            $reader ReadAllNormalsOn
1899            $reader ReadAllTCoordsOn
1900            $reader ReadAllScalarsOn
1901            $reader ReadAllColorScalarsOn
1902            $reader ReadAllVectorsOn
1903            $reader ReadAllTensorsOn
1904            $reader ReadAllFieldsOn
1905            $reader Update
1906            file delete $tmpfile
1907            set dataset [$reader GetOutput]
1908            if { $dataset == "" } {
1909                # Invalid VTK file -- loader failed to parse
1910                continue
1911            }
1912            set _limits($tag) [$dataset GetBounds]
1913            set dataAttrs [$dataset GetPointData]
1914            if { $dataAttrs != ""} {
1915                set numArrays [$dataAttrs GetNumberOfArrays]
1916                for {set i 0} {$i < $numArrays} {incr i} {
1917                    set array [$dataAttrs GetArray $i]
1918                    set fname [$dataAttrs GetArrayName $i]
1919                    foreach {min max} [$array GetRange -1] break
1920                    lappend _fieldlimits($tag) $fname pointdata [list $min $max]
1921                    set _fieldComponents($tag-$fname) [$array GetNumberOfComponents]
1922                }
1923            }
1924            set dataAttrs [$dataset GetCellData]
1925            if { $dataAttrs != ""} {
1926                set numArrays [$dataAttrs GetNumberOfArrays]
1927                for {set i 0} {$i < $numArrays} {incr i} {
1928                    set array [$dataAttrs GetArray $i]
1929                    set fname [$dataAttrs GetArrayName $i]
1930                    foreach {min max} [$array GetRange -1] break
1931                    lappend _fieldlimits($tag) $fname celldata [list $min $max]
1932                    set _fieldComponents($tag-$fname) [$array GetNumberOfComponents]
1933                }
1934            }
1935            set dataAttrs [$dataset GetFieldData]
1936            if { $dataAttrs != ""} {
1937                set numArrays [$dataAttrs GetNumberOfArrays]
1938                for {set i 0} {$i < $numArrays} {incr i} {
1939                    set array [$dataAttrs GetArray $i]
1940                    set fname [$dataAttrs GetArrayName $i]
1941                    foreach {min max} [$array GetRange -1] break
1942                    lappend _fieldlimits($tag) $fname fielddata [list $min $max]
1943                    set _fieldComponents($tag-$fname) [$array GetNumberOfComponents]
1944                }
1945            }
1946            rename $dataset ""
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.colormap_l -text "Colormap" -font "Arial 9" -anchor w
2013    itk_component add glyphscolormap {
2014        Rappture::Combobox $inner.colormap -width 10 -editable 0
2015    }
2016    $inner.colormap choices insert end [GetColormapList]
2017    $itk_component(glyphscolormap) value [$itk_component(glyphscolormap) label $_settings(glyphs-colormap)]
2018    bind $inner.colormap <<Value>> \
2019        [itcl::code $this AdjustSetting glyphs-colormap]
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.colormap_l -anchor w -pady 2 \
2038        6,1 $inner.colormap   -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.colormap_l -text "Colormap" -font "Arial 9" -anchor w
2085    itk_component add polydatacolormap {
2086        Rappture::Combobox $inner.colormap -width 10 -editable 0
2087    }
2088    $inner.colormap choices insert end [GetColormapList]
2089    $itk_component(polydatacolormap) value [$itk_component(polydatacolormap) label $_settings(polydata-colormap)]
2090    bind $inner.colormap <<Value>> \
2091        [itcl::code $this AdjustSetting polydata-colormap]
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.colormap_l -anchor w -pady 2 \
2110        6,1 $inner.colormap   -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 0
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 1 \
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 1 \
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 1 \
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 0
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 0
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.colormap_l -text "Colormap" -font "Arial 9"
2459    itk_component add moleculecolormap {
2460        Rappture::Combobox $inner.colormap -width 10 -editable 0
2461    }
2462    $inner.colormap choices insert end [GetColormapList -includeElementDefault]
2463    $itk_component(moleculecolormap) value [$itk_component(moleculecolormap) label $_settings(molecule-colormap)]
2464    bind $inner.colormap <<Value>> \
2465        [itcl::code $this AdjustSetting molecule-colormap]
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.colormap_l    -anchor w  -pady 0 \
2514        9,0 $inner.colormap      -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 "Arial 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    switch -- $type {
2635        "glyphs" {
2636            array set style {
2637                -color BCGYR
2638                -colormode constant
2639                -constcolor white
2640                -edgecolor black
2641                -edges 0
2642                -gscale 1
2643                -lighting 1
2644                -linewidth 1.0
2645                -normscale 0
2646                -opacity 1.0
2647                -orientglyphs 0
2648                -outline 0
2649                -ptsize 1.0
2650                -quality 1
2651                -scalemode "vcomp"
2652                -shape "sphere"
2653                -visible 1
2654                -wireframe 0
2655            }
2656            array set style [$dataobj style $comp]
2657            set shape [$dataobj shape $comp]
2658            if {$shape != ""} {
2659                set style(-shape) $shape
2660            }
2661            # Backwards compat with camel case style option
2662            if { [info exists style(-orientGlyphs)] } {
2663                set style(-orientglyphs) $style(-orientGlyphs)
2664                array unset style -orientGlyphs
2665            }
2666            # Backwards compat with camel case style option
2667            if { [info exists style(-scaleMode)] } {
2668                set style(-scalemode) $style(-scaleMode)
2669                array unset style -scaleMode
2670            }
2671            SendCmd "outline add $tag"
2672            SendCmd "outline color [Color2RGB $style(-constcolor)] $tag"
2673            SendCmd "outline visible $style(-outline) $tag"
2674            set _settings(glyphs-outline) $style(-outline)
2675
2676            SendCmd "glyphs add $style(-shape) $tag"
2677            SendCmd "glyphs normscale $style(-normscale) $tag"
2678            SendCmd "glyphs gscale $style(-gscale) $tag"
2679            SendCmd "glyphs wireframe $style(-wireframe) $tag"
2680            SendCmd "glyphs color [Color2RGB $style(-constcolor)] $tag"
2681            #SendCmd "glyphs colormode $style(-colormode) {} $tag"
2682            # Colormap is set by call to SetColormap below
2683            set _settings(glyphs-colormap) $style(-color)
2684            $itk_component(glyphscolormap) value [$itk_component(glyphscolormap) label $style(-color)]
2685            # Omitting field name for gorient and smode commands
2686            # defaults to active scalars or vectors depending on mode
2687            SendCmd "glyphs gorient $style(-orientglyphs) {} $tag"
2688            SendCmd "glyphs smode $style(-scalemode) {} $tag"
2689            SendCmd "glyphs edges $style(-edges) $tag"
2690            SendCmd "glyphs linecolor [Color2RGB $style(-edgecolor)] $tag"
2691            SendCmd "glyphs linewidth $style(-linewidth) $tag"
2692            SendCmd "glyphs ptsize $style(-ptsize) $tag"
2693            SendCmd "glyphs quality $style(-quality) $tag"
2694            SendCmd "glyphs lighting $style(-lighting) $tag"
2695            SendCmd "glyphs opacity $style(-opacity) $tag"
2696            set _settings(glyphs-opacity) [expr 100.0 * $style(-opacity)]
2697            SendCmd "glyphs visible $style(-visible) $tag"
2698            set _settings(glyphs-wireframe) $style(-wireframe)
2699        }
2700        "molecule" {
2701            array set style {
2702                -atomscale 0.3
2703                -atomsvisible 1
2704                -bondconstcolor white
2705                -bondcolormode "by_elements"
2706                -bondscale 0.075
2707                -bondstyle "cylinder"
2708                -bondsvisible 1
2709                -color "elementDefault"
2710                -colormode "by_elements"
2711                -colorfield "element"
2712                -constcolor white
2713                -edgecolor black
2714                -edges 0
2715                -labels 0
2716                -lighting 1
2717                -linewidth 1.0
2718                -opacity 1.0
2719                -outline 0
2720                -quality 1.0
2721                -representation ""
2722                -rscale "covalent"
2723                -visible 1
2724                -wireframe 0
2725            }
2726            set haveElementArray 0
2727            foreach {fname assoc limits} $_fieldlimits($tag) {
2728                DebugTrace "$assoc field $fname ([lindex $limits 0],[lindex $limits 1])"
2729                if {$assoc == "pointdata" && $fname == "element"} {
2730                    set haveElementArray 1
2731                    break
2732                }
2733            }
2734            if {!$haveElementArray} {
2735                set style(-bondcolormode) constant
2736                set style(-colorfield) ""
2737                set style(-color) BCGYR
2738                set style(-colormode) scalar
2739            }
2740            array set style [$dataobj style $comp]
2741
2742            SendCmd "outline add $tag"
2743            SendCmd "outline color [Color2RGB $style(-constcolor)] $tag"
2744            SendCmd "outline visible $style(-outline) $tag"
2745            set _settings(molecule-outline) $style(-outline)
2746
2747            SendCmd "molecule add $tag"
2748            if {$style(-representation) != ""} {
2749                switch -- $style(-representation) {
2750                    "ballandstick" {
2751                        set _settings(molecule-rscale) covalent
2752                        set _settings(molecule-atoms-visible) 1
2753                        set _settings(molecule-bonds-visible) 1
2754                        set _settings(molecule-bondstyle) cylinder
2755                        set _settings(molecule-atomscale) 0.3
2756                        set _settings(molecule-bondscale) 0.075
2757                    }
2758                    "balls" - "spheres" {
2759                        set _settings(molecule-rscale) covalent
2760                        set _settings(molecule-atoms-visible) 1
2761                        set _settings(molecule-bonds-visible) 0
2762                        set _settings(molecule-bondstyle) cylinder
2763                        set _settings(molecule-atomscale) 0.3
2764                        set _settings(molecule-bondscale) 0.075
2765                    }
2766                    "sticks" {
2767                        set _settings(molecule-rscale) none
2768                        set _settings(molecule-atoms-visible) 1
2769                        set _settings(molecule-bonds-visible) 1
2770                        set _settings(molecule-bondstyle) cylinder
2771                        set _settings(molecule-atomscale) 0.075
2772                        set _settings(molecule-bondscale) 0.075
2773                    }
2774                    "spacefilling" {
2775                        set _settings(molecule-rscale) van_der_waals
2776                        set _settings(molecule-atoms-visible) 1
2777                        set _settings(molecule-bonds-visible) 0
2778                        set _settings(molecule-bondstyle) cylinder
2779                        set _settings(molecule-atomscale) 1.0
2780                        set _settings(molecule-bondscale) 0.075
2781                    }
2782                    "rods"  {
2783                        set _settings(molecule-rscale) none
2784                        set _settings(molecule-atoms-visible) 1
2785                        set _settings(molecule-bonds-visible) 1
2786                        set _settings(molecule-bondstyle) cylinder
2787                        set _settings(molecule-atomscale) 0.1
2788                        set _settings(molecule-bondscale) 0.1
2789                    }
2790                    "wireframe" - "lines" {
2791                        set _settings(molecule-rscale) none
2792                        set _settings(molecule-atoms-visible) 0
2793                        set _settings(molecule-bonds-visible) 1
2794                        set _settings(molecule-bondstyle) line
2795                        set _settings(molecule-atomscale) 1.0
2796                        set _settings(molecule-bondscale) 1.0
2797                    }
2798                    default {
2799                        error "unknown representation $value"
2800                    }
2801                }
2802                SendCmd "molecule rscale $_settings(molecule-rscale) $tag"
2803                SendCmd "molecule atoms $_settings(molecule-atoms-visible) $tag"
2804                SendCmd "molecule bonds $_settings(molecule-bonds-visible) $tag"
2805                SendCmd "molecule bstyle $_settings(molecule-bondstyle) $tag"
2806                SendCmd "molecule ascale $_settings(molecule-atomscale) $tag"
2807                SendCmd "molecule bscale $_settings(molecule-bondscale) $tag"
2808                $itk_component(representation) value [$itk_component(representation) label $style(-representation)]
2809                $itk_component(rscale) value [$itk_component(rscale) label $_settings(molecule-rscale)]
2810                switch -- $style(-representation) {
2811                    "ballandstick" - "balls" - "spheres" {
2812                        $itk_component(rscale) configure -state normal
2813                    }
2814                    default {
2815                        $itk_component(rscale) configure -state disabled
2816                    }
2817                }
2818            } else {
2819                SendCmd "molecule rscale $style(-rscale) $tag"
2820                set _settings(molecule-rscale) $style(-rscale)
2821                SendCmd "molecule atoms $style(-atomsvisible) $tag"
2822                set _settings(molecule-atoms-visible) $style(-atomsvisible)
2823                SendCmd "molecule bonds $style(-bondsvisible) $tag"
2824                set _settings(molecule-bonds-visible) $style(-bondsvisible)
2825                SendCmd "molecule bstyle $style(-bondstyle) $tag"
2826                set _settings(molecule-bondstyle) $style(-bondstyle)
2827                SendCmd "molecule ascale $style(-atomscale) $tag"
2828                set _settings(molecule-atomscale) $style(-atomscale)
2829                SendCmd "molecule bscale $style(-bondscale) $tag"
2830                set _settings(molecule-bondscale) $style(-bondscale)
2831            }
2832            SendCmd "molecule labels $style(-labels) $tag"
2833            set _settings(molecule-labels) $style(-labels)
2834            SendCmd "molecule color [Color2RGB $style(-constcolor)] $tag"
2835            SendCmd "molecule bcolor [Color2RGB $style(-bondconstcolor)] $tag"
2836            SendCmd [list molecule colormode $style(-colormode) $style(-colorfield) $tag]
2837            SendCmd "molecule bcmode $style(-bondcolormode) $tag"
2838            set _settings(molecule-colormode) $style(-colormode)
2839            set _settings(molecule-colorfield) $style(-colorfield)
2840            # Colormap is set by call to SetColormap below
2841            set _settings(molecule-colormap) $style(-color)
2842            $itk_component(moleculecolormap) value [$itk_component(moleculecolormap) label $style(-color)]
2843            SendCmd "molecule linecolor [Color2RGB $style(-edgecolor)] $tag"
2844            SendCmd "molecule linewidth $style(-linewidth) $tag"
2845            SendCmd "molecule edges $style(-edges) $tag"
2846            set _settings(molecule-edges) $style(-edges)
2847            SendCmd "molecule lighting $style(-lighting) $tag"
2848            set _settings(molecule-lighting) $style(-lighting)
2849            SendCmd "molecule aquality $style(-quality) $tag"
2850            SendCmd "molecule bquality $style(-quality) $tag"
2851            set _settings(molecule-quality) $style(-quality)
2852            SendCmd "molecule visible $style(-visible) $tag"
2853            set _settings(molecule-visible) $style(-visible)
2854            set _haveMolecules 1
2855        }
2856        "polydata" {
2857            array set style {
2858                -cloudstyle "mesh"
2859                -color BCGYR
2860                -colormode constant
2861                -constcolor white
2862                -edgecolor black
2863                -edges 1
2864                -lighting 1
2865                -linewidth 1.0
2866                -opacity 1.0
2867                -outline 0
2868                -visible 1
2869                -wireframe 0
2870            }
2871            array set style [$dataobj style $comp]
2872
2873            SendCmd "outline add $tag"
2874            SendCmd "outline color [Color2RGB $style(-constcolor)] $tag"
2875            SendCmd "outline visible $style(-outline) $tag"
2876            set _settings(polydata-outline) $style(-outline)
2877
2878            SendCmd "polydata add $tag"
2879            SendCmd "polydata visible $style(-visible) $tag"
2880            set _settings(polydata-visible) $style(-visible)
2881            SendCmd "polydata edges $style(-edges) $tag"
2882            set _settings(polydata-edges) $style(-edges)
2883            SendCmd "polydata cloudstyle $style(-cloudstyle) $tag"
2884            SendCmd "polydata color [Color2RGB $style(-constcolor)] $tag"
2885            #SendCmd "polydata colormode $style(-colormode) {} $tag"
2886            # Colormap is set by call to SetColormap below
2887            set _settings(polydata-colormap) $style(-color)
2888            $itk_component(polydatacolormap) value [$itk_component(polydatacolormap) label $style(-color)]
2889            SendCmd "polydata lighting $style(-lighting) $tag"
2890            set _settings(polydata-lighting) $style(-lighting)
2891            SendCmd "polydata linecolor [Color2RGB $style(-edgecolor)] $tag"
2892            SendCmd "polydata linewidth $style(-linewidth) $tag"
2893            SendCmd "polydata opacity $style(-opacity) $tag"
2894            set _settings(polydata-opacity) [expr 100.0 * $style(-opacity)]
2895            SendCmd "polydata wireframe $style(-wireframe) $tag"
2896            set _settings(polydata-wireframe) $style(-wireframe)
2897            set havePolyData 1
2898        }
2899    }
2900    SetColormap $dataobj $comp
2901}
2902
2903itcl::body Rappture::VtkViewer::IsValidObject { dataobj } {
2904    if {[catch {$dataobj isa Rappture::Drawing} valid] != 0 || !$valid} {
2905        return 0
2906    }
2907    return 1
2908}
2909
2910# ----------------------------------------------------------------------
2911# USAGE: ReceiveLegend <colormap> <title> <min> <max> <size>
2912#
2913# Invoked automatically whenever the "legend" command comes in from
2914# the rendering server.  Indicates that binary image data with the
2915# specified <size> will follow.
2916# ----------------------------------------------------------------------
2917itcl::body Rappture::VtkViewer::ReceiveLegend { colormap title min max size } {
2918    set _limits(vmin) $min
2919    set _limits(vmax) $max
2920    set _title $title
2921    if { [IsConnected] } {
2922        set bytes [ReceiveBytes $size]
2923        if { ![info exists _image(legend)] } {
2924            set _image(legend) [image create photo]
2925        }
2926        $_image(legend) configure -data $bytes
2927        DrawLegend
2928    }
2929}
2930
2931#
2932# DrawLegend --
2933#
2934# Draws the legend in it's own canvas which resides to the right
2935# of the contour plot area.
2936#
2937itcl::body Rappture::VtkViewer::DrawLegend {} {
2938    set c $itk_component(view)
2939    set w [winfo width $c]
2940    set h [winfo height $c]
2941    set font "Arial 8"
2942    set lineht [font metrics $font -linespace]
2943
2944    if { $_settings(legend) } {
2945        set x [expr $w - 2]
2946        if { [$c find withtag "legend"] == "" } {
2947            $c create image $x [expr {$lineht+2}] \
2948                -anchor ne \
2949                -image $_image(legend) -tags "colormap legend"
2950            $c create text $x 2 \
2951                -anchor ne \
2952                -fill $itk_option(-plotforeground) -tags "vmax legend" \
2953                -font $font
2954            $c create text $x [expr {$h-2}] \
2955                -anchor se \
2956                -fill $itk_option(-plotforeground) -tags "vmin legend" \
2957                -font $font
2958            #$c bind colormap <Enter> [itcl::code $this EnterLegend %x %y]
2959            $c bind colormap <Leave> [itcl::code $this LeaveLegend]
2960            $c bind colormap <Motion> [itcl::code $this MotionLegend %x %y]
2961        }
2962        # Reset the item coordinates according the current size of the plot.
2963        $c coords colormap $x [expr {$lineht+2}]
2964        if { $_limits(vmin) != "" } {
2965            $c itemconfigure vmin -text [format %g $_limits(vmin)]
2966        }
2967        if { $_limits(vmax) != "" } {
2968            $c itemconfigure vmax -text [format %g $_limits(vmax)]
2969        }
2970        $c coords vmin $x [expr {$h-2}]
2971        $c coords vmax $x 2
2972    }
2973}
2974
2975#
2976# EnterLegend --
2977#
2978itcl::body Rappture::VtkViewer::EnterLegend { x y } {
2979    SetLegendTip $x $y
2980}
2981
2982#
2983# MotionLegend --
2984#
2985itcl::body Rappture::VtkViewer::MotionLegend { x y } {
2986    Rappture::Tooltip::tooltip cancel
2987    set c $itk_component(view)
2988    SetLegendTip $x $y
2989}
2990
2991#
2992# LeaveLegend --
2993#
2994itcl::body Rappture::VtkViewer::LeaveLegend { } {
2995    Rappture::Tooltip::tooltip cancel
2996    .rappturetooltip configure -icon ""
2997}
2998
2999#
3000# SetLegendTip --
3001#
3002itcl::body Rappture::VtkViewer::SetLegendTip { x y } {
3003    set c $itk_component(view)
3004    set w [winfo width $c]
3005    set h [winfo height $c]
3006    set font "Arial 8"
3007    set lineht [font metrics $font -linespace]
3008
3009    set imgHeight [image height $_image(legend)]
3010    set coords [$c coords colormap]
3011    set imgX [expr $w - [image width $_image(legend)] - 2]
3012    set imgY [expr $y - $lineht - 2]
3013
3014    # Make a swatch of the selected color
3015    if { [catch { $_image(legend) get 10 $imgY } pixel] != 0 } {
3016        return
3017    }
3018    if { ![info exists _image(swatch)] } {
3019        set _image(swatch) [image create photo -width 24 -height 24]
3020    }
3021    set color [eval format "\#%02x%02x%02x" $pixel]
3022    $_image(swatch) put black  -to 0 0 23 23
3023    $_image(swatch) put $color -to 1 1 22 22
3024    .rappturetooltip configure -icon $_image(swatch)
3025
3026    # Compute the value of the point
3027    set t [expr 1.0 - (double($imgY) / double($imgHeight-1))]
3028    set value [expr $t * ($_limits(vmax) - $_limits(vmin)) + $_limits(vmin)]
3029    set tipx [expr $x + 15]
3030    set tipy [expr $y - 5]
3031    Rappture::Tooltip::text $c "$_title $value"
3032    Rappture::Tooltip::tooltip show $c +$tipx,+$tipy
3033}
3034
3035# ----------------------------------------------------------------------
3036# USAGE: Slice move x|y|z <newval>
3037#
3038# Called automatically when the user drags the slider to move the
3039# cut plane that slices 3D data.  Gets the current value from the
3040# slider and moves the cut plane to the appropriate point in the
3041# data set.
3042# ----------------------------------------------------------------------
3043itcl::body Rappture::VtkViewer::Slice {option args} {
3044    switch -- $option {
3045        "move" {
3046            set axis [lindex $args 0]
3047            set newval [lindex $args 1]
3048            if {[llength $args] != 2} {
3049                error "wrong # args: should be \"Slice move x|y|z newval\""
3050            }
3051            set newpos [expr {0.01*$newval}]
3052            SendCmd "renderer clipplane $axis $newpos -1"
3053        }
3054        "tooltip" {
3055            set axis [lindex $args 0]
3056            set val [$itk_component(${axis}CutScale) get]
3057            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
3058        }
3059        default {
3060            error "bad option \"$option\": should be axis, move, or tooltip"
3061        }
3062    }
3063}
3064
3065itcl::body Rappture::VtkViewer::SetOrientation { side } {
3066    array set positions {
3067        front "1 0 0 0"
3068        back  "0 0 1 0"
3069        left  "0.707107 0 -0.707107 0"
3070        right "0.707107 0 0.707107 0"
3071        top   "0.707107 -0.707107 0 0"
3072        bottom "0.707107 0.707107 0 0"
3073    }
3074    foreach name { -qw -qx -qy -qz } value $positions($side) {
3075        set _view($name) $value
3076    }
3077    set q [ViewToQuaternion]
3078    $_arcball quaternion $q
3079    SendCmd "camera orient $q"
3080    SendCmd "camera reset"
3081    set _view(-xpan) 0
3082    set _view(-ypan) 0
3083    set _view(-zoom) 1.0
3084}
3085
3086itcl::body Rappture::VtkViewer::SetOpacity { dataset } {
3087    foreach {dataobj comp} [split $dataset -] break
3088    set type [$dataobj type $comp]
3089    set val $_settings($type-opacity)
3090    set sval [expr { 0.01 * double($val) }]
3091    if { !$_obj2ovride($dataobj-raise) } {
3092        # This is wrong.  Need to figure out why raise isn't set with 1
3093        #set sval [expr $sval * .6]
3094    }
3095    SendCmd "$type opacity $sval $dataset"
3096}
Note: See TracBrowser for help on using the repository browser.