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

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

Add outlines, glyphs tab

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