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

Last change on this file since 5347 was 5347, checked in by ldelgass, 5 years ago

set _downloadPopup to private access

File size: 88.8 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    $inner.colormap choices insert end [GetColormapList -includeNone]
1736    $itk_component(colormap) value "BCGYR"
1737    bind $inner.colormap <<Value>> \
1738        [itcl::code $this AdjustSetting -colormap]
1739
1740    blt::table $inner \
1741        0,0 $inner.field_l      -anchor w -pady 2  \
1742        0,1 $inner.field        -anchor w -pady 2  -fill x \
1743        1,0 $inner.colormap_l   -anchor w -pady 2  \
1744        1,1 $inner.colormap     -anchor w -pady 2  -fill x \
1745        2,0 $inner.gshape_l     -anchor w -pady 2  \
1746        2,1 $inner.gshape       -anchor w -pady 2  -fill x \
1747        3,0 $inner.background_l -anchor w -pady 2 \
1748        3,1 $inner.background   -anchor w -pady 2  -fill x \
1749        4,0 $inner.scaleMode_l  -anchor w -pady 2  \
1750        4,1 $inner.scaleMode    -anchor w -pady 2  -fill x \
1751        5,0 $inner.gscale_l     -anchor w -pady 2 \
1752        5,1 $inner.gscale       -anchor w -pady 2  -fill x \
1753        6,0 $inner.normscale    -anchor w -pady 2 -cspan 2 \
1754        7,0 $inner.gorient      -anchor w -pady 2 -cspan 2 \
1755        8,0 $inner.wireframe    -anchor w -pady 2 -cspan 2 \
1756        9,0 $inner.lighting     -anchor w -pady 2 -cspan 2 \
1757        10,0 $inner.edges       -anchor w -pady 2 -cspan 2 \
1758        11,0 $inner.outline     -anchor w -pady 2 -cspan 2 \
1759        12,0 $inner.legend      -anchor w -pady 2 \
1760        13,0 $inner.opacity_l   -anchor w -pady 2 \
1761        13,1 $inner.opacity     -fill x   -pady 2 -fill x \
1762
1763    blt::table configure $inner r* c* -resize none
1764    blt::table configure $inner r14 c1 -resize expand
1765}
1766
1767itcl::body Rappture::VtkGlyphViewer::BuildAxisTab {} {
1768
1769    set fg [option get $itk_component(hull) font Font]
1770    #set bfg [option get $itk_component(hull) boldFont Font]
1771
1772    set inner [$itk_component(main) insert end \
1773        -title "Axis Settings" \
1774        -icon [Rappture::icon axis2]]
1775    $inner configure -borderwidth 4
1776
1777    checkbutton $inner.visible \
1778        -text "Axes" \
1779        -variable [itcl::scope _settings(-axesvisible)] \
1780        -command [itcl::code $this AdjustSetting -axesvisible] \
1781        -font "Arial 9"
1782
1783    checkbutton $inner.labels \
1784        -text "Axis Labels" \
1785        -variable [itcl::scope _settings(-axislabels)] \
1786        -command [itcl::code $this AdjustSetting -axislabels] \
1787        -font "Arial 9"
1788    label $inner.grid_l -text "Grid" -font "Arial 9"
1789    checkbutton $inner.xgrid \
1790        -text "X" \
1791        -variable [itcl::scope _settings(-xgrid)] \
1792        -command [itcl::code $this AdjustSetting -xgrid] \
1793        -font "Arial 9"
1794    checkbutton $inner.ygrid \
1795        -text "Y" \
1796        -variable [itcl::scope _settings(-ygrid)] \
1797        -command [itcl::code $this AdjustSetting -ygrid] \
1798        -font "Arial 9"
1799    checkbutton $inner.zgrid \
1800        -text "Z" \
1801        -variable [itcl::scope _settings(-zgrid)] \
1802        -command [itcl::code $this AdjustSetting -zgrid] \
1803        -font "Arial 9"
1804    checkbutton $inner.minorticks \
1805        -text "Minor Ticks" \
1806        -variable [itcl::scope _settings(-axisminorticks)] \
1807        -command [itcl::code $this AdjustSetting -axisminorticks] \
1808        -font "Arial 9"
1809
1810    label $inner.mode_l -text "Mode" -font "Arial 9"
1811
1812    itk_component add axisMode {
1813        Rappture::Combobox $inner.mode -width 10 -editable no
1814    }
1815    $inner.mode choices insert end \
1816        "static_triad"    "static" \
1817        "closest_triad"   "closest" \
1818        "furthest_triad"  "farthest" \
1819        "outer_edges"     "outer"         
1820    $itk_component(axisMode) value $_settings(-axismode)
1821    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting -axismode]
1822
1823    blt::table $inner \
1824        0,0 $inner.visible    -anchor w -cspan 4 \
1825        1,0 $inner.labels     -anchor w -cspan 4 \
1826        2,0 $inner.minorticks -anchor w -cspan 4 \
1827        4,0 $inner.grid_l     -anchor w \
1828        4,1 $inner.xgrid      -anchor w \
1829        4,2 $inner.ygrid      -anchor w \
1830        4,3 $inner.zgrid      -anchor w \
1831        5,0 $inner.mode_l     -anchor w -padx { 2 0 } \
1832        5,1 $inner.mode       -fill x   -cspan 3
1833
1834    blt::table configure $inner r* c* -resize none
1835    blt::table configure $inner r7 c6 -resize expand
1836    blt::table configure $inner r3 -height 0.125i
1837}
1838
1839itcl::body Rappture::VtkGlyphViewer::BuildCameraTab {} {
1840    set inner [$itk_component(main) insert end \
1841        -title "Camera Settings" \
1842        -icon [Rappture::icon camera]]
1843    $inner configure -borderwidth 4
1844
1845    label $inner.view_l -text "view" -font "Arial 9"
1846    set f [frame $inner.view]
1847    foreach side { front back left right top bottom } {
1848        button $f.$side  -image [Rappture::icon view$side] \
1849            -command [itcl::code $this SetOrientation $side]
1850        Rappture::Tooltip::for $f.$side "Change the view to $side"
1851        pack $f.$side -side left
1852    }
1853
1854    blt::table $inner \
1855        0,0 $inner.view_l -anchor e -pady 2 \
1856        0,1 $inner.view -anchor w -pady 2
1857    blt::table configure $inner r0 -resize none
1858
1859    set labels { qx qy qz qw xpan ypan zoom }
1860    set row 1
1861    foreach tag $labels {
1862        label $inner.${tag}label -text $tag -font "Arial 9"
1863        entry $inner.${tag} -font "Arial 9"  -bg white \
1864            -textvariable [itcl::scope _view(-$tag)]
1865        bind $inner.${tag} <Return> \
1866            [itcl::code $this camera set -${tag}]
1867        bind $inner.${tag} <KP_Enter> \
1868            [itcl::code $this camera set -${tag}]
1869        blt::table $inner \
1870            $row,0 $inner.${tag}label -anchor e -pady 2 \
1871            $row,1 $inner.${tag} -anchor w -pady 2
1872        blt::table configure $inner r$row -resize none
1873        incr row
1874    }
1875    checkbutton $inner.ortho \
1876        -text "Orthographic Projection" \
1877        -variable [itcl::scope _view(-ortho)] \
1878        -command [itcl::code $this camera set -ortho] \
1879        -font "Arial 9"
1880    blt::table $inner \
1881            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1882    blt::table configure $inner r$row -resize none
1883    incr row
1884
1885    blt::table configure $inner c* -resize none
1886    blt::table configure $inner c2 -resize expand
1887    blt::table configure $inner r$row -resize expand
1888}
1889
1890itcl::body Rappture::VtkGlyphViewer::BuildCutplaneTab {} {
1891
1892    set fg [option get $itk_component(hull) font Font]
1893   
1894    set inner [$itk_component(main) insert end \
1895        -title "Cutplane Settings" \
1896        -icon [Rappture::icon cutbutton]]
1897
1898    $inner configure -borderwidth 4
1899
1900    checkbutton $inner.visible \
1901        -text "Cutplanes" \
1902        -variable [itcl::scope _settings(-cutplanevisible)] \
1903        -command [itcl::code $this AdjustSetting -cutplanevisible] \
1904        -font "Arial 9"
1905
1906    checkbutton $inner.wireframe \
1907        -text "Wireframe" \
1908        -variable [itcl::scope _settings(-cutplanewireframe)] \
1909        -command [itcl::code $this AdjustSetting -cutplanewireframe] \
1910        -font "Arial 9"
1911
1912    checkbutton $inner.lighting \
1913        -text "Enable Lighting" \
1914        -variable [itcl::scope _settings(-cutplanelighting)] \
1915        -command [itcl::code $this AdjustSetting -cutplanelighting] \
1916        -font "Arial 9"
1917
1918    checkbutton $inner.edges \
1919        -text "Edges" \
1920        -variable [itcl::scope _settings(-cutplaneedges)] \
1921        -command [itcl::code $this AdjustSetting -cutplaneedges] \
1922        -font "Arial 9"
1923
1924    checkbutton $inner.preinterp \
1925        -text "Interpolate Scalars" \
1926        -variable [itcl::scope _settings(-cutplanepreinterp)] \
1927        -command [itcl::code $this AdjustSetting -cutplanepreinterp] \
1928        -font "Arial 9"
1929
1930    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1931    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1932        -variable [itcl::scope _settings(-cutplaneopacity)] \
1933        -width 10 \
1934        -showvalue off \
1935        -command [itcl::code $this AdjustSetting -cutplaneopacity]
1936    $inner.opacity set $_settings(-cutplaneopacity)
1937
1938    # X-value slicer...
1939    itk_component add xbutton {
1940        Rappture::PushButton $inner.xbutton \
1941            -onimage [Rappture::icon x-cutplane-red] \
1942            -offimage [Rappture::icon x-cutplane-red] \
1943            -command [itcl::code $this AdjustSetting -cutplanexvisible] \
1944            -variable [itcl::scope _settings(-cutplanexvisible)] \
1945    }
1946    Rappture::Tooltip::for $itk_component(xbutton) \
1947        "Toggle the X-axis cutplane on/off"
1948    $itk_component(xbutton) select
1949    itk_component add xposition {
1950        ::scale $inner.xval -from 100 -to 0 \
1951            -width 10 -orient vertical -showvalue yes \
1952            -borderwidth 1 -highlightthickness 0 \
1953            -command [itcl::code $this EventuallySetCutplane x] \
1954            -variable [itcl::scope _settings(-cutplanexposition)] \
1955            -foreground red2 -font "Arial 9 bold"
1956    } {
1957        usual
1958        ignore -borderwidth -highlightthickness -foreground -font -background
1959    }
1960    # Set the default cutplane value before disabling the scale.
1961    $itk_component(xposition) set 50
1962    $itk_component(xposition) configure -state disabled
1963    Rappture::Tooltip::for $itk_component(xposition) \
1964        "@[itcl::code $this Slice tooltip x]"
1965
1966    # Y-value slicer...
1967    itk_component add ybutton {
1968        Rappture::PushButton $inner.ybutton \
1969            -onimage [Rappture::icon y-cutplane-green] \
1970            -offimage [Rappture::icon y-cutplane-green] \
1971            -command [itcl::code $this AdjustSetting -cutplaneyvisible] \
1972            -variable [itcl::scope _settings(-cutplaneyvisible)] \
1973    }
1974    Rappture::Tooltip::for $itk_component(ybutton) \
1975        "Toggle the Y-axis cutplane on/off"
1976    $itk_component(ybutton) select
1977
1978    itk_component add yposition {
1979        ::scale $inner.yval -from 100 -to 0 \
1980            -width 10 -orient vertical -showvalue yes \
1981            -borderwidth 1 -highlightthickness 0 \
1982            -command [itcl::code $this EventuallySetCutplane y] \
1983            -variable [itcl::scope _settings(-cutplaneyposition)] \
1984            -foreground green3 -font "Arial 9 bold"
1985    } {
1986        usual
1987        ignore -borderwidth -highlightthickness -foreground -font
1988    }
1989    Rappture::Tooltip::for $itk_component(yposition) \
1990        "@[itcl::code $this Slice tooltip y]"
1991    # Set the default cutplane value before disabling the scale.
1992    $itk_component(yposition) set 50
1993    $itk_component(yposition) configure -state disabled
1994
1995    # Z-value slicer...
1996    itk_component add zbutton {
1997        Rappture::PushButton $inner.zbutton \
1998            -onimage [Rappture::icon z-cutplane-blue] \
1999            -offimage [Rappture::icon z-cutplane-blue] \
2000            -command [itcl::code $this AdjustSetting -cutplanezvisible] \
2001            -variable [itcl::scope _settings(-cutplanezvisible)] \
2002    } {
2003        usual
2004        ignore -foreground
2005    }
2006    Rappture::Tooltip::for $itk_component(zbutton) \
2007        "Toggle the Z-axis cutplane on/off"
2008    $itk_component(zbutton) select
2009
2010    itk_component add zposition {
2011        ::scale $inner.zval -from 100 -to 0 \
2012            -width 10 -orient vertical -showvalue yes \
2013            -borderwidth 1 -highlightthickness 0 \
2014            -command [itcl::code $this EventuallySetCutplane z] \
2015            -variable [itcl::scope _settings(-cutplanezposition)] \
2016            -foreground blue3 -font "Arial 9 bold"
2017    } {
2018        usual
2019        ignore -borderwidth -highlightthickness -foreground -font
2020    }
2021    $itk_component(zposition) set 50
2022    $itk_component(zposition) configure -state disabled
2023    Rappture::Tooltip::for $itk_component(zposition) \
2024        "@[itcl::code $this Slice tooltip z]"
2025
2026    blt::table $inner \
2027        0,0 $inner.visible   -anchor w -pady 2 -cspan 3 \
2028        1,0 $inner.lighting  -anchor w -pady 2 -cspan 3 \
2029        2,0 $inner.wireframe -anchor w -pady 2 -cspan 3 \
2030        3,0 $inner.edges     -anchor w -pady 2 -cspan 3 \
2031        4,0 $inner.preinterp -anchor w -pady 2 -cspan 3 \
2032        5,0 $inner.opacity_l -anchor w -pady 2 -cspan 1 \
2033        5,1 $inner.opacity   -fill x   -pady 2 -cspan 3 \
2034        6,0 $inner.xbutton   -anchor w -padx 2 -pady 2 \
2035        7,0 $inner.ybutton   -anchor w -padx 2 -pady 2 \
2036        8,0 $inner.zbutton   -anchor w -padx 2 -pady 2 \
2037        6,1 $inner.xval      -fill y -rspan 4 \
2038        6,2 $inner.yval      -fill y -rspan 4 \
2039        6,3 $inner.zval      -fill y -rspan 4 \
2040
2041    blt::table configure $inner r* c* -resize none
2042    blt::table configure $inner r9 c4 -resize expand
2043}
2044
2045#
2046#  camera --
2047#
2048itcl::body Rappture::VtkGlyphViewer::camera {option args} {
2049    switch -- $option {
2050        "show" {
2051            puts [array get _view]
2052        }
2053        "set" {
2054            set what [lindex $args 0]
2055            set x $_view($what)
2056            set code [catch { string is double $x } result]
2057            if { $code != 0 || !$result } {
2058                return
2059            }
2060            switch -- $what {
2061                "-ortho" {
2062                    if {$_view($what)} {
2063                        SendCmd "camera mode ortho"
2064                    } else {
2065                        SendCmd "camera mode persp"
2066                    }
2067                }
2068                "-xpan" - "-ypan" {
2069                    PanCamera
2070                }
2071                "-qx" - "-qy" - "-qz" - "-qw" {
2072                    set q [ViewToQuaternion]
2073                    $_arcball quaternion $q
2074                    EventuallyRotate $q
2075                }
2076                "-zoom" {
2077                    SendCmd "camera zoom $_view($what)"
2078                }
2079             }
2080        }
2081    }
2082}
2083
2084itcl::body Rappture::VtkGlyphViewer::GetVtkData { args } {
2085    set bytes ""
2086    foreach dataobj [get] {
2087        foreach cname [$dataobj components] {
2088            set tag $dataobj-$cname
2089            set contents [$dataobj vtkdata $cname]
2090            append bytes "$contents\n"
2091        }
2092    }
2093    return [list .vtk $bytes]
2094}
2095
2096itcl::body Rappture::VtkGlyphViewer::GetImage { args } {
2097    if { [image width $_image(download)] > 0 &&
2098         [image height $_image(download)] > 0 } {
2099        set bytes [$_image(download) data -format "jpeg -quality 100"]
2100        set bytes [Rappture::encoding::decode -as b64 $bytes]
2101        return [list .jpg $bytes]
2102    }
2103    return ""
2104}
2105
2106itcl::body Rappture::VtkGlyphViewer::BuildDownloadPopup { popup command } {
2107    Rappture::Balloon $popup \
2108        -title "[Rappture::filexfer::label downloadWord] as..."
2109    set inner [$popup component inner]
2110    label $inner.summary -text "" -anchor w
2111    radiobutton $inner.vtk_button -text "VTK data file" \
2112        -variable [itcl::scope _downloadPopup(format)] \
2113        -font "Arial 9 " \
2114        -value vtk 
2115    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2116    radiobutton $inner.image_button -text "Image File" \
2117        -variable [itcl::scope _downloadPopup(format)] \
2118        -font "Arial 9 " \
2119        -value image
2120    Rappture::Tooltip::for $inner.image_button \
2121        "Save as digital image."
2122
2123    button $inner.ok -text "Save" \
2124        -highlightthickness 0 -pady 2 -padx 3 \
2125        -command $command \
2126        -compound left \
2127        -image [Rappture::icon download]
2128
2129    button $inner.cancel -text "Cancel" \
2130        -highlightthickness 0 -pady 2 -padx 3 \
2131        -command [list $popup deactivate] \
2132        -compound left \
2133        -image [Rappture::icon cancel]
2134
2135    blt::table $inner \
2136        0,0 $inner.summary -cspan 2  \
2137        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2138        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2139        4,1 $inner.cancel -width .9i -fill y \
2140        4,0 $inner.ok -padx 2 -width .9i -fill y
2141    blt::table configure $inner r3 -height 4
2142    blt::table configure $inner r4 -pady 4
2143    raise $inner.image_button
2144    $inner.vtk_button invoke
2145    return $inner
2146}
2147
2148itcl::body Rappture::VtkGlyphViewer::SetObjectStyle { dataobj comp } {
2149    # Parse style string.
2150    set tag $dataobj-$comp
2151    array set style {
2152        -color white
2153        -colormap BCGYR
2154        -colormode vmag
2155        -edgecolor black
2156        -edges 0
2157        -gscale 1
2158        -lighting 1
2159        -linewidth 1.0
2160        -normscale 1
2161        -opacity 1.0
2162        -orientglyphs 1
2163        -outline 0
2164        -ptsize 1.0
2165        -quality 1
2166        -scalemode vmag
2167        -shape arrow
2168        -wireframe 0
2169    }
2170    set $style(-color) $itk_option(-plotforeground)
2171    set numComponents [$dataobj numComponents $comp]
2172    if {$numComponents == 3} {
2173        set style(-shape) "arrow"
2174        set style(-orientglyphs) 1
2175        set style(-scalemode) "vmag"
2176        set style(-colormode) "vmag"
2177    } else {
2178        set style(-shape) "sphere"
2179        set style(-orientglyphs) 0
2180        set style(-scalemode) "scalar"
2181        set style(-colormode) "scalar"
2182    }
2183    array set style [$dataobj style $comp]
2184    # Backwards compat with camel case style option
2185    if { [info exists style(-orientGlyphs)] } {
2186        set style(-orientglyphs) $style(-orientGlyphs)
2187        array unset style -orientGlyphs
2188    }
2189    # Backwards compat with camel case style option
2190    if { [info exists style(-scaleMode)] } {
2191        set style(-scalemode) $style(-scaleMode)
2192        array unset style -scaleMode
2193    }
2194    if { $dataobj != $_first } {
2195        set style(-opacity) 1
2196    }
2197    if 0 {
2198    SendCmd "cutplane add $tag"
2199    SendCmd "cutplane visible 0 $tag"
2200    }
2201    # This is too complicated.  We want to set the colormap, number of
2202    # glyph and opacity for the dataset.  They can be the default values,
2203    # the style hints loaded with the dataset, or set by user controls.  As
2204    # datasets get loaded, they first use the defaults that are overidden
2205    # by the style hints.  If the user changes the global controls, then that
2206    # overrides everything else.  I don't know what it means when global
2207    # controls are specified as style hints by each dataset.  It complicates
2208    # the code to handle aberrant cases.
2209
2210    if { $_changed(-glyphopacity) } {
2211        set style(-opacity) [expr $_settings(-glyphopacity) * 0.01]
2212    }
2213    if { $_changed(-colormap) } {
2214        set style(-colormap) $_settings(-colormap)
2215    }
2216    if { $_currentColormap == "" } {
2217        $itk_component(colormap) value $style(-colormap)
2218    }
2219
2220    SendCmd "outline add $tag"
2221    SendCmd "outline color [Color2RGB $style(-color)] $tag"
2222    SendCmd "outline visible $style(-outline) $tag"
2223    set _settings(-glyphoutline) $style(-outline)
2224
2225    SendCmd "glyphs add $style(-shape) $tag"
2226    set _settings(-glyphshape) $style(-shape)
2227    $itk_component(gshape) value $style(-shape)
2228    SendCmd "glyphs edges $style(-edges) $tag"
2229    set _settings(-glyphedges) $style(-edges)
2230
2231    # normscale=1 and gscale=1 are defaults
2232    if {$style(-normscale) != 1} {
2233        SendCmd "glyphs normscale $style(-normscale) $tag"
2234    }
2235    if {$style(-gscale) != 1} {
2236        SendCmd "glyphs gscale $style(-gscale) $tag"
2237    }
2238    set _settings(-glyphnormscale) $style(-normscale)
2239    set _settings(-glyphscale) $style(-gscale)
2240
2241    if {$style(-colormode) == "constant" || $style(-colormap) == "none"} {
2242        SendCmd "glyphs colormode constant {} $tag"
2243        set _settings(-colormapvisible) 0
2244        set _settings(-colormap) "none"
2245    } else {
2246        SendCmd "glyphs colormode $style(-colormode) $_curFldName $tag"
2247        set _settings(-colormapvisible) 1
2248        set _settings(-colormap) $style(-colormap)
2249        SetCurrentColormap $style(-colormap)
2250    }
2251    $itk_component(colormap) value $_settings(-colormap)
2252    set _colorMode $style(-colormode)
2253    # constant color only used if colormode set to constant
2254    SendCmd "glyphs color [Color2RGB $style(-color)] $tag"
2255    # Omitting field name for gorient and smode commands
2256    # defaults to active scalars or vectors depending on mode
2257    SendCmd "glyphs gorient $style(-orientglyphs) {} $tag"
2258    set _settings(-glyphorient) $style(-orientglyphs)
2259    SendCmd "glyphs smode $style(-scalemode) {} $tag"
2260    set _settings(-glyphscalemode) $style(-scalemode)
2261    $itk_component(scaleMode) value "[$itk_component(scaleMode) label $style(-scalemode)]"
2262    SendCmd "glyphs quality $style(-quality) $tag"
2263    SendCmd "glyphs lighting $style(-lighting) $tag"
2264    set _settings(-glyphlighting) $style(-lighting)
2265    SendCmd "glyphs linecolor [Color2RGB $style(-edgecolor)] $tag"
2266    SendCmd "glyphs linewidth $style(-linewidth) $tag"
2267    SendCmd "glyphs ptsize $style(-ptsize) $tag"
2268    SendCmd "glyphs opacity $style(-opacity) $tag"
2269    set _settings(-glyphopacity) [expr $style(-opacity) * 100.0]
2270    SendCmd "glyphs wireframe $style(-wireframe) $tag"
2271    set _settings(-glyphwireframe) $style(-wireframe)
2272}
2273
2274itcl::body Rappture::VtkGlyphViewer::IsValidObject { dataobj } {
2275    if {[catch {$dataobj isa Rappture::Field} valid] != 0 || !$valid} {
2276        return 0
2277    }
2278    return 1
2279}
2280
2281#
2282# EnterLegend --
2283#
2284itcl::body Rappture::VtkGlyphViewer::EnterLegend { x y } {
2285    SetLegendTip $x $y
2286}
2287
2288#
2289# MotionLegend --
2290#
2291itcl::body Rappture::VtkGlyphViewer::MotionLegend { x y } {
2292    Rappture::Tooltip::tooltip cancel
2293    set c $itk_component(view)
2294    set cw [winfo width $c]
2295    set ch [winfo height $c]
2296    if { $x >= 0 && $x < $cw && $y >= 0 && $y < $ch } {
2297        SetLegendTip $x $y
2298    }
2299}
2300
2301#
2302# LeaveLegend --
2303#
2304itcl::body Rappture::VtkGlyphViewer::LeaveLegend { } {
2305    Rappture::Tooltip::tooltip cancel
2306    .rappturetooltip configure -icon ""
2307}
2308
2309#
2310# SetLegendTip --
2311#
2312itcl::body Rappture::VtkGlyphViewer::SetLegendTip { x y } {
2313    set fname $_curFldName
2314    set c $itk_component(view)
2315    set w [winfo width $c]
2316    set h [winfo height $c]
2317
2318    set font "Arial 8"
2319    set lineht [font metrics $font -linespace]
2320   
2321    set ih [image height $_image(legend)]
2322    set iy [expr $y - ($lineht + 2)]
2323
2324    if { [string match "component*" $fname] } {
2325        set title ""
2326    } else {
2327        if { [info exists _fields($fname)] } {
2328            foreach { title units } $_fields($fname) break
2329            if { $units != "" } {
2330                set title [format "%s (%s)" $title $units]
2331            }
2332        } else {
2333            set title $fname
2334        }
2335    }
2336    # If there's a legend title, increase the offset by the line height.
2337    if { $title != "" } {
2338        incr iy -$lineht
2339    }
2340    # Make a swatch of the selected color
2341    if { [catch { $_image(legend) get 10 $iy } pixel] != 0 } {
2342        return
2343    }
2344    if { ![info exists _image(swatch)] } {
2345        set _image(swatch) [image create photo -width 24 -height 24]
2346    }
2347    set color [eval format "\#%02x%02x%02x" $pixel]
2348    $_image(swatch) put black  -to 0 0 23 23
2349    $_image(swatch) put $color -to 1 1 22 22
2350    .rappturetooltip configure -icon $_image(swatch)
2351
2352    # Compute the value of the point
2353    if { [info exists _limits($_curFldName)] } {
2354        foreach { vmin vmax } $_limits($_curFldName) break
2355        set t [expr 1.0 - (double($iy) / double($ih-1))]
2356        set value [expr $t * ($vmax - $vmin) + $vmin]
2357    } else {
2358        set value 0.0
2359    }
2360    set tx [expr $x + 15]
2361    set ty [expr $y - 5]
2362    Rappture::Tooltip::text $c [format "$title %g" $value]
2363    Rappture::Tooltip::tooltip show $c +$tx,+$ty   
2364}
2365
2366# ----------------------------------------------------------------------
2367# USAGE: Slice move x|y|z <newval>
2368#
2369# Called automatically when the user drags the slider to move the
2370# cut plane that slices 3D data.  Gets the current value from the
2371# slider and moves the cut plane to the appropriate point in the
2372# data set.
2373# ----------------------------------------------------------------------
2374itcl::body Rappture::VtkGlyphViewer::Slice {option args} {
2375    switch -- $option {
2376        "move" {
2377            set axis [lindex $args 0]
2378            set newval [lindex $args 1]
2379            if {[llength $args] != 2} {
2380                error "wrong # args: should be \"Slice move x|y|z newval\""
2381            }
2382            set newpos [expr {0.01*$newval}]
2383            SendCmd "cutplane slice $axis $newpos"
2384        }
2385        "tooltip" {
2386            set axis [lindex $args 0]
2387            set val [$itk_component(${axis}position) get]
2388            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
2389        }
2390        default {
2391            error "bad option \"$option\": should be axis, move, or tooltip"
2392        }
2393    }
2394}
2395
2396#
2397# ReceiveLegend --
2398#
2399#    Invoked automatically whenever the "legend" command comes in from
2400#    the rendering server.  Indicates that binary image data with the
2401#    specified <size> will follow.
2402#
2403itcl::body Rappture::VtkGlyphViewer::ReceiveLegend { colormap title min max size } {
2404    #puts stderr "ReceiveLegend colormap=$colormap title=$title range=$min,$max size=$size"
2405    set _title $title
2406    regsub {\(mag\)} $title "" _title
2407    if { [IsConnected] } {
2408        set bytes [ReceiveBytes $size]
2409        if { ![info exists _image(legend)] } {
2410            set _image(legend) [image create photo]
2411        }
2412        $_image(legend) configure -data $bytes
2413        #puts stderr "read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
2414        if { [catch {DrawLegend} errs] != 0 } {
2415            global errorInfo
2416            puts stderr "errs=$errs errorInfo=$errorInfo"
2417        }
2418    }
2419}
2420
2421#
2422# DrawLegend --
2423#
2424#    Draws the legend in the own canvas on the right side of the plot area.
2425#
2426itcl::body Rappture::VtkGlyphViewer::DrawLegend {} {
2427    set fname $_curFldName
2428    set c $itk_component(view)
2429    set w [winfo width $c]
2430    set h [winfo height $c]
2431    set font "Arial 8"
2432    set lineht [font metrics $font -linespace]
2433   
2434    if { [string match "component*" $fname] } {
2435        set title ""
2436    } else {
2437        if { [info exists _fields($fname)] } {
2438            foreach { title units } $_fields($fname) break
2439            if { $units != "" } {
2440                set title [format "%s (%s)" $title $units]
2441            }
2442        } else {
2443            set title $fname
2444        }
2445    }
2446    set x [expr $w - 2]
2447    if { !$_settings(-legendvisible) } {
2448        $c delete legend
2449        return
2450    }
2451    if { [$c find withtag "legend"] == "" } {
2452        set y 2
2453        # If there's a legend title, create a text item for the title.
2454        $c create text $x $y \
2455            -anchor ne \
2456            -fill $itk_option(-plotforeground) -tags "title legend" \
2457            -font $font
2458        if { $title != "" } {
2459            incr y $lineht
2460        }
2461        $c create text $x $y \
2462            -anchor ne \
2463            -fill $itk_option(-plotforeground) -tags "vmax legend" \
2464            -font $font
2465        incr y $lineht
2466        $c create image $x $y \
2467            -anchor ne \
2468            -image $_image(legend) -tags "colormap legend"
2469        $c create rectangle $x $y 1 1 \
2470            -fill "" -outline "" -tags "sensor legend"
2471        $c create text $x [expr {$h-2}] \
2472            -anchor se \
2473            -fill $itk_option(-plotforeground) -tags "vmin legend" \
2474            -font $font
2475        $c bind sensor <Enter> [itcl::code $this EnterLegend %x %y]
2476        $c bind sensor <Leave> [itcl::code $this LeaveLegend]
2477        $c bind sensor <Motion> [itcl::code $this MotionLegend %x %y]
2478    }
2479    set x2 $x
2480    set iw [image width $_image(legend)]
2481    set ih [image height $_image(legend)]
2482    set x1 [expr $x2 - ($iw*12)/10]
2483
2484    $c bind title <ButtonPress> [itcl::code $this Combo post]
2485    $c bind title <Enter> [itcl::code $this Combo activate]
2486    $c bind title <Leave> [itcl::code $this Combo deactivate]
2487    # Reset the item coordinates according the current size of the plot.
2488    $c itemconfigure title -text $title
2489    if { [info exists _limits($_curFldName)] } {
2490        foreach { vmin vmax } $_limits($_curFldName) break
2491        $c itemconfigure vmin -text [format %g $vmin]
2492        $c itemconfigure vmax -text [format %g $vmax]
2493    }
2494    set y 2
2495    # If there's a legend title, move the title to the correct position
2496    if { $title != "" } {
2497        $c itemconfigure title -text $title
2498        $c coords title $x $y
2499        incr y $lineht
2500        $c raise title
2501    }
2502    $c coords vmax $x $y
2503    incr y $lineht
2504    $c coords colormap $x $y
2505    $c coords sensor [expr $x - $iw] $y $x [expr $y + $ih]
2506    $c raise sensor
2507    $c coords vmin $x [expr {$h - 2}]
2508}
2509
2510# ----------------------------------------------------------------------
2511# USAGE: _dropdown post
2512# USAGE: _dropdown unpost
2513# USAGE: _dropdown select
2514#
2515# Used internally to handle the dropdown list for this combobox.  The
2516# post/unpost options are invoked when the list is posted or unposted
2517# to manage the relief of the controlling button.  The select option
2518# is invoked whenever there is a selection from the list, to assign
2519# the value back to the gauge.
2520# ----------------------------------------------------------------------
2521itcl::body Rappture::VtkGlyphViewer::Combo {option} {
2522    set c $itk_component(view)
2523    switch -- $option {
2524        post {
2525            foreach { x1 y1 x2 y2 } [$c bbox title] break
2526            set cw [winfo width $itk_component(view)]
2527            set mw [winfo reqwidth $itk_component(fieldmenu)]
2528            set x1 [expr $cw - $mw]
2529            set x [expr $x1 + [winfo rootx $itk_component(view)]]
2530            set y [expr $y2 + [winfo rooty $itk_component(view)]]
2531            tk_popup $itk_component(fieldmenu) $x $y
2532        }
2533        activate {
2534            $c itemconfigure title -fill red
2535        }
2536        deactivate {
2537            $c itemconfigure title -fill $itk_option(-plotforeground)
2538        }
2539        invoke {
2540            $itk_component(field) value $_curFldLabel
2541            AdjustSetting -field
2542        }
2543        default {
2544            error "bad option \"$option\": should be post, unpost, select"
2545        }
2546    }
2547}
2548
2549#
2550# SetCurrentColormap --
2551#
2552itcl::body Rappture::VtkGlyphViewer::SetCurrentColormap { name } {
2553    # Keep track of the colormaps that we build.
2554    if { ![info exists _colormaps($name)] } {
2555        BuildColormap $name
2556        set _colormaps($name) 1
2557    }
2558    set _currentColormap $name
2559    SendCmd "glyphs colormap $_currentColormap"
2560}
2561
2562#
2563# BuildColormap --
2564#
2565#    Build the designated colormap on the server.
2566#
2567itcl::body Rappture::VtkGlyphViewer::BuildColormap { name } {
2568    set cmap [ColorsToColormap $name]
2569    if { [llength $cmap] == 0 } {
2570        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
2571    }
2572    set amap "0.0 1.0 1.0 1.0"
2573    SendCmd "colormap add $name { $cmap } { $amap }"
2574}
2575
2576itcl::body Rappture::VtkGlyphViewer::SetOrientation { side } {
2577    array set positions {
2578        front "1 0 0 0"
2579        back  "0 0 1 0"
2580        left  "0.707107 0 -0.707107 0"
2581        right "0.707107 0 0.707107 0"
2582        top   "0.707107 -0.707107 0 0"
2583        bottom "0.707107 0.707107 0 0"
2584    }
2585    foreach name { -qw -qx -qy -qz } value $positions($side) {
2586        set _view($name) $value
2587    }
2588    set q [ViewToQuaternion]
2589    $_arcball quaternion $q
2590    SendCmd "camera orient $q"
2591    SendCmd "camera reset"
2592    set _view(-xpan) 0
2593    set _view(-ypan) 0
2594    set _view(-zoom) 1.0
2595}
Note: See TracBrowser for help on using the repository browser.