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

Last change on this file since 4766 was 4766, checked in by ldelgass, 6 years ago

merge r4765 from trunk

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