source: tags/1.3.5/gui/scripts/vtkglyphviewer.tcl @ 4754

Last change on this file since 4754 was 4754, checked in by ldelgass, 7 years ago

merge r4668:4669 from trunk

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