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

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

merge r4928 from 1.3 branch (remove updates)

File size: 83.7 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        -axismode                   "static"
234        -cutplaneedges              0
235        -cutplanelighting           1
236        -cutplaneopacity            100
237        -cutplanevisible            0
238        -cutplanewireframe          0
239        -cutplanexposition          50
240        -cutplanexvisible           1
241        -cutplaneyposition          50
242        -cutplaneyvisible           1
243        -cutplanezposition          50
244        -cutplanezvisible           1
245        -legendvisible              1
246        -streamlineslighting        1
247        -streamlinesmode            lines
248        -streamlinesnumseeds        200
249        -streamlinesopacity         100
250        -streamlinesscale           1
251        -streamlinesseedsvisible    0
252        -streamlinesvisible         1
253        -volumeedges                0
254        -volumelighting             1
255        -volumeopacity              40
256        -volumevisible              1
257        -volumewireframe            0
258        -xgrid                      0
259        -ygrid                      0
260        -zgrid                      0
261    }]
262
263    itk_component add view {
264        canvas $itk_component(plotarea).view \
265            -highlightthickness 0 -borderwidth 0
266    } {
267        usual
268        ignore -highlightthickness -borderwidth  -background
269    }
270
271    itk_component add fieldmenu {
272        menu $itk_component(plotarea).menu -bg black -fg white -relief flat \
273            -tearoff no
274    } {
275        usual
276        ignore -background -foreground -relief -tearoff
277    }
278    set c $itk_component(view)
279    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
280    bind $c <4> [itcl::code $this Zoom in 0.25]
281    bind $c <5> [itcl::code $this Zoom out 0.25]
282    bind $c <KeyPress-Left>  [list %W xview scroll 10 units]
283    bind $c <KeyPress-Right> [list %W xview scroll -10 units]
284    bind $c <KeyPress-Up>    [list %W yview scroll 10 units]
285    bind $c <KeyPress-Down>  [list %W yview scroll -10 units]
286    bind $c <Enter> "focus %W"
287    bind $c <Control-F1> [itcl::code $this ToggleConsole]
288
289    # Fix the scrollregion in case we go off screen
290    $c configure -scrollregion [$c bbox all]
291
292    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
293    set _map(cwidth) -1
294    set _map(cheight) -1
295    set _map(zoom) 1.0
296    set _map(original) ""
297
298    set f [$itk_component(main) component controls]
299    itk_component add reset {
300        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
301            -highlightthickness 0 \
302            -image [Rappture::icon reset-view] \
303            -command [itcl::code $this Zoom reset]
304    } {
305        usual
306        ignore -highlightthickness
307    }
308    pack $itk_component(reset) -side top -padx 2 -pady 2
309    Rappture::Tooltip::for $itk_component(reset) "Reset the view to the default zoom level"
310
311    itk_component add zoomin {
312        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
313            -highlightthickness 0 \
314            -image [Rappture::icon zoom-in] \
315            -command [itcl::code $this Zoom in]
316    } {
317        usual
318        ignore -highlightthickness
319    }
320    pack $itk_component(zoomin) -side top -padx 2 -pady 2
321    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
322
323    itk_component add zoomout {
324        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
325            -highlightthickness 0 \
326            -image [Rappture::icon zoom-out] \
327            -command [itcl::code $this Zoom out]
328    } {
329        usual
330        ignore -highlightthickness
331    }
332    pack $itk_component(zoomout) -side top -padx 2 -pady 2
333    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
334
335    itk_component add volume {
336        Rappture::PushButton $f.volume \
337            -onimage [Rappture::icon volume-on] \
338            -offimage [Rappture::icon volume-off] \
339            -variable [itcl::scope _settings(-volumevisible)] \
340            -command [itcl::code $this AdjustSetting -volumevisible]
341    }
342    $itk_component(volume) select
343    Rappture::Tooltip::for $itk_component(volume) \
344        "Don't display the volume"
345    pack $itk_component(volume) -padx 2 -pady 2
346
347    itk_component add streamlines {
348        Rappture::PushButton $f.streamlines \
349            -onimage [Rappture::icon streamlines-on] \
350            -offimage [Rappture::icon streamlines-off] \
351            -variable [itcl::scope _settings(-streamlinesvisible)] \
352            -command [itcl::code $this AdjustSetting -streamlinesvisible] \
353    }
354    $itk_component(streamlines) select
355    Rappture::Tooltip::for $itk_component(streamlines) \
356        "Toggle the streamlines on/off"
357    pack $itk_component(streamlines) -padx 2 -pady 2
358
359    itk_component add cutplane {
360        Rappture::PushButton $f.cutplane \
361            -onimage [Rappture::icon cutbutton] \
362            -offimage [Rappture::icon cutbutton] \
363            -variable [itcl::scope _settings(-cutplanevisible)] \
364            -command [itcl::code $this AdjustSetting -cutplanevisible]
365    }
366    Rappture::Tooltip::for $itk_component(cutplane) \
367        "Show/Hide cutplanes"
368    pack $itk_component(cutplane) -padx 2 -pady 2
369
370
371    if { [catch {
372        BuildVolumeTab
373        BuildStreamsTab
374        BuildCutplaneTab
375        BuildAxisTab
376        BuildCameraTab
377    } errs] != 0 } {
378        puts stderr errs=$errs
379    }
380    # Legend
381
382    set _image(legend) [image create photo]
383    itk_component add legend {
384        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
385    } {
386        usual
387        ignore -highlightthickness
388        rename -background -plotbackground plotBackground Background
389    }
390
391    # Hack around the Tk panewindow.  The problem is that the requested
392    # size of the 3d view isn't set until an image is retrieved from
393    # the server.  So the panewindow uses the tiny size.
394    set w 10000
395    pack forget $itk_component(view)
396    blt::table $itk_component(plotarea) \
397        0,0 $itk_component(view) -fill both -reqwidth $w
398    blt::table configure $itk_component(plotarea) c1 -resize none
399
400    # Bindings for rotation via mouse
401    bind $itk_component(view) <ButtonPress-1> \
402        [itcl::code $this Rotate click %x %y]
403    bind $itk_component(view) <B1-Motion> \
404        [itcl::code $this Rotate drag %x %y]
405    bind $itk_component(view) <ButtonRelease-1> \
406        [itcl::code $this Rotate release %x %y]
407
408    # Bindings for panning via mouse
409    bind $itk_component(view) <ButtonPress-2> \
410        [itcl::code $this Pan click %x %y]
411    bind $itk_component(view) <B2-Motion> \
412        [itcl::code $this Pan drag %x %y]
413    bind $itk_component(view) <ButtonRelease-2> \
414        [itcl::code $this Pan release %x %y]
415
416    #bind $itk_component(view) <ButtonRelease-3> \
417    #    [itcl::code $this Pick %x %y]
418
419    # Bindings for panning via keyboard
420    bind $itk_component(view) <KeyPress-Left> \
421        [itcl::code $this Pan set -10 0]
422    bind $itk_component(view) <KeyPress-Right> \
423        [itcl::code $this Pan set 10 0]
424    bind $itk_component(view) <KeyPress-Up> \
425        [itcl::code $this Pan set 0 -10]
426    bind $itk_component(view) <KeyPress-Down> \
427        [itcl::code $this Pan set 0 10]
428    bind $itk_component(view) <Shift-KeyPress-Left> \
429        [itcl::code $this Pan set -2 0]
430    bind $itk_component(view) <Shift-KeyPress-Right> \
431        [itcl::code $this Pan set 2 0]
432    bind $itk_component(view) <Shift-KeyPress-Up> \
433        [itcl::code $this Pan set 0 -2]
434    bind $itk_component(view) <Shift-KeyPress-Down> \
435        [itcl::code $this Pan set 0 2]
436
437    # Bindings for zoom via keyboard
438    bind $itk_component(view) <KeyPress-Prior> \
439        [itcl::code $this Zoom out]
440    bind $itk_component(view) <KeyPress-Next> \
441        [itcl::code $this Zoom in]
442
443    bind $itk_component(view) <Enter> "focus $itk_component(view)"
444
445    if {[string equal "x11" [tk windowingsystem]]} {
446        # Bindings for zoom via mouse
447        bind $itk_component(view) <4> [itcl::code $this Zoom out]
448        bind $itk_component(view) <5> [itcl::code $this Zoom in]
449    }
450
451    set _image(download) [image create photo]
452
453    eval itk_initialize $args
454    Connect
455}
456
457# ----------------------------------------------------------------------
458# DESTRUCTOR
459# ----------------------------------------------------------------------
460itcl::body Rappture::VtkStreamlinesViewer::destructor {} {
461    Disconnect
462    image delete $_image(plot)
463    image delete $_image(download)
464    catch { blt::arcball destroy $_arcball }
465}
466
467itcl::body Rappture::VtkStreamlinesViewer::DoResize {} {
468    if { $_width < 2 } {
469        set _width 500
470    }
471    if { $_height < 2 } {
472        set _height 500
473    }
474    set _start [clock clicks -milliseconds]
475    SendCmd "screen size $_width $_height"
476
477    set _legendPending 1
478    set _resizePending 0
479}
480
481itcl::body Rappture::VtkStreamlinesViewer::DoRotate {} {
482    SendCmd "camera orient [ViewToQuaternion]"
483    set _rotatePending 0
484}
485
486itcl::body Rappture::VtkStreamlinesViewer::DoReseed {} {
487    foreach dataset [CurrentDatasets -visible] {
488        foreach {dataobj comp} [split $dataset -] break
489        # This command works for either random or fmesh seeds
490        SendCmd "streamlines seed numpts $_numSeeds $dataset"
491    }
492    set _reseedPending 0
493}
494
495itcl::body Rappture::VtkStreamlinesViewer::EventuallyResize { w h } {
496    set _width $w
497    set _height $h
498    $_arcball resize $w $h
499    if { !$_resizePending } {
500        set _resizePending 1
501        $_dispatcher event -after 400 !resize
502    }
503}
504
505itcl::body Rappture::VtkStreamlinesViewer::EventuallyReseed { numPoints } {
506    set _numSeeds $numPoints
507    if { !$_reseedPending } {
508        set _reseedPending 1
509        $_dispatcher event -after 600 !reseed
510    }
511}
512
513set rotate_delay 100
514
515itcl::body Rappture::VtkStreamlinesViewer::EventuallyRotate { q } {
516    QuaternionToView $q
517    if { !$_rotatePending } {
518        set _rotatePending 1
519        global rotate_delay
520        $_dispatcher event -after $rotate_delay !rotate
521    }
522}
523
524itcl::body Rappture::VtkStreamlinesViewer::EventuallySetCutplane { axis args } {
525    if { !$_cutplanePending } {
526        set _cutplanePending 1
527        $_dispatcher event -after 100 !${axis}cutplane
528    }
529}
530
531# ----------------------------------------------------------------------
532# USAGE: add <dataobj> ?<settings>?
533#
534# Clients use this to add a data object to the plot.  The optional
535# <settings> are used to configure the plot.  Allowed settings are
536# -color, -brightness, -width, -linestyle, and -raise.
537# ----------------------------------------------------------------------
538itcl::body Rappture::VtkStreamlinesViewer::add {dataobj {settings ""}} {
539    array set params {
540        -color auto
541        -width 1
542        -linestyle solid
543        -brightness 0
544        -raise 0
545        -description ""
546        -param ""
547        -type ""
548    }
549    array set params $settings
550    set params(-description) ""
551    set params(-param) ""
552    array set params $settings
553
554    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
555        # can't handle -autocolors yet
556        set params(-color) black
557    }
558    set pos [lsearch -exact $_dlist $dataobj]
559    if {$pos < 0} {
560        lappend _dlist $dataobj
561    }
562    set _obj2ovride($dataobj-color) $params(-color)
563    set _obj2ovride($dataobj-width) $params(-width)
564    set _obj2ovride($dataobj-raise) $params(-raise)
565    $_dispatcher event -idle !rebuild
566}
567
568
569# ----------------------------------------------------------------------
570# USAGE: delete ?<dataobj1> <dataobj2> ...?
571#
572#       Clients use this to delete a dataobj from the plot.  If no dataobjs
573#       are specified, then all dataobjs are deleted.  No data objects are
574#       deleted.  They are only removed from the display list.
575#
576# ----------------------------------------------------------------------
577itcl::body Rappture::VtkStreamlinesViewer::delete {args} {
578    if { [llength $args] == 0} {
579        set args $_dlist
580    }
581    # Delete all specified dataobjs
582    set changed 0
583    foreach dataobj $args {
584        set pos [lsearch -exact $_dlist $dataobj]
585        if { $pos < 0 } {
586            continue;                   # Don't know anything about it.
587        }
588        # Remove it from the dataobj list.
589        set _dlist [lreplace $_dlist $pos $pos]
590        array unset _obj2ovride $dataobj-*
591        array unset _settings $dataobj-*
592        set changed 1
593    }
594    # If anything changed, then rebuild the plot
595    if { $changed } {
596        $_dispatcher event -idle !rebuild
597    }
598}
599
600# ----------------------------------------------------------------------
601# USAGE: get ?-objects?
602# USAGE: get ?-visible?
603# USAGE: get ?-image view?
604#
605# Clients use this to query the list of objects being plotted, in
606# order from bottom to top of this result.  The optional "-image"
607# flag can also request the internal images being shown.
608# ----------------------------------------------------------------------
609itcl::body Rappture::VtkStreamlinesViewer::get {args} {
610    if {[llength $args] == 0} {
611        set args "-objects"
612    }
613
614    set op [lindex $args 0]
615    switch -- $op {
616        "-objects" {
617            # put the dataobj list in order according to -raise options
618            set dlist {}
619            foreach dataobj $_dlist {
620                if { ![IsValidObject $dataobj] } {
621                    continue
622                }
623                if {[info exists _obj2ovride($dataobj-raise)] &&
624                    $_obj2ovride($dataobj-raise)} {
625                    set dlist [linsert $dlist 0 $dataobj]
626                } else {
627                    lappend dlist $dataobj
628                }
629            }
630            return $dlist
631        }
632        "-visible" {
633            set dlist {}
634            foreach dataobj $_dlist {
635                if { ![IsValidObject $dataobj] } {
636                    continue
637                }
638                if { ![info exists _obj2ovride($dataobj-raise)] } {
639                    # No setting indicates that the object isn't visible.
640                    continue
641                }
642                # Otherwise use the -raise parameter to put the object to
643                # the front of the list.
644                if { $_obj2ovride($dataobj-raise) } {
645                    set dlist [linsert $dlist 0 $dataobj]
646                } else {
647                    lappend dlist $dataobj
648                }
649            }
650            return $dlist
651        }           
652        -image {
653            if {[llength $args] != 2} {
654                error "wrong # args: should be \"get -image view\""
655            }
656            switch -- [lindex $args end] {
657                view {
658                    return $_image(plot)
659                }
660                default {
661                    error "bad image name \"[lindex $args end]\": should be view"
662                }
663            }
664        }
665        default {
666            error "bad option \"$op\": should be -objects or -image"
667        }
668    }
669}
670
671# ----------------------------------------------------------------------
672# USAGE: scale ?<data1> <data2> ...?
673#
674# Sets the default limits for the overall plot according to the
675# limits of the data for all of the given <data> objects.  This
676# accounts for all objects--even those not showing on the screen.
677# Because of this, the limits are appropriate for all objects as
678# the user scans through data in the ResultSet viewer.
679# ----------------------------------------------------------------------
680itcl::body Rappture::VtkStreamlinesViewer::scale {args} {
681    foreach dataobj $args {
682        foreach axis { x y z } {
683            set lim [$dataobj limits $axis]
684            if { ![info exists _limits($axis)] } {
685                set _limits($axis) $lim
686                continue
687            }
688            foreach {min max} $lim break
689            foreach {amin amax} $_limits($axis) break
690            if { $amin > $min } {
691                set amin $min
692            }
693            if { $amax < $max } {
694                set amax $max
695            }
696            set _limits($axis) [list $amin $amax]
697        }
698        foreach { fname lim } [$dataobj fieldlimits] {
699            if { ![info exists _limits($fname)] } {
700                set _limits($fname) $lim
701                continue
702            }
703            foreach {min max} $lim break
704            foreach {fmin fmax} $_limits($fname) break
705            if { $fmin > $min } {
706                set fmin $min
707            }
708            if { $fmax < $max } {
709                set fmax $max
710            }
711            set _limits($fname) [list $fmin $fmax]
712        }
713    }
714}
715
716# ----------------------------------------------------------------------
717# USAGE: download coming
718# USAGE: download controls <downloadCommand>
719# USAGE: download now
720#
721# Clients use this method to create a downloadable representation
722# of the plot.  Returns a list of the form {ext string}, where
723# "ext" is the file extension (indicating the type of data) and
724# "string" is the data itself.
725# ----------------------------------------------------------------------
726itcl::body Rappture::VtkStreamlinesViewer::download {option args} {
727    switch $option {
728        coming {
729            if {[catch {
730                blt::winop snap $itk_component(plotarea) $_image(download)
731            }]} {
732                $_image(download) configure -width 1 -height 1
733                $_image(download) put #000000
734            }
735        }
736        controls {
737            set popup .vtkviewerdownload
738            if { ![winfo exists .vtkviewerdownload] } {
739                set inner [BuildDownloadPopup $popup [lindex $args 0]]
740            } else {
741                set inner [$popup component inner]
742            }
743            set _downloadPopup(image_controls) $inner.image_frame
744            set num [llength [get]]
745            set num [expr {($num == 1) ? "1 result" : "$num results"}]
746            set word [Rappture::filexfer::label downloadWord]
747            $inner.summary configure -text "$word $num in the following format:"
748            update idletasks            ;# Fix initial sizes
749            return $popup
750        }
751        now {
752            set popup .vtkviewerdownload
753            if {[winfo exists .vtkviewerdownload]} {
754                $popup deactivate
755            }
756            switch -- $_downloadPopup(format) {
757                "image" {
758                    return [$this GetImage [lindex $args 0]]
759                }
760                "vtk" {
761                    return [$this GetVtkData [lindex $args 0]]
762                }
763            }
764            return ""
765        }
766        default {
767            error "bad option \"$option\": should be coming, controls, now"
768        }
769    }
770}
771
772# ----------------------------------------------------------------------
773# USAGE: Connect ?<host:port>,<host:port>...?
774#
775# Clients use this method to establish a connection to a new
776# server, or to reestablish a connection to the previous server.
777# Any existing connection is automatically closed.
778# ----------------------------------------------------------------------
779itcl::body Rappture::VtkStreamlinesViewer::Connect {} {
780    set _hosts [GetServerList "vtkvis"]
781    if { "" == $_hosts } {
782        return 0
783    }
784    set result [VisViewer::Connect $_hosts]
785    if { $result } {
786        if { $_reportClientInfo }  {
787            # Tell the server the viewer, hub, user and session.
788            # Do this immediately on connect before buffering any commands
789            global env
790
791            set info {}
792            set user "???"
793            if { [info exists env(USER)] } {
794                set user $env(USER)
795            }
796            set session "???"
797            if { [info exists env(SESSION)] } {
798                set session $env(SESSION)
799            }
800            lappend info "version" "$Rappture::version"
801            lappend info "build" "$Rappture::build"
802            lappend info "svnurl" "$Rappture::svnurl"
803            lappend info "installdir" "$Rappture::installdir"
804            lappend info "hub" [exec hostname]
805            lappend info "client" "vtkstreamlinesviewer"
806            lappend info "user" $user
807            lappend info "session" $session
808            SendCmd "clientinfo [list $info]"
809        }
810
811        set w [winfo width $itk_component(view)]
812        set h [winfo height $itk_component(view)]
813        EventuallyResize $w $h
814    }
815    return $result
816}
817
818#
819# isconnected --
820#
821#       Indicates if we are currently connected to the visualization server.
822#
823itcl::body Rappture::VtkStreamlinesViewer::isconnected {} {
824    return [VisViewer::IsConnected]
825}
826
827#
828# disconnect --
829#
830itcl::body Rappture::VtkStreamlinesViewer::disconnect {} {
831    Disconnect
832    set _reset 1
833}
834
835#
836# Disconnect --
837#
838#       Clients use this method to disconnect from the current rendering
839#       server.
840#
841itcl::body Rappture::VtkStreamlinesViewer::Disconnect {} {
842    VisViewer::Disconnect
843
844    $_dispatcher cancel !rebuild
845    $_dispatcher cancel !resize
846    $_dispatcher cancel !reseed
847    $_dispatcher cancel !rotate
848    $_dispatcher cancel !xcutplane
849    $_dispatcher cancel !ycutplane
850    $_dispatcher cancel !zcutplane
851    $_dispatcher cancel !legend
852    # disconnected -- no more data sitting on server
853    array unset _datasets
854    array unset _data
855    array unset _colormaps
856    array unset _seeds
857    array unset _dataset2style
858    array unset _obj2datasets
859}
860
861# ----------------------------------------------------------------------
862# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
863#
864# Invoked automatically whenever the "image" command comes in from
865# the rendering server.  Indicates that binary image data with the
866# specified <size> will follow.
867# ----------------------------------------------------------------------
868itcl::body Rappture::VtkStreamlinesViewer::ReceiveImage { args } {
869    array set info {
870        -token "???"
871        -bytes 0
872        -type image
873    }
874    array set info $args
875    set bytes [ReceiveBytes $info(-bytes)]
876    if { $info(-type) == "image" } {
877        if 0 {
878            set f [open "last.ppm" "w"]
879            fconfigure $f -encoding binary
880            puts -nonewline $f $bytes
881            close $f
882        }
883        $_image(plot) configure -data $bytes
884        set time [clock seconds]
885        set date [clock format $time]
886        #puts stderr "$date: received image [image width $_image(plot)]x[image height $_image(plot)] image>"       
887        if { $_start > 0 } {
888            set finish [clock clicks -milliseconds]
889            #puts stderr "round trip time [expr $finish -$_start] milliseconds"
890            set _start 0
891        }
892    } elseif { $info(type) == "print" } {
893        set tag $this-print-$info(-token)
894        set _hardcopy($tag) $bytes
895    }
896    if { $_legendPending } {
897        RequestLegend
898    }
899}
900
901#
902# ReceiveDataset --
903#
904itcl::body Rappture::VtkStreamlinesViewer::ReceiveDataset { args } {
905    if { ![isconnected] } {
906        return
907    }
908    set option [lindex $args 0]
909    switch -- $option {
910        "scalar" {
911            set option [lindex $args 1]
912            switch -- $option {
913                "world" {
914                    foreach { x y z value tag } [lrange $args 2 end] break
915                }
916                "pixel" {
917                    foreach { x y value tag } [lrange $args 2 end] break
918                }
919            }
920        }
921        "vector" {
922            set option [lindex $args 1]
923            switch -- $option {
924                "world" {
925                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
926                }
927                "pixel" {
928                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
929                }
930            }
931        }
932        "names" {
933            foreach { name } [lindex $args 1] {
934                #puts stderr "Dataset: $name"
935            }
936        }
937        default {
938            error "unknown dataset option \"$option\" from server"
939        }
940    }
941}
942
943# ----------------------------------------------------------------------
944# USAGE: Rebuild
945#
946# Called automatically whenever something changes that affects the
947# data in the widget.  Clears any existing data and rebuilds the
948# widget to display new data.
949# ----------------------------------------------------------------------
950itcl::body Rappture::VtkStreamlinesViewer::Rebuild {} {
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 -xgrid -ygrid -zgrid -axismode \
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        "-axesvisible" {
1326            set bool $_settings($what)
1327            SendCmd "axis visible all $bool"
1328        }
1329        "-axislabelsvisible" {
1330            set bool $_settings($what)
1331            SendCmd "axis labels all $bool"
1332        }
1333        "-axisminorticks" {
1334            set bool $_settings($what)
1335            SendCmd "axis minticks all $bool"
1336        }
1337        "-axismode" {
1338            set mode [$itk_component(axismode) value]
1339            set mode [$itk_component(axismode) translate $mode]
1340            set _settings($what) $mode
1341            SendCmd "axis flymode $mode"
1342        }
1343        "-cutplaneedges" {
1344            set bool $_settings($what)
1345            SendCmd "cutplane edges $bool"
1346        }
1347        "-cutplanevisible" {
1348            set bool $_settings($what)
1349            SendCmd "cutplane visible $bool"
1350        }
1351        "-cutplanewireframe" {
1352            set bool $_settings($what)
1353            SendCmd "cutplane wireframe $bool"
1354        }
1355        "-cutplanelighting" {
1356            set bool $_settings($what)
1357            SendCmd "cutplane lighting $bool"
1358        }
1359        "-cutplaneopacity" {
1360            set val $_settings($what)
1361            set sval [expr { 0.01 * double($val) }]
1362            SendCmd "cutplane opacity $sval"
1363        }
1364        "-cutplanexvisible" - "-cutplaneyvisible" - "-cutplanezvisible" {
1365            set axis [string range $what 9 9]
1366            set bool $_settings($what)
1367            if { $bool } {
1368                $itk_component(${axis}CutScale) configure -state normal \
1369                    -troughcolor white
1370            } else {
1371                $itk_component(${axis}CutScale) configure -state disabled \
1372                    -troughcolor grey82
1373            }
1374            SendCmd "cutplane axis $axis $bool"
1375        }
1376        "-cutplanexposition" - "-cutplaneyposition" - "-cutplanezposition" {
1377            set axis [string range $what 9 9]
1378            set pos [expr $_settings($what) * 0.01]
1379            SendCmd "cutplane slice ${axis} ${pos}"
1380            set _cutplanePending 0
1381        }
1382        "-field" {
1383            set label [$itk_component(field) value]
1384            set fname [$itk_component(field) translate $label]
1385            set _settings($what) $fname
1386            if { [info exists _fields($fname)] } {
1387                foreach { label units components } $_fields($fname) break
1388                if { $components > 1 } {
1389                    set _colorMode vmag
1390                } else {
1391                    set _colorMode scalar
1392                }
1393                set _curFldName $fname
1394                set _curFldLabel $label
1395            } else {
1396                puts stderr "unknown field \"$fname\""
1397                return
1398            }
1399            # Get the new limits because the field changed.
1400            if { ![info exists _limits($_curFldName)] } {
1401                SendCmd "dataset maprange all"
1402            } else {
1403                SendCmd "dataset maprange explicit $_limits($_curFldName) $_curFldName"
1404            }
1405            SendCmd "streamlines colormode $_colorMode $_curFldName"
1406            SendCmd "cutplane colormode $_colorMode $_curFldName"
1407            DrawLegend
1408        }
1409        "-streamlinesseedsvisible" {
1410            set bool $_settings($what)
1411            SendCmd "streamlines seed visible $bool"
1412        }
1413        "-streamlinesnumseeds" {
1414            set density $_settings($what)
1415            EventuallyReseed $density
1416        }
1417        "-streamlinesvisible" {
1418            set bool $_settings($what)
1419            SendCmd "streamlines visible $bool"
1420            if { $bool } {
1421                Rappture::Tooltip::for $itk_component(streamlines) \
1422                    "Hide the streamlines"
1423            } else {
1424                Rappture::Tooltip::for $itk_component(streamlines) \
1425                    "Show the streamlines"
1426            }
1427        }
1428        "-streamlinesmode" {
1429            set mode [$itk_component(streammode) value]
1430            set _settings($what) $mode
1431            switch -- $mode {
1432                "lines" {
1433                    SendCmd "streamlines lines"
1434                }
1435                "ribbons" {
1436                    SendCmd "streamlines ribbons 3 0"
1437                }
1438                "tubes" {
1439                    SendCmd "streamlines tubes 5 3"
1440                }
1441            }
1442        }
1443        "-streamlinescolormap" {
1444            set colormap [$itk_component(colormap) value]
1445            set _settings($what) $colormap
1446            foreach dataset [CurrentDatasets -visible $_first] {
1447                foreach {dataobj comp} [split $dataset -] break
1448                ChangeColormap $dataobj $comp $colormap
1449            }
1450            set _legendPending 1
1451        }
1452        "-streamlinesopacity" {
1453            set val $_settings($what)
1454            set sval [expr { 0.01 * double($val) }]
1455            SendCmd "streamlines opacity $sval"
1456        }
1457        "-streamlinesscale" {
1458            set val $_settings($what)
1459            set sval [expr { 0.01 * double($val) }]
1460            SendCmd "streamlines scale $sval $sval $sval"
1461        }
1462        "-streamlineslighting" {
1463            set bool $_settings($what)
1464            SendCmd "streamlines lighting $bool"
1465        }
1466        "-volumeopacity" {
1467            set val $_settings($what)
1468            set sval [expr { 0.01 * double($val) }]
1469            SendCmd "polydata opacity $sval"
1470        }
1471        "-volumewireframe" {
1472            set bool $_settings($what)
1473            SendCmd "polydata wireframe $bool"
1474        }
1475        "-volumevisible" {
1476            set bool $_settings($what)
1477            SendCmd "polydata visible $bool"
1478            if { $bool } {
1479                Rappture::Tooltip::for $itk_component(volume) \
1480                    "Hide the volume"
1481            } else {
1482                Rappture::Tooltip::for $itk_component(volume) \
1483                    "Show the volume"
1484            }
1485        }
1486        "-volumelighting" {
1487            set bool $_settings($what)
1488            SendCmd "polydata lighting $bool"
1489        }
1490        "-volumeedges" {
1491            set bool $_settings($what)
1492            SendCmd "polydata edges $bool"
1493        }
1494        "-xgrid" - "-ygrid" - "-zgrid" {
1495            set axis [string range $what 1 1]
1496            set bool $_settings($what)
1497            SendCmd "axis grid $axis $bool"
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# BuildColormap --
1584#
1585itcl::body Rappture::VtkStreamlinesViewer::BuildColormap { name styles } {
1586    array set style $styles
1587    set cmap [ColorsToColormap $style(-color)]
1588    if { [llength $cmap] == 0 } {
1589        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
1590    }
1591    if { ![info exists _settings(-volumeopacity)] } {
1592        set _settings(-volumeopacity) $style(-opacity)
1593    }
1594    set max $_settings(-volumeopacity)
1595
1596    set wmap "0.0 1.0 1.0 1.0"
1597    SendCmd "colormap add $name { $cmap } { $wmap }"
1598}
1599
1600# ----------------------------------------------------------------------
1601# CONFIGURATION OPTION: -plotbackground
1602# ----------------------------------------------------------------------
1603itcl::configbody Rappture::VtkStreamlinesViewer::plotbackground {
1604    if { [isconnected] } {
1605        set rgb [Color2RGB $itk_option(-plotbackground)]
1606        SendCmd "screen bgcolor $rgb"
1607    }
1608}
1609
1610# ----------------------------------------------------------------------
1611# CONFIGURATION OPTION: -plotforeground
1612# ----------------------------------------------------------------------
1613itcl::configbody Rappture::VtkStreamlinesViewer::plotforeground {
1614    if { [isconnected] } {
1615        set rgb [Color2RGB $itk_option(-plotforeground)]
1616        SendCmd "axis color all $rgb"
1617        SendCmd "outline color $rgb"
1618        SendCmd "cutplane color $rgb"
1619    }
1620}
1621
1622itcl::body Rappture::VtkStreamlinesViewer::BuildVolumeTab {} {
1623
1624    set fg [option get $itk_component(hull) font Font]
1625    #set bfg [option get $itk_component(hull) boldFont Font]
1626
1627    set inner [$itk_component(main) insert end \
1628        -title "Volume Settings" \
1629        -icon [Rappture::icon volume-on]]
1630    $inner configure -borderwidth 4
1631
1632    checkbutton $inner.volume \
1633        -text "Show Volume" \
1634        -variable [itcl::scope _settings(-volumevisible)] \
1635        -command [itcl::code $this AdjustSetting -volumevisible] \
1636        -font "Arial 9"
1637
1638    checkbutton $inner.wireframe \
1639        -text "Show Wireframe" \
1640        -variable [itcl::scope _settings(-volumewireframe)] \
1641        -command [itcl::code $this AdjustSetting -volumewireframe] \
1642        -font "Arial 9"
1643
1644    checkbutton $inner.lighting \
1645        -text "Enable Lighting" \
1646        -variable [itcl::scope _settings(-volumelighting)] \
1647        -command [itcl::code $this AdjustSetting -volumelighting] \
1648        -font "Arial 9"
1649
1650    checkbutton $inner.edges \
1651        -text "Show Edges" \
1652        -variable [itcl::scope _settings(-volumeedges)] \
1653        -command [itcl::code $this AdjustSetting -volumeedges] \
1654        -font "Arial 9"
1655
1656    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1657    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1658        -variable [itcl::scope _settings(-volumeopacity)] \
1659        -width 10 \
1660        -showvalue off \
1661        -command [itcl::code $this AdjustSetting -volumeopacity]
1662
1663    blt::table $inner \
1664        0,0 $inner.wireframe -anchor w -pady 2 -cspan 2 \
1665        1,0 $inner.lighting  -anchor w -pady 2 -cspan 2 \
1666        2,0 $inner.edges     -anchor w -pady 2 -cspan 3 \
1667        3,0 $inner.opacity_l -anchor w -pady 2 \
1668        3,1 $inner.opacity   -fill x   -pady 2
1669
1670    blt::table configure $inner r* c* -resize none
1671    blt::table configure $inner r4 c1 -resize expand
1672}
1673
1674itcl::body Rappture::VtkStreamlinesViewer::BuildStreamsTab {} {
1675
1676    set fg [option get $itk_component(hull) font Font]
1677    #set bfg [option get $itk_component(hull) boldFont Font]
1678
1679    set inner [$itk_component(main) insert end \
1680        -title "Streams Settings" \
1681        -icon [Rappture::icon streamlines-on]]
1682    $inner configure -borderwidth 4
1683
1684    checkbutton $inner.streamlines \
1685        -text "Show Streamlines" \
1686        -variable [itcl::scope _settings(-streamlinesvisible)] \
1687        -command [itcl::code $this AdjustSetting -streamlinesvisible] \
1688        -font "Arial 9"
1689   
1690    checkbutton $inner.lighting \
1691        -text "Enable Lighting" \
1692        -variable [itcl::scope _settings(-streamlineslighting)] \
1693        -command [itcl::code $this AdjustSetting -streamlineslighting] \
1694        -font "Arial 9"
1695
1696    checkbutton $inner.seeds \
1697        -text "Show Seeds" \
1698        -variable [itcl::scope _settings(-streamlinesseedsvisible)] \
1699        -command [itcl::code $this AdjustSetting -streamlinesseedsvisible] \
1700        -font "Arial 9"
1701
1702    label $inner.mode_l -text "Mode" -font "Arial 9"
1703    itk_component add streammode {
1704        Rappture::Combobox $inner.mode -width 10 -editable no
1705    }
1706    $inner.mode choices insert end \
1707        "lines"    "lines" \
1708        "ribbons"   "ribbons" \
1709        "tubes"     "tubes"
1710    $itk_component(streammode) value $_settings(-streamlinesmode)
1711    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting -streamlinesmode]
1712
1713    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1714    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1715        -variable [itcl::scope _settings(-streamlinesopacity)] \
1716        -width 10 \
1717        -showvalue off \
1718        -command [itcl::code $this AdjustSetting -streamlinesopacity]
1719
1720    label $inner.density_l -text "No. Seeds" -font "Arial 9"
1721    ::scale $inner.density -from 1 -to 1000 -orient horizontal \
1722        -variable [itcl::scope _settings(-streamlinesnumseeds)] \
1723        -width 10 \
1724        -showvalue on \
1725        -command [itcl::code $this AdjustSetting -streamlinesnumseeds]
1726
1727    label $inner.scale_l -text "Scale" -font "Arial 9"
1728    ::scale $inner.scale -from 1 -to 100 -orient horizontal \
1729        -variable [itcl::scope _settings(-streamlinesscale)] \
1730        -width 10 \
1731        -showvalue off \
1732        -command [itcl::code $this AdjustSetting -streamlinesscale]
1733
1734    label $inner.field_l -text "Color by" -font "Arial 9"
1735    itk_component add field {
1736        Rappture::Combobox $inner.field -width 10 -editable no
1737    }
1738    bind $inner.field <<Value>> \
1739        [itcl::code $this AdjustSetting -field]
1740
1741    label $inner.colormap_l -text "Colormap" -font "Arial 9"
1742    itk_component add colormap {
1743        Rappture::Combobox $inner.colormap -width 10 -editable no
1744    }
1745    $inner.colormap choices insert end [GetColormapList]
1746
1747    $itk_component(colormap) value "BCGYR"
1748    bind $inner.colormap <<Value>> \
1749        [itcl::code $this AdjustSetting -streamlinescolormap]
1750
1751    blt::table $inner \
1752        0,0 $inner.field_l     -anchor w -pady 2  \
1753        0,1 $inner.field       -fill x   -pady 2  \
1754        1,0 $inner.colormap_l  -anchor w -pady 2  \
1755        1,1 $inner.colormap    -fill x   -pady 2  \
1756        2,0 $inner.mode_l      -anchor w -pady 2  \
1757        2,1 $inner.mode        -fill x   -pady 2  \
1758        3,0 $inner.opacity_l   -anchor w -pady 2  \
1759        3,1 $inner.opacity     -fill x -pady 2 \
1760        5,0 $inner.lighting    -anchor w -pady 2 -cspan 2 \
1761        6,0 $inner.seeds       -anchor w -pady 2 -cspan 2 \
1762        7,0 $inner.density_l   -anchor w -pady 2  \
1763        7,1 $inner.density     -fill x   -pady 2  \
1764
1765    blt::table configure $inner r* c* -resize none
1766    blt::table configure $inner r10 c1 c2 -resize expand
1767}
1768
1769itcl::body Rappture::VtkStreamlinesViewer::BuildAxisTab {} {
1770
1771    set fg [option get $itk_component(hull) font Font]
1772    #set bfg [option get $itk_component(hull) boldFont Font]
1773
1774    set inner [$itk_component(main) insert end \
1775        -title "Axis Settings" \
1776        -icon [Rappture::icon axis2]]
1777    $inner configure -borderwidth 4
1778
1779    checkbutton $inner.visible \
1780        -text "Axes" \
1781        -variable [itcl::scope _settings(-axesvisible)] \
1782        -command [itcl::code $this AdjustSetting -axesvisible] \
1783        -font "Arial 9"
1784
1785    checkbutton $inner.labels \
1786        -text "Axis Labels" \
1787        -variable [itcl::scope _settings(-axislabelsvisible)] \
1788        -command [itcl::code $this AdjustSetting -axislabelsvisible] \
1789        -font "Arial 9"
1790    label $inner.grid_l -text "Grid" -font "Arial 9"
1791    checkbutton $inner.xgrid \
1792        -text "X" \
1793        -variable [itcl::scope _settings(-xgrid)] \
1794        -command [itcl::code $this AdjustSetting -xgrid] \
1795        -font "Arial 9"
1796    checkbutton $inner.ygrid \
1797        -text "Y" \
1798        -variable [itcl::scope _settings(-ygrid)] \
1799        -command [itcl::code $this AdjustSetting -ygrid] \
1800        -font "Arial 9"
1801    checkbutton $inner.zgrid \
1802        -text "Z" \
1803        -variable [itcl::scope _settings(-zgrid)] \
1804        -command [itcl::code $this AdjustSetting -zgrid] \
1805        -font "Arial 9"
1806    checkbutton $inner.minorticks \
1807        -text "Minor Ticks" \
1808        -variable [itcl::scope _settings(-axisminorticks)] \
1809        -command [itcl::code $this AdjustSetting -axisminorticks] \
1810        -font "Arial 9"
1811
1812    label $inner.mode_l -text "Mode" -font "Arial 9"
1813
1814    itk_component add axismode {
1815        Rappture::Combobox $inner.mode -width 10 -editable no
1816    }
1817    $inner.mode choices insert end \
1818        "static_triad"    "static" \
1819        "closest_triad"   "closest" \
1820        "furthest_triad"  "farthest" \
1821        "outer_edges"     "outer"         
1822    $itk_component(axismode) value $_settings(-axismode)
1823    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting -axismode]
1824
1825    blt::table $inner \
1826        0,0 $inner.visible -anchor w -cspan 4 \
1827        1,0 $inner.labels  -anchor w -cspan 4 \
1828        2,0 $inner.minorticks  -anchor w -cspan 4 \
1829        4,0 $inner.grid_l  -anchor w \
1830        4,1 $inner.xgrid   -anchor w \
1831        4,2 $inner.ygrid   -anchor w \
1832        4,3 $inner.zgrid   -anchor w \
1833        5,0 $inner.mode_l  -anchor w -padx { 2 0 } \
1834        5,1 $inner.mode    -fill x   -cspan 3
1835
1836    blt::table configure $inner r* c* -resize none
1837    blt::table configure $inner r7 c6 -resize expand
1838    blt::table configure $inner r3 -height 0.125i
1839}
1840
1841itcl::body Rappture::VtkStreamlinesViewer::BuildCameraTab {} {
1842    set inner [$itk_component(main) insert end \
1843        -title "Camera Settings" \
1844        -icon [Rappture::icon camera]]
1845    $inner configure -borderwidth 4
1846
1847    label $inner.view_l -text "view" -font "Arial 9"
1848    set f [frame $inner.view]
1849    foreach side { front back left right top bottom } {
1850        button $f.$side  -image [Rappture::icon view$side] \
1851            -command [itcl::code $this SetOrientation $side]
1852        Rappture::Tooltip::for $f.$side "Change the view to $side"
1853        pack $f.$side -side left
1854    }
1855
1856    blt::table $inner \
1857        0,0 $inner.view_l -anchor e -pady 2 \
1858        0,1 $inner.view -anchor w -pady 2
1859    blt::table configure $inner r0 -resize none
1860
1861    set labels { qx qy qz qw xpan ypan zoom }
1862    set row 1
1863    foreach tag $labels {
1864        label $inner.${tag}label -text $tag -font "Arial 9"
1865        entry $inner.${tag} -font "Arial 9"  -bg white \
1866            -textvariable [itcl::scope _view(-$tag)]
1867        bind $inner.${tag} <Return> \
1868            [itcl::code $this camera set -${tag}]
1869        bind $inner.${tag} <KP_Enter> \
1870            [itcl::code $this camera set -${tag}]
1871        blt::table $inner \
1872            $row,0 $inner.${tag}label -anchor e -pady 2 \
1873            $row,1 $inner.${tag} -anchor w -pady 2
1874        blt::table configure $inner r$row -resize none
1875        incr row
1876    }
1877    checkbutton $inner.ortho \
1878        -text "Orthographic Projection" \
1879        -variable [itcl::scope _view(-ortho)] \
1880        -command [itcl::code $this camera set -ortho] \
1881        -font "Arial 9"
1882    blt::table $inner \
1883            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1884    blt::table configure $inner r$row -resize none
1885    incr row
1886
1887    blt::table configure $inner c* -resize none
1888    blt::table configure $inner c2 -resize expand
1889    blt::table configure $inner r$row -resize expand
1890}
1891
1892itcl::body Rappture::VtkStreamlinesViewer::BuildCutplaneTab {} {
1893
1894    set fg [option get $itk_component(hull) font Font]
1895   
1896    set inner [$itk_component(main) insert end \
1897        -title "Cutplane Settings" \
1898        -icon [Rappture::icon cutbutton]]
1899
1900    $inner configure -borderwidth 4
1901
1902    checkbutton $inner.visible \
1903        -text "Show Cutplanes" \
1904        -variable [itcl::scope _settings(-cutplanevisible)] \
1905        -command [itcl::code $this AdjustSetting -cutplanevisible] \
1906        -font "Arial 9"
1907
1908    checkbutton $inner.wireframe \
1909        -text "Show Wireframe" \
1910        -variable [itcl::scope _settings(-cutplanewireframe)] \
1911        -command [itcl::code $this AdjustSetting -cutplanewireframe] \
1912        -font "Arial 9"
1913
1914    checkbutton $inner.lighting \
1915        -text "Enable Lighting" \
1916        -variable [itcl::scope _settings(-cutplanelighting)] \
1917        -command [itcl::code $this AdjustSetting -cutplanelighting] \
1918        -font "Arial 9"
1919
1920    checkbutton $inner.edges \
1921        -text "Show Edges" \
1922        -variable [itcl::scope _settings(-cutplaneedges)] \
1923        -command [itcl::code $this AdjustSetting -cutplaneedges] \
1924        -font "Arial 9"
1925
1926    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1927    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1928        -variable [itcl::scope _settings(-cutplaneopacity)] \
1929        -width 10 \
1930        -showvalue off \
1931        -command [itcl::code $this AdjustSetting -cutplaneopacity]
1932    $inner.opacity set $_settings(-cutplaneopacity)
1933
1934    # X-value slicer...
1935    itk_component add xCutButton {
1936        Rappture::PushButton $inner.xbutton \
1937            -onimage [Rappture::icon x-cutplane-red] \
1938            -offimage [Rappture::icon x-cutplane-red] \
1939            -command [itcl::code $this AdjustSetting -cutplanexvisible] \
1940            -variable [itcl::scope _settings(-cutplanexvisible)]
1941    }
1942    Rappture::Tooltip::for $itk_component(xCutButton) \
1943        "Toggle the X-axis cutplane on/off"
1944    $itk_component(xCutButton) select
1945
1946    itk_component add xCutScale {
1947        ::scale $inner.xval -from 100 -to 0 \
1948            -width 10 -orient vertical -showvalue yes \
1949            -borderwidth 1 -highlightthickness 0 \
1950            -command [itcl::code $this EventuallySetCutplane x] \
1951            -variable [itcl::scope _settings(-cutplanexposition)] \
1952            -foreground red3 -font "Arial 9 bold"
1953    } {
1954        usual
1955        ignore -borderwidth -highlightthickness -foreground -font
1956    }
1957    # Set the default cutplane value before disabling the scale.
1958    $itk_component(xCutScale) set 50
1959    $itk_component(xCutScale) configure -state disabled
1960    Rappture::Tooltip::for $itk_component(xCutScale) \
1961        "@[itcl::code $this Slice tooltip x]"
1962
1963    # Y-value slicer...
1964    itk_component add yCutButton {
1965        Rappture::PushButton $inner.ybutton \
1966            -onimage [Rappture::icon y-cutplane-green] \
1967            -offimage [Rappture::icon y-cutplane-green] \
1968            -command [itcl::code $this AdjustSetting -cutplaneyvisible] \
1969            -variable [itcl::scope _settings(-cutplaneyvisible)]
1970    }
1971    Rappture::Tooltip::for $itk_component(yCutButton) \
1972        "Toggle the Y-axis cutplane on/off"
1973    $itk_component(yCutButton) select
1974
1975    itk_component add yCutScale {
1976        ::scale $inner.yval -from 100 -to 0 \
1977            -width 10 -orient vertical -showvalue yes \
1978            -borderwidth 1 -highlightthickness 0 \
1979            -command [itcl::code $this EventuallySetCutplane y] \
1980            -variable [itcl::scope _settings(-cutplaneyposition)] \
1981            -foreground green3 -font "Arial 9 bold"
1982    } {
1983        usual
1984        ignore -borderwidth -highlightthickness -foreground -font
1985    }
1986    Rappture::Tooltip::for $itk_component(yCutScale) \
1987        "@[itcl::code $this Slice tooltip y]"
1988    # Set the default cutplane value before disabling the scale.
1989    $itk_component(yCutScale) set 50
1990    $itk_component(yCutScale) configure -state disabled
1991
1992    # Z-value slicer...
1993    itk_component add zCutButton {
1994        Rappture::PushButton $inner.zbutton \
1995            -onimage [Rappture::icon z-cutplane-blue] \
1996            -offimage [Rappture::icon z-cutplane-blue] \
1997            -command [itcl::code $this AdjustSetting -cutplanezvisible] \
1998            -variable [itcl::scope _settings(-cutplanezvisible)]
1999    }
2000    Rappture::Tooltip::for $itk_component(zCutButton) \
2001        "Toggle the Z-axis cutplane on/off"
2002    $itk_component(zCutButton) select
2003
2004    itk_component add zCutScale {
2005        ::scale $inner.zval -from 100 -to 0 \
2006            -width 10 -orient vertical -showvalue yes \
2007            -borderwidth 1 -highlightthickness 0 \
2008            -command [itcl::code $this EventuallySetCutplane z] \
2009            -variable [itcl::scope _settings(-cutplanezposition)] \
2010            -foreground blue3 -font "Arial 9 bold"
2011    } {
2012        usual
2013        ignore -borderwidth -highlightthickness -foreground -font
2014    }
2015    $itk_component(zCutScale) set 50
2016    $itk_component(zCutScale) configure -state disabled
2017    Rappture::Tooltip::for $itk_component(zCutScale) \
2018        "@[itcl::code $this Slice tooltip z]"
2019
2020    blt::table $inner \
2021        0,0 $inner.lighting             -anchor w -pady 2 -cspan 4 \
2022        1,0 $inner.wireframe            -anchor w -pady 2 -cspan 4 \
2023        2,0 $inner.edges                -anchor w -pady 2 -cspan 4 \
2024        3,0 $inner.opacity_l            -anchor w -pady 2 -cspan 1 \
2025        3,1 $inner.opacity              -fill x   -pady 2 -cspan 3 \
2026        4,0 $itk_component(xCutButton)  -anchor w -padx 2 -pady 2 \
2027        5,0 $itk_component(yCutButton)  -anchor w -padx 2 -pady 2 \
2028        6,0 $itk_component(zCutButton)  -anchor w -padx 2 -pady 2 \
2029        4,1 $itk_component(xCutScale)   -fill y -rspan 4 \
2030        4,2 $itk_component(yCutScale)   -fill y -rspan 4 \
2031        4,3 $itk_component(zCutScale)   -fill y -rspan 4 \
2032
2033    blt::table configure $inner r* c* -resize none
2034    blt::table configure $inner r7 c4 -resize expand
2035}
2036
2037#
2038#  camera --
2039#
2040itcl::body Rappture::VtkStreamlinesViewer::camera {option args} {
2041    switch -- $option {
2042        "show" {
2043            puts [array get _view]
2044        }
2045        "set" {
2046            set what [lindex $args 0]
2047            set x $_view($what)
2048            set code [catch { string is double $x } result]
2049            if { $code != 0 || !$result } {
2050                return
2051            }
2052            switch -- $what {
2053                "-ortho" {
2054                    if {$_view($what)} {
2055                        SendCmd "camera mode ortho"
2056                    } else {
2057                        SendCmd "camera mode persp"
2058                    }
2059                }
2060                "-xpan" - "-ypan" {
2061                    PanCamera
2062                }
2063                "-qx" - "-qy" - "-qz" - "-qw" {
2064                    set q [ViewToQuaternion]
2065                    $_arcball quaternion $q
2066                    EventuallyRotate $q
2067                }
2068                "-zoom" {
2069                    SendCmd "camera zoom $_view($what)"
2070                }
2071            }
2072        }
2073    }
2074}
2075
2076itcl::body Rappture::VtkStreamlinesViewer::GetVtkData { args } {
2077    set bytes ""
2078    foreach dataobj [get] {
2079        foreach comp [$dataobj components] {
2080            set tag $dataobj-$comp
2081            set contents [$dataobj vtkdata $comp]
2082            append bytes "$contents\n"
2083        }
2084    }
2085    return [list .vtk $bytes]
2086}
2087
2088itcl::body Rappture::VtkStreamlinesViewer::GetImage { args } {
2089    if { [image width $_image(download)] > 0 &&
2090         [image height $_image(download)] > 0 } {
2091        set bytes [$_image(download) data -format "jpeg -quality 100"]
2092        set bytes [Rappture::encoding::decode -as b64 $bytes]
2093        return [list .jpg $bytes]
2094    }
2095    return ""
2096}
2097
2098itcl::body Rappture::VtkStreamlinesViewer::BuildDownloadPopup { popup command } {
2099    Rappture::Balloon $popup \
2100        -title "[Rappture::filexfer::label downloadWord] as..."
2101    set inner [$popup component inner]
2102    label $inner.summary -text "" -anchor w
2103    radiobutton $inner.vtk_button -text "VTK data file" \
2104        -variable [itcl::scope _downloadPopup(format)] \
2105        -font "Helvetica 9 " \
2106        -value vtk 
2107    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2108    radiobutton $inner.image_button -text "Image File" \
2109        -variable [itcl::scope _downloadPopup(format)] \
2110        -value image
2111    Rappture::Tooltip::for $inner.image_button \
2112        "Save as digital image."
2113
2114    button $inner.ok -text "Save" \
2115        -highlightthickness 0 -pady 2 -padx 3 \
2116        -command $command \
2117        -compound left \
2118        -image [Rappture::icon download]
2119
2120    button $inner.cancel -text "Cancel" \
2121        -highlightthickness 0 -pady 2 -padx 3 \
2122        -command [list $popup deactivate] \
2123        -compound left \
2124        -image [Rappture::icon cancel]
2125
2126    blt::table $inner \
2127        0,0 $inner.summary -cspan 2  \
2128        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2129        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2130        4,1 $inner.cancel -width .9i -fill y \
2131        4,0 $inner.ok -padx 2 -width .9i -fill y
2132    blt::table configure $inner r3 -height 4
2133    blt::table configure $inner r4 -pady 4
2134    raise $inner.image_button
2135    $inner.vtk_button invoke
2136    return $inner
2137}
2138
2139itcl::body Rappture::VtkStreamlinesViewer::SetObjectStyle { dataobj comp } {
2140    # Parse style string.
2141    set tag $dataobj-$comp
2142    set style [$dataobj style $comp]
2143    array set settings {
2144        -color \#808080
2145        -edges 0
2146        -edgecolor black
2147        -linewidth 1.0
2148        -opacity 0.4
2149        -wireframe 0
2150        -lighting 1
2151        -seeds 1
2152        -seedcolor white
2153        -visible 1
2154    }
2155    if { $dataobj != $_first } {
2156        set settings(-opacity) 1
2157    }
2158    array set settings $style
2159    StartBufferingCommands
2160    SendCmd "streamlines add $tag"
2161    SendCmd "streamlines seed visible off $tag"
2162    set seeds [$dataobj hints seeds]
2163    if { $seeds != "" && ![info exists _seeds($dataobj)] } {
2164        set length [string length $seeds]
2165        SendCmd "streamlines seed fmesh 200 data follows $length $tag"
2166        append _outbuf $seeds
2167        set _seeds($dataobj) 1
2168    }
2169    SendCmd "cutplane add $tag"
2170    SendCmd "polydata add $tag"
2171    SendCmd "polydata colormode constant {} $tag"
2172    set _settings(-volumeedges) $settings(-edges)
2173    set _settings(-volumelighting) $settings(-lighting)
2174    set _settings(-volumeopacity) $settings(-opacity)
2175    set _settings(-volumewireframe) $settings(-wireframe)
2176    set _settings(-volumeopacity) [expr $settings(-opacity) * 100.0]
2177    StopBufferingCommands
2178    SetColormap $dataobj $comp
2179}
2180
2181itcl::body Rappture::VtkStreamlinesViewer::IsValidObject { dataobj } {
2182    if {[catch {$dataobj isa Rappture::Field} valid] != 0 || !$valid} {
2183        return 0
2184    }
2185    return 1
2186}
2187
2188# ----------------------------------------------------------------------
2189# USAGE: ReceiveLegend <colormap> <title> <vmin> <vmax> <size>
2190#
2191# Invoked automatically whenever the "legend" command comes in from
2192# the rendering server.  Indicates that binary image data with the
2193# specified <size> will follow.
2194# ----------------------------------------------------------------------
2195itcl::body Rappture::VtkStreamlinesViewer::ReceiveLegend { colormap title vmin vmax size } {
2196    set _legendPending 0
2197    set _title $title
2198    regsub {\(mag\)} $title "" _title
2199    if { [IsConnected] } {
2200        set bytes [ReceiveBytes $size]
2201        if { ![info exists _image(legend)] } {
2202            set _image(legend) [image create photo]
2203        }
2204        $_image(legend) configure -data $bytes
2205        #puts stderr "read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
2206        if { [catch {DrawLegend} errs] != 0 } {
2207            puts stderr errs=$errs
2208        }
2209    }
2210}
2211
2212#
2213# DrawLegend --
2214#
2215#       Draws the legend in it's own canvas which resides to the right
2216#       of the contour plot area.
2217#
2218itcl::body Rappture::VtkStreamlinesViewer::DrawLegend {} {
2219    set fname $_curFldName
2220    set c $itk_component(view)
2221    set w [winfo width $c]
2222    set h [winfo height $c]
2223    set font "Arial 8"
2224    set lineht [font metrics $font -linespace]
2225   
2226    if { [info exists _fields($fname)] } {
2227        foreach { title units } $_fields($fname) break
2228        if { $units != "" } {
2229            set title [format "%s (%s)" $title $units]
2230        }
2231    } else {
2232        set title $fname
2233    }
2234    if { $_settings(-legendvisible) } {
2235        set x [expr $w - 2]
2236        if { [$c find withtag "legend"] == "" } {
2237            set y 2
2238            $c create text $x $y \
2239                -anchor ne \
2240                -fill $itk_option(-plotforeground) -tags "title legend" \
2241                -font $font
2242            incr y $lineht
2243            $c create text $x $y \
2244                -anchor ne \
2245                -fill $itk_option(-plotforeground) -tags "vmax legend" \
2246                -font $font
2247            incr y $lineht
2248            $c create image $x $y \
2249                -anchor ne \
2250                -image $_image(legend) -tags "colormap legend"
2251            $c create text $x [expr {$h-2}] \
2252                -anchor se \
2253                -fill $itk_option(-plotforeground) -tags "vmin legend" \
2254                -font $font
2255            #$c bind colormap <Enter> [itcl::code $this EnterLegend %x %y]
2256            $c bind colormap <Leave> [itcl::code $this LeaveLegend]
2257            $c bind colormap <Motion> [itcl::code $this MotionLegend %x %y]
2258        }
2259        $c bind title <ButtonPress> [itcl::code $this Combo post]
2260        $c bind title <Enter> [itcl::code $this Combo activate]
2261        $c bind title <Leave> [itcl::code $this Combo deactivate]
2262        # Reset the item coordinates according the current size of the plot.
2263        $c itemconfigure title -text $title
2264        if { [info exists _limits($_curFldName)] } {
2265            foreach {vmin vmax} $_limits($_curFldName) break
2266            $c itemconfigure vmin -text [format %g $vmin]
2267            $c itemconfigure vmax -text [format %g $vmax]
2268        }
2269        set y 2
2270        $c coords title $x $y
2271        incr y $lineht
2272        $c coords vmax $x $y
2273        incr y $lineht
2274        $c coords colormap $x $y
2275        $c coords vmin $x [expr {$h - 2}]
2276    }
2277}
2278
2279#
2280# EnterLegend --
2281#
2282itcl::body Rappture::VtkStreamlinesViewer::EnterLegend { x y } {
2283    SetLegendTip $x $y
2284}
2285
2286#
2287# MotionLegend --
2288#
2289itcl::body Rappture::VtkStreamlinesViewer::MotionLegend { x y } {
2290    Rappture::Tooltip::tooltip cancel
2291    set c $itk_component(view)
2292    SetLegendTip $x $y
2293}
2294
2295#
2296# LeaveLegend --
2297#
2298itcl::body Rappture::VtkStreamlinesViewer::LeaveLegend { } {
2299    Rappture::Tooltip::tooltip cancel
2300    .rappturetooltip configure -icon ""
2301}
2302
2303#
2304# SetLegendTip --
2305#
2306itcl::body Rappture::VtkStreamlinesViewer::SetLegendTip { x y } {
2307    set c $itk_component(view)
2308    set w [winfo width $c]
2309    set h [winfo height $c]
2310    set font "Arial 8"
2311    set lineht [font metrics $font -linespace]
2312   
2313    set imgHeight [image height $_image(legend)]
2314    set coords [$c coords colormap]
2315    set imgX [expr $w - [image width $_image(legend)] - 2]
2316    set imgY [expr $y - 2 * ($lineht + 2)]
2317
2318    if { [info exists _fields($_title)] } {
2319        foreach { title units } $_fields($_title) break
2320        if { $units != "" } {
2321            set title [format "%s (%s)" $title $units]
2322        }
2323    } else {
2324        set title $_title
2325    }
2326    # Make a swatch of the selected color
2327    if { [catch { $_image(legend) get 10 $imgY } pixel] != 0 } {
2328        #puts stderr "out of range: $imgY"
2329        return
2330    }
2331    if { ![info exists _image(swatch)] } {
2332        set _image(swatch) [image create photo -width 24 -height 24]
2333    }
2334    set color [eval format "\#%02x%02x%02x" $pixel]
2335    $_image(swatch) put black  -to 0 0 23 23
2336    $_image(swatch) put $color -to 1 1 22 22
2337    .rappturetooltip configure -icon $_image(swatch)
2338
2339    # Compute the value of the point
2340    if { [info exists _limits($_curFldName)] } {
2341        foreach {vmin vmax} $_limits($_curFldName) break
2342        set t [expr 1.0 - (double($imgY) / double($imgHeight-1))]
2343        set value [expr $t * ($vmax - $vmin) + $vmin]
2344    } else {
2345        set value 0.0
2346    }
2347    set tipx [expr $x + 15]
2348    set tipy [expr $y - 5]
2349    Rappture::Tooltip::text $c "$title $value"
2350    Rappture::Tooltip::tooltip show $c +$tipx,+$tipy   
2351}
2352
2353# ----------------------------------------------------------------------
2354# USAGE: Slice move x|y|z <newval>
2355#
2356# Called automatically when the user drags the slider to move the
2357# cut plane that slices 3D data.  Gets the current value from the
2358# slider and moves the cut plane to the appropriate point in the
2359# data set.
2360# ----------------------------------------------------------------------
2361itcl::body Rappture::VtkStreamlinesViewer::Slice {option args} {
2362    switch -- $option {
2363        "move" {
2364            set axis [lindex $args 0]
2365            set newval [lindex $args 1]
2366            if {[llength $args] != 2} {
2367                error "wrong # args: should be \"Slice move x|y|z newval\""
2368            }
2369            set newpos [expr {0.01*$newval}]
2370            SendCmd "cutplane slice $axis $newpos"
2371        }
2372        "tooltip" {
2373            set axis [lindex $args 0]
2374            set val [$itk_component(${axis}CutScale) get]
2375            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
2376        }
2377        default {
2378            error "bad option \"$option\": should be axis, move, or tooltip"
2379        }
2380    }
2381}
2382
2383# ----------------------------------------------------------------------
2384# USAGE: _dropdown post
2385# USAGE: _dropdown unpost
2386# USAGE: _dropdown select
2387#
2388# Used internally to handle the dropdown list for this combobox.  The
2389# post/unpost options are invoked when the list is posted or unposted
2390# to manage the relief of the controlling button.  The select option
2391# is invoked whenever there is a selection from the list, to assign
2392# the value back to the gauge.
2393# ----------------------------------------------------------------------
2394itcl::body Rappture::VtkStreamlinesViewer::Combo {option} {
2395    set c $itk_component(view)
2396    switch -- $option {
2397        post {
2398            foreach { x1 y1 x2 y2 } [$c bbox title] break
2399            set x1 [expr [winfo width $itk_component(view)] - [winfo reqwidth $itk_component(fieldmenu)]]
2400            set x [expr $x1 + [winfo rootx $itk_component(view)]]
2401            set y [expr $y2 + [winfo rooty $itk_component(view)]]
2402            tk_popup $itk_component(fieldmenu) $x $y
2403        }
2404        activate {
2405            $c itemconfigure title -fill red
2406        }
2407        deactivate {
2408            $c itemconfigure title -fill white
2409        }
2410        invoke {
2411            $itk_component(field) value $_curFldLabel
2412            AdjustSetting -field
2413        }
2414        default {
2415            error "bad option \"$option\": should be post, unpost, select"
2416        }
2417    }
2418}
2419
2420itcl::body Rappture::VtkStreamlinesViewer::SetOrientation { side } {
2421    array set positions {
2422        front "1 0 0 0"
2423        back  "0 0 1 0"
2424        left  "0.707107 0 -0.707107 0"
2425        right "0.707107 0 0.707107 0"
2426        top   "0.707107 -0.707107 0 0"
2427        bottom "0.707107 0.707107 0 0"
2428    }
2429    foreach name { -qw -qx -qy -qz } value $positions($side) {
2430        set _view($name) $value
2431    }
2432    set q [ViewToQuaternion]
2433    $_arcball quaternion $q
2434    SendCmd "camera orient $q"
2435    SendCmd "camera reset"
2436    set _view(-xpan) 0
2437    set _view(-ypan) 0
2438    set _view(-zoom) 1.0
2439}
Note: See TracBrowser for help on using the repository browser.