source: branches/1.4/gui/scripts/vtkviewer.tcl @ 5312

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

merge r5195 from trunk

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