source: trunk/gui/scripts/vtkglyphviewer.tcl @ 6151

Last change on this file since 6151 was 6151, checked in by ldelgass, 8 years ago

fix setting name

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