source: branches/1.6/gui/scripts/vtkviewer.tcl @ 6212

Last change on this file since 6212 was 6212, checked in by ldelgass, 7 years ago

merge viewer cleanups from trunk

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