source: trunk/gui/scripts/vtkstreamlinesviewer.tcl @ 4767

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

More syncing of code between clients

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