source: branches/1.3/gui/scripts/vtkglyphviewer.tcl @ 4693

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

merge r4642 from trunk

File size: 81.7 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: vtkglyphviewer - Vtk 3D glyphs 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 *VtkGlyphViewer.width 4i widgetDefault
19option add *VtkGlyphViewer*cursor crosshair widgetDefault
20option add *VtkGlyphViewer.height 4i widgetDefault
21option add *VtkGlyphViewer.foreground black widgetDefault
22option add *VtkGlyphViewer.controlBackground gray widgetDefault
23option add *VtkGlyphViewer.controlDarkBackground #999999 widgetDefault
24option add *VtkGlyphViewer.plotBackground black widgetDefault
25option add *VtkGlyphViewer.plotForeground white widgetDefault
26option add *VtkGlyphViewer.font \
27    -*-helvetica-medium-r-normal-*-12-* widgetDefault
28
29# must use this name -- plugs into Rappture::resources::load
30proc VtkGlyphViewer_init_resources {} {
31    Rappture::resources::register \
32        vtkvis_server Rappture::VtkGlyphViewer::SetServerList
33}
34
35itcl::class Rappture::VtkGlyphViewer {
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 }
85    private method BuildCutplaneTab {}
86    private method BuildDownloadPopup { widget command }
87    private method BuildGlyphTab {}
88    private method DrawLegend {}
89    private method Combo { option }
90    private method EnterLegend { x y }
91    private method EventuallyResize { w h }
92    private method EventuallyRotate { q }
93    private method EventuallyRequestLegend {}
94    private method EventuallySetCutplane { axis args }
95    private method GetImage { args }
96    private method GetVtkData { args }
97    private method IsValidObject { dataobj }
98    private method LeaveLegend {}
99    private method MotionLegend { x y }
100    private method PanCamera {}
101    private method RequestLegend {}
102    private method SetLegendTip { x y }
103    private method SetObjectStyle { dataobj comp }
104    private method Slice {option args}
105    private method SetCurrentColormap { color }
106    private method SetOrientation { side }
107
108    private variable _arcball ""
109
110    private variable _dlist ""     ;    # list of data objects
111    private variable _obj2datasets
112    private variable _obj2ovride   ;    # maps dataobj => style override
113    private variable _datasets     ;    # contains all the dataobj-component
114                                   ;    # datasets in the server
115    private variable _colormaps    ;    # contains all the colormaps
116                                   ;    # in the server.
117    # The name of the current colormap used.  The colormap is global to all
118    # heightmaps displayed.
119    private variable _currentColormap ""
120
121    private variable _dataset2style    ;# maps dataobj-component to transfunc
122
123    private variable _click        ;    # info used for rotate operations
124    private variable _limits       ;    # autoscale min/max for all axes
125    private variable _view         ;    # view params for 3D view
126    private variable _settings
127    private variable _style;            # Array of current component styles.
128    private variable _changed
129    private variable _initialStyle;     # Array of initial component styles.
130    private variable _reset 1;          # indicates if camera needs to be reset
131                                        # to starting position.
132
133    private variable _first ""     ;    # This is the topmost dataset.
134    private variable _start 0
135    private variable _title ""
136
137    common _downloadPopup;              # download options from popup
138    private common _hardcopy
139    private variable _width 0
140    private variable _height 0
141    private variable _resizePending 0
142    private variable _rotatePending 0
143    private variable _cutplanePending 0
144    private variable _legendPending 0
145    private variable _field      ""
146    private variable _colorMode "vmag"; #  Mode of colormap (vmag or scalar)
147    private variable _fieldNames {}
148    private variable _fields
149    private variable _curFldName ""
150    private variable _curFldLabel ""
151}
152
153itk::usual VtkGlyphViewer {
154    keep -background -foreground -cursor -font
155    keep -plotbackground -plotforeground
156}
157
158# ----------------------------------------------------------------------
159# CONSTRUCTOR
160# ----------------------------------------------------------------------
161itcl::body Rappture::VtkGlyphViewer::constructor {hostlist args} {
162    package require vtk
163    set _serverType "vtkvis"
164
165    # Rebuild event
166    $_dispatcher register !rebuild
167    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
168
169    # Resize event
170    $_dispatcher register !resize
171    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
172
173    # Rotate event
174    $_dispatcher register !rotate
175    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
176
177    # Legend event
178    $_dispatcher register !legend
179    $_dispatcher dispatch $this !legend "[itcl::code $this RequestLegend]; list"
180
181    # X-Cutplane event
182    $_dispatcher register !xcutplane
183    $_dispatcher dispatch $this !xcutplane \
184        "[itcl::code $this AdjustSetting cutplaneXPosition]; list"
185
186    # Y-Cutplane event
187    $_dispatcher register !ycutplane
188    $_dispatcher dispatch $this !ycutplane \
189        "[itcl::code $this AdjustSetting cutplaneYPosition]; list"
190
191    # Z-Cutplane event
192    $_dispatcher register !zcutplane
193    $_dispatcher dispatch $this !zcutplane \
194        "[itcl::code $this AdjustSetting cutplaneZPosition]; list"
195
196    #
197    # Populate parser with commands handle incoming requests
198    #
199    $_parser alias image [itcl::code $this ReceiveImage]
200    $_parser alias dataset [itcl::code $this ReceiveDataset]
201    $_parser alias legend [itcl::code $this ReceiveLegend]
202
203    # Initialize the view to some default parameters.
204    array set _view {
205        qw              0.853553
206        qx              -0.353553
207        qy              0.353553
208        qz              0.146447
209        zoom            1.0
210        xpan            0
211        ypan            0
212        ortho           0
213    }
214    set _arcball [blt::arcball create 100 100]
215    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
216    $_arcball quaternion $q
217
218    array set _settings [subst {
219        background              black
220        colormap                BCGYR
221        colormapVisible         1
222        field                   "Default"
223        axesVisible             1
224        axisLabelsVisible       1
225        axisXGrid               0
226        axisYGrid               0
227        axisZGrid               0
228        cutplaneEdges           0
229        cutplaneLighting        1
230        cutplanePreinterp       1
231        cutplaneOpacity         100
232        cutplaneVisible         0
233        cutplaneWireframe       0
234        cutplaneXPosition       50
235        cutplaneXVisible        1
236        cutplaneYPosition       50
237        cutplaneYVisible        1
238        cutplaneZPosition       50
239        cutplaneZVisible        1
240        glyphEdges              0
241        glyphLighting           1
242        glyphOpacity            100
243        saveGlyphOpacity        100
244        glyphOutline            0
245        glyphVisible            1
246        glyphWireframe          0
247        legendVisible           1
248    }]
249    array set _changed {
250        glyphOpacity            0
251        colormap                0
252    }
253
254    itk_component add view {
255        canvas $itk_component(plotarea).view \
256            -highlightthickness 0 -borderwidth 0
257    } {
258        usual
259        ignore -highlightthickness -borderwidth  -background
260    }
261
262    itk_component add fieldmenu {
263        menu $itk_component(plotarea).menu -bg black -fg white -relief flat \
264            -tearoff no
265    } {
266        usual
267        ignore -background -foreground -relief -tearoff
268    }
269    set c $itk_component(view)
270    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
271    bind $c <4> [itcl::code $this Zoom in 0.25]
272    bind $c <5> [itcl::code $this Zoom out 0.25]
273    bind $c <KeyPress-Left>  [list %W xview scroll 10 units]
274    bind $c <KeyPress-Right> [list %W xview scroll -10 units]
275    bind $c <KeyPress-Up>    [list %W yview scroll 10 units]
276    bind $c <KeyPress-Down>  [list %W yview scroll -10 units]
277    bind $c <Enter> "focus %W"
278    bind $c <Control-F1> [itcl::code $this ToggleConsole]
279
280    # Fix the scrollregion in case we go off screen
281    $c configure -scrollregion [$c bbox all]
282
283    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
284    set _map(cwidth) -1
285    set _map(cheight) -1
286    set _map(zoom) 1.0
287    set _map(original) ""
288
289    set f [$itk_component(main) component controls]
290    itk_component add reset {
291        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
292            -highlightthickness 0 \
293            -image [Rappture::icon reset-view] \
294            -command [itcl::code $this Zoom reset]
295    } {
296        usual
297        ignore -highlightthickness
298    }
299    pack $itk_component(reset) -side top -padx 2 -pady 2
300    Rappture::Tooltip::for $itk_component(reset) \
301        "Reset the view to the default zoom level"
302
303    itk_component add zoomin {
304        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
305            -highlightthickness 0 \
306            -image [Rappture::icon zoom-in] \
307            -command [itcl::code $this Zoom in]
308    } {
309        usual
310        ignore -highlightthickness
311    }
312    pack $itk_component(zoomin) -side top -padx 2 -pady 2
313    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
314
315    itk_component add zoomout {
316        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
317            -highlightthickness 0 \
318            -image [Rappture::icon zoom-out] \
319            -command [itcl::code $this Zoom out]
320    } {
321        usual
322        ignore -highlightthickness
323    }
324    pack $itk_component(zoomout) -side top -padx 2 -pady 2
325    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
326
327    itk_component add glyphs {
328        Rappture::PushButton $f.glyphs \
329            -onimage [Rappture::icon volume-on] \
330            -offimage [Rappture::icon volume-off] \
331            -variable [itcl::scope _settings(glyphVisible)] \
332            -command [itcl::code $this AdjustSetting glyphVisible]
333    }
334    $itk_component(glyphs) select
335    Rappture::Tooltip::for $itk_component(glyphs) \
336        "Don't display the glyphs"
337    pack $itk_component(glyphs) -padx 2 -pady 2
338
339    if {0} {
340    itk_component add cutplane {
341        Rappture::PushButton $f.cutplane \
342            -onimage [Rappture::icon cutbutton] \
343            -offimage [Rappture::icon cutbutton] \
344            -variable [itcl::scope _settings(cutplaneVisible)] \
345            -command [itcl::code $this AdjustSetting cutplaneVisible]
346    }
347    Rappture::Tooltip::for $itk_component(cutplane) \
348        "Show/Hide cutplanes"
349    pack $itk_component(cutplane) -padx 2 -pady 2
350    }
351
352    if { [catch {
353        BuildGlyphTab
354        #BuildCutplaneTab
355        BuildAxisTab
356        BuildCameraTab
357    } errs] != 0 } {
358        puts stderr errs=$errs
359    }
360    # Legend
361
362    set _image(legend) [image create photo]
363    itk_component add legend {
364        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
365    } {
366        usual
367        ignore -highlightthickness
368        rename -background -plotbackground plotBackground Background
369    }
370
371    # Hack around the Tk panewindow.  The problem is that the requested
372    # size of the 3d view isn't set until an image is retrieved from
373    # the server.  So the panewindow uses the tiny size.
374    set w 10000
375    pack forget $itk_component(view)
376    blt::table $itk_component(plotarea) \
377        0,0 $itk_component(view) -fill both -reqwidth $w
378    blt::table configure $itk_component(plotarea) c1 -resize none
379
380    # Bindings for rotation via mouse
381    bind $itk_component(view) <ButtonPress-1> \
382        [itcl::code $this Rotate click %x %y]
383    bind $itk_component(view) <B1-Motion> \
384        [itcl::code $this Rotate drag %x %y]
385    bind $itk_component(view) <ButtonRelease-1> \
386        [itcl::code $this Rotate release %x %y]
387
388    # Bindings for panning via mouse
389    bind $itk_component(view) <ButtonPress-2> \
390        [itcl::code $this Pan click %x %y]
391    bind $itk_component(view) <B2-Motion> \
392        [itcl::code $this Pan drag %x %y]
393    bind $itk_component(view) <ButtonRelease-2> \
394        [itcl::code $this Pan release %x %y]
395
396    #bind $itk_component(view) <ButtonRelease-3> \
397    #    [itcl::code $this Pick %x %y]
398
399    # Bindings for panning via keyboard
400    bind $itk_component(view) <KeyPress-Left> \
401        [itcl::code $this Pan set -10 0]
402    bind $itk_component(view) <KeyPress-Right> \
403        [itcl::code $this Pan set 10 0]
404    bind $itk_component(view) <KeyPress-Up> \
405        [itcl::code $this Pan set 0 -10]
406    bind $itk_component(view) <KeyPress-Down> \
407        [itcl::code $this Pan set 0 10]
408    bind $itk_component(view) <Shift-KeyPress-Left> \
409        [itcl::code $this Pan set -2 0]
410    bind $itk_component(view) <Shift-KeyPress-Right> \
411        [itcl::code $this Pan set 2 0]
412    bind $itk_component(view) <Shift-KeyPress-Up> \
413        [itcl::code $this Pan set 0 -2]
414    bind $itk_component(view) <Shift-KeyPress-Down> \
415        [itcl::code $this Pan set 0 2]
416
417    # Bindings for zoom via keyboard
418    bind $itk_component(view) <KeyPress-Prior> \
419        [itcl::code $this Zoom out]
420    bind $itk_component(view) <KeyPress-Next> \
421        [itcl::code $this Zoom in]
422
423    bind $itk_component(view) <Enter> "focus $itk_component(view)"
424
425    if {[string equal "x11" [tk windowingsystem]]} {
426        # Bindings for zoom via mouse
427        bind $itk_component(view) <4> [itcl::code $this Zoom out]
428        bind $itk_component(view) <5> [itcl::code $this Zoom in]
429    }
430
431    set _image(download) [image create photo]
432
433    eval itk_initialize $args
434    Connect
435}
436
437# ----------------------------------------------------------------------
438# DESTRUCTOR
439# ----------------------------------------------------------------------
440itcl::body Rappture::VtkGlyphViewer::destructor {} {
441    Disconnect
442    image delete $_image(plot)
443    image delete $_image(download)
444    catch { blt::arcball destroy $_arcball }
445}
446
447itcl::body Rappture::VtkGlyphViewer::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    EventuallyRequestLegend
458    set _resizePending 0
459}
460
461itcl::body Rappture::VtkGlyphViewer::DoRotate {} {
462    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
463    SendCmd "camera orient $q"
464    set _rotatePending 0
465}
466
467itcl::body Rappture::VtkGlyphViewer::EventuallyRequestLegend {} {
468    if { !$_legendPending } {
469        set _legendPending 1
470        $_dispatcher event -idle !legend
471    }
472}
473
474itcl::body Rappture::VtkGlyphViewer::EventuallyResize { w h } {
475    set _width $w
476    set _height $h
477    $_arcball resize $w $h
478    if { !$_resizePending } {
479        set _resizePending 1
480        $_dispatcher event -after 400 !resize
481    }
482}
483
484set rotate_delay 100
485
486itcl::body Rappture::VtkGlyphViewer::EventuallyRotate { q } {
487    foreach { _view(qw) _view(qx) _view(qy) _view(qz) } $q break
488    if { !$_rotatePending } {
489        set _rotatePending 1
490        global rotate_delay
491        $_dispatcher event -after $rotate_delay !rotate
492    }
493}
494
495itcl::body Rappture::VtkGlyphViewer::EventuallySetCutplane { axis args } {
496    if { !$_cutplanePending } {
497        set _cutplanePending 1
498        $_dispatcher event -after 100 !${axis}cutplane
499    }
500}
501
502# ----------------------------------------------------------------------
503# USAGE: add <dataobj> ?<settings>?
504#
505# Clients use this to add a data object to the plot.  The optional
506# <settings> are used to configure the plot.  Allowed settings are
507# -color, -brightness, -width, -linestyle, and -raise.
508# ----------------------------------------------------------------------
509itcl::body Rappture::VtkGlyphViewer::add {dataobj {settings ""}} {
510    if { ![$dataobj isvalid] } {
511        return;                         # Object doesn't contain valid data.
512    }
513    array set params {
514        -color auto
515        -width 1
516        -linestyle solid
517        -brightness 0
518        -raise 0
519        -description ""
520        -param ""
521        -type ""
522    }
523    array set params $settings
524    set params(-description) ""
525    set params(-param) ""
526    array set params $settings
527
528    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
529        # can't handle -autocolors yet
530        set params(-color) black
531    }
532    set pos [lsearch -exact $_dlist $dataobj]
533    if {$pos < 0} {
534        lappend _dlist $dataobj
535    }
536    set _obj2ovride($dataobj-color) $params(-color)
537    set _obj2ovride($dataobj-width) $params(-width)
538    set _obj2ovride($dataobj-raise) $params(-raise)
539    $_dispatcher event -idle !rebuild
540}
541
542
543# ----------------------------------------------------------------------
544# USAGE: delete ?<dataobj1> <dataobj2> ...?
545#
546#       Clients use this to delete a dataobj from the plot.  If no dataobjs
547#       are specified, then all dataobjs are deleted.  No data objects are
548#       deleted.  They are only removed from the display list.
549#
550# ----------------------------------------------------------------------
551itcl::body Rappture::VtkGlyphViewer::delete {args} {
552    if { [llength $args] == 0} {
553        set args $_dlist
554    }
555    # Delete all specified dataobjs
556    set changed 0
557    foreach dataobj $args {
558        set pos [lsearch -exact $_dlist $dataobj]
559        if { $pos < 0 } {
560            continue;                   # Don't know anything about it.
561        }
562        # Remove it from the dataobj list.
563        set _dlist [lreplace $_dlist $pos $pos]
564        array unset _obj2ovride $dataobj-*
565        array unset _settings $dataobj-*
566        set changed 1
567    }
568    # If anything changed, then rebuild the plot
569    if { $changed } {
570        $_dispatcher event -idle !rebuild
571    }
572}
573
574# ----------------------------------------------------------------------
575# USAGE: get ?-objects?
576# USAGE: get ?-visible?
577# USAGE: get ?-image view?
578#
579# Clients use this to query the list of objects being plotted, in
580# order from bottom to top of this result.  The optional "-image"
581# flag can also request the internal images being shown.
582# ----------------------------------------------------------------------
583itcl::body Rappture::VtkGlyphViewer::get {args} {
584    if {[llength $args] == 0} {
585        set args "-objects"
586    }
587
588    set op [lindex $args 0]
589    switch -- $op {
590        "-objects" {
591            # put the dataobj list in order according to -raise options
592            set dlist {}
593            foreach dataobj $_dlist {
594                if { ![IsValidObject $dataobj] } {
595                    continue
596                }
597                if {[info exists _obj2ovride($dataobj-raise)] &&
598                    $_obj2ovride($dataobj-raise)} {
599                    set dlist [linsert $dlist 0 $dataobj]
600                } else {
601                    lappend dlist $dataobj
602                }
603            }
604            return $dlist
605        }
606        "-visible" {
607            set dlist {}
608            foreach dataobj $_dlist {
609                if { ![IsValidObject $dataobj] } {
610                    continue
611                }
612                if { ![info exists _obj2ovride($dataobj-raise)] } {
613                    # No setting indicates that the object isn't visible.
614                    continue
615                }
616                # Otherwise use the -raise parameter to put the object to
617                # the front of the list.
618                if { $_obj2ovride($dataobj-raise) } {
619                    set dlist [linsert $dlist 0 $dataobj]
620                } else {
621                    lappend dlist $dataobj
622                }
623            }
624            return $dlist
625        }           
626        -image {
627            if {[llength $args] != 2} {
628                error "wrong # args: should be \"get -image view\""
629            }
630            switch -- [lindex $args end] {
631                view {
632                    return $_image(plot)
633                }
634                default {
635                    error "bad image name \"[lindex $args end]\": should be view"
636                }
637            }
638        }
639        default {
640            error "bad option \"$op\": should be -objects or -image"
641        }
642    }
643}
644
645# ----------------------------------------------------------------------
646# USAGE: scale ?<data1> <data2> ...?
647#
648# Sets the default limits for the overall plot according to the
649# limits of the data for all of the given <data> objects.  This
650# accounts for all objects--even those not showing on the screen.
651# Because of this, the limits are appropriate for all objects as
652# the user scans through data in the ResultSet viewer.
653# ----------------------------------------------------------------------
654itcl::body Rappture::VtkGlyphViewer::scale { args } {
655    foreach dataobj $args {
656        if { ![$dataobj isvalid] } {
657            continue;                   # Object doesn't contain valid data.
658        }
659        foreach axis { x y z } {
660            set lim [$dataobj limits $axis]
661            if { ![info exists _limits($axis)] } {
662                set _limits($axis) $lim
663                continue
664            }
665            foreach {min max} $lim break
666            foreach {amin amax} $_limits($axis) break
667            if { $amin > $min } {
668                set amin $min
669            }
670            if { $amax < $max } {
671                set amax $max
672            }
673            set _limits($axis) [list $amin $amax]
674        }
675        foreach { fname lim } [$dataobj fieldlimits] {
676            if { ![info exists _limits($fname)] } {
677                set _limits($fname) $lim
678                continue
679            }
680            foreach {min max} $lim break
681            foreach {fmin fmax} $_limits($fname) break
682            if { $fmin > $min } {
683                set fmin $min
684            }
685            if { $fmax < $max } {
686                set fmax $max
687            }
688            set _limits($fname) [list $fmin $fmax]
689        }
690    }
691}
692
693# ----------------------------------------------------------------------
694# USAGE: download coming
695# USAGE: download controls <downloadCommand>
696# USAGE: download now
697#
698# Clients use this method to create a downloadable representation
699# of the plot.  Returns a list of the form {ext string}, where
700# "ext" is the file extension (indicating the type of data) and
701# "string" is the data itself.
702# ----------------------------------------------------------------------
703itcl::body Rappture::VtkGlyphViewer::download {option args} {
704    switch $option {
705        coming {
706            if {[catch {
707                blt::winop snap $itk_component(plotarea) $_image(download)
708            }]} {
709                $_image(download) configure -width 1 -height 1
710                $_image(download) put #000000
711            }
712        }
713        controls {
714            set popup .vtkviewerdownload
715            if { ![winfo exists .vtkviewerdownload] } {
716                set inner [BuildDownloadPopup $popup [lindex $args 0]]
717            } else {
718                set inner [$popup component inner]
719            }
720            set _downloadPopup(image_controls) $inner.image_frame
721            set num [llength [get]]
722            set num [expr {($num == 1) ? "1 result" : "$num results"}]
723            set word [Rappture::filexfer::label downloadWord]
724            $inner.summary configure -text "$word $num in the following format:"
725            update idletasks            ;# Fix initial sizes
726            return $popup
727        }
728        now {
729            set popup .vtkviewerdownload
730            if {[winfo exists .vtkviewerdownload]} {
731                $popup deactivate
732            }
733            switch -- $_downloadPopup(format) {
734                "image" {
735                    return [$this GetImage [lindex $args 0]]
736                }
737                "vtk" {
738                    return [$this GetVtkData [lindex $args 0]]
739                }
740            }
741            return ""
742        }
743        default {
744            error "bad option \"$option\": should be coming, controls, now"
745        }
746    }
747}
748
749# ----------------------------------------------------------------------
750# USAGE: Connect ?<host:port>,<host:port>...?
751#
752# Clients use this method to establish a connection to a new
753# server, or to reestablish a connection to the previous server.
754# Any existing connection is automatically closed.
755# ----------------------------------------------------------------------
756itcl::body Rappture::VtkGlyphViewer::Connect {} {
757    set _hosts [GetServerList "vtkvis"]
758    if { "" == $_hosts } {
759        return 0
760    }
761    set result [VisViewer::Connect $_hosts]
762    if { $result } {
763        if { $_reportClientInfo }  {
764            # Tell the server the viewer, hub, user and session.
765            # Do this immediately on connect before buffering any commands
766            global env
767
768            set info {}
769            set user "???"
770            if { [info exists env(USER)] } {
771                set user $env(USER)
772            }
773            set session "???"
774            if { [info exists env(SESSION)] } {
775                set session $env(SESSION)
776            }
777            lappend info "version" "$Rappture::version"
778            lappend info "build" "$Rappture::build"
779            lappend info "svnurl" "$Rappture::svnurl"
780            lappend info "installdir" "$Rappture::installdir"
781            lappend info "hub" [exec hostname]
782            lappend info "client" "vtkglyphviewer"
783            lappend info "user" $user
784            lappend info "session" $session
785            SendCmd "clientinfo [list $info]"
786        }
787
788        set w [winfo width $itk_component(view)]
789        set h [winfo height $itk_component(view)]
790        EventuallyResize $w $h
791    }
792    return $result
793}
794
795#
796# isconnected --
797#
798#       Indicates if we are currently connected to the visualization server.
799#
800itcl::body Rappture::VtkGlyphViewer::isconnected {} {
801    return [VisViewer::IsConnected]
802}
803
804#
805# disconnect --
806#
807itcl::body Rappture::VtkGlyphViewer::disconnect {} {
808    Disconnect
809    set _reset 1
810}
811
812#
813# Disconnect --
814#
815#       Clients use this method to disconnect from the current rendering
816#       server.
817#
818itcl::body Rappture::VtkGlyphViewer::Disconnect {} {
819    VisViewer::Disconnect
820
821    $_dispatcher cancel !rebuild
822    $_dispatcher cancel !resize
823    $_dispatcher cancel !rotate
824    $_dispatcher cancel !xcutplane
825    $_dispatcher cancel !ycutplane
826    $_dispatcher cancel !zcutplane
827    $_dispatcher cancel !legend
828    # disconnected -- no more data sitting on server
829    set _outbuf ""
830    array unset _datasets
831    array unset _data
832    array unset _colormaps
833    array unset _seeds
834    array unset _dataset2style
835    array unset _obj2datasets
836}
837
838# ----------------------------------------------------------------------
839# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
840#
841# Invoked automatically whenever the "image" command comes in from
842# the rendering server.  Indicates that binary image data with the
843# specified <size> will follow.
844# ----------------------------------------------------------------------
845itcl::body Rappture::VtkGlyphViewer::ReceiveImage { args } {
846    array set info {
847        -token "???"
848        -bytes 0
849        -type image
850    }
851    array set info $args
852    set bytes [ReceiveBytes $info(-bytes)]
853    if { $info(-type) == "image" } {
854        if 0 {
855            set f [open "last.ppm" "w"]
856            puts $f $bytes
857            close $f
858        }
859        $_image(plot) configure -data $bytes
860        set time [clock seconds]
861        set date [clock format $time]
862        #puts stderr "$date: received image [image width $_image(plot)]x[image height $_image(plot)] image>"       
863        if { $_start > 0 } {
864            set finish [clock clicks -milliseconds]
865            #puts stderr "round trip time [expr $finish -$_start] milliseconds"
866            set _start 0
867        }
868    } elseif { $info(type) == "print" } {
869        set tag $this-print-$info(-token)
870        set _hardcopy($tag) $bytes
871    }
872}
873
874#
875# ReceiveDataset --
876#
877itcl::body Rappture::VtkGlyphViewer::ReceiveDataset { args } {
878    if { ![isconnected] } {
879        return
880    }
881    set option [lindex $args 0]
882    switch -- $option {
883        "scalar" {
884            set option [lindex $args 1]
885            switch -- $option {
886                "world" {
887                    foreach { x y z value tag } [lrange $args 2 end] break
888                }
889                "pixel" {
890                    foreach { x y value tag } [lrange $args 2 end] break
891                }
892            }
893        }
894        "vector" {
895            set option [lindex $args 1]
896            switch -- $option {
897                "world" {
898                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
899                }
900                "pixel" {
901                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
902                }
903            }
904        }
905        "names" {
906            foreach { name } [lindex $args 1] {
907                #puts stderr "Dataset: $name"
908            }
909        }
910        default {
911            error "unknown dataset option \"$option\" from server"
912        }
913    }
914}
915
916# ----------------------------------------------------------------------
917# USAGE: Rebuild
918#
919# Called automatically whenever something changes that affects the
920# data in the widget.  Clears any existing data and rebuilds the
921# widget to display new data.
922# ----------------------------------------------------------------------
923itcl::body Rappture::VtkGlyphViewer::Rebuild {} {
924    set w [winfo width $itk_component(view)]
925    set h [winfo height $itk_component(view)]
926    if { $w < 2 || $h < 2 } {
927        $_dispatcher event -idle !rebuild
928        return
929    }
930
931    # Turn on buffering of commands to the server.  We don't want to
932    # be preempted by a server disconnect/reconnect (which automatically
933    # generates a new call to Rebuild).   
934    StartBufferingCommands
935
936    if { $_reset } {
937        set _width $w
938        set _height $h
939        $_arcball resize $w $h
940        DoResize
941        #
942        # Reset the camera and other view parameters
943        #
944        set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
945        $_arcball quaternion $q
946        if {$_view(ortho)} {
947            SendCmd "camera mode ortho"
948        } else {
949            SendCmd "camera mode persp"
950        }
951        DoRotate
952        PanCamera
953        set _first ""
954        InitSettings axisXGrid axisYGrid axisZGrid axisMode \
955            axesVisible axisLabelsVisible
956        foreach axis { x y z } {
957            SendCmd "axis lformat $axis %g"
958        }
959        StopBufferingCommands
960        SendCmd "imgflush"
961        StartBufferingCommands
962    }
963    set _first ""
964    SendCmd "dataset visible 0"
965    foreach dataobj [get -objects] {
966        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
967            set _first $dataobj
968        }
969        set _obj2datasets($dataobj) ""
970        foreach comp [$dataobj components] {
971            set tag $dataobj-$comp
972            if { ![info exists _datasets($tag)] } {
973                set bytes [$dataobj vtkdata $comp]
974                if 0 {
975                    set f [open "/tmp/glyph.vtk" "w"]
976                    puts $f $bytes
977                    close $f
978                }
979                set length [string length $bytes]
980                if { $_reportClientInfo }  {
981                    set info {}
982                    lappend info "tool_id"       [$dataobj hints toolId]
983                    lappend info "tool_name"     [$dataobj hints toolName]
984                    lappend info "tool_version"  [$dataobj hints toolRevision]
985                    lappend info "tool_title"    [$dataobj hints toolTitle]
986                    lappend info "dataset_label" [$dataobj hints label]
987                    lappend info "dataset_size"  $length
988                    lappend info "dataset_tag"   $tag
989                    SendCmd "clientinfo [list $info]"
990                }
991                append _outbuf "dataset add $tag data follows $length\n"
992                append _outbuf $bytes
993                set _datasets($tag) 1
994                SetObjectStyle $dataobj $comp
995            }
996            lappend _obj2datasets($dataobj) $tag
997            if { [info exists _obj2ovride($dataobj-raise)] } {
998                # Setting dataset visible enables outline
999                # and glyphs
1000                SendCmd "dataset visible 1 $tag"
1001            }
1002        }
1003    }
1004
1005    if { $_first != "" } {
1006        $itk_component(field) choices delete 0 end
1007        $itk_component(fieldmenu) delete 0 end
1008        array unset _fields
1009        set _curFldName ""
1010        foreach cname [$_first components] {
1011            foreach fname [$_first fieldnames $cname] {
1012                if { [info exists _fields($fname)] } {
1013                    continue
1014                }
1015                foreach { label units components } \
1016                    [$_first fieldinfo $fname] break
1017                $itk_component(field) choices insert end "$fname" "$label"
1018                $itk_component(fieldmenu) add radiobutton -label "$label" \
1019                    -value $label -variable [itcl::scope _curFldLabel] \
1020                    -selectcolor red \
1021                    -activebackground $itk_option(-plotbackground) \
1022                    -activeforeground $itk_option(-plotforeground) \
1023                    -font "Arial 8" \
1024                    -command [itcl::code $this Combo invoke]
1025                set _fields($fname) [list $label $units $components]
1026                if { $_curFldName == "" } {
1027                    set _curFldName $fname
1028                    set _curFldLabel $label
1029                }
1030            }
1031        }
1032        $itk_component(field) value $_curFldLabel
1033    }
1034    InitSettings glyphOutline
1035        #cutplaneVisible
1036    if { $_reset } {
1037        # These are settings that rely on a dataset being loaded.
1038        InitSettings \
1039            glyphLighting \
1040            field \
1041            glyphEdges glyphLighting glyphOpacity \
1042            glyphWireframe
1043
1044        #cutplaneXPosition cutplaneYPosition cutplaneZPosition \
1045            cutplaneXVisible cutplaneYVisible cutplaneZVisible \
1046            cutplanePreinterp
1047
1048        Zoom reset
1049        foreach axis { x y z } {
1050            # Another problem fixed by a <view>. We looking into a data
1051            # object for the name of the axes. This should be global to
1052            # the viewer itself.
1053            set label [$_first hints ${axis}label]
1054            if { $label == "" } {
1055                set label [string toupper $axis]
1056            }
1057            # May be a space in the axis label.
1058            SendCmd [list axis name $axis $label]
1059        }
1060        if { [array size _fields] < 2 } {
1061            catch {blt::table forget $itk_component(field) $itk_component(field_l)}
1062        }
1063        set _reset 0
1064    }
1065
1066    # Actually write the commands to the server socket.  If it fails, we don't
1067    # care.  We're finished here.
1068    blt::busy hold $itk_component(hull)
1069    StopBufferingCommands;              # Turn off buffering and send commands.
1070    blt::busy release $itk_component(hull)
1071}
1072
1073# ----------------------------------------------------------------------
1074# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
1075#
1076# Returns a list of server IDs for the current datasets being displayed.  This
1077# is normally a single ID, but it might be a list of IDs if the current data
1078# object has multiple components.
1079# ----------------------------------------------------------------------
1080itcl::body Rappture::VtkGlyphViewer::CurrentDatasets {args} {
1081    set flag [lindex $args 0]
1082    switch -- $flag {
1083        "-all" {
1084            if { [llength $args] > 1 } {
1085                error "CurrentDatasets: can't specify dataobj after \"-all\""
1086            }
1087            set dlist [get -objects]
1088        }
1089        "-visible" {
1090            if { [llength $args] > 1 } {
1091                set dlist {}
1092                set args [lrange $args 1 end]
1093                foreach dataobj $args {
1094                    if { [info exists _obj2ovride($dataobj-raise)] } {
1095                        lappend dlist $dataobj
1096                    }
1097                }
1098            } else {
1099                set dlist [get -visible]
1100            }
1101        }           
1102        default {
1103            set dlist $args
1104        }
1105    }
1106    set rlist ""
1107    foreach dataobj $dlist {
1108        foreach comp [$dataobj components] {
1109            set tag $dataobj-$comp
1110            if { [info exists _datasets($tag)] && $_datasets($tag) } {
1111                lappend rlist $tag
1112            }
1113        }
1114    }
1115    return $rlist
1116}
1117
1118# ----------------------------------------------------------------------
1119# USAGE: Zoom in
1120# USAGE: Zoom out
1121# USAGE: Zoom reset
1122#
1123# Called automatically when the user clicks on one of the zoom
1124# controls for this widget.  Changes the zoom for the current view.
1125# ----------------------------------------------------------------------
1126itcl::body Rappture::VtkGlyphViewer::Zoom {option} {
1127    switch -- $option {
1128        "in" {
1129            set _view(zoom) [expr {$_view(zoom)*1.25}]
1130            SendCmd "camera zoom $_view(zoom)"
1131        }
1132        "out" {
1133            set _view(zoom) [expr {$_view(zoom)*0.8}]
1134            SendCmd "camera zoom $_view(zoom)"
1135        }
1136        "reset" {
1137            array set _view {
1138                qw     0.853553
1139                qx     -0.353553
1140                qy     0.353553
1141                qz     0.146447
1142                zoom   1.0
1143                xpan   0
1144                ypan   0
1145            }
1146            if { $_first != "" } {
1147                set location [$_first hints camera]
1148                if { $location != "" } {
1149                    array set _view $location
1150                }
1151            }
1152            set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1153            $_arcball quaternion $q
1154            DoRotate
1155            SendCmd "camera reset"
1156        }
1157    }
1158}
1159
1160itcl::body Rappture::VtkGlyphViewer::PanCamera {} {
1161    set x $_view(xpan)
1162    set y $_view(ypan)
1163    SendCmd "camera pan $x $y"
1164}
1165
1166
1167# ----------------------------------------------------------------------
1168# USAGE: Rotate click <x> <y>
1169# USAGE: Rotate drag <x> <y>
1170# USAGE: Rotate release <x> <y>
1171#
1172# Called automatically when the user clicks/drags/releases in the
1173# plot area.  Moves the plot according to the user's actions.
1174# ----------------------------------------------------------------------
1175itcl::body Rappture::VtkGlyphViewer::Rotate {option x y} {
1176    switch -- $option {
1177        "click" {
1178            $itk_component(view) configure -cursor fleur
1179            set _click(x) $x
1180            set _click(y) $y
1181        }
1182        "drag" {
1183            if {[array size _click] == 0} {
1184                Rotate click $x $y
1185            } else {
1186                set w [winfo width $itk_component(view)]
1187                set h [winfo height $itk_component(view)]
1188                if {$w <= 0 || $h <= 0} {
1189                    return
1190                }
1191
1192                if {[catch {
1193                    # this fails sometimes for no apparent reason
1194                    set dx [expr {double($x-$_click(x))/$w}]
1195                    set dy [expr {double($y-$_click(y))/$h}]
1196                }]} {
1197                    return
1198                }
1199                if { $dx == 0 && $dy == 0 } {
1200                    return
1201                }
1202                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1203                EventuallyRotate $q
1204                set _click(x) $x
1205                set _click(y) $y
1206            }
1207        }
1208        "release" {
1209            Rotate drag $x $y
1210            $itk_component(view) configure -cursor ""
1211            catch {unset _click}
1212        }
1213        default {
1214            error "bad option \"$option\": should be click, drag, release"
1215        }
1216    }
1217}
1218
1219itcl::body Rappture::VtkGlyphViewer::Pick {x y} {
1220    foreach tag [CurrentDatasets -visible] {
1221        SendCmdNoSplash "dataset getscalar pixel $x $y $tag"
1222    }
1223}
1224
1225# ----------------------------------------------------------------------
1226# USAGE: $this Pan click x y
1227#        $this Pan drag x y
1228#        $this Pan release x y
1229#
1230# Called automatically when the user clicks on one of the zoom
1231# controls for this widget.  Changes the zoom for the current view.
1232# ----------------------------------------------------------------------
1233itcl::body Rappture::VtkGlyphViewer::Pan {option x y} {
1234    switch -- $option {
1235        "set" {
1236            set w [winfo width $itk_component(view)]
1237            set h [winfo height $itk_component(view)]
1238            set x [expr $x / double($w)]
1239            set y [expr $y / double($h)]
1240            set _view(xpan) [expr $_view(xpan) + $x]
1241            set _view(ypan) [expr $_view(ypan) + $y]
1242            PanCamera
1243            return
1244        }
1245        "click" {
1246            set _click(x) $x
1247            set _click(y) $y
1248            $itk_component(view) configure -cursor hand1
1249        }
1250        "drag" {
1251            if { ![info exists _click(x)] } {
1252                set _click(x) $x
1253            }
1254            if { ![info exists _click(y)] } {
1255                set _click(y) $y
1256            }
1257            set w [winfo width $itk_component(view)]
1258            set h [winfo height $itk_component(view)]
1259            set dx [expr ($_click(x) - $x)/double($w)]
1260            set dy [expr ($_click(y) - $y)/double($h)]
1261            set _click(x) $x
1262            set _click(y) $y
1263            set _view(xpan) [expr $_view(xpan) - $dx]
1264            set _view(ypan) [expr $_view(ypan) - $dy]
1265            PanCamera
1266        }
1267        "release" {
1268            Pan drag $x $y
1269            $itk_component(view) configure -cursor ""
1270        }
1271        default {
1272            error "unknown option \"$option\": should set, click, drag, or release"
1273        }
1274    }
1275}
1276
1277# ----------------------------------------------------------------------
1278# USAGE: InitSettings <what> ?<value>?
1279#
1280# Used internally to update rendering settings whenever parameters
1281# change in the popup settings panel.  Sends the new settings off
1282# to the back end.
1283# ----------------------------------------------------------------------
1284itcl::body Rappture::VtkGlyphViewer::InitSettings { args } {
1285    foreach spec $args {
1286        if { [info exists _settings($_first-$spec)] } {
1287            # Reset global setting with dataobj specific setting
1288            set _settings($spec) $_settings($_first-$spec)
1289        }
1290        AdjustSetting $spec
1291    }
1292}
1293
1294#
1295# AdjustSetting --
1296#
1297#       Changes/updates a specific setting in the widget.  There are
1298#       usually user-setable option.  Commands are sent to the render
1299#       server.
1300#
1301itcl::body Rappture::VtkGlyphViewer::AdjustSetting {what {value ""}} {
1302    if { ![isconnected] } {
1303        return
1304    }
1305    switch -- $what {
1306        "background" {
1307            set bgcolor [$itk_component(background) value]
1308            array set fgcolors {
1309                "black" "white"
1310                "white" "black"
1311                "grey"  "black"
1312            }
1313            configure -plotbackground $bgcolor \
1314                -plotforeground $fgcolors($bgcolor)
1315            $itk_component(view) delete "legend"
1316            DrawLegend
1317        }
1318        "axesVisible" {
1319            set bool $_settings(axesVisible)
1320            SendCmd "axis visible all $bool"
1321        }
1322        "axisLabelsVisible" {
1323            set bool $_settings(axisLabelsVisible)
1324            SendCmd "axis labels all $bool"
1325        }
1326        "axisXGrid" - "axisYGrid" - "axisZGrid" {
1327            set axis [string tolower [string range $what 4 4]]
1328            set bool $_settings($what)
1329            SendCmd "axis grid $axis $bool"
1330        }
1331        "axisMode" {
1332            set mode [$itk_component(axisMode) value]
1333            set mode [$itk_component(axisMode) translate $mode]
1334            set _settings($what) $mode
1335            SendCmd "axis flymode $mode"
1336        }
1337        "cutplaneEdges" {
1338            set bool $_settings($what)
1339            SendCmd "cutplane edges $bool"
1340        }
1341        "cutplaneVisible" {
1342            set bool $_settings($what)
1343            SendCmd "cutplane visible $bool"
1344        }
1345        "cutplaneWireframe" {
1346            set bool $_settings($what)
1347            SendCmd "cutplane wireframe $bool"
1348        }
1349        "cutplaneLighting" {
1350            set bool $_settings($what)
1351            SendCmd "cutplane lighting $bool"
1352        }
1353        "cutplaneOpacity" {
1354            set val $_settings($what)
1355            set sval [expr { 0.01 * double($val) }]
1356            SendCmd "cutplane opacity $sval"
1357        }
1358        "cutplanePreinterp" {
1359            set bool $_settings($what)
1360            SendCmd "cutplane preinterp $bool"
1361        }
1362        "cutplaneXVisible" - "cutplaneYVisible" - "cutplaneZVisible" {
1363            set axis [string tolower [string range $what 8 8]]
1364            set bool $_settings($what)
1365            if { $bool } {
1366                $itk_component(${axis}position) configure -state normal \
1367                    -troughcolor white
1368            } else {
1369                $itk_component(${axis}position) configure -state disabled \
1370                    -troughcolor grey82
1371            }
1372            SendCmd "cutplane axis $axis $bool"
1373        }
1374        "cutplaneXPosition" - "cutplaneYPosition" - "cutplaneZPosition" {
1375            set axis [string tolower [string range $what 8 8]]
1376            set pos [expr $_settings($what) * 0.01]
1377            SendCmd "cutplane slice ${axis} ${pos}"
1378            set _cutplanePending 0
1379        }
1380        "colormap" {
1381            set _changed(colormap) 1
1382            StartBufferingCommands
1383            set color [$itk_component(colormap) value]
1384            set _settings(colormap) $color
1385            if { $color == "none" } {
1386                if { $_settings(colormapVisible) } {
1387                    SendCmd "glyphs colormode constant {}"
1388                    set _settings(colormapVisible) 0
1389                }
1390            } else {
1391                if { !$_settings(colormapVisible) } {
1392                    SendCmd "glyphs colormode $_colorMode $_curFldName"
1393                    set _settings(colormapVisible) 1
1394                }
1395                SetCurrentColormap $color
1396            }
1397            StopBufferingCommands
1398            EventuallyRequestLegend
1399        }
1400        "glyphWireframe" {
1401            set bool $_settings(glyphWireframe)
1402            SendCmd "glyphs wireframe $bool"
1403        }
1404        "glyphVisible" {
1405            set bool $_settings(glyphVisible)
1406            SendCmd "glyphs visible $bool"
1407            if { $bool } {
1408                Rappture::Tooltip::for $itk_component(glyphs) \
1409                    "Hide the glyph"
1410            } else {
1411                Rappture::Tooltip::for $itk_component(glyphs) \
1412                    "Show the glyph"
1413            }
1414            DrawLegend
1415        }
1416        "glyphLighting" {
1417            set bool $_settings(glyphLighting)
1418            SendCmd "glyphs lighting $bool"
1419        }
1420        "glyphEdges" {
1421            set bool $_settings(glyphEdges)
1422            SendCmd "glyphs edges $bool"
1423        }
1424        "glyphOutline" {
1425            set bool $_settings(glyphOutline)
1426            SendCmd "outline visible $bool"
1427        }
1428        "glyphOpacity" {
1429            set val $_settings(glyphOpacity)
1430            set sval [expr { 0.01 * double($val) }]
1431            SendCmd "glyphs opacity $sval"
1432        }
1433        "field" {
1434            set label [$itk_component(field) value]
1435            set fname [$itk_component(field) translate $label]
1436            set _settings(field) $fname
1437            if { [info exists _fields($fname)] } {
1438                foreach { label units components } $_fields($fname) break
1439                if { $components > 1 } {
1440                    set _colorMode vmag
1441                } else {
1442                    set _colorMode scalar
1443                }
1444                set _curFldName $fname
1445                set _curFldLabel $label
1446            } else {
1447                puts stderr "unknown field \"$fname\""
1448                return
1449            }
1450            #SendCmd "dataset maprange explicit $_limits($_curFldName) $_curFldName"
1451            #SendCmd "cutplane colormode $_colorMode $_curFldName"
1452            SendCmd "glyphs colormode $_colorMode $_curFldName"
1453            DrawLegend
1454        }
1455        "legendVisible" {
1456            if { !$_settings(legendVisible) } {
1457                $itk_component(view) delete legend
1458            }
1459            DrawLegend
1460        }
1461        default {
1462            error "don't know how to fix $what"
1463        }
1464    }
1465}
1466
1467
1468#
1469# RequestLegend --
1470#
1471#       Request a new legend from the server.  The size of the legend
1472#       is determined from the height of the canvas. 
1473#
1474# This should be called when
1475#       1.  A new current colormap is set.
1476#       2.  Window is resized.
1477#       3.  The limits of the data have changed.  (Just need a redraw).
1478#       4.  Number of glyph have changed. (Just need a redraw).
1479#       5.  Legend becomes visible (Just need a redraw).
1480#
1481itcl::body Rappture::VtkGlyphViewer::RequestLegend {} {
1482    set _legendPending 0
1483    set font "Arial 8"
1484    set w 12
1485    set lineht [font metrics $font -linespace]
1486    # color ramp height = (canvas height) - (min and max value lines) - 2
1487    set h [expr {$_height - 2 * ($lineht + 2)}]
1488
1489    set fname $_curFldName
1490    if { [string match "component*" $fname] } {
1491        set title ""
1492    } else {
1493        if { [info exists _fields($fname)] } {
1494            foreach { title units } $_fields($fname) break
1495            if { $units != "" } {
1496                set title [format "%s (%s)" $title $units]
1497            }
1498        } else {
1499            set title $fname
1500        }
1501    }
1502    # If there's a title too, substract one more line
1503    if { $title != "" } {
1504        incr h -$lineht
1505    }
1506    if { $h < 1 } {
1507        return
1508    }
1509    # Set the legend on the first heightmap dataset.
1510    if { $_currentColormap != ""  } {
1511        set cmap $_currentColormap
1512        SendCmdNoWait "legend $cmap $_colorMode $_curFldName {} $w $h 0"
1513    }
1514}
1515
1516# ----------------------------------------------------------------------
1517# CONFIGURATION OPTION: -plotbackground
1518# ----------------------------------------------------------------------
1519itcl::configbody Rappture::VtkGlyphViewer::plotbackground {
1520    if { [isconnected] } {
1521        set rgb [Color2RGB $itk_option(-plotbackground)]
1522        SendCmd "screen bgcolor $rgb"
1523    }
1524}
1525
1526# ----------------------------------------------------------------------
1527# CONFIGURATION OPTION: -plotforeground
1528# ----------------------------------------------------------------------
1529itcl::configbody Rappture::VtkGlyphViewer::plotforeground {
1530    if { [isconnected] } {
1531        set rgb [Color2RGB $itk_option(-plotforeground)]
1532        SendCmd "axis color all $rgb"
1533        SendCmd "outline color $rgb"
1534        #SendCmd "cutplane color $rgb"
1535    }
1536}
1537
1538itcl::body Rappture::VtkGlyphViewer::limits { dataobj } {
1539    foreach { limits(xmin) limits(xmax) } [$dataobj limits x] break
1540    foreach { limits(ymin) limits(ymax) } [$dataobj limits y] break
1541    foreach { limits(zmin) limits(zmax) } [$dataobj limits z] break
1542    foreach { limits(vmin) limits(vmax) } [$dataobj limits v] break
1543    return [array get limits]
1544}
1545
1546itcl::body Rappture::VtkGlyphViewer::BuildGlyphTab {} {
1547
1548    set fg [option get $itk_component(hull) font Font]
1549    #set bfg [option get $itk_component(hull) boldFont Font]
1550
1551    set inner [$itk_component(main) insert end \
1552        -title "Glyph Settings" \
1553        -icon [Rappture::icon volume-on]]
1554    $inner configure -borderwidth 4
1555
1556    checkbutton $inner.glyphs \
1557        -text "Glyphs" \
1558        -variable [itcl::scope _settings(glyphVisible)] \
1559        -command [itcl::code $this AdjustSetting glyphVisible] \
1560        -font "Arial 9"
1561
1562    checkbutton $inner.wireframe \
1563        -text "Wireframe" \
1564        -variable [itcl::scope _settings(glyphWireframe)] \
1565        -command [itcl::code $this AdjustSetting glyphWireframe] \
1566        -font "Arial 9"
1567
1568    checkbutton $inner.lighting \
1569        -text "Enable Lighting" \
1570        -variable [itcl::scope _settings(glyphLighting)] \
1571        -command [itcl::code $this AdjustSetting glyphLighting] \
1572        -font "Arial 9"
1573
1574    checkbutton $inner.edges \
1575        -text "Edges" \
1576        -variable [itcl::scope _settings(glyphEdges)] \
1577        -command [itcl::code $this AdjustSetting glyphEdges] \
1578        -font "Arial 9"
1579
1580    checkbutton $inner.outline \
1581        -text "Outline" \
1582        -variable [itcl::scope _settings(glyphOutline)] \
1583        -command [itcl::code $this AdjustSetting glyphOutline] \
1584        -font "Arial 9"
1585
1586    checkbutton $inner.legend \
1587        -text "Legend" \
1588        -variable [itcl::scope _settings(legendVisible)] \
1589        -command [itcl::code $this AdjustSetting legendVisible] \
1590        -font "Arial 9"
1591
1592    label $inner.background_l -text "Background" -font "Arial 9"
1593    itk_component add background {
1594        Rappture::Combobox $inner.background -width 10 -editable no
1595    }
1596    $inner.background choices insert end \
1597        "black"              "black"            \
1598        "white"              "white"            \
1599        "grey"               "grey"             
1600
1601    $itk_component(background) value $_settings(background)
1602    bind $inner.background <<Value>> [itcl::code $this AdjustSetting background]
1603
1604    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1605    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1606        -variable [itcl::scope _settings(glyphOpacity)] \
1607        -width 10 \
1608        -showvalue off \
1609        -command [itcl::code $this AdjustSetting glyphOpacity]
1610
1611    label $inner.scale_l -text "Scale" -font "Arial 9"
1612    ::scale $inner.scale -from 1 -to 100 -orient horizontal \
1613        -variable [itcl::scope _settings(glyphs-scale)] \
1614        -width 10 \
1615        -showvalue off \
1616        -command [itcl::code $this AdjustSetting glyphs-scale]
1617
1618    itk_component add field_l {
1619        label $inner.field_l -text "Color By" -font "Arial 9"
1620    } {
1621        ignore -font
1622    }
1623    itk_component add field {
1624        Rappture::Combobox $inner.field -width 10 -editable no
1625    }
1626    bind $inner.field <<Value>> \
1627        [itcl::code $this AdjustSetting field]
1628
1629    label $inner.colormap_l -text "Colormap" -font "Arial 9"
1630    itk_component add colormap {
1631        Rappture::Combobox $inner.colormap -width 10 -editable no
1632    }
1633
1634    $inner.colormap choices insert end [GetColormapList]
1635    $itk_component(colormap) value "BCGYR"
1636    bind $inner.colormap <<Value>> \
1637        [itcl::code $this AdjustSetting colormap]
1638
1639    blt::table $inner \
1640        0,0 $inner.field_l   -anchor w -pady 2  \
1641        0,1 $inner.field     -anchor w -pady 2  -fill x \
1642        1,0 $inner.colormap_l -anchor w -pady 2  \
1643        1,1 $inner.colormap   -anchor w -pady 2  -fill x \
1644        3,0 $inner.background_l -anchor w -pady 2 \
1645        3,1 $inner.background -anchor w -pady 2  -fill x \
1646        5,0 $inner.wireframe -anchor w -pady 2 -cspan 2 \
1647        6,0 $inner.lighting  -anchor w -pady 2 -cspan 2 \
1648        7,0 $inner.edges     -anchor w -pady 2 -cspan 2 \
1649        8,0 $inner.outline   -anchor w -pady 2 -cspan 2 \
1650        9,0 $inner.legend    -anchor w -pady 2 \
1651        10,0 $inner.opacity_l -anchor w -pady 2 \
1652        10,1 $inner.opacity   -fill x   -pady 2 -fill x \
1653
1654    blt::table configure $inner r* c* -resize none
1655    blt::table configure $inner r11 c1 -resize expand
1656}
1657
1658itcl::body Rappture::VtkGlyphViewer::BuildAxisTab {} {
1659
1660    set fg [option get $itk_component(hull) font Font]
1661    #set bfg [option get $itk_component(hull) boldFont Font]
1662
1663    set inner [$itk_component(main) insert end \
1664        -title "Axis Settings" \
1665        -icon [Rappture::icon axis2]]
1666    $inner configure -borderwidth 4
1667
1668    checkbutton $inner.visible \
1669        -text "Show Axes" \
1670        -variable [itcl::scope _settings(axesVisible)] \
1671        -command [itcl::code $this AdjustSetting axesVisible] \
1672        -font "Arial 9"
1673
1674    checkbutton $inner.labels \
1675        -text "Show Axis Labels" \
1676        -variable [itcl::scope _settings(axisLabelsVisible)] \
1677        -command [itcl::code $this AdjustSetting axisLabelsVisible] \
1678        -font "Arial 9"
1679
1680    checkbutton $inner.gridx \
1681        -text "Show X Grid" \
1682        -variable [itcl::scope _settings(axisXGrid)] \
1683        -command [itcl::code $this AdjustSetting axisXGrid] \
1684        -font "Arial 9"
1685    checkbutton $inner.gridy \
1686        -text "Show Y Grid" \
1687        -variable [itcl::scope _settings(axisYGrid)] \
1688        -command [itcl::code $this AdjustSetting axisYGrid] \
1689        -font "Arial 9"
1690    checkbutton $inner.gridz \
1691        -text "Show Z Grid" \
1692        -variable [itcl::scope _settings(axisZGrid)] \
1693        -command [itcl::code $this AdjustSetting axisZGrid] \
1694        -font "Arial 9"
1695
1696    label $inner.mode_l -text "Mode" -font "Arial 9"
1697
1698    itk_component add axisMode {
1699        Rappture::Combobox $inner.mode -width 10 -editable no
1700    }
1701    $inner.mode choices insert end \
1702        "static_triad"    "static" \
1703        "closest_triad"   "closest" \
1704        "furthest_triad"  "farthest" \
1705        "outer_edges"     "outer"         
1706    $itk_component(axisMode) value "static"
1707    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting axisMode]
1708
1709    blt::table $inner \
1710        0,0 $inner.visible -anchor w -cspan 2 \
1711        1,0 $inner.labels  -anchor w -cspan 2 \
1712        2,0 $inner.gridx   -anchor w -cspan 2 \
1713        3,0 $inner.gridy   -anchor w -cspan 2 \
1714        4,0 $inner.gridz   -anchor w -cspan 2 \
1715        5,0 $inner.mode_l  -anchor w -cspan 2 -padx { 2 0 } \
1716        6,0 $inner.mode    -fill x   -cspan 2
1717
1718    blt::table configure $inner r* c* -resize none
1719    blt::table configure $inner r7 c1 -resize expand
1720}
1721
1722
1723itcl::body Rappture::VtkGlyphViewer::BuildCameraTab {} {
1724    set inner [$itk_component(main) insert end \
1725        -title "Camera Settings" \
1726        -icon [Rappture::icon camera]]
1727    $inner configure -borderwidth 4
1728
1729    label $inner.view_l -text "view" -font "Arial 9"
1730    set f [frame $inner.view]
1731    foreach side { front back left right top bottom } {
1732        button $f.$side  -image [Rappture::icon view$side] \
1733            -command [itcl::code $this SetOrientation $side]
1734        Rappture::Tooltip::for $f.$side "Change the view to $side"
1735        pack $f.$side -side left
1736    }
1737
1738    blt::table $inner \
1739        0,0 $inner.view_l -anchor e -pady 2 \
1740        0,1 $inner.view -anchor w -pady 2
1741
1742    set labels { qx qy qz qw xpan ypan zoom }
1743    set row 1
1744    foreach tag $labels {
1745        label $inner.${tag}label -text $tag -font "Arial 9"
1746        entry $inner.${tag} -font "Arial 9"  -bg white \
1747            -textvariable [itcl::scope _view($tag)]
1748        bind $inner.${tag} <KeyPress-Return> \
1749            [itcl::code $this camera set ${tag}]
1750        blt::table $inner \
1751            $row,0 $inner.${tag}label -anchor e -pady 2 \
1752            $row,1 $inner.${tag} -anchor w -pady 2
1753        blt::table configure $inner r$row -resize none
1754        incr row
1755    }
1756    checkbutton $inner.ortho \
1757        -text "Orthographic Projection" \
1758        -variable [itcl::scope _view(ortho)] \
1759        -command [itcl::code $this camera set ortho] \
1760        -font "Arial 9"
1761    blt::table $inner \
1762            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1763    blt::table configure $inner r$row -resize none
1764    incr row
1765
1766    blt::table configure $inner c* r* -resize none
1767    blt::table configure $inner c2 -resize expand
1768    blt::table configure $inner r$row -resize expand
1769}
1770
1771itcl::body Rappture::VtkGlyphViewer::BuildCutplaneTab {} {
1772
1773    set fg [option get $itk_component(hull) font Font]
1774   
1775    set inner [$itk_component(main) insert end \
1776        -title "Cutplane Settings" \
1777        -icon [Rappture::icon cutbutton]]
1778
1779    $inner configure -borderwidth 4
1780
1781    checkbutton $inner.visible \
1782        -text "Cutplanes" \
1783        -variable [itcl::scope _settings(cutplaneVisible)] \
1784        -command [itcl::code $this AdjustSetting cutplaneVisible] \
1785        -font "Arial 9"
1786
1787    checkbutton $inner.wireframe \
1788        -text "Wireframe" \
1789        -variable [itcl::scope _settings(cutplaneWireframe)] \
1790        -command [itcl::code $this AdjustSetting cutplaneWireframe] \
1791        -font "Arial 9"
1792
1793    checkbutton $inner.lighting \
1794        -text "Enable Lighting" \
1795        -variable [itcl::scope _settings(cutplaneLighting)] \
1796        -command [itcl::code $this AdjustSetting cutplaneLighting] \
1797        -font "Arial 9"
1798
1799    checkbutton $inner.edges \
1800        -text "Edges" \
1801        -variable [itcl::scope _settings(cutplaneEdges)] \
1802        -command [itcl::code $this AdjustSetting cutplaneEdges] \
1803        -font "Arial 9"
1804
1805    checkbutton $inner.preinterp \
1806        -text "Interpolate Scalars" \
1807        -variable [itcl::scope _settings(cutplanePreinterp)] \
1808        -command [itcl::code $this AdjustSetting cutplanePreinterp] \
1809        -font "Arial 9"
1810
1811    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1812    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1813        -variable [itcl::scope _settings(cutplaneOpacity)] \
1814        -width 10 \
1815        -showvalue off \
1816        -command [itcl::code $this AdjustSetting cutplaneOpacity]
1817    $inner.opacity set $_settings(cutplaneOpacity)
1818
1819    # X-value slicer...
1820    itk_component add xbutton {
1821        Rappture::PushButton $inner.xbutton \
1822            -onimage [Rappture::icon x-cutplane-red] \
1823            -offimage [Rappture::icon x-cutplane-red] \
1824            -command [itcl::code $this AdjustSetting cutplaneXVisible] \
1825            -variable [itcl::scope _settings(cutplaneXVisible)] \
1826    }
1827    Rappture::Tooltip::for $itk_component(xbutton) \
1828        "Toggle the X-axis cutplane on/off"
1829    $itk_component(xbutton) select
1830    itk_component add xposition {
1831        ::scale $inner.xval -from 100 -to 0 \
1832            -width 10 -orient vertical -showvalue yes \
1833            -borderwidth 1 -highlightthickness 0 \
1834            -command [itcl::code $this EventuallySetCutplane x] \
1835            -variable [itcl::scope _settings(cutplaneXPosition)] \
1836            -foreground red2 -font "Arial 9 bold"
1837    } {
1838        usual
1839        ignore -borderwidth -highlightthickness -foreground -font -background
1840    }
1841    # Set the default cutplane value before disabling the scale.
1842    $itk_component(xposition) set 50
1843    $itk_component(xposition) configure -state disabled
1844    Rappture::Tooltip::for $itk_component(xposition) \
1845        "@[itcl::code $this Slice tooltip x]"
1846
1847    # Y-value slicer...
1848    itk_component add ybutton {
1849        Rappture::PushButton $inner.ybutton \
1850            -onimage [Rappture::icon y-cutplane-green] \
1851            -offimage [Rappture::icon y-cutplane-green] \
1852            -command [itcl::code $this AdjustSetting cutplaneYVisible] \
1853            -variable [itcl::scope _settings(cutplaneYVisible)] \
1854    }
1855    Rappture::Tooltip::for $itk_component(ybutton) \
1856        "Toggle the Y-axis cutplane on/off"
1857    $itk_component(ybutton) select
1858
1859    itk_component add yposition {
1860        ::scale $inner.yval -from 100 -to 0 \
1861            -width 10 -orient vertical -showvalue yes \
1862            -borderwidth 1 -highlightthickness 0 \
1863            -command [itcl::code $this EventuallySetCutplane y] \
1864            -variable [itcl::scope _settings(cutplaneYPosition)] \
1865            -foreground green3 -font "Arial 9 bold"
1866    } {
1867        usual
1868        ignore -borderwidth -highlightthickness -foreground -font
1869    }
1870    Rappture::Tooltip::for $itk_component(yposition) \
1871        "@[itcl::code $this Slice tooltip y]"
1872    # Set the default cutplane value before disabling the scale.
1873    $itk_component(yposition) set 50
1874    $itk_component(yposition) configure -state disabled
1875
1876    # Z-value slicer...
1877    itk_component add zbutton {
1878        Rappture::PushButton $inner.zbutton \
1879            -onimage [Rappture::icon z-cutplane-blue] \
1880            -offimage [Rappture::icon z-cutplane-blue] \
1881            -command [itcl::code $this AdjustSetting cutplaneZVisible] \
1882            -variable [itcl::scope _settings(cutplaneZVisible)] \
1883    } {
1884        usual
1885        ignore -foreground
1886    }
1887    Rappture::Tooltip::for $itk_component(zbutton) \
1888        "Toggle the Z-axis cutplane on/off"
1889    $itk_component(zbutton) select
1890
1891    itk_component add zposition {
1892        ::scale $inner.zval -from 100 -to 0 \
1893            -width 10 -orient vertical -showvalue yes \
1894            -borderwidth 1 -highlightthickness 0 \
1895            -command [itcl::code $this EventuallySetCutplane z] \
1896            -variable [itcl::scope _settings(cutplaneZPosition)] \
1897            -foreground blue3 -font "Arial 9 bold"
1898    } {
1899        usual
1900        ignore -borderwidth -highlightthickness -foreground -font
1901    }
1902    $itk_component(zposition) set 50
1903    $itk_component(zposition) configure -state disabled
1904    Rappture::Tooltip::for $itk_component(zposition) \
1905        "@[itcl::code $this Slice tooltip z]"
1906
1907    blt::table $inner \
1908        0,0 $inner.visible              -anchor w -pady 2 -cspan 3 \
1909        1,0 $inner.lighting             -anchor w -pady 2 -cspan 3 \
1910        2,0 $inner.wireframe            -anchor w -pady 2 -cspan 3 \
1911        3,0 $inner.edges                -anchor w -pady 2 -cspan 3 \
1912        4,0 $inner.preinterp            -anchor w -pady 2 -cspan 3 \
1913        5,0 $inner.opacity_l            -anchor w -pady 2 -cspan 1 \
1914        5,1 $inner.opacity              -fill x   -pady 2 -cspan 3 \
1915        6,0 $inner.xbutton              -anchor w -padx 2 -pady 2 \
1916        7,0 $inner.ybutton              -anchor w -padx 2 -pady 2 \
1917        8,0 $inner.zbutton              -anchor w -padx 2 -pady 2 \
1918        6,1 $inner.xval                 -fill y -rspan 4 \
1919        6,2 $inner.yval                 -fill y -rspan 4 \
1920        6,3 $inner.zval                 -fill y -rspan 4 \
1921
1922
1923    blt::table configure $inner r* c* -resize none
1924    blt::table configure $inner r9 c4 -resize expand
1925}
1926
1927
1928
1929#
1930#  camera --
1931#
1932itcl::body Rappture::VtkGlyphViewer::camera {option args} {
1933    switch -- $option {
1934        "show" {
1935            puts [array get _view]
1936        }
1937        "set" {
1938            set who [lindex $args 0]
1939            set x $_view($who)
1940            set code [catch { string is double $x } result]
1941            if { $code != 0 || !$result } {
1942                return
1943            }
1944            switch -- $who {
1945                "ortho" {
1946                    if {$_view(ortho)} {
1947                        SendCmd "camera mode ortho"
1948                    } else {
1949                        SendCmd "camera mode persp"
1950                    }
1951                }
1952                "xpan" - "ypan" {
1953                    PanCamera
1954                }
1955                "qx" - "qy" - "qz" - "qw" {
1956                    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1957                    $_arcball quaternion $q
1958                    EventuallyRotate $q
1959                }
1960                "zoom" {
1961                    SendCmd "camera zoom $_view(zoom)"
1962                }
1963             }
1964        }
1965    }
1966}
1967
1968itcl::body Rappture::VtkGlyphViewer::GetVtkData { args } {
1969    set bytes ""
1970    foreach dataobj [get] {
1971        foreach cname [$dataobj components] {
1972            set tag $dataobj-$cname
1973            set contents [$dataobj vtkdata $cname]
1974            append bytes "$contents\n"
1975        }
1976    }
1977    return [list .vtk $bytes]
1978}
1979
1980itcl::body Rappture::VtkGlyphViewer::GetImage { args } {
1981    if { [image width $_image(download)] > 0 &&
1982         [image height $_image(download)] > 0 } {
1983        set bytes [$_image(download) data -format "jpeg -quality 100"]
1984        set bytes [Rappture::encoding::decode -as b64 $bytes]
1985        return [list .jpg $bytes]
1986    }
1987    return ""
1988}
1989
1990itcl::body Rappture::VtkGlyphViewer::BuildDownloadPopup { popup command } {
1991    Rappture::Balloon $popup \
1992        -title "[Rappture::filexfer::label downloadWord] as..."
1993    set inner [$popup component inner]
1994    label $inner.summary -text "" -anchor w
1995    radiobutton $inner.vtk_button -text "VTK data file" \
1996        -variable [itcl::scope _downloadPopup(format)] \
1997        -font "Arial 9 " \
1998        -value vtk 
1999    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2000    radiobutton $inner.image_button -text "Image File" \
2001        -variable [itcl::scope _downloadPopup(format)] \
2002        -font "Arial 9 " \
2003        -value image
2004    Rappture::Tooltip::for $inner.image_button \
2005        "Save as digital image."
2006
2007    button $inner.ok -text "Save" \
2008        -highlightthickness 0 -pady 2 -padx 3 \
2009        -command $command \
2010        -compound left \
2011        -image [Rappture::icon download]
2012
2013    button $inner.cancel -text "Cancel" \
2014        -highlightthickness 0 -pady 2 -padx 3 \
2015        -command [list $popup deactivate] \
2016        -compound left \
2017        -image [Rappture::icon cancel]
2018
2019    blt::table $inner \
2020        0,0 $inner.summary -cspan 2  \
2021        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2022        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2023        4,1 $inner.cancel -width .9i -fill y \
2024        4,0 $inner.ok -padx 2 -width .9i -fill y
2025    blt::table configure $inner r3 -height 4
2026    blt::table configure $inner r4 -pady 4
2027    raise $inner.image_button
2028    $inner.vtk_button invoke
2029    return $inner
2030}
2031
2032itcl::body Rappture::VtkGlyphViewer::SetObjectStyle { dataobj comp } {
2033    # Parse style string.
2034    set tag $dataobj-$comp
2035    array set style {
2036        -color BCGYR
2037        -edgecolor black
2038        -edges 0
2039        -gscale 1
2040        -lighting 1
2041        -linewidth 1.0
2042        -normscale 1
2043        -opacity 1.0
2044        -orientGlyphs 1
2045        -outline 0
2046        -ptsize 1.0
2047        -quality 1
2048        -scaleMode "vmag"
2049        -shape "arrow"
2050        -wireframe 0
2051    }
2052    set numComponents [$dataobj numComponents $comp]
2053    if {$numComponents == 3} {
2054        set style(-shape) "arrow"
2055        set style(-orientGlyphs) 1
2056        set style(-scaleMode) "vmag"
2057    } else {
2058        set style(-shape) "sphere"
2059        set style(-orientGlyphs) 0
2060        set style(-scaleMode) "scalar"
2061    }
2062    array set style [$dataobj style $comp]
2063    if { $dataobj != $_first } {
2064        set style(-opacity) 1
2065    }
2066    if 0 {
2067    SendCmd "cutplane add $tag"
2068    SendCmd "cutplane visible 0 $tag"
2069    }
2070    # This is too complicated.  We want to set the colormap, number of
2071    # glyph and opacity for the dataset.  They can be the default values,
2072    # the style hints loaded with the dataset, or set by user controls.  As
2073    # datasets get loaded, they first use the defaults that are overidden
2074    # by the style hints.  If the user changes the global controls, then that
2075    # overrides everything else.  I don't know what it means when global
2076    # controls are specified as style hints by each dataset.  It complicates
2077    # the code to handle aberrant cases.
2078
2079    if { $_changed(glyphOpacity) } {
2080        set style(-opacity) [expr $_settings(glyphOpacity) * 0.01]
2081    }
2082    if { $_changed(colormap) } {
2083        set style(-color) $_settings(colormap)
2084    }
2085    if { $_currentColormap == "" } {
2086        $itk_component(colormap) value $style(-color)
2087    }
2088    SendCmd "glyphs add $style(-shape) $tag"
2089    SendCmd "glyphs edges $style(-edges) $tag"
2090    # normscale=1 and gscale=1 are defaults
2091    if {$style(-normscale) != 1} {
2092        SendCmd "glyphs normscale $style(-normscale) $tag"
2093    }
2094    if {$style(-gscale) != 1} {
2095        SendCmd "glyphs gscale $style(-gscale) $tag"
2096    }
2097    SendCmd "outline add $tag"
2098    SendCmd "outline color [Color2RGB $itk_option(-plotforeground)] $tag"
2099    SendCmd "outline visible $style(-outline) $tag"
2100    set _settings(glyphOutline) $style(-outline)
2101    set _settings(glyphEdges) $style(-edges)
2102    # constant color only used if colormode set to constant
2103    SendCmd "glyphs color [Color2RGB $itk_option(-plotforeground)] $tag"
2104    # Omitting field name for gorient and smode commands
2105    # defaults to active scalars or vectors depending on mode
2106    SendCmd "glyphs gorient $style(-orientGlyphs) {} $tag"
2107    SendCmd "glyphs smode $style(-scaleMode) {} $tag"
2108    SendCmd "glyphs quality $style(-quality) $tag"
2109    SendCmd "glyphs lighting $style(-lighting) $tag"
2110    set _settings(glyphLighting) $style(-lighting)
2111    SendCmd "glyphs linecolor [Color2RGB $style(-edgecolor)] $tag"
2112    SendCmd "glyphs linewidth $style(-linewidth) $tag"
2113    SendCmd "glyphs ptsize $style(-ptsize) $tag"
2114    SendCmd "glyphs opacity $style(-opacity) $tag"
2115    set _settings(glyphOpacity) [expr $style(-opacity) * 100.0]
2116    SetCurrentColormap $style(-color)
2117    SendCmd "glyphs wireframe $style(-wireframe) $tag"
2118    set _settings(glyphWireframe) $style(-wireframe)
2119}
2120
2121itcl::body Rappture::VtkGlyphViewer::IsValidObject { dataobj } {
2122    if {[catch {$dataobj isa Rappture::Field} valid] != 0 || !$valid} {
2123        return 0
2124    }
2125    return 1
2126}
2127
2128#
2129# EnterLegend --
2130#
2131itcl::body Rappture::VtkGlyphViewer::EnterLegend { x y } {
2132    SetLegendTip $x $y
2133}
2134
2135#
2136# MotionLegend --
2137#
2138itcl::body Rappture::VtkGlyphViewer::MotionLegend { x y } {
2139    Rappture::Tooltip::tooltip cancel
2140    set c $itk_component(view)
2141    set cw [winfo width $c]
2142    set ch [winfo height $c]
2143    if { $x >= 0 && $x < $cw && $y >= 0 && $y < $ch } {
2144        SetLegendTip $x $y
2145    }
2146}
2147
2148#
2149# LeaveLegend --
2150#
2151itcl::body Rappture::VtkGlyphViewer::LeaveLegend { } {
2152    Rappture::Tooltip::tooltip cancel
2153    .rappturetooltip configure -icon ""
2154}
2155
2156#
2157# SetLegendTip --
2158#
2159itcl::body Rappture::VtkGlyphViewer::SetLegendTip { x y } {
2160    set fname $_curFldName
2161    set c $itk_component(view)
2162    set w [winfo width $c]
2163    set h [winfo height $c]
2164
2165    set font "Arial 8"
2166    set lineht [font metrics $font -linespace]
2167   
2168    set ih [image height $_image(legend)]
2169    set iy [expr $y - ($lineht + 2)]
2170
2171    if { [string match "component*" $fname] } {
2172        set title ""
2173    } else {
2174        if { [info exists _fields($fname)] } {
2175            foreach { title units } $_fields($fname) break
2176            if { $units != "" } {
2177                set title [format "%s (%s)" $title $units]
2178            }
2179        } else {
2180            set title $fname
2181        }
2182    }
2183    # If there's a legend title, increase the offset by the line height.
2184    if { $title != "" } {
2185        incr iy -$lineht
2186    }
2187    # Make a swatch of the selected color
2188    if { [catch { $_image(legend) get 10 $iy } pixel] != 0 } {
2189        return
2190    }
2191    if { ![info exists _image(swatch)] } {
2192        set _image(swatch) [image create photo -width 24 -height 24]
2193    }
2194    set color [eval format "\#%02x%02x%02x" $pixel]
2195    $_image(swatch) put black  -to 0 0 23 23
2196    $_image(swatch) put $color -to 1 1 22 22
2197    .rappturetooltip configure -icon $_image(swatch)
2198
2199    # Compute the value of the point
2200    if { [info exists _limits($_curFldName)] } {
2201        foreach { vmin vmax } $_limits($_curFldName) break
2202        set t [expr 1.0 - (double($iy) / double($ih-1))]
2203        set value [expr $t * ($vmax - $vmin) + $vmin]
2204    } else {
2205        set value 0.0
2206    }
2207    set tx [expr $x + 15]
2208    set ty [expr $y - 5]
2209    Rappture::Tooltip::text $c [format "$title %g" $value]
2210    Rappture::Tooltip::tooltip show $c +$tx,+$ty   
2211}
2212
2213
2214# ----------------------------------------------------------------------
2215# USAGE: Slice move x|y|z <newval>
2216#
2217# Called automatically when the user drags the slider to move the
2218# cut plane that slices 3D data.  Gets the current value from the
2219# slider and moves the cut plane to the appropriate point in the
2220# data set.
2221# ----------------------------------------------------------------------
2222itcl::body Rappture::VtkGlyphViewer::Slice {option args} {
2223    switch -- $option {
2224        "move" {
2225            set axis [lindex $args 0]
2226            set newval [lindex $args 1]
2227            if {[llength $args] != 2} {
2228                error "wrong # args: should be \"Slice move x|y|z newval\""
2229            }
2230            set newpos [expr {0.01*$newval}]
2231            SendCmd "cutplane slice $axis $newpos"
2232        }
2233        "tooltip" {
2234            set axis [lindex $args 0]
2235            set val [$itk_component(${axis}position) get]
2236            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
2237        }
2238        default {
2239            error "bad option \"$option\": should be axis, move, or tooltip"
2240        }
2241    }
2242}
2243
2244#
2245# ReceiveLegend --
2246#
2247#       Invoked automatically whenever the "legend" command comes in from
2248#       the rendering server.  Indicates that binary image data with the
2249#       specified <size> will follow.
2250#
2251itcl::body Rappture::VtkGlyphViewer::ReceiveLegend { colormap title min max size } {
2252    #puts stderr "ReceiveLegend colormap=$colormap title=$title range=$min,$max size=$size"
2253    set _title $title
2254    regsub {\(mag\)} $title "" _title
2255    if { [IsConnected] } {
2256        set bytes [ReceiveBytes $size]
2257        if { ![info exists _image(legend)] } {
2258            set _image(legend) [image create photo]
2259        }
2260        $_image(legend) configure -data $bytes
2261        #puts stderr "read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
2262        if { [catch {DrawLegend} errs] != 0 } {
2263            global errorInfo
2264            puts stderr "errs=$errs errorInfo=$errorInfo"
2265        }
2266    }
2267}
2268
2269#
2270# DrawLegend --
2271#
2272#       Draws the legend in the own canvas on the right side of the plot area.
2273#
2274itcl::body Rappture::VtkGlyphViewer::DrawLegend {} {
2275    set fname $_curFldName
2276    set c $itk_component(view)
2277    set w [winfo width $c]
2278    set h [winfo height $c]
2279    set font "Arial 8"
2280    set lineht [font metrics $font -linespace]
2281   
2282    if { [string match "component*" $fname] } {
2283        set title ""
2284    } else {
2285        if { [info exists _fields($fname)] } {
2286            foreach { title units } $_fields($fname) break
2287            if { $units != "" } {
2288                set title [format "%s (%s)" $title $units]
2289            }
2290        } else {
2291            set title $fname
2292        }
2293    }
2294    set x [expr $w - 2]
2295    if { !$_settings(legendVisible) } {
2296        $c delete legend
2297        return
2298    }
2299    if { [$c find withtag "legend"] == "" } {
2300        set y 2
2301        # If there's a legend title, create a text item for the title.
2302        $c create text $x $y \
2303            -anchor ne \
2304            -fill $itk_option(-plotforeground) -tags "title legend" \
2305            -font $font
2306        if { $title != "" } {
2307            incr y $lineht
2308        }
2309        $c create text $x $y \
2310            -anchor ne \
2311            -fill $itk_option(-plotforeground) -tags "vmax legend" \
2312            -font $font
2313        incr y $lineht
2314        $c create image $x $y \
2315            -anchor ne \
2316            -image $_image(legend) -tags "colormap legend"
2317        $c create rectangle $x $y 1 1 \
2318            -fill "" -outline "" -tags "sensor legend"
2319        $c create text $x [expr {$h-2}] \
2320            -anchor se \
2321            -fill $itk_option(-plotforeground) -tags "vmin legend" \
2322            -font $font
2323        $c bind sensor <Enter> [itcl::code $this EnterLegend %x %y]
2324        $c bind sensor <Leave> [itcl::code $this LeaveLegend]
2325        $c bind sensor <Motion> [itcl::code $this MotionLegend %x %y]
2326    }
2327    set x2 $x
2328    set iw [image width $_image(legend)]
2329    set ih [image height $_image(legend)]
2330    set x1 [expr $x2 - ($iw*12)/10]
2331
2332    $c bind title <ButtonPress> [itcl::code $this Combo post]
2333    $c bind title <Enter> [itcl::code $this Combo activate]
2334    $c bind title <Leave> [itcl::code $this Combo deactivate]
2335    # Reset the item coordinates according the current size of the plot.
2336    $c itemconfigure title -text $title
2337    if { [info exists _limits($_curFldName)] } {
2338        foreach { vmin vmax } $_limits($_curFldName) break
2339        $c itemconfigure vmin -text [format %g $vmin]
2340        $c itemconfigure vmax -text [format %g $vmax]
2341    }
2342    set y 2
2343    # If there's a legend title, move the title to the correct position
2344    if { $title != "" } {
2345        $c itemconfigure title -text $title
2346        $c coords title $x $y
2347        incr y $lineht
2348        $c raise title
2349    }
2350    $c coords vmax $x $y
2351    incr y $lineht
2352    $c coords colormap $x $y
2353    $c coords sensor [expr $x - $iw] $y $x [expr $y + $ih]
2354    $c raise sensor
2355    $c coords vmin $x [expr {$h - 2}]
2356}
2357
2358# ----------------------------------------------------------------------
2359# USAGE: _dropdown post
2360# USAGE: _dropdown unpost
2361# USAGE: _dropdown select
2362#
2363# Used internally to handle the dropdown list for this combobox.  The
2364# post/unpost options are invoked when the list is posted or unposted
2365# to manage the relief of the controlling button.  The select option
2366# is invoked whenever there is a selection from the list, to assign
2367# the value back to the gauge.
2368# ----------------------------------------------------------------------
2369itcl::body Rappture::VtkGlyphViewer::Combo {option} {
2370    set c $itk_component(view)
2371    switch -- $option {
2372        post {
2373            foreach { x1 y1 x2 y2 } [$c bbox title] break
2374            set cw [winfo width $itk_component(view)]
2375            set mw [winfo reqwidth $itk_component(fieldmenu)]
2376            set x1 [expr $cw - $mw]
2377            set x [expr $x1 + [winfo rootx $itk_component(view)]]
2378            set y [expr $y2 + [winfo rooty $itk_component(view)]]
2379            tk_popup $itk_component(fieldmenu) $x $y
2380        }
2381        activate {
2382            $c itemconfigure title -fill red
2383        }
2384        deactivate {
2385            $c itemconfigure title -fill $itk_option(-plotforeground)
2386        }
2387        invoke {
2388            $itk_component(field) value $_curFldLabel
2389            AdjustSetting field
2390        }
2391        default {
2392            error "bad option \"$option\": should be post, unpost, select"
2393        }
2394    }
2395}
2396
2397#
2398# SetCurrentColormap --
2399#
2400itcl::body Rappture::VtkGlyphViewer::SetCurrentColormap { name } {
2401    # Keep track of the colormaps that we build.
2402    if { ![info exists _colormaps($name)] } {
2403        BuildColormap $name
2404        set _colormaps($name) 1
2405    }
2406    set _currentColormap $name
2407    SendCmd "glyphs colormap $_currentColormap"
2408}
2409
2410#
2411# BuildColormap --
2412#
2413#       Build the designated colormap on the server.
2414#
2415itcl::body Rappture::VtkGlyphViewer::BuildColormap { name } {
2416    set cmap [ColorsToColormap $name]
2417    if { [llength $cmap] == 0 } {
2418        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
2419    }
2420    set wmap "0.0 1.0 1.0 1.0"
2421    SendCmd "colormap add $name { $cmap } { $wmap }"
2422}
2423
2424itcl::body Rappture::VtkGlyphViewer::SetOrientation { side } {
2425    array set positions {
2426        front "1 0 0 0"
2427        back  "0 0 1 0"
2428        left  "0.707107 0 -0.707107 0"
2429        right "0.707107 0 0.707107 0"
2430        top   "0.707107 -0.707107 0 0"
2431        bottom "0.707107 0.707107 0 0"
2432    }
2433    foreach name { qw qx qy qz } value $positions($side) {
2434        set _view($name) $value
2435    }
2436    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
2437    $_arcball quaternion $q
2438    SendCmd "camera orient $q"
2439    SendCmd "camera reset"
2440    set _view(xpan) 0
2441    set _view(ypan) 0
2442    set _view(zoom) 1.0
2443}
2444
Note: See TracBrowser for help on using the repository browser.