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

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

Merge fix from r4454 from trunk

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