source: tags/1.3.5/gui/scripts/vtkstreamlinesviewer.tcl @ 4753

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

merge r4749 from trunk

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