source: branches/1.4/gui/scripts/vtkglyphviewer.tcl @ 5513

Last change on this file since 5513 was 5513, checked in by ldelgass, 9 years ago

whitespace

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