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

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

Fix: glyph viewer shouldn't have cutplane button (not isosurface viewer)

File size: 82.3 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 "hub" [exec hostname]
781            lappend info "client" "vtkglyphviewer"
782            lappend info "user" $user
783            lappend info "session" $session
784            SendCmd "clientinfo [list $info]"
785        }
786
787        set w [winfo width $itk_component(view)]
788        set h [winfo height $itk_component(view)]
789        EventuallyResize $w $h
790    }
791    return $result
792}
793
794#
795# isconnected --
796#
797#       Indicates if we are currently connected to the visualization server.
798#
799itcl::body Rappture::VtkGlyphViewer::isconnected {} {
800    return [VisViewer::IsConnected]
801}
802
803#
804# disconnect --
805#
806itcl::body Rappture::VtkGlyphViewer::disconnect {} {
807    Disconnect
808    set _reset 1
809}
810
811#
812# Disconnect --
813#
814#       Clients use this method to disconnect from the current rendering
815#       server.
816#
817itcl::body Rappture::VtkGlyphViewer::Disconnect {} {
818    VisViewer::Disconnect
819
820    $_dispatcher cancel !rebuild
821    $_dispatcher cancel !resize
822    $_dispatcher cancel !rotate
823    $_dispatcher cancel !xcutplane
824    $_dispatcher cancel !ycutplane
825    $_dispatcher cancel !zcutplane
826    $_dispatcher cancel !legend
827    # disconnected -- no more data sitting on server
828    set _outbuf ""
829    array unset _datasets
830    array unset _data
831    array unset _colormaps
832    array unset _seeds
833    array unset _dataset2style
834    array unset _obj2datasets
835}
836
837# ----------------------------------------------------------------------
838# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
839#
840# Invoked automatically whenever the "image" command comes in from
841# the rendering server.  Indicates that binary image data with the
842# specified <size> will follow.
843# ----------------------------------------------------------------------
844itcl::body Rappture::VtkGlyphViewer::ReceiveImage { args } {
845    array set info {
846        -token "???"
847        -bytes 0
848        -type image
849    }
850    array set info $args
851    set bytes [ReceiveBytes $info(-bytes)]
852    if { $info(-type) == "image" } {
853        if 0 {
854            set f [open "last.ppm" "w"]
855            puts $f $bytes
856            close $f
857        }
858        $_image(plot) configure -data $bytes
859        set time [clock seconds]
860        set date [clock format $time]
861        #puts stderr "$date: received image [image width $_image(plot)]x[image height $_image(plot)] image>"       
862        if { $_start > 0 } {
863            set finish [clock clicks -milliseconds]
864            #puts stderr "round trip time [expr $finish -$_start] milliseconds"
865            set _start 0
866        }
867    } elseif { $info(type) == "print" } {
868        set tag $this-print-$info(-token)
869        set _hardcopy($tag) $bytes
870    }
871}
872
873#
874# ReceiveDataset --
875#
876itcl::body Rappture::VtkGlyphViewer::ReceiveDataset { args } {
877    if { ![isconnected] } {
878        return
879    }
880    set option [lindex $args 0]
881    switch -- $option {
882        "scalar" {
883            set option [lindex $args 1]
884            switch -- $option {
885                "world" {
886                    foreach { x y z value tag } [lrange $args 2 end] break
887                }
888                "pixel" {
889                    foreach { x y value tag } [lrange $args 2 end] break
890                }
891            }
892        }
893        "vector" {
894            set option [lindex $args 1]
895            switch -- $option {
896                "world" {
897                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
898                }
899                "pixel" {
900                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
901                }
902            }
903        }
904        "names" {
905            foreach { name } [lindex $args 1] {
906                #puts stderr "Dataset: $name"
907            }
908        }
909        default {
910            error "unknown dataset option \"$option\" from server"
911        }
912    }
913}
914
915# ----------------------------------------------------------------------
916# USAGE: Rebuild
917#
918# Called automatically whenever something changes that affects the
919# data in the widget.  Clears any existing data and rebuilds the
920# widget to display new data.
921# ----------------------------------------------------------------------
922itcl::body Rappture::VtkGlyphViewer::Rebuild {} {
923    set w [winfo width $itk_component(view)]
924    set h [winfo height $itk_component(view)]
925    if { $w < 2 || $h < 2 } {
926        $_dispatcher event -idle !rebuild
927        return
928    }
929
930    # Turn on buffering of commands to the server.  We don't want to
931    # be preempted by a server disconnect/reconnect (which automatically
932    # generates a new call to Rebuild).   
933    StartBufferingCommands
934
935    if { $_reset } {
936        set _width $w
937        set _height $h
938        $_arcball resize $w $h
939        DoResize
940        #
941        # Reset the camera and other view parameters
942        #
943        set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
944        $_arcball quaternion $q
945        if {$_view(ortho)} {
946            SendCmd "camera mode ortho"
947        } else {
948            SendCmd "camera mode persp"
949        }
950        DoRotate
951        PanCamera
952        set _first ""
953        InitSettings axisXGrid axisYGrid axisZGrid axisMode \
954            axesVisible axisLabelsVisible
955        foreach axis { x y z } {
956            SendCmd "axis lformat $axis %g"
957        }
958        StopBufferingCommands
959        SendCmd "imgflush"
960        StartBufferingCommands
961    }
962    set _first ""
963    SendCmd "dataset visible 0"
964    foreach dataobj [get -objects] {
965        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
966            set _first $dataobj
967        }
968        set _obj2datasets($dataobj) ""
969        foreach comp [$dataobj components] {
970            set tag $dataobj-$comp
971            if { ![info exists _datasets($tag)] } {
972                set bytes [$dataobj vtkdata $comp]
973                if 0 {
974                    set f [open "/tmp/glyph.vtk" "w"]
975                    puts $f $bytes
976                    close $f
977                }
978                set length [string length $bytes]
979                if { $_reportClientInfo }  {
980                    set info {}
981                    lappend info "tool_id"       [$dataobj hints toolId]
982                    lappend info "tool_name"     [$dataobj hints toolName]
983                    lappend info "tool_version"  [$dataobj hints toolRevision]
984                    lappend info "tool_title"    [$dataobj hints toolTitle]
985                    lappend info "dataset_label" [$dataobj hints label]
986                    lappend info "dataset_size"  $length
987                    lappend info "dataset_tag"   $tag
988                    SendCmd "clientinfo [list $info]"
989                }
990                append _outbuf "dataset add $tag data follows $length\n"
991                append _outbuf $bytes
992                set _datasets($tag) 1
993                SetObjectStyle $dataobj $comp
994            }
995            lappend _obj2datasets($dataobj) $tag
996            if { [info exists _obj2ovride($dataobj-raise)] } {
997                # Setting dataset visible enables outline
998                # and glyphs
999                SendCmd "dataset visible 1 $tag"
1000            }
1001        }
1002    }
1003
1004    if { $_first != "" } {
1005        $itk_component(field) choices delete 0 end
1006        $itk_component(fieldmenu) delete 0 end
1007        array unset _fields
1008        set _curFldName ""
1009        foreach cname [$_first components] {
1010            foreach fname [$_first fieldnames $cname] {
1011                if { [info exists _fields($fname)] } {
1012                    continue
1013                }
1014                foreach { label units components } \
1015                    [$_first fieldinfo $fname] break
1016                $itk_component(field) choices insert end "$fname" "$label"
1017                $itk_component(fieldmenu) add radiobutton -label "$label" \
1018                    -value $label -variable [itcl::scope _curFldLabel] \
1019                    -selectcolor red \
1020                    -activebackground $itk_option(-plotbackground) \
1021                    -activeforeground $itk_option(-plotforeground) \
1022                    -font "Arial 8" \
1023                    -command [itcl::code $this Combo invoke]
1024                set _fields($fname) [list $label $units $components]
1025                if { $_curFldName == "" } {
1026                    set _curFldName $fname
1027                    set _curFldLabel $label
1028                }
1029            }
1030        }
1031        $itk_component(field) value $_curFldLabel
1032    }
1033    InitSettings glyphOutline
1034        #cutplaneVisible
1035    if { $_reset } {
1036        # These are settings that rely on a dataset being loaded.
1037        InitSettings \
1038            glyphLighting \
1039            field \
1040            glyphEdges glyphLighting glyphOpacity \
1041            glyphWireframe
1042
1043        #cutplaneXPosition cutplaneYPosition cutplaneZPosition \
1044            cutplaneXVisible cutplaneYVisible cutplaneZVisible \
1045            cutplanePreinterp
1046
1047        Zoom reset
1048        foreach axis { x y z } {
1049            # Another problem fixed by a <view>. We looking into a data
1050            # object for the name of the axes. This should be global to
1051            # the viewer itself.
1052            set label [$_first hints ${axis}label]
1053            if { $label == "" } {
1054                set label [string toupper $axis]
1055            }
1056            # May be a space in the axis label.
1057            SendCmd [list axis name $axis $label]
1058        }
1059        if { [array size _fields] < 2 } {
1060            blt::table forget $itk_component(field) $itk_component(field_l)
1061        }
1062        set _reset 0
1063    }
1064
1065    # Actually write the commands to the server socket.  If it fails, we don't
1066    # care.  We're finished here.
1067    blt::busy hold $itk_component(hull)
1068    StopBufferingCommands;              # Turn off buffering and send commands.
1069    blt::busy release $itk_component(hull)
1070}
1071
1072# ----------------------------------------------------------------------
1073# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
1074#
1075# Returns a list of server IDs for the current datasets being displayed.  This
1076# is normally a single ID, but it might be a list of IDs if the current data
1077# object has multiple components.
1078# ----------------------------------------------------------------------
1079itcl::body Rappture::VtkGlyphViewer::CurrentDatasets {args} {
1080    set flag [lindex $args 0]
1081    switch -- $flag {
1082        "-all" {
1083            if { [llength $args] > 1 } {
1084                error "CurrentDatasets: can't specify dataobj after \"-all\""
1085            }
1086            set dlist [get -objects]
1087        }
1088        "-visible" {
1089            if { [llength $args] > 1 } {
1090                set dlist {}
1091                set args [lrange $args 1 end]
1092                foreach dataobj $args {
1093                    if { [info exists _obj2ovride($dataobj-raise)] } {
1094                        lappend dlist $dataobj
1095                    }
1096                }
1097            } else {
1098                set dlist [get -visible]
1099            }
1100        }           
1101        default {
1102            set dlist $args
1103        }
1104    }
1105    set rlist ""
1106    foreach dataobj $dlist {
1107        foreach comp [$dataobj components] {
1108            set tag $dataobj-$comp
1109            if { [info exists _datasets($tag)] && $_datasets($tag) } {
1110                lappend rlist $tag
1111            }
1112        }
1113    }
1114    return $rlist
1115}
1116
1117# ----------------------------------------------------------------------
1118# USAGE: Zoom in
1119# USAGE: Zoom out
1120# USAGE: Zoom reset
1121#
1122# Called automatically when the user clicks on one of the zoom
1123# controls for this widget.  Changes the zoom for the current view.
1124# ----------------------------------------------------------------------
1125itcl::body Rappture::VtkGlyphViewer::Zoom {option} {
1126    switch -- $option {
1127        "in" {
1128            set _view(zoom) [expr {$_view(zoom)*1.25}]
1129            SendCmd "camera zoom $_view(zoom)"
1130        }
1131        "out" {
1132            set _view(zoom) [expr {$_view(zoom)*0.8}]
1133            SendCmd "camera zoom $_view(zoom)"
1134        }
1135        "reset" {
1136            array set _view {
1137                qw     0.853553
1138                qx     -0.353553
1139                qy     0.353553
1140                qz     0.146447
1141                zoom   1.0
1142                xpan   0
1143                ypan   0
1144            }
1145            if { $_first != "" } {
1146                set location [$_first hints camera]
1147                if { $location != "" } {
1148                    array set _view $location
1149                }
1150            }
1151            set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1152            $_arcball quaternion $q
1153            DoRotate
1154            SendCmd "camera reset"
1155        }
1156    }
1157}
1158
1159itcl::body Rappture::VtkGlyphViewer::PanCamera {} {
1160    set x $_view(xpan)
1161    set y $_view(ypan)
1162    SendCmd "camera pan $x $y"
1163}
1164
1165
1166# ----------------------------------------------------------------------
1167# USAGE: Rotate click <x> <y>
1168# USAGE: Rotate drag <x> <y>
1169# USAGE: Rotate release <x> <y>
1170#
1171# Called automatically when the user clicks/drags/releases in the
1172# plot area.  Moves the plot according to the user's actions.
1173# ----------------------------------------------------------------------
1174itcl::body Rappture::VtkGlyphViewer::Rotate {option x y} {
1175    switch -- $option {
1176        "click" {
1177            $itk_component(view) configure -cursor fleur
1178            set _click(x) $x
1179            set _click(y) $y
1180        }
1181        "drag" {
1182            if {[array size _click] == 0} {
1183                Rotate click $x $y
1184            } else {
1185                set w [winfo width $itk_component(view)]
1186                set h [winfo height $itk_component(view)]
1187                if {$w <= 0 || $h <= 0} {
1188                    return
1189                }
1190
1191                if {[catch {
1192                    # this fails sometimes for no apparent reason
1193                    set dx [expr {double($x-$_click(x))/$w}]
1194                    set dy [expr {double($y-$_click(y))/$h}]
1195                }]} {
1196                    return
1197                }
1198                if { $dx == 0 && $dy == 0 } {
1199                    return
1200                }
1201                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1202                EventuallyRotate $q
1203                set _click(x) $x
1204                set _click(y) $y
1205            }
1206        }
1207        "release" {
1208            Rotate drag $x $y
1209            $itk_component(view) configure -cursor ""
1210            catch {unset _click}
1211        }
1212        default {
1213            error "bad option \"$option\": should be click, drag, release"
1214        }
1215    }
1216}
1217
1218itcl::body Rappture::VtkGlyphViewer::Pick {x y} {
1219    foreach tag [CurrentDatasets -visible] {
1220        SendCmdNoSplash "dataset getscalar pixel $x $y $tag"
1221    }
1222}
1223
1224# ----------------------------------------------------------------------
1225# USAGE: $this Pan click x y
1226#        $this Pan drag x y
1227#        $this Pan release x y
1228#
1229# Called automatically when the user clicks on one of the zoom
1230# controls for this widget.  Changes the zoom for the current view.
1231# ----------------------------------------------------------------------
1232itcl::body Rappture::VtkGlyphViewer::Pan {option x y} {
1233    switch -- $option {
1234        "set" {
1235            set w [winfo width $itk_component(view)]
1236            set h [winfo height $itk_component(view)]
1237            set x [expr $x / double($w)]
1238            set y [expr $y / double($h)]
1239            set _view(xpan) [expr $_view(xpan) + $x]
1240            set _view(ypan) [expr $_view(ypan) + $y]
1241            PanCamera
1242            return
1243        }
1244        "click" {
1245            set _click(x) $x
1246            set _click(y) $y
1247            $itk_component(view) configure -cursor hand1
1248        }
1249        "drag" {
1250            if { ![info exists _click(x)] } {
1251                set _click(x) $x
1252            }
1253            if { ![info exists _click(y)] } {
1254                set _click(y) $y
1255            }
1256            set w [winfo width $itk_component(view)]
1257            set h [winfo height $itk_component(view)]
1258            set dx [expr ($_click(x) - $x)/double($w)]
1259            set dy [expr ($_click(y) - $y)/double($h)]
1260            set _click(x) $x
1261            set _click(y) $y
1262            set _view(xpan) [expr $_view(xpan) - $dx]
1263            set _view(ypan) [expr $_view(ypan) - $dy]
1264            PanCamera
1265        }
1266        "release" {
1267            Pan drag $x $y
1268            $itk_component(view) configure -cursor ""
1269        }
1270        default {
1271            error "unknown option \"$option\": should set, click, drag, or release"
1272        }
1273    }
1274}
1275
1276# ----------------------------------------------------------------------
1277# USAGE: InitSettings <what> ?<value>?
1278#
1279# Used internally to update rendering settings whenever parameters
1280# change in the popup settings panel.  Sends the new settings off
1281# to the back end.
1282# ----------------------------------------------------------------------
1283itcl::body Rappture::VtkGlyphViewer::InitSettings { args } {
1284    foreach spec $args {
1285        if { [info exists _settings($_first-$spec)] } {
1286            # Reset global setting with dataobj specific setting
1287            set _settings($spec) $_settings($_first-$spec)
1288        }
1289        AdjustSetting $spec
1290    }
1291}
1292
1293#
1294# AdjustSetting --
1295#
1296#       Changes/updates a specific setting in the widget.  There are
1297#       usually user-setable option.  Commands are sent to the render
1298#       server.
1299#
1300itcl::body Rappture::VtkGlyphViewer::AdjustSetting {what {value ""}} {
1301    if { ![isconnected] } {
1302        return
1303    }
1304    switch -- $what {
1305        "background" {
1306            set bgcolor [$itk_component(background) value]
1307            array set fgcolors {
1308                "black" "white"
1309                "white" "black"
1310                "grey"  "black"
1311            }
1312            configure -plotbackground $bgcolor \
1313                -plotforeground $fgcolors($bgcolor)
1314            $itk_component(view) delete "legend"
1315            DrawLegend
1316        }
1317        "axesVisible" {
1318            set bool $_settings(axesVisible)
1319            SendCmd "axis visible all $bool"
1320        }
1321        "axisLabelsVisible" {
1322            set bool $_settings(axisLabelsVisible)
1323            SendCmd "axis labels all $bool"
1324        }
1325        "axisXGrid" - "axisYGrid" - "axisZGrid" {
1326            set axis [string tolower [string range $what 4 4]]
1327            set bool $_settings($what)
1328            SendCmd "axis grid $axis $bool"
1329        }
1330        "axisMode" {
1331            set mode [$itk_component(axisMode) value]
1332            set mode [$itk_component(axisMode) translate $mode]
1333            set _settings($what) $mode
1334            SendCmd "axis flymode $mode"
1335        }
1336        "cutplaneEdges" {
1337            set bool $_settings($what)
1338            SendCmd "cutplane edges $bool"
1339        }
1340        "cutplaneVisible" {
1341            set bool $_settings($what)
1342            SendCmd "cutplane visible $bool"
1343        }
1344        "cutplaneWireframe" {
1345            set bool $_settings($what)
1346            SendCmd "cutplane wireframe $bool"
1347        }
1348        "cutplaneLighting" {
1349            set bool $_settings($what)
1350            SendCmd "cutplane lighting $bool"
1351        }
1352        "cutplaneOpacity" {
1353            set val $_settings($what)
1354            set sval [expr { 0.01 * double($val) }]
1355            SendCmd "cutplane opacity $sval"
1356        }
1357        "cutplanePreinterp" {
1358            set bool $_settings($what)
1359            SendCmd "cutplane preinterp $bool"
1360        }
1361        "cutplaneXVisible" - "cutplaneYVisible" - "cutplaneZVisible" {
1362            set axis [string tolower [string range $what 8 8]]
1363            set bool $_settings($what)
1364            if { $bool } {
1365                $itk_component(${axis}position) configure -state normal \
1366                    -troughcolor white
1367            } else {
1368                $itk_component(${axis}position) configure -state disabled \
1369                    -troughcolor grey82
1370            }
1371            SendCmd "cutplane axis $axis $bool"
1372        }
1373        "cutplaneXPosition" - "cutplaneYPosition" - "cutplaneZPosition" {
1374            set axis [string tolower [string range $what 8 8]]
1375            set pos [expr $_settings($what) * 0.01]
1376            SendCmd "cutplane slice ${axis} ${pos}"
1377            set _cutplanePending 0
1378        }
1379        "colormap" {
1380            set _changed(colormap) 1
1381            StartBufferingCommands
1382            set color [$itk_component(colormap) value]
1383            set _settings(colormap) $color
1384            if { $color == "none" } {
1385                if { $_settings(colormapVisible) } {
1386                    SendCmd "glyphs colormode constant {}"
1387                    set _settings(colormapVisible) 0
1388                }
1389            } else {
1390                if { !$_settings(colormapVisible) } {
1391                    SendCmd "glyphs colormode $_colorMode $_curFldName"
1392                    set _settings(colormapVisible) 1
1393                }
1394                SetCurrentColormap $color
1395            }
1396            StopBufferingCommands
1397            EventuallyRequestLegend
1398        }
1399        "glyphWireframe" {
1400            set bool $_settings(glyphWireframe)
1401            SendCmd "glyphs wireframe $bool"
1402        }
1403        "glyphVisible" {
1404            set bool $_settings(glyphVisible)
1405            SendCmd "glyphs visible $bool"
1406            if { $bool } {
1407                Rappture::Tooltip::for $itk_component(glyphs) \
1408                    "Hide the glyph"
1409            } else {
1410                Rappture::Tooltip::for $itk_component(glyphs) \
1411                    "Show the glyph"
1412            }
1413            DrawLegend
1414        }
1415        "glyphLighting" {
1416            set bool $_settings(glyphLighting)
1417            SendCmd "glyphs lighting $bool"
1418        }
1419        "glyphEdges" {
1420            set bool $_settings(glyphEdges)
1421            SendCmd "glyphs edges $bool"
1422        }
1423        "glyphOutline" {
1424            set bool $_settings(glyphOutline)
1425            SendCmd "outline visible $bool"
1426        }
1427        "glyphOpacity" {
1428            set val $_settings(glyphOpacity)
1429            set sval [expr { 0.01 * double($val) }]
1430            SendCmd "glyphs opacity $sval"
1431        }
1432        "field" {
1433            set label [$itk_component(field) value]
1434            set fname [$itk_component(field) translate $label]
1435            set _settings(field) $fname
1436            if { [info exists _fields($fname)] } {
1437                foreach { label units components } $_fields($fname) break
1438                if { $components > 1 } {
1439                    set _colorMode vmag
1440                } else {
1441                    set _colorMode scalar
1442                }
1443                set _curFldName $fname
1444                set _curFldLabel $label
1445            } else {
1446                puts stderr "unknown field \"$fname\""
1447                return
1448            }
1449            #SendCmd "dataset maprange explicit $_limits($_curFldName) $_curFldName"
1450            #SendCmd "cutplane colormode $_colorMode $_curFldName"
1451            SendCmd "glyphs colormode $_colorMode $_curFldName"
1452            DrawLegend
1453        }
1454        "legendVisible" {
1455            if { !$_settings(legendVisible) } {
1456                $itk_component(view) delete legend
1457            }
1458            DrawLegend
1459        }
1460        default {
1461            error "don't know how to fix $what"
1462        }
1463    }
1464}
1465
1466
1467#
1468# RequestLegend --
1469#
1470#       Request a new legend from the server.  The size of the legend
1471#       is determined from the height of the canvas. 
1472#
1473# This should be called when
1474#       1.  A new current colormap is set.
1475#       2.  Window is resized.
1476#       3.  The limits of the data have changed.  (Just need a redraw).
1477#       4.  Number of glyph have changed. (Just need a redraw).
1478#       5.  Legend becomes visible (Just need a redraw).
1479#
1480itcl::body Rappture::VtkGlyphViewer::RequestLegend {} {
1481    set _legendPending 0
1482    if { ![info exists _fields($_curFldName)] } {
1483        return
1484    }
1485    set fname $_curFldName
1486    set font "Arial 8"
1487    set lineht [font metrics $font -linespace]
1488    set w 12
1489    set h [expr {$_height - 2 * ($lineht + 2)}]
1490    if { $h < 1 } {
1491        return
1492    }
1493    if { [string match "component*" $fname] } {
1494        set title ""
1495    } else {
1496        if { [info exists _fields($fname)] } {
1497            foreach { title units } $_fields($fname) break
1498            if { $units != "" } {
1499                set title [format "%s (%s)" $title $units]
1500            }
1501        } else {
1502            set title $fname
1503        }
1504    }
1505    # If there's a title too, substract one more line
1506    if { $title != "" } {
1507        incr h -$lineht
1508    }
1509    # Set the legend on the first heightmap dataset.
1510    if { $_currentColormap != ""  } {
1511        set cmap $_currentColormap
1512        SendCmdNoWait "legend $cmap $_colorMode $_curFldName {} $w $h 0"
1513    }
1514}
1515
1516# ----------------------------------------------------------------------
1517# CONFIGURATION OPTION: -plotbackground
1518# ----------------------------------------------------------------------
1519itcl::configbody Rappture::VtkGlyphViewer::plotbackground {
1520    if { [isconnected] } {
1521        set rgb [Color2RGB $itk_option(-plotbackground)]
1522        SendCmd "screen bgcolor $rgb"
1523    }
1524}
1525
1526# ----------------------------------------------------------------------
1527# CONFIGURATION OPTION: -plotforeground
1528# ----------------------------------------------------------------------
1529itcl::configbody Rappture::VtkGlyphViewer::plotforeground {
1530    if { [isconnected] } {
1531        set rgb [Color2RGB $itk_option(-plotforeground)]
1532        SendCmd "axis color all $rgb"
1533        SendCmd "outline color $rgb"
1534        #SendCmd "cutplane color $rgb"
1535    }
1536}
1537
1538itcl::body Rappture::VtkGlyphViewer::limits { dataobj } {
1539    foreach { limits(xmin) limits(xmax) } [$dataobj limits x] break
1540    foreach { limits(ymin) limits(ymax) } [$dataobj limits y] break
1541    foreach { limits(zmin) limits(zmax) } [$dataobj limits z] break
1542    foreach { limits(vmin) limits(vmax) } [$dataobj limits v] break
1543    return [array get limits]
1544}
1545
1546itcl::body Rappture::VtkGlyphViewer::BuildGlyphTab {} {
1547
1548    set fg [option get $itk_component(hull) font Font]
1549    #set bfg [option get $itk_component(hull) boldFont Font]
1550
1551    set inner [$itk_component(main) insert end \
1552        -title "Glyph Settings" \
1553        -icon [Rappture::icon volume-on]]
1554    $inner configure -borderwidth 4
1555
1556    checkbutton $inner.glyphs \
1557        -text "Glyphs" \
1558        -variable [itcl::scope _settings(glyphVisible)] \
1559        -command [itcl::code $this AdjustSetting glyphVisible] \
1560        -font "Arial 9"
1561
1562    checkbutton $inner.wireframe \
1563        -text "Wireframe" \
1564        -variable [itcl::scope _settings(glyphWireframe)] \
1565        -command [itcl::code $this AdjustSetting glyphWireframe] \
1566        -font "Arial 9"
1567
1568    checkbutton $inner.lighting \
1569        -text "Enable Lighting" \
1570        -variable [itcl::scope _settings(glyphLighting)] \
1571        -command [itcl::code $this AdjustSetting glyphLighting] \
1572        -font "Arial 9"
1573
1574    checkbutton $inner.edges \
1575        -text "Edges" \
1576        -variable [itcl::scope _settings(glyphEdges)] \
1577        -command [itcl::code $this AdjustSetting glyphEdges] \
1578        -font "Arial 9"
1579
1580    checkbutton $inner.outline \
1581        -text "Outline" \
1582        -variable [itcl::scope _settings(glyphOutline)] \
1583        -command [itcl::code $this AdjustSetting glyphOutline] \
1584        -font "Arial 9"
1585
1586    checkbutton $inner.legend \
1587        -text "Legend" \
1588        -variable [itcl::scope _settings(legendVisible)] \
1589        -command [itcl::code $this AdjustSetting legendVisible] \
1590        -font "Arial 9"
1591
1592    label $inner.background_l -text "Background" -font "Arial 9"
1593    itk_component add background {
1594        Rappture::Combobox $inner.background -width 10 -editable no
1595    }
1596    $inner.background choices insert end \
1597        "black"              "black"            \
1598        "white"              "white"            \
1599        "grey"               "grey"             
1600
1601    $itk_component(background) value $_settings(background)
1602    bind $inner.background <<Value>> [itcl::code $this AdjustSetting background]
1603
1604    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1605    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1606        -variable [itcl::scope _settings(glyphOpacity)] \
1607        -width 10 \
1608        -showvalue off \
1609        -command [itcl::code $this AdjustSetting glyphOpacity]
1610
1611    label $inner.scale_l -text "Scale" -font "Arial 9"
1612    ::scale $inner.scale -from 1 -to 100 -orient horizontal \
1613        -variable [itcl::scope _settings(glyphs-scale)] \
1614        -width 10 \
1615        -showvalue off \
1616        -command [itcl::code $this AdjustSetting glyphs-scale]
1617
1618    itk_component add field_l {
1619        label $inner.field_l -text "Color By" -font "Arial 9"
1620    } {
1621        ignore -font
1622    }
1623    itk_component add field {
1624        Rappture::Combobox $inner.field -width 10 -editable no
1625    }
1626    bind $inner.field <<Value>> \
1627        [itcl::code $this AdjustSetting field]
1628
1629    label $inner.colormap_l -text "Colormap" -font "Arial 9"
1630    itk_component add colormap {
1631        Rappture::Combobox $inner.colormap -width 10 -editable no
1632    }
1633    $inner.colormap choices insert end \
1634        "BCGYR"              "BCGYR"            \
1635        "BGYOR"              "BGYOR"            \
1636        "blue"               "blue"             \
1637        "blue-to-brown"      "blue-to-brown"    \
1638        "blue-to-orange"     "blue-to-orange"   \
1639        "blue-to-grey"       "blue-to-grey"     \
1640        "green-to-magenta"   "green-to-magenta" \
1641        "greyscale"          "greyscale"        \
1642        "nanohub"            "nanohub"          \
1643        "rainbow"            "rainbow"          \
1644        "spectral"           "spectral"         \
1645        "ROYGB"              "ROYGB"            \
1646        "RYGCB"              "RYGCB"            \
1647        "brown-to-blue"      "brown-to-blue"    \
1648        "grey-to-blue"       "grey-to-blue"     \
1649        "orange-to-blue"     "orange-to-blue"   
1650
1651    $itk_component(colormap) value "BCGYR"
1652    bind $inner.colormap <<Value>> \
1653        [itcl::code $this AdjustSetting colormap]
1654
1655    blt::table $inner \
1656        0,0 $inner.field_l   -anchor w -pady 2  \
1657        0,1 $inner.field     -anchor w -pady 2  -fill x \
1658        1,0 $inner.colormap_l -anchor w -pady 2  \
1659        1,1 $inner.colormap   -anchor w -pady 2  -fill x \
1660        3,0 $inner.background_l -anchor w -pady 2 \
1661        3,1 $inner.background -anchor w -pady 2  -fill x \
1662        5,0 $inner.wireframe -anchor w -pady 2 -cspan 2 \
1663        6,0 $inner.lighting  -anchor w -pady 2 -cspan 2 \
1664        7,0 $inner.edges     -anchor w -pady 2 -cspan 2 \
1665        8,0 $inner.outline   -anchor w -pady 2 -cspan 2 \
1666        9,0 $inner.legend    -anchor w -pady 2 \
1667        10,0 $inner.opacity_l -anchor w -pady 2 \
1668        10,1 $inner.opacity   -fill x   -pady 2 -fill x \
1669
1670    blt::table configure $inner r* c* -resize none
1671    blt::table configure $inner r11 c1 -resize expand
1672}
1673
1674itcl::body Rappture::VtkGlyphViewer::BuildAxisTab {} {
1675
1676    set fg [option get $itk_component(hull) font Font]
1677    #set bfg [option get $itk_component(hull) boldFont Font]
1678
1679    set inner [$itk_component(main) insert end \
1680        -title "Axis Settings" \
1681        -icon [Rappture::icon axis2]]
1682    $inner configure -borderwidth 4
1683
1684    checkbutton $inner.visible \
1685        -text "Show Axes" \
1686        -variable [itcl::scope _settings(axesVisible)] \
1687        -command [itcl::code $this AdjustSetting axesVisible] \
1688        -font "Arial 9"
1689
1690    checkbutton $inner.labels \
1691        -text "Show Axis Labels" \
1692        -variable [itcl::scope _settings(axisLabelsVisible)] \
1693        -command [itcl::code $this AdjustSetting axisLabelsVisible] \
1694        -font "Arial 9"
1695
1696    checkbutton $inner.gridx \
1697        -text "Show X Grid" \
1698        -variable [itcl::scope _settings(axisXGrid)] \
1699        -command [itcl::code $this AdjustSetting axisXGrid] \
1700        -font "Arial 9"
1701    checkbutton $inner.gridy \
1702        -text "Show Y Grid" \
1703        -variable [itcl::scope _settings(axisYGrid)] \
1704        -command [itcl::code $this AdjustSetting axisYGrid] \
1705        -font "Arial 9"
1706    checkbutton $inner.gridz \
1707        -text "Show Z Grid" \
1708        -variable [itcl::scope _settings(axisZGrid)] \
1709        -command [itcl::code $this AdjustSetting axisZGrid] \
1710        -font "Arial 9"
1711
1712    label $inner.mode_l -text "Mode" -font "Arial 9"
1713
1714    itk_component add axisMode {
1715        Rappture::Combobox $inner.mode -width 10 -editable no
1716    }
1717    $inner.mode choices insert end \
1718        "static_triad"    "static" \
1719        "closest_triad"   "closest" \
1720        "furthest_triad"  "farthest" \
1721        "outer_edges"     "outer"         
1722    $itk_component(axisMode) value "static"
1723    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting axisMode]
1724
1725    blt::table $inner \
1726        0,0 $inner.visible -anchor w -cspan 2 \
1727        1,0 $inner.labels  -anchor w -cspan 2 \
1728        2,0 $inner.gridx   -anchor w -cspan 2 \
1729        3,0 $inner.gridy   -anchor w -cspan 2 \
1730        4,0 $inner.gridz   -anchor w -cspan 2 \
1731        5,0 $inner.mode_l  -anchor w -cspan 2 -padx { 2 0 } \
1732        6,0 $inner.mode    -fill x   -cspan 2
1733
1734    blt::table configure $inner r* c* -resize none
1735    blt::table configure $inner r7 c1 -resize expand
1736}
1737
1738
1739itcl::body Rappture::VtkGlyphViewer::BuildCameraTab {} {
1740    set inner [$itk_component(main) insert end \
1741        -title "Camera Settings" \
1742        -icon [Rappture::icon camera]]
1743    $inner configure -borderwidth 4
1744
1745    label $inner.view_l -text "view" -font "Arial 9"
1746    set f [frame $inner.view]
1747    foreach side { front back left right top bottom } {
1748        button $f.$side  -image [Rappture::icon view$side] \
1749            -command [itcl::code $this SetOrientation $side]
1750        Rappture::Tooltip::for $f.$side "Change the view to $side"
1751        pack $f.$side -side left
1752    }
1753
1754    blt::table $inner \
1755        0,0 $inner.view_l -anchor e -pady 2 \
1756        0,1 $inner.view -anchor w -pady 2
1757
1758    set labels { qx qy qz qw xpan ypan zoom }
1759    set row 1
1760    foreach tag $labels {
1761        label $inner.${tag}label -text $tag -font "Arial 9"
1762        entry $inner.${tag} -font "Arial 9"  -bg white \
1763            -textvariable [itcl::scope _view($tag)]
1764        bind $inner.${tag} <KeyPress-Return> \
1765            [itcl::code $this camera set ${tag}]
1766        blt::table $inner \
1767            $row,0 $inner.${tag}label -anchor e -pady 2 \
1768            $row,1 $inner.${tag} -anchor w -pady 2
1769        blt::table configure $inner r$row -resize none
1770        incr row
1771    }
1772    checkbutton $inner.ortho \
1773        -text "Orthographic Projection" \
1774        -variable [itcl::scope _view(ortho)] \
1775        -command [itcl::code $this camera set ortho] \
1776        -font "Arial 9"
1777    blt::table $inner \
1778            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1779    blt::table configure $inner r$row -resize none
1780    incr row
1781
1782    blt::table configure $inner c* r* -resize none
1783    blt::table configure $inner c2 -resize expand
1784    blt::table configure $inner r$row -resize expand
1785}
1786
1787itcl::body Rappture::VtkGlyphViewer::BuildCutplaneTab {} {
1788
1789    set fg [option get $itk_component(hull) font Font]
1790   
1791    set inner [$itk_component(main) insert end \
1792        -title "Cutplane Settings" \
1793        -icon [Rappture::icon cutbutton]]
1794
1795    $inner configure -borderwidth 4
1796
1797    checkbutton $inner.visible \
1798        -text "Cutplanes" \
1799        -variable [itcl::scope _settings(cutplaneVisible)] \
1800        -command [itcl::code $this AdjustSetting cutplaneVisible] \
1801        -font "Arial 9"
1802
1803    checkbutton $inner.wireframe \
1804        -text "Wireframe" \
1805        -variable [itcl::scope _settings(cutplaneWireframe)] \
1806        -command [itcl::code $this AdjustSetting cutplaneWireframe] \
1807        -font "Arial 9"
1808
1809    checkbutton $inner.lighting \
1810        -text "Enable Lighting" \
1811        -variable [itcl::scope _settings(cutplaneLighting)] \
1812        -command [itcl::code $this AdjustSetting cutplaneLighting] \
1813        -font "Arial 9"
1814
1815    checkbutton $inner.edges \
1816        -text "Edges" \
1817        -variable [itcl::scope _settings(cutplaneEdges)] \
1818        -command [itcl::code $this AdjustSetting cutplaneEdges] \
1819        -font "Arial 9"
1820
1821    checkbutton $inner.preinterp \
1822        -text "Interpolate Scalars" \
1823        -variable [itcl::scope _settings(cutplanePreinterp)] \
1824        -command [itcl::code $this AdjustSetting cutplanePreinterp] \
1825        -font "Arial 9"
1826
1827    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1828    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1829        -variable [itcl::scope _settings(cutplaneOpacity)] \
1830        -width 10 \
1831        -showvalue off \
1832        -command [itcl::code $this AdjustSetting cutplaneOpacity]
1833    $inner.opacity set $_settings(cutplaneOpacity)
1834
1835    # X-value slicer...
1836    itk_component add xbutton {
1837        Rappture::PushButton $inner.xbutton \
1838            -onimage [Rappture::icon x-cutplane-red] \
1839            -offimage [Rappture::icon x-cutplane-red] \
1840            -command [itcl::code $this AdjustSetting cutplaneXVisible] \
1841            -variable [itcl::scope _settings(cutplaneXVisible)] \
1842    }
1843    Rappture::Tooltip::for $itk_component(xbutton) \
1844        "Toggle the X-axis cutplane on/off"
1845    $itk_component(xbutton) select
1846    itk_component add xposition {
1847        ::scale $inner.xval -from 100 -to 0 \
1848            -width 10 -orient vertical -showvalue yes \
1849            -borderwidth 1 -highlightthickness 0 \
1850            -command [itcl::code $this EventuallySetCutplane x] \
1851            -variable [itcl::scope _settings(cutplaneXPosition)] \
1852            -foreground red2 -font "Arial 9 bold"
1853    } {
1854        usual
1855        ignore -borderwidth -highlightthickness -foreground -font -background
1856    }
1857    # Set the default cutplane value before disabling the scale.
1858    $itk_component(xposition) set 50
1859    $itk_component(xposition) configure -state disabled
1860    Rappture::Tooltip::for $itk_component(xposition) \
1861        "@[itcl::code $this Slice tooltip x]"
1862
1863    # Y-value slicer...
1864    itk_component add ybutton {
1865        Rappture::PushButton $inner.ybutton \
1866            -onimage [Rappture::icon y-cutplane-green] \
1867            -offimage [Rappture::icon y-cutplane-green] \
1868            -command [itcl::code $this AdjustSetting cutplaneYVisible] \
1869            -variable [itcl::scope _settings(cutplaneYVisible)] \
1870    }
1871    Rappture::Tooltip::for $itk_component(ybutton) \
1872        "Toggle the Y-axis cutplane on/off"
1873    $itk_component(ybutton) select
1874
1875    itk_component add yposition {
1876        ::scale $inner.yval -from 100 -to 0 \
1877            -width 10 -orient vertical -showvalue yes \
1878            -borderwidth 1 -highlightthickness 0 \
1879            -command [itcl::code $this EventuallySetCutplane y] \
1880            -variable [itcl::scope _settings(cutplaneYPosition)] \
1881            -foreground green3 -font "Arial 9 bold"
1882    } {
1883        usual
1884        ignore -borderwidth -highlightthickness -foreground -font
1885    }
1886    Rappture::Tooltip::for $itk_component(yposition) \
1887        "@[itcl::code $this Slice tooltip y]"
1888    # Set the default cutplane value before disabling the scale.
1889    $itk_component(yposition) set 50
1890    $itk_component(yposition) configure -state disabled
1891
1892    # Z-value slicer...
1893    itk_component add zbutton {
1894        Rappture::PushButton $inner.zbutton \
1895            -onimage [Rappture::icon z-cutplane-blue] \
1896            -offimage [Rappture::icon z-cutplane-blue] \
1897            -command [itcl::code $this AdjustSetting cutplaneZVisible] \
1898            -variable [itcl::scope _settings(cutplaneZVisible)] \
1899    } {
1900        usual
1901        ignore -foreground
1902    }
1903    Rappture::Tooltip::for $itk_component(zbutton) \
1904        "Toggle the Z-axis cutplane on/off"
1905    $itk_component(zbutton) select
1906
1907    itk_component add zposition {
1908        ::scale $inner.zval -from 100 -to 0 \
1909            -width 10 -orient vertical -showvalue yes \
1910            -borderwidth 1 -highlightthickness 0 \
1911            -command [itcl::code $this EventuallySetCutplane z] \
1912            -variable [itcl::scope _settings(cutplaneZPosition)] \
1913            -foreground blue3 -font "Arial 9 bold"
1914    } {
1915        usual
1916        ignore -borderwidth -highlightthickness -foreground -font
1917    }
1918    $itk_component(zposition) set 50
1919    $itk_component(zposition) configure -state disabled
1920    Rappture::Tooltip::for $itk_component(zposition) \
1921        "@[itcl::code $this Slice tooltip z]"
1922
1923    blt::table $inner \
1924        0,0 $inner.visible              -anchor w -pady 2 -cspan 3 \
1925        1,0 $inner.lighting             -anchor w -pady 2 -cspan 3 \
1926        2,0 $inner.wireframe            -anchor w -pady 2 -cspan 3 \
1927        3,0 $inner.edges                -anchor w -pady 2 -cspan 3 \
1928        4,0 $inner.preinterp            -anchor w -pady 2 -cspan 3 \
1929        5,0 $inner.opacity_l            -anchor w -pady 2 -cspan 1 \
1930        5,1 $inner.opacity              -fill x   -pady 2 -cspan 3 \
1931        6,0 $inner.xbutton              -anchor w -padx 2 -pady 2 \
1932        7,0 $inner.ybutton              -anchor w -padx 2 -pady 2 \
1933        8,0 $inner.zbutton              -anchor w -padx 2 -pady 2 \
1934        6,1 $inner.xval                 -fill y -rspan 4 \
1935        6,2 $inner.yval                 -fill y -rspan 4 \
1936        6,3 $inner.zval                 -fill y -rspan 4 \
1937
1938
1939    blt::table configure $inner r* c* -resize none
1940    blt::table configure $inner r9 c4 -resize expand
1941}
1942
1943
1944
1945#
1946#  camera --
1947#
1948itcl::body Rappture::VtkGlyphViewer::camera {option args} {
1949    switch -- $option {
1950        "show" {
1951            puts [array get _view]
1952        }
1953        "set" {
1954            set who [lindex $args 0]
1955            set x $_view($who)
1956            set code [catch { string is double $x } result]
1957            if { $code != 0 || !$result } {
1958                return
1959            }
1960            switch -- $who {
1961                "ortho" {
1962                    if {$_view(ortho)} {
1963                        SendCmd "camera mode ortho"
1964                    } else {
1965                        SendCmd "camera mode persp"
1966                    }
1967                }
1968                "xpan" - "ypan" {
1969                    PanCamera
1970                }
1971                "qx" - "qy" - "qz" - "qw" {
1972                    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1973                    $_arcball quaternion $q
1974                    EventuallyRotate $q
1975                }
1976                "zoom" {
1977                    SendCmd "camera zoom $_view(zoom)"
1978                }
1979             }
1980        }
1981    }
1982}
1983
1984itcl::body Rappture::VtkGlyphViewer::GetVtkData { args } {
1985    set bytes ""
1986    foreach dataobj [get] {
1987        foreach cname [$dataobj components] {
1988            set tag $dataobj-$cname
1989            set contents [$dataobj vtkdata $cname]
1990            append bytes "$contents\n"
1991        }
1992    }
1993    return [list .vtk $bytes]
1994}
1995
1996itcl::body Rappture::VtkGlyphViewer::GetImage { args } {
1997    if { [image width $_image(download)] > 0 &&
1998         [image height $_image(download)] > 0 } {
1999        set bytes [$_image(download) data -format "jpeg -quality 100"]
2000        set bytes [Rappture::encoding::decode -as b64 $bytes]
2001        return [list .jpg $bytes]
2002    }
2003    return ""
2004}
2005
2006itcl::body Rappture::VtkGlyphViewer::BuildDownloadPopup { popup command } {
2007    Rappture::Balloon $popup \
2008        -title "[Rappture::filexfer::label downloadWord] as..."
2009    set inner [$popup component inner]
2010    label $inner.summary -text "" -anchor w
2011    radiobutton $inner.vtk_button -text "VTK data file" \
2012        -variable [itcl::scope _downloadPopup(format)] \
2013        -font "Arial 9 " \
2014        -value vtk 
2015    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2016    radiobutton $inner.image_button -text "Image File" \
2017        -variable [itcl::scope _downloadPopup(format)] \
2018        -font "Arial 9 " \
2019        -value image
2020    Rappture::Tooltip::for $inner.image_button \
2021        "Save as digital image."
2022
2023    button $inner.ok -text "Save" \
2024        -highlightthickness 0 -pady 2 -padx 3 \
2025        -command $command \
2026        -compound left \
2027        -image [Rappture::icon download]
2028
2029    button $inner.cancel -text "Cancel" \
2030        -highlightthickness 0 -pady 2 -padx 3 \
2031        -command [list $popup deactivate] \
2032        -compound left \
2033        -image [Rappture::icon cancel]
2034
2035    blt::table $inner \
2036        0,0 $inner.summary -cspan 2  \
2037        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2038        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2039        4,1 $inner.cancel -width .9i -fill y \
2040        4,0 $inner.ok -padx 2 -width .9i -fill y
2041    blt::table configure $inner r3 -height 4
2042    blt::table configure $inner r4 -pady 4
2043    raise $inner.image_button
2044    $inner.vtk_button invoke
2045    return $inner
2046}
2047
2048itcl::body Rappture::VtkGlyphViewer::SetObjectStyle { dataobj comp } {
2049    # Parse style string.
2050    set tag $dataobj-$comp
2051    array set style {
2052        -color BCGYR
2053        -edgecolor black
2054        -edges 0
2055        -gscale 1
2056        -lighting 1
2057        -linewidth 1.0
2058        -normscale 1
2059        -opacity 1.0
2060        -orientGlyphs 1
2061        -outline 0
2062        -ptsize 1.0
2063        -quality 1
2064        -scaleMode "vmag"
2065        -shape "arrow"
2066        -wireframe 0
2067    }
2068    set numComponents [$dataobj numComponents $comp]
2069    if {$numComponents == 3} {
2070        set style(-shape) "arrow"
2071        set style(-orientGlyphs) 1
2072        set style(-scaleMode) "vmag"
2073    } else {
2074        set style(-shape) "sphere"
2075        set style(-orientGlyphs) 0
2076        set style(-scaleMode) "scalar"
2077    }
2078    array set style [$dataobj style $comp]
2079    if { $dataobj != $_first } {
2080        set style(-opacity) 1
2081    }
2082    if 0 {
2083    SendCmd "cutplane add $tag"
2084    SendCmd "cutplane visible 0 $tag"
2085    }
2086    # This is too complicated.  We want to set the colormap, number of
2087    # glyph and opacity for the dataset.  They can be the default values,
2088    # the style hints loaded with the dataset, or set by user controls.  As
2089    # datasets get loaded, they first use the defaults that are overidden
2090    # by the style hints.  If the user changes the global controls, then that
2091    # overrides everything else.  I don't know what it means when global
2092    # controls are specified as style hints by each dataset.  It complicates
2093    # the code to handle aberrant cases.
2094
2095    if { $_changed(glyphOpacity) } {
2096        set style(-opacity) $_settings(glyphOpacity)
2097    }
2098    if { $_changed(colormap) } {
2099        set style(-color) $_settings(colormap)
2100    }
2101    if { $_currentColormap == "" } {
2102        $itk_component(colormap) value $style(-color)
2103    }
2104    set _currentOpacity $style(-opacity)
2105    SendCmd "glyphs add $style(-shape) $tag"
2106    SendCmd "glyphs edges $style(-edges) $tag"
2107    # normscale=1 and gscale=1 are defaults
2108    if {$style(-normscale) != 1} {
2109        SendCmd "glyphs normscale $style(-normscale) $tag"
2110    }
2111    if {$style(-gscale) != 1} {
2112        SendCmd "glyphs gscale $style(-gscale) $tag"
2113    }
2114    SendCmd "outline add $tag"
2115    SendCmd "outline color [Color2RGB $itk_option(-plotforeground)] $tag"
2116    SendCmd "outline visible $style(-outline) $tag"
2117    set _settings(glyphOutline) $style(-outline)
2118    set _settings(glyphEdges) $style(-edges)
2119    # constant color only used if colormode set to constant
2120    SendCmd "glyphs color [Color2RGB $itk_option(-plotforeground)] $tag"
2121    # Omitting field name for gorient and smode commands
2122    # defaults to active scalars or vectors depending on mode
2123    SendCmd "glyphs gorient $style(-orientGlyphs) {} $tag"
2124    SendCmd "glyphs smode $style(-scaleMode) {} $tag"
2125    SendCmd "glyphs quality $style(-quality) $tag"
2126    SendCmd "glyphs lighting $style(-lighting) $tag"
2127    set _settings(glyphLighting) $style(-lighting)
2128    SendCmd "glyphs linecolor [Color2RGB $style(-edgecolor)] $tag"
2129    SendCmd "glyphs linewidth $style(-linewidth) $tag"
2130    SendCmd "glyphs ptsize $style(-ptsize) $tag"
2131    SendCmd "glyphs opacity $_currentOpacity $tag"
2132    set _settings(glyphOpacity) $style(-opacity)
2133    SetCurrentColormap $style(-color)
2134    SendCmd "glyphs wireframe $style(-wireframe) $tag"
2135    set _settings(glyphWireframe) $style(-wireframe)
2136    set _settings(glyphOpacity) [expr $style(-opacity) * 100.0]
2137}
2138
2139itcl::body Rappture::VtkGlyphViewer::IsValidObject { dataobj } {
2140    if {[catch {$dataobj isa Rappture::Field} valid] != 0 || !$valid} {
2141        return 0
2142    }
2143    return 1
2144}
2145
2146#
2147# EnterLegend --
2148#
2149itcl::body Rappture::VtkGlyphViewer::EnterLegend { x y } {
2150    SetLegendTip $x $y
2151}
2152
2153#
2154# MotionLegend --
2155#
2156itcl::body Rappture::VtkGlyphViewer::MotionLegend { x y } {
2157    Rappture::Tooltip::tooltip cancel
2158    set c $itk_component(view)
2159    set cw [winfo width $c]
2160    set ch [winfo height $c]
2161    if { $x >= 0 && $x < $cw && $y >= 0 && $y < $ch } {
2162        SetLegendTip $x $y
2163    }
2164}
2165
2166#
2167# LeaveLegend --
2168#
2169itcl::body Rappture::VtkGlyphViewer::LeaveLegend { } {
2170    Rappture::Tooltip::tooltip cancel
2171    .rappturetooltip configure -icon ""
2172}
2173
2174#
2175# SetLegendTip --
2176#
2177itcl::body Rappture::VtkGlyphViewer::SetLegendTip { x y } {
2178    set fname $_curFldName
2179    set c $itk_component(view)
2180    set w [winfo width $c]
2181    set h [winfo height $c]
2182
2183    set font "Arial 8"
2184    set lineht [font metrics $font -linespace]
2185   
2186    set ih [image height $_image(legend)]
2187    set iy [expr $y - ($lineht + 2)]
2188
2189    if { [string match "component*" $fname] } {
2190        set title ""
2191    } else {
2192        if { [info exists _fields($fname)] } {
2193            foreach { title units } $_fields($fname) break
2194            if { $units != "" } {
2195                set title [format "%s (%s)" $title $units]
2196            }
2197        } else {
2198            set title $fname
2199        }
2200    }
2201    # If there's a legend title, increase the offset by the line height.
2202    if { $title != "" } {
2203        incr iy -$lineht
2204    }
2205    # Make a swatch of the selected color
2206    if { [catch { $_image(legend) get 10 $iy } pixel] != 0 } {
2207        return
2208    }
2209    if { ![info exists _image(swatch)] } {
2210        set _image(swatch) [image create photo -width 24 -height 24]
2211    }
2212    set color [eval format "\#%02x%02x%02x" $pixel]
2213    $_image(swatch) put black  -to 0 0 23 23
2214    $_image(swatch) put $color -to 1 1 22 22
2215    .rappturetooltip configure -icon $_image(swatch)
2216
2217    # Compute the value of the point
2218    if { [info exists _limits($_curFldName)] } {
2219        foreach { vmin vmax } $_limits($_curFldName) break
2220        set t [expr 1.0 - (double($iy) / double($ih-1))]
2221        set value [expr $t * ($vmax - $vmin) + $vmin]
2222    } else {
2223        set value 0.0
2224    }
2225    set tx [expr $x + 15]
2226    set ty [expr $y - 5]
2227    Rappture::Tooltip::text $c [format "$title %g" $value]
2228    Rappture::Tooltip::tooltip show $c +$tx,+$ty   
2229}
2230
2231
2232# ----------------------------------------------------------------------
2233# USAGE: Slice move x|y|z <newval>
2234#
2235# Called automatically when the user drags the slider to move the
2236# cut plane that slices 3D data.  Gets the current value from the
2237# slider and moves the cut plane to the appropriate point in the
2238# data set.
2239# ----------------------------------------------------------------------
2240itcl::body Rappture::VtkGlyphViewer::Slice {option args} {
2241    switch -- $option {
2242        "move" {
2243            set axis [lindex $args 0]
2244            set newval [lindex $args 1]
2245            if {[llength $args] != 2} {
2246                error "wrong # args: should be \"Slice move x|y|z newval\""
2247            }
2248            set newpos [expr {0.01*$newval}]
2249            SendCmd "cutplane slice $axis $newpos"
2250        }
2251        "tooltip" {
2252            set axis [lindex $args 0]
2253            set val [$itk_component(${axis}position) get]
2254            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
2255        }
2256        default {
2257            error "bad option \"$option\": should be axis, move, or tooltip"
2258        }
2259    }
2260}
2261
2262#
2263# ReceiveLegend --
2264#
2265#       Invoked automatically whenever the "legend" command comes in from
2266#       the rendering server.  Indicates that binary image data with the
2267#       specified <size> will follow.
2268#
2269itcl::body Rappture::VtkGlyphViewer::ReceiveLegend { colormap title min max size } {
2270    #puts stderr "ReceiveLegend colormap=$colormap title=$title range=$min,$max size=$size"
2271    set _title $title
2272    regsub {\(mag\)} $title "" _title
2273    if { [IsConnected] } {
2274        set bytes [ReceiveBytes $size]
2275        if { ![info exists _image(legend)] } {
2276            set _image(legend) [image create photo]
2277        }
2278        $_image(legend) configure -data $bytes
2279        #puts stderr "read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
2280        if { [catch {DrawLegend} errs] != 0 } {
2281            global errorInfo
2282            puts stderr "errs=$errs errorInfo=$errorInfo"
2283        }
2284    }
2285}
2286
2287#
2288# DrawLegend --
2289#
2290#       Draws the legend in the own canvas on the right side of the plot area.
2291#
2292itcl::body Rappture::VtkGlyphViewer::DrawLegend {} {
2293    set fname $_curFldName
2294    set c $itk_component(view)
2295    set w [winfo width $c]
2296    set h [winfo height $c]
2297    set font "Arial 8"
2298    set lineht [font metrics $font -linespace]
2299   
2300    if { [string match "component*" $fname] } {
2301        set title ""
2302    } else {
2303        if { [info exists _fields($fname)] } {
2304            foreach { title units } $_fields($fname) break
2305            if { $units != "" } {
2306                set title [format "%s (%s)" $title $units]
2307            }
2308        } else {
2309            set title $fname
2310        }
2311    }
2312    set x [expr $w - 2]
2313    if { !$_settings(legendVisible) } {
2314        $c delete legend
2315        return
2316    }
2317    if { [$c find withtag "legend"] == "" } {
2318        set y 2
2319        # If there's a legend title, create a text item for the title.
2320        $c create text $x $y \
2321                -anchor ne \
2322                -fill $itk_option(-plotforeground) -tags "title legend" \
2323                -font $font
2324            incr y $lineht
2325        $c create text $x $y \
2326            -anchor ne \
2327            -fill $itk_option(-plotforeground) -tags "vmax legend" \
2328            -font $font
2329        incr y $lineht
2330        $c create image $x $y \
2331            -anchor ne \
2332            -image $_image(legend) -tags "colormap legend"
2333        $c create rectangle $x $y 1 1 \
2334            -fill "" -outline "" -tags "sensor legend"
2335        $c create text $x [expr {$h-2}] \
2336            -anchor se \
2337            -fill $itk_option(-plotforeground) -tags "vmin legend" \
2338            -font $font
2339        $c bind sensor <Enter> [itcl::code $this EnterLegend %x %y]
2340        $c bind sensor <Leave> [itcl::code $this LeaveLegend]
2341        $c bind sensor <Motion> [itcl::code $this MotionLegend %x %y]
2342    }
2343    set x2 $x
2344    set iw [image width $_image(legend)]
2345    set ih [image height $_image(legend)]
2346    set x1 [expr $x2 - ($iw*12)/10]
2347
2348    $c bind title <ButtonPress> [itcl::code $this Combo post]
2349    $c bind title <Enter> [itcl::code $this Combo activate]
2350    $c bind title <Leave> [itcl::code $this Combo deactivate]
2351    # Reset the item coordinates according the current size of the plot.
2352    $c itemconfigure title -text $title
2353    if { [info exists _limits($_curFldName)] } {
2354        foreach { vmin vmax } $_limits($_curFldName) break
2355        $c itemconfigure vmin -text [format %g $vmin]
2356        $c itemconfigure vmax -text [format %g $vmax]
2357    }
2358    set y 2
2359    # If there's a legend title, move the title to the correct position
2360    if { $title != "" } {
2361        $c itemconfigure title -text $title
2362        $c coords title $x $y
2363        incr y $lineht
2364        $c raise title
2365    }
2366    $c coords vmax $x $y
2367    incr y $lineht
2368    $c coords colormap $x $y
2369    $c coords sensor [expr $x - $iw] $y $x [expr $y + $ih]
2370    $c raise sensor
2371    $c coords vmin $x [expr {$h - 2}]
2372}
2373
2374# ----------------------------------------------------------------------
2375# USAGE: _dropdown post
2376# USAGE: _dropdown unpost
2377# USAGE: _dropdown select
2378#
2379# Used internally to handle the dropdown list for this combobox.  The
2380# post/unpost options are invoked when the list is posted or unposted
2381# to manage the relief of the controlling button.  The select option
2382# is invoked whenever there is a selection from the list, to assign
2383# the value back to the gauge.
2384# ----------------------------------------------------------------------
2385itcl::body Rappture::VtkGlyphViewer::Combo {option} {
2386    set c $itk_component(view)
2387    switch -- $option {
2388        post {
2389            foreach { x1 y1 x2 y2 } [$c bbox title] break
2390            set cw [winfo width $itk_component(view)]
2391            set mw [winfo reqwidth $itk_component(fieldmenu)]
2392            set x1 [expr $cw - $mw]
2393            set x [expr $x1 + [winfo rootx $itk_component(view)]]
2394            set y [expr $y2 + [winfo rooty $itk_component(view)]]
2395            tk_popup $itk_component(fieldmenu) $x $y
2396        }
2397        activate {
2398            $c itemconfigure title -fill red
2399        }
2400        deactivate {
2401            $c itemconfigure title -fill $itk_option(-plotforeground)
2402        }
2403        invoke {
2404            $itk_component(field) value $_curFldLabel
2405            AdjustSetting field
2406        }
2407        default {
2408            error "bad option \"$option\": should be post, unpost, select"
2409        }
2410    }
2411}
2412
2413#
2414# SetCurrentColormap --
2415#
2416itcl::body Rappture::VtkGlyphViewer::SetCurrentColormap { name } {
2417    # Keep track of the colormaps that we build.
2418    if { ![info exists _colormaps($name)] } {
2419        BuildColormap $name
2420        set _colormaps($name) 1
2421    }
2422    set _currentColormap $name
2423    SendCmd "glyphs colormap $_currentColormap"
2424}
2425
2426#
2427# BuildColormap --
2428#
2429#       Build the designated colormap on the server.
2430#
2431itcl::body Rappture::VtkGlyphViewer::BuildColormap { name } {
2432    set cmap [ColorsToColormap $name]
2433    if { [llength $cmap] == 0 } {
2434        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
2435    }
2436    set wmap "0.0 1.0 1.0 1.0"
2437    SendCmd "colormap add $name { $cmap } { $wmap }"
2438}
2439
2440itcl::body Rappture::VtkGlyphViewer::SetOrientation { side } {
2441    array set positions {
2442        front "1 0 0 0"
2443        back  "0 0 1 0"
2444        left  "0.707107 0 -0.707107 0"
2445        right "0.707107 0 0.707107 0"
2446        top   "0.707107 -0.707107 0 0"
2447        bottom "0.707107 0.707107 0 0"
2448    }
2449    foreach name { qw qx qy qz } value $positions($side) {
2450        set _view($name) $value
2451    }
2452    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
2453    $_arcball quaternion $q
2454    SendCmd "camera orient $q"
2455    SendCmd "camera reset"
2456    set _view(xpan) 0
2457    set _view(ypan) 0
2458    set _view(zoom) 1.0
2459}
2460
Note: See TracBrowser for help on using the repository browser.