source: branches/1.3/gui/scripts/vtkstreamlinesviewer.tcl @ 4681

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

merge r4679 from trunk

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