source: branches/1.4/gui/scripts/vtkstreamlinesviewer.tcl @ 5766

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

Merge r5758:r5765 from trunk

File size: 85.8 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 BuildSurfaceTab {}
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 _obj2ovride   ;    # maps dataobj => style override
119    private variable _datasets     ;    # contains all the dataobj-component
120                                   ;    # datasets in the server
121    private variable _colormaps    ;    # contains all the colormaps
122                                   ;    # in the server.
123    private variable _dataset2style    ;# maps dataobj-component to transfunc
124
125    private variable _click        ;    # info used for rotate operations
126    private variable _limits       ;    # autoscale min/max for all axes
127    private variable _view         ;    # view params for 3D view
128    private variable _settings
129    private variable _style;            # Array of current component styles.
130    private variable _initialStyle;     # Array of initial component styles.
131    private variable _reset 1;          # Connection to server has been reset.
132
133    private variable _first ""     ;    # This is the topmost dataset.
134    private variable _start 0
135    private variable _title ""
136    private variable _seeds
137    private variable _width 0
138    private variable _height 0
139    private variable _resizePending 0
140    private variable _reseedPending 0
141    private variable _rotatePending 0
142    private variable _cutplanePending 0
143    private variable _legendPending 0
144    private variable _fields
145    private variable _curFldName ""
146    private variable _curFldLabel ""
147    private variable _field      ""
148    private variable _numSeeds 200
149    private variable _colorMode "vmag";#  Mode of colormap (vmag or scalar)
150
151    private common _downloadPopup;      # download options from popup
152    private common _hardcopy
153}
154
155itk::usual VtkStreamlinesViewer {
156    keep -background -foreground -cursor -font
157    keep -plotbackground -plotforeground
158}
159
160# ----------------------------------------------------------------------
161# CONSTRUCTOR
162# ----------------------------------------------------------------------
163itcl::body Rappture::VtkStreamlinesViewer::constructor {hostlist args} {
164    set _serverType "vtkvis"
165
166    EnableWaitDialog 2000
167
168    # Rebuild event
169    $_dispatcher register !rebuild
170    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
171
172    # Resize event
173    $_dispatcher register !resize
174    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
175
176    # Reseed event
177    $_dispatcher register !reseed
178    $_dispatcher dispatch $this !reseed "[itcl::code $this DoReseed]; list"
179
180    # Rotate event
181    $_dispatcher register !rotate
182    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
183
184    # Legend event
185    $_dispatcher register !legend
186    $_dispatcher dispatch $this !legend "[itcl::code $this RequestLegend]; list"
187
188    # X-Cutplane event
189    $_dispatcher register !xcutplane
190    $_dispatcher dispatch $this !xcutplane \
191        "[itcl::code $this AdjustSetting -cutplanexposition]; list"
192
193    # Y-Cutplane event
194    $_dispatcher register !ycutplane
195    $_dispatcher dispatch $this !ycutplane \
196        "[itcl::code $this AdjustSetting -cutplaneyposition]; list"
197
198    # Z-Cutplane event
199    $_dispatcher register !zcutplane
200    $_dispatcher dispatch $this !zcutplane \
201        "[itcl::code $this AdjustSetting -cutplanezposition]; list"
202
203    #
204    # Populate parser with commands handle incoming requests
205    #
206    $_parser alias image [itcl::code $this ReceiveImage]
207    $_parser alias dataset [itcl::code $this ReceiveDataset]
208    $_parser alias legend [itcl::code $this ReceiveLegend]
209
210    # Initialize the view to some default parameters.
211    array set _view {
212        -ortho           0
213        -qw              0.853553
214        -qx              -0.353553
215        -qy              0.353553
216        -qz              0.146447
217        -xpan            0
218        -ypan            0
219        -zoom            1.0
220    }
221    set _arcball [blt::arcball create 100 100]
222    $_arcball quaternion [ViewToQuaternion]
223
224    array set _settings [subst {
225        -axesvisible                1
226        -axislabelsvisible          1
227        -axisminorticks             1
228        -axismode                   "static"
229        -cutplaneedges              0
230        -cutplanelighting           1
231        -cutplaneopacity            100
232        -cutplanevisible            0
233        -cutplanewireframe          0
234        -cutplanexposition          50
235        -cutplanexvisible           1
236        -cutplaneyposition          50
237        -cutplaneyvisible           1
238        -cutplanezposition          50
239        -cutplanezvisible           1
240        -legendvisible              1
241        -streamlineslighting        1
242        -streamlinesmode            lines
243        -streamlinesnumseeds        200
244        -streamlinesopacity         100
245        -streamlineslength          70
246        -streamlinesseedsvisible    0
247        -streamlinesvisible         1
248        -surfaceedges               0
249        -surfacelighting            1
250        -surfaceopacity             40
251        -surfacevisible             1
252        -surfacewireframe           0
253        -xgrid                      0
254        -ygrid                      0
255        -zgrid                      0
256    }]
257
258    itk_component add view {
259        canvas $itk_component(plotarea).view \
260            -highlightthickness 0 -borderwidth 0
261    } {
262        usual
263        ignore -highlightthickness -borderwidth  -background
264    }
265
266    itk_component add fieldmenu {
267        menu $itk_component(plotarea).menu -bg black -fg white -relief flat \
268            -tearoff no
269    } {
270        usual
271        ignore -background -foreground -relief -tearoff
272    }
273    set c $itk_component(view)
274    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
275    bind $c <4> [itcl::code $this Zoom in 0.25]
276    bind $c <5> [itcl::code $this Zoom out 0.25]
277    bind $c <KeyPress-Left>  [list %W xview scroll 10 units]
278    bind $c <KeyPress-Right> [list %W xview scroll -10 units]
279    bind $c <KeyPress-Up>    [list %W yview scroll 10 units]
280    bind $c <KeyPress-Down>  [list %W yview scroll -10 units]
281    bind $c <Enter> "focus %W"
282    bind $c <Control-F1> [itcl::code $this ToggleConsole]
283
284    # Fix the scrollregion in case we go off screen
285    $c configure -scrollregion [$c bbox all]
286
287    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
288    set _map(cwidth) -1
289    set _map(cheight) -1
290    set _map(zoom) 1.0
291    set _map(original) ""
292
293    set f [$itk_component(main) component controls]
294    itk_component add reset {
295        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
296            -highlightthickness 0 \
297            -image [Rappture::icon reset-view] \
298            -command [itcl::code $this Zoom reset]
299    } {
300        usual
301        ignore -highlightthickness
302    }
303    pack $itk_component(reset) -side top -padx 2 -pady 2
304    Rappture::Tooltip::for $itk_component(reset) "Reset the view to the default zoom level"
305
306    itk_component add zoomin {
307        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
308            -highlightthickness 0 \
309            -image [Rappture::icon zoom-in] \
310            -command [itcl::code $this Zoom in]
311    } {
312        usual
313        ignore -highlightthickness
314    }
315    pack $itk_component(zoomin) -side top -padx 2 -pady 2
316    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
317
318    itk_component add zoomout {
319        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
320            -highlightthickness 0 \
321            -image [Rappture::icon zoom-out] \
322            -command [itcl::code $this Zoom out]
323    } {
324        usual
325        ignore -highlightthickness
326    }
327    pack $itk_component(zoomout) -side top -padx 2 -pady 2
328    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
329
330    itk_component add surface {
331        Rappture::PushButton $f.surface \
332            -onimage [Rappture::icon volume-on] \
333            -offimage [Rappture::icon volume-off] \
334            -variable [itcl::scope _settings(-surfacevisible)] \
335            -command [itcl::code $this AdjustSetting -surfacevisible]
336    }
337    $itk_component(surface) select
338    Rappture::Tooltip::for $itk_component(surface) \
339        "Show/Hide the boundary surface"
340    pack $itk_component(surface) -padx 2 -pady 2
341
342    itk_component add streamlines {
343        Rappture::PushButton $f.streamlines \
344            -onimage [Rappture::icon streamlines-on] \
345            -offimage [Rappture::icon streamlines-off] \
346            -variable [itcl::scope _settings(-streamlinesvisible)] \
347            -command [itcl::code $this AdjustSetting -streamlinesvisible] \
348    }
349    $itk_component(streamlines) select
350    Rappture::Tooltip::for $itk_component(streamlines) \
351        "Show/Hide the streamlines"
352    pack $itk_component(streamlines) -padx 2 -pady 2
353
354    itk_component add cutplane {
355        Rappture::PushButton $f.cutplane \
356            -onimage [Rappture::icon cutbutton] \
357            -offimage [Rappture::icon cutbutton] \
358            -variable [itcl::scope _settings(-cutplanevisible)] \
359            -command [itcl::code $this AdjustSetting -cutplanevisible]
360    }
361    Rappture::Tooltip::for $itk_component(cutplane) \
362        "Show/Hide the cutplanes"
363    pack $itk_component(cutplane) -padx 2 -pady 2
364
365    if { [catch {
366        BuildSurfaceTab
367        BuildStreamsTab
368        BuildCutplaneTab
369        BuildAxisTab
370        BuildCameraTab
371    } errs] != 0 } {
372        puts stderr errs=$errs
373    }
374    # Legend
375
376    set _image(legend) [image create photo]
377    itk_component add legend {
378        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
379    } {
380        usual
381        ignore -highlightthickness
382        rename -background -plotbackground plotBackground Background
383    }
384
385    # Hack around the Tk panewindow.  The problem is that the requested
386    # size of the 3d view isn't set until an image is retrieved from
387    # the server.  So the panewindow uses the tiny size.
388    set w 10000
389    pack forget $itk_component(view)
390    blt::table $itk_component(plotarea) \
391        0,0 $itk_component(view) -fill both -reqwidth $w
392    blt::table configure $itk_component(plotarea) c1 -resize none
393
394    # Bindings for rotation via mouse
395    bind $itk_component(view) <ButtonPress-1> \
396        [itcl::code $this Rotate click %x %y]
397    bind $itk_component(view) <B1-Motion> \
398        [itcl::code $this Rotate drag %x %y]
399    bind $itk_component(view) <ButtonRelease-1> \
400        [itcl::code $this Rotate release %x %y]
401
402    # Bindings for panning via mouse
403    bind $itk_component(view) <ButtonPress-2> \
404        [itcl::code $this Pan click %x %y]
405    bind $itk_component(view) <B2-Motion> \
406        [itcl::code $this Pan drag %x %y]
407    bind $itk_component(view) <ButtonRelease-2> \
408        [itcl::code $this Pan release %x %y]
409
410    #bind $itk_component(view) <ButtonRelease-3> \
411    #    [itcl::code $this Pick %x %y]
412
413    # Bindings for panning via keyboard
414    bind $itk_component(view) <KeyPress-Left> \
415        [itcl::code $this Pan set -10 0]
416    bind $itk_component(view) <KeyPress-Right> \
417        [itcl::code $this Pan set 10 0]
418    bind $itk_component(view) <KeyPress-Up> \
419        [itcl::code $this Pan set 0 -10]
420    bind $itk_component(view) <KeyPress-Down> \
421        [itcl::code $this Pan set 0 10]
422    bind $itk_component(view) <Shift-KeyPress-Left> \
423        [itcl::code $this Pan set -2 0]
424    bind $itk_component(view) <Shift-KeyPress-Right> \
425        [itcl::code $this Pan set 2 0]
426    bind $itk_component(view) <Shift-KeyPress-Up> \
427        [itcl::code $this Pan set 0 -2]
428    bind $itk_component(view) <Shift-KeyPress-Down> \
429        [itcl::code $this Pan set 0 2]
430
431    # Bindings for zoom via keyboard
432    bind $itk_component(view) <KeyPress-Prior> \
433        [itcl::code $this Zoom out]
434    bind $itk_component(view) <KeyPress-Next> \
435        [itcl::code $this Zoom in]
436
437    bind $itk_component(view) <Enter> "focus $itk_component(view)"
438
439    if {[string equal "x11" [tk windowingsystem]]} {
440        # Bindings for zoom via mouse
441        bind $itk_component(view) <4> [itcl::code $this Zoom out]
442        bind $itk_component(view) <5> [itcl::code $this Zoom in]
443    }
444
445    set _image(download) [image create photo]
446
447    eval itk_initialize $args
448    Connect
449}
450
451# ----------------------------------------------------------------------
452# DESTRUCTOR
453# ----------------------------------------------------------------------
454itcl::body Rappture::VtkStreamlinesViewer::destructor {} {
455    Disconnect
456    image delete $_image(plot)
457    image delete $_image(download)
458    catch { blt::arcball destroy $_arcball }
459}
460
461itcl::body Rappture::VtkStreamlinesViewer::DoResize {} {
462    if { $_width < 2 } {
463        set _width 500
464    }
465    if { $_height < 2 } {
466        set _height 500
467    }
468    set _start [clock clicks -milliseconds]
469    SendCmd "screen size $_width $_height"
470
471    set _legendPending 1
472    set _resizePending 0
473}
474
475itcl::body Rappture::VtkStreamlinesViewer::DoRotate {} {
476    SendCmd "camera orient [ViewToQuaternion]"
477    set _rotatePending 0
478}
479
480itcl::body Rappture::VtkStreamlinesViewer::DoReseed {} {
481    foreach dataset [CurrentDatasets -visible] {
482        foreach {dataobj comp} [split $dataset -] break
483        # This command works for either random or fmesh seeds
484        SendCmd "streamlines seed numpts $_numSeeds $dataset"
485    }
486    set _reseedPending 0
487}
488
489itcl::body Rappture::VtkStreamlinesViewer::EventuallyResize { w h } {
490    set _width $w
491    set _height $h
492    $_arcball resize $w $h
493    if { !$_resizePending } {
494        set _resizePending 1
495        $_dispatcher event -after 400 !resize
496    }
497}
498
499itcl::body Rappture::VtkStreamlinesViewer::EventuallyReseed { numPoints } {
500    set _numSeeds $numPoints
501    if { !$_reseedPending } {
502        set _reseedPending 1
503        $_dispatcher event -after 600 !reseed
504    }
505}
506
507set rotate_delay 100
508
509itcl::body Rappture::VtkStreamlinesViewer::EventuallyRotate { q } {
510    QuaternionToView $q
511    if { !$_rotatePending } {
512        set _rotatePending 1
513        global rotate_delay
514        $_dispatcher event -after $rotate_delay !rotate
515    }
516}
517
518itcl::body Rappture::VtkStreamlinesViewer::EventuallySetCutplane { axis args } {
519    if { !$_cutplanePending } {
520        set _cutplanePending 1
521        $_dispatcher event -after 100 !${axis}cutplane
522    }
523}
524
525# ----------------------------------------------------------------------
526# USAGE: add <dataobj> ?<settings>?
527#
528# Clients use this to add a data object to the plot.  The optional
529# <settings> are used to configure the plot.  Allowed settings are
530# -color, -brightness, -width, -linestyle, and -raise.
531# ----------------------------------------------------------------------
532itcl::body Rappture::VtkStreamlinesViewer::add {dataobj {settings ""}} {
533    array set params {
534        -color auto
535        -width 1
536        -linestyle solid
537        -brightness 0
538        -raise 0
539        -description ""
540        -param ""
541        -type ""
542    }
543    array set params $settings
544    set params(-description) ""
545    set params(-param) ""
546    array set params $settings
547
548    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
549        # can't handle -autocolors yet
550        set params(-color) black
551    }
552    set pos [lsearch -exact $_dlist $dataobj]
553    if {$pos < 0} {
554        lappend _dlist $dataobj
555    }
556    set _obj2ovride($dataobj-color) $params(-color)
557    set _obj2ovride($dataobj-width) $params(-width)
558    set _obj2ovride($dataobj-raise) $params(-raise)
559    $_dispatcher event -idle !rebuild
560}
561
562
563# ----------------------------------------------------------------------
564# USAGE: delete ?<dataobj1> <dataobj2> ...?
565#
566#       Clients use this to delete a dataobj from the plot.  If no dataobjs
567#       are specified, then all dataobjs are deleted.  No data objects are
568#       deleted.  They are only removed from the display list.
569#
570# ----------------------------------------------------------------------
571itcl::body Rappture::VtkStreamlinesViewer::delete {args} {
572    if { [llength $args] == 0} {
573        set args $_dlist
574    }
575    # Delete all specified dataobjs
576    set changed 0
577    foreach dataobj $args {
578        set pos [lsearch -exact $_dlist $dataobj]
579        if { $pos < 0 } {
580            continue;                   # Don't know anything about it.
581        }
582        # Remove it from the dataobj list.
583        set _dlist [lreplace $_dlist $pos $pos]
584        array unset _obj2ovride $dataobj-*
585        array unset _settings $dataobj-*
586        set changed 1
587    }
588    # If anything changed, then rebuild the plot
589    if { $changed } {
590        $_dispatcher event -idle !rebuild
591    }
592}
593
594# ----------------------------------------------------------------------
595# USAGE: get ?-objects?
596# USAGE: get ?-visible?
597# USAGE: get ?-image view?
598#
599# Clients use this to query the list of objects being plotted, in
600# order from bottom to top of this result.  The optional "-image"
601# flag can also request the internal images being shown.
602# ----------------------------------------------------------------------
603itcl::body Rappture::VtkStreamlinesViewer::get {args} {
604    if {[llength $args] == 0} {
605        set args "-objects"
606    }
607
608    set op [lindex $args 0]
609    switch -- $op {
610        "-objects" {
611            # put the dataobj list in order according to -raise options
612            set dlist {}
613            foreach dataobj $_dlist {
614                if { ![IsValidObject $dataobj] } {
615                    continue
616                }
617                if {[info exists _obj2ovride($dataobj-raise)] &&
618                    $_obj2ovride($dataobj-raise)} {
619                    set dlist [linsert $dlist 0 $dataobj]
620                } else {
621                    lappend dlist $dataobj
622                }
623            }
624            return $dlist
625        }
626        "-visible" {
627            set dlist {}
628            foreach dataobj $_dlist {
629                if { ![IsValidObject $dataobj] } {
630                    continue
631                }
632                if { ![info exists _obj2ovride($dataobj-raise)] } {
633                    # No setting indicates that the object isn't visible.
634                    continue
635                }
636                # Otherwise use the -raise parameter to put the object to
637                # the front of the list.
638                if { $_obj2ovride($dataobj-raise) } {
639                    set dlist [linsert $dlist 0 $dataobj]
640                } else {
641                    lappend dlist $dataobj
642                }
643            }
644            return $dlist
645        }
646        -image {
647            if {[llength $args] != 2} {
648                error "wrong # args: should be \"get -image view\""
649            }
650            switch -- [lindex $args end] {
651                view {
652                    return $_image(plot)
653                }
654                default {
655                    error "bad image name \"[lindex $args end]\": should be view"
656                }
657            }
658        }
659        default {
660            error "bad option \"$op\": should be -objects or -image"
661        }
662    }
663}
664
665# ----------------------------------------------------------------------
666# USAGE: scale ?<data1> <data2> ...?
667#
668# Sets the default limits for the overall plot according to the
669# limits of the data for all of the given <data> objects.  This
670# accounts for all objects--even those not showing on the screen.
671# Because of this, the limits are appropriate for all objects as
672# the user scans through data in the ResultSet viewer.
673# ----------------------------------------------------------------------
674itcl::body Rappture::VtkStreamlinesViewer::scale {args} {
675    foreach dataobj $args {
676        foreach axis { x y z } {
677            set lim [$dataobj limits $axis]
678            if { ![info exists _limits($axis)] } {
679                set _limits($axis) $lim
680                continue
681            }
682            foreach {min max} $lim break
683            foreach {amin amax} $_limits($axis) break
684            if { $amin > $min } {
685                set amin $min
686            }
687            if { $amax < $max } {
688                set amax $max
689            }
690            set _limits($axis) [list $amin $amax]
691        }
692        foreach { fname lim } [$dataobj fieldlimits] {
693            if { ![info exists _limits($fname)] } {
694                set _limits($fname) $lim
695                continue
696            }
697            foreach {min max} $lim break
698            foreach {fmin fmax} $_limits($fname) break
699            if { $fmin > $min } {
700                set fmin $min
701            }
702            if { $fmax < $max } {
703                set fmax $max
704            }
705            set _limits($fname) [list $fmin $fmax]
706        }
707    }
708}
709
710# ----------------------------------------------------------------------
711# USAGE: download coming
712# USAGE: download controls <downloadCommand>
713# USAGE: download now
714#
715# Clients use this method to create a downloadable representation
716# of the plot.  Returns a list of the form {ext string}, where
717# "ext" is the file extension (indicating the type of data) and
718# "string" is the data itself.
719# ----------------------------------------------------------------------
720itcl::body Rappture::VtkStreamlinesViewer::download {option args} {
721    switch $option {
722        coming {
723            if {[catch {
724                blt::winop snap $itk_component(plotarea) $_image(download)
725            }]} {
726                $_image(download) configure -width 1 -height 1
727                $_image(download) put #000000
728            }
729        }
730        controls {
731            set popup .vtkviewerdownload
732            if { ![winfo exists .vtkviewerdownload] } {
733                set inner [BuildDownloadPopup $popup [lindex $args 0]]
734            } else {
735                set inner [$popup component inner]
736            }
737            set _downloadPopup(image_controls) $inner.image_frame
738            set num [llength [get]]
739            set num [expr {($num == 1) ? "1 result" : "$num results"}]
740            set word [Rappture::filexfer::label downloadWord]
741            $inner.summary configure -text "$word $num in the following format:"
742            update idletasks            ;# Fix initial sizes
743            return $popup
744        }
745        now {
746            set popup .vtkviewerdownload
747            if {[winfo exists .vtkviewerdownload]} {
748                $popup deactivate
749            }
750            switch -- $_downloadPopup(format) {
751                "image" {
752                    return [$this GetImage [lindex $args 0]]
753                }
754                "vtk" {
755                    return [$this GetVtkData [lindex $args 0]]
756                }
757            }
758            return ""
759        }
760        default {
761            error "bad option \"$option\": should be coming, controls, now"
762        }
763    }
764}
765
766# ----------------------------------------------------------------------
767# USAGE: Connect ?<host:port>,<host:port>...?
768#
769# Clients use this method to establish a connection to a new
770# server, or to reestablish a connection to the previous server.
771# Any existing connection is automatically closed.
772# ----------------------------------------------------------------------
773itcl::body Rappture::VtkStreamlinesViewer::Connect {} {
774    set _hosts [GetServerList "vtkvis"]
775    if { "" == $_hosts } {
776        return 0
777    }
778    set _reset 1
779    set result [VisViewer::Connect $_hosts]
780    if { $result } {
781        if { $_reportClientInfo }  {
782            # Tell the server the viewer, hub, user and session.
783            # Do this immediately on connect before buffering any commands
784            global env
785
786            set info {}
787            set user "???"
788            if { [info exists env(USER)] } {
789                set user $env(USER)
790            }
791            set session "???"
792            if { [info exists env(SESSION)] } {
793                set session $env(SESSION)
794            }
795            lappend info "version" "$Rappture::version"
796            lappend info "build" "$Rappture::build"
797            lappend info "svnurl" "$Rappture::svnurl"
798            lappend info "installdir" "$Rappture::installdir"
799            lappend info "hub" [exec hostname]
800            lappend info "client" "vtkstreamlinesviewer"
801            lappend info "user" $user
802            lappend info "session" $session
803            SendCmd "clientinfo [list $info]"
804        }
805
806        set w [winfo width $itk_component(view)]
807        set h [winfo height $itk_component(view)]
808        EventuallyResize $w $h
809    }
810    return $result
811}
812
813#
814# isconnected --
815#
816#       Indicates if we are currently connected to the visualization server.
817#
818itcl::body Rappture::VtkStreamlinesViewer::isconnected {} {
819    return [VisViewer::IsConnected]
820}
821
822#
823# disconnect --
824#
825itcl::body Rappture::VtkStreamlinesViewer::disconnect {} {
826    Disconnect
827    set _reset 1
828}
829
830#
831# Disconnect --
832#
833#       Clients use this method to disconnect from the current rendering
834#       server.
835#
836itcl::body Rappture::VtkStreamlinesViewer::Disconnect {} {
837    VisViewer::Disconnect
838
839    $_dispatcher cancel !rebuild
840    $_dispatcher cancel !resize
841    $_dispatcher cancel !reseed
842    $_dispatcher cancel !rotate
843    $_dispatcher cancel !xcutplane
844    $_dispatcher cancel !ycutplane
845    $_dispatcher cancel !zcutplane
846    $_dispatcher cancel !legend
847    # disconnected -- no more data sitting on server
848    array unset _datasets
849    array unset _colormaps
850    array unset _seeds
851    array unset _dataset2style
852}
853
854# ----------------------------------------------------------------------
855# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
856#
857# Invoked automatically whenever the "image" command comes in from
858# the rendering server.  Indicates that binary image data with the
859# specified <size> will follow.
860# ----------------------------------------------------------------------
861itcl::body Rappture::VtkStreamlinesViewer::ReceiveImage { args } {
862    array set info {
863        -token "???"
864        -bytes 0
865        -type image
866    }
867    array set info $args
868    set bytes [ReceiveBytes $info(-bytes)]
869    if { $info(-type) == "image" } {
870        if 0 {
871            set f [open "last.ppm" "w"]
872            fconfigure $f -encoding binary
873            puts -nonewline $f $bytes
874            close $f
875        }
876        $_image(plot) configure -data $bytes
877        set time [clock seconds]
878        set date [clock format $time]
879        #puts stderr "$date: received image [image width $_image(plot)]x[image height $_image(plot)] image>"
880        if { $_start > 0 } {
881            set finish [clock clicks -milliseconds]
882            #puts stderr "round trip time [expr $finish -$_start] milliseconds"
883            set _start 0
884        }
885    } elseif { $info(type) == "print" } {
886        set tag $this-print-$info(-token)
887        set _hardcopy($tag) $bytes
888    }
889    if { $_legendPending } {
890        RequestLegend
891    }
892}
893
894#
895# ReceiveDataset --
896#
897itcl::body Rappture::VtkStreamlinesViewer::ReceiveDataset { args } {
898    if { ![isconnected] } {
899        return
900    }
901    set option [lindex $args 0]
902    switch -- $option {
903        "scalar" {
904            set option [lindex $args 1]
905            switch -- $option {
906                "world" {
907                    foreach { x y z value tag } [lrange $args 2 end] break
908                }
909                "pixel" {
910                    foreach { x y value tag } [lrange $args 2 end] break
911                }
912            }
913        }
914        "vector" {
915            set option [lindex $args 1]
916            switch -- $option {
917                "world" {
918                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
919                }
920                "pixel" {
921                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
922                }
923            }
924        }
925        "names" {
926            foreach { name } [lindex $args 1] {
927                #puts stderr "Dataset: $name"
928            }
929        }
930        default {
931            error "unknown dataset option \"$option\" from server"
932        }
933    }
934}
935
936# ----------------------------------------------------------------------
937# USAGE: Rebuild
938#
939# Called automatically whenever something changes that affects the
940# data in the widget.  Clears any existing data and rebuilds the
941# widget to display new data.
942# ----------------------------------------------------------------------
943itcl::body Rappture::VtkStreamlinesViewer::Rebuild {} {
944    set w [winfo width $itk_component(view)]
945    set h [winfo height $itk_component(view)]
946    if { $w < 2 || $h < 2 } {
947        update
948        $_dispatcher event -idle !rebuild
949        return
950    }
951
952    # Turn on buffering of commands to the server.  We don't want to
953    # be preempted by a server disconnect/reconnect (which automatically
954    # generates a new call to Rebuild).
955    StartBufferingCommands
956    set _legendPending 1
957
958    set _first ""
959    if { $_reset } {
960        set _width $w
961        set _height $h
962        $_arcball resize $w $h
963        DoResize
964        InitSettings -xgrid -ygrid -zgrid -axismode \
965            -axesvisible -axislabelsvisible -axisminorticks
966        # This "imgflush" is to force an image returned before vtkvis starts
967        # reading a (big) dataset.  This will display an empty plot with axes
968        # at the correct image size.
969        SendCmd "imgflush"
970    }
971
972    set _first ""
973    SendCmd "dataset visible 0"
974    foreach dataobj [get -objects] {
975        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
976            set _first $dataobj
977        }
978        foreach comp [$dataobj components] {
979            set tag $dataobj-$comp
980            if { ![info exists _datasets($tag)] } {
981                set bytes [$dataobj vtkdata $comp]
982                set length [string length $bytes]
983                if 0 {
984                    set f [open /tmp/vtkstreamlines.vtk "w"]
985                    fconfigure $f -translation binary -encoding binary
986                    puts -nonewline $f $bytes
987                    close $f
988                }
989                if { $_reportClientInfo }  {
990                    set info {}
991                    lappend info "tool_id"       [$dataobj hints toolid]
992                    lappend info "tool_name"     [$dataobj hints toolname]
993                    lappend info "tool_title"    [$dataobj hints tooltitle]
994                    lappend info "tool_command"  [$dataobj hints toolcommand]
995                    lappend info "tool_revision" [$dataobj hints toolrevision]
996                    lappend info "dataset_label" [$dataobj hints label]
997                    lappend info "dataset_size"  $length
998                    lappend info "dataset_tag"   $tag
999                    SendCmd "clientinfo [list $info]"
1000                }
1001                SendCmd "dataset add $tag data follows $length"
1002                SendData $bytes
1003                set _datasets($tag) 1
1004                SetObjectStyle $dataobj $comp
1005            }
1006            if { [info exists _obj2ovride($dataobj-raise)] } {
1007                SendCmd "dataset visible 1 $tag"
1008            }
1009        }
1010    }
1011    if {"" != $_first} {
1012        set location [$_first hints camera]
1013        if { $location != "" } {
1014            array set view $location
1015        }
1016        foreach axis { x y z } {
1017            set label [$_first hints ${axis}label]
1018            if { $label != "" } {
1019                SendCmd [list axis name $axis $label]
1020            }
1021            set units [$_first hints ${axis}units]
1022            if { $units != "" } {
1023                SendCmd [list axis units $axis $units]
1024            }
1025        }
1026        $itk_component(field) choices delete 0 end
1027        $itk_component(fieldmenu) delete 0 end
1028        array unset _fields
1029        set _curFldName ""
1030        set _curFldLabel ""
1031        foreach cname [$_first components] {
1032            foreach fname [$_first fieldnames $cname] {
1033                if { [info exists _fields($fname)] } {
1034                    continue
1035                }
1036                foreach { label units components } \
1037                    [$_first fieldinfo $fname] break
1038                $itk_component(field) choices insert end "$fname" "$label"
1039                $itk_component(fieldmenu) add radiobutton -label "$label" \
1040                    -value $label -variable [itcl::scope _curFldLabel] \
1041                    -selectcolor red \
1042                    -activebackground $itk_option(-plotbackground) \
1043                    -activeforeground $itk_option(-plotforeground) \
1044                    -font "Arial 8" \
1045                    -command [itcl::code $this Combo invoke]
1046                set _fields($fname) [list $label $units $components]
1047                if { $_curFldName == "" && $components == 3 } {
1048                    set _curFldName $fname
1049                    set _curFldLabel $label
1050                }
1051            }
1052        }
1053        $itk_component(field) value $_curFldLabel
1054    }
1055
1056    if { $_reset } {
1057        InitSettings -streamlinesseedsvisible -streamlinesopacity \
1058            -streamlinesvisible \
1059            -streamlineslighting \
1060            -streamlinescolormap -field \
1061            -surfacevisible -surfaceedges -surfacelighting -surfaceopacity \
1062            -surfacewireframe \
1063            -cutplanevisible \
1064            -cutplanexposition -cutplaneyposition -cutplanezposition \
1065            -cutplanexvisible -cutplaneyvisible -cutplanezvisible
1066
1067        # Reset the camera and other view parameters
1068        $_arcball quaternion [ViewToQuaternion]
1069        if {$_view(-ortho)} {
1070            SendCmd "camera mode ortho"
1071        } else {
1072            SendCmd "camera mode persp"
1073        }
1074        DoRotate
1075        PanCamera
1076        Zoom reset
1077        SendCmd "camera reset"
1078        set _reset 0
1079    }
1080    # Actually write the commands to the server socket.  If it fails, we don't
1081    # care.  We're finished here.
1082    blt::busy hold $itk_component(hull)
1083    StopBufferingCommands
1084    blt::busy release $itk_component(hull)
1085}
1086
1087# ----------------------------------------------------------------------
1088# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
1089#
1090# Returns a list of server IDs for the current datasets being displayed.  This
1091# is normally a single ID, but it might be a list of IDs if the current data
1092# object has multiple components.
1093# ----------------------------------------------------------------------
1094itcl::body Rappture::VtkStreamlinesViewer::CurrentDatasets {args} {
1095    set flag [lindex $args 0]
1096    switch -- $flag {
1097        "-all" {
1098            if { [llength $args] > 1 } {
1099                error "CurrentDatasets: can't specify dataobj after \"-all\""
1100            }
1101            set dlist [get -objects]
1102        }
1103        "-visible" {
1104            if { [llength $args] > 1 } {
1105                set dlist {}
1106                set args [lrange $args 1 end]
1107                foreach dataobj $args {
1108                    if { [info exists _obj2ovride($dataobj-raise)] } {
1109                        lappend dlist $dataobj
1110                    }
1111                }
1112            } else {
1113                set dlist [get -visible]
1114            }
1115        }
1116        default {
1117            set dlist $args
1118        }
1119    }
1120    set rlist ""
1121    foreach dataobj $dlist {
1122        foreach comp [$dataobj components] {
1123            set tag $dataobj-$comp
1124            if { [info exists _datasets($tag)] && $_datasets($tag) } {
1125                lappend rlist $tag
1126            }
1127        }
1128    }
1129    return $rlist
1130}
1131
1132# ----------------------------------------------------------------------
1133# USAGE: Zoom in
1134# USAGE: Zoom out
1135# USAGE: Zoom reset
1136#
1137# Called automatically when the user clicks on one of the zoom
1138# controls for this widget.  Changes the zoom for the current view.
1139# ----------------------------------------------------------------------
1140itcl::body Rappture::VtkStreamlinesViewer::Zoom {option} {
1141    switch -- $option {
1142        "in" {
1143            set _view(-zoom) [expr {$_view(-zoom)*1.25}]
1144            SendCmd "camera zoom $_view(-zoom)"
1145        }
1146        "out" {
1147            set _view(-zoom) [expr {$_view(-zoom)*0.8}]
1148            SendCmd "camera zoom $_view(-zoom)"
1149        }
1150        "reset" {
1151            array set _view {
1152                -qw      0.853553
1153                -qx      -0.353553
1154                -qy      0.353553
1155                -qz      0.146447
1156                -xpan    0
1157                -ypan    0
1158                -zoom    1.0
1159            }
1160            if { $_first != "" } {
1161                set location [$_first hints camera]
1162                if { $location != "" } {
1163                    array set _view $location
1164                }
1165            }
1166            $_arcball quaternion [ViewToQuaternion]
1167            DoRotate
1168            SendCmd "camera reset"
1169        }
1170    }
1171}
1172
1173itcl::body Rappture::VtkStreamlinesViewer::PanCamera {} {
1174    set x $_view(-xpan)
1175    set y $_view(-ypan)
1176    SendCmd "camera pan $x $y"
1177}
1178
1179# ----------------------------------------------------------------------
1180# USAGE: Rotate click <x> <y>
1181# USAGE: Rotate drag <x> <y>
1182# USAGE: Rotate release <x> <y>
1183#
1184# Called automatically when the user clicks/drags/releases in the
1185# plot area.  Moves the plot according to the user's actions.
1186# ----------------------------------------------------------------------
1187itcl::body Rappture::VtkStreamlinesViewer::Rotate {option x y} {
1188    switch -- $option {
1189        "click" {
1190            $itk_component(view) configure -cursor fleur
1191            set _click(x) $x
1192            set _click(y) $y
1193        }
1194        "drag" {
1195            if {[array size _click] == 0} {
1196                Rotate click $x $y
1197            } else {
1198                set w [winfo width $itk_component(view)]
1199                set h [winfo height $itk_component(view)]
1200                if {$w <= 0 || $h <= 0} {
1201                    return
1202                }
1203
1204                if {[catch {
1205                    # this fails sometimes for no apparent reason
1206                    set dx [expr {double($x-$_click(x))/$w}]
1207                    set dy [expr {double($y-$_click(y))/$h}]
1208                }]} {
1209                    return
1210                }
1211                if { $dx == 0 && $dy == 0 } {
1212                    return
1213                }
1214                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1215                EventuallyRotate $q
1216                set _click(x) $x
1217                set _click(y) $y
1218            }
1219        }
1220        "release" {
1221            Rotate drag $x $y
1222            $itk_component(view) configure -cursor ""
1223            catch {unset _click}
1224        }
1225        default {
1226            error "bad option \"$option\": should be click, drag, release"
1227        }
1228    }
1229}
1230
1231itcl::body Rappture::VtkStreamlinesViewer::Pick {x y} {
1232    foreach tag [CurrentDatasets -visible] {
1233        SendCmd "dataset getscalar pixel $x $y $tag"
1234    }
1235}
1236
1237# ----------------------------------------------------------------------
1238# USAGE: $this Pan click x y
1239#        $this Pan drag x y
1240#        $this Pan release x y
1241#
1242# Called automatically when the user clicks on one of the zoom
1243# controls for this widget.  Changes the zoom for the current view.
1244# ----------------------------------------------------------------------
1245itcl::body Rappture::VtkStreamlinesViewer::Pan {option x y} {
1246    switch -- $option {
1247        "set" {
1248            set w [winfo width $itk_component(view)]
1249            set h [winfo height $itk_component(view)]
1250            set x [expr $x / double($w)]
1251            set y [expr $y / double($h)]
1252            set _view(-xpan) [expr $_view(-xpan) + $x]
1253            set _view(-ypan) [expr $_view(-ypan) + $y]
1254            PanCamera
1255            return
1256        }
1257        "click" {
1258            set _click(x) $x
1259            set _click(y) $y
1260            $itk_component(view) configure -cursor hand1
1261        }
1262        "drag" {
1263            if { ![info exists _click(x)] } {
1264                set _click(x) $x
1265            }
1266            if { ![info exists _click(y)] } {
1267                set _click(y) $y
1268            }
1269            set w [winfo width $itk_component(view)]
1270            set h [winfo height $itk_component(view)]
1271            set dx [expr ($_click(x) - $x)/double($w)]
1272            set dy [expr ($_click(y) - $y)/double($h)]
1273            set _click(x) $x
1274            set _click(y) $y
1275            set _view(-xpan) [expr $_view(-xpan) - $dx]
1276            set _view(-ypan) [expr $_view(-ypan) - $dy]
1277            PanCamera
1278        }
1279        "release" {
1280            Pan drag $x $y
1281            $itk_component(view) configure -cursor ""
1282        }
1283        default {
1284            error "unknown option \"$option\": should set, click, drag, or release"
1285        }
1286    }
1287}
1288
1289# ----------------------------------------------------------------------
1290# USAGE: InitSettings <what> ?<value>?
1291#
1292# Used internally to update rendering settings whenever parameters
1293# change in the popup settings panel.  Sends the new settings off
1294# to the back end.
1295# ----------------------------------------------------------------------
1296itcl::body Rappture::VtkStreamlinesViewer::InitSettings { args } {
1297    foreach spec $args {
1298        if { [info exists _settings($_first${spec})] } {
1299            # Reset global setting with dataobj specific setting
1300            set _settings($spec) $_settings($_first${spec})
1301        }
1302        AdjustSetting $spec
1303    }
1304}
1305
1306#
1307# AdjustSetting --
1308#
1309#       Changes/updates a specific setting in the widget.  There are
1310#       usually user-setable option.  Commands are sent to the render
1311#       server.
1312#
1313itcl::body Rappture::VtkStreamlinesViewer::AdjustSetting {what {value ""}} {
1314    if { ![isconnected] } {
1315        return
1316    }
1317    switch -- $what {
1318        "-axesvisible" {
1319            set bool $_settings($what)
1320            SendCmd "axis visible all $bool"
1321        }
1322        "-axislabelsvisible" {
1323            set bool $_settings($what)
1324            SendCmd "axis labels all $bool"
1325        }
1326        "-axisminorticks" {
1327            set bool $_settings($what)
1328            SendCmd "axis minticks all $bool"
1329        }
1330        "-axismode" {
1331            set mode [$itk_component(axismode) value]
1332            set mode [$itk_component(axismode) translate $mode]
1333            set _settings($what) $mode
1334            SendCmd "axis flymode $mode"
1335        }
1336        "-cutplaneedges" {
1337            set bool $_settings($what)
1338            SendCmd "cutplane edges $bool"
1339        }
1340        "-cutplanevisible" {
1341            set bool $_settings($what)
1342            SendCmd "cutplane visible $bool"
1343            if { $bool } {
1344                Rappture::Tooltip::for $itk_component(cutplane) \
1345                    "Hide the cutplanes"
1346            } else {
1347                Rappture::Tooltip::for $itk_component(cutplane) \
1348                    "Show the cutplanes"
1349            }
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        "-streamlineslength" {
1458            set val $_settings($what)
1459            set sval [expr { (0.01 * double($val)) / 0.7 }]
1460            foreach axis {x y z} {
1461                foreach {min max} $_limits($axis) break
1462                set ${axis}len [expr double($max) - double($min)]
1463            }
1464            set length [expr { $sval * ($xlen + $ylen + $zlen) } ]
1465            SendCmd "streamlines length $length"
1466        }
1467        "-streamlineslighting" {
1468            set bool $_settings($what)
1469            SendCmd "streamlines lighting $bool"
1470        }
1471        "-surfaceopacity" {
1472            set val $_settings($what)
1473            set sval [expr { 0.01 * double($val) }]
1474            SendCmd "polydata opacity $sval"
1475        }
1476        "-surfacewireframe" {
1477            set bool $_settings($what)
1478            SendCmd "polydata wireframe $bool"
1479        }
1480        "-surfacevisible" {
1481            set bool $_settings($what)
1482            SendCmd "polydata visible $bool"
1483            if { $bool } {
1484                Rappture::Tooltip::for $itk_component(surface) \
1485                    "Hide the boundary surface"
1486            } else {
1487                Rappture::Tooltip::for $itk_component(surface) \
1488                    "Show the boundary surface"
1489            }
1490        }
1491        "-surfacelighting" {
1492            set bool $_settings($what)
1493            SendCmd "polydata lighting $bool"
1494        }
1495        "-surfaceedges" {
1496            set bool $_settings($what)
1497            SendCmd "polydata edges $bool"
1498        }
1499        "-xgrid" - "-ygrid" - "-zgrid" {
1500            set axis [string range $what 1 1]
1501            set bool $_settings($what)
1502            SendCmd "axis grid $axis $bool"
1503        }
1504        default {
1505            error "don't know how to fix $what"
1506        }
1507    }
1508}
1509
1510#
1511# RequestLegend --
1512#
1513#       Request a new legend from the server.  The size of the legend
1514#       is determined from the height of the canvas.  It will be rotated
1515#       to be vertical when drawn.
1516#
1517itcl::body Rappture::VtkStreamlinesViewer::RequestLegend {} {
1518    set font "Arial 8"
1519    set lineht [font metrics $font -linespace]
1520    set w 12
1521    set h [expr {$_height - 3 * ($lineht + 2)}]
1522    if { $h < 1 } {
1523        return
1524    }
1525    # Set the legend on the first streamlines dataset.
1526    foreach dataset [CurrentDatasets -visible $_first] {
1527        foreach {dataobj comp} [split $dataset -] break
1528        if { [info exists _dataset2style($dataset)] } {
1529            SendCmd \
1530                "legend $_dataset2style($dataset) $_colorMode $_curFldName {} $w $h 0"
1531            break;
1532        }
1533    }
1534}
1535
1536#
1537# ChangeColormap --
1538#
1539itcl::body Rappture::VtkStreamlinesViewer::ChangeColormap {dataobj comp color} {
1540    set tag $dataobj-$comp
1541    if { ![info exist _style($tag)] } {
1542        error "no initial colormap"
1543    }
1544    array set style $_style($tag)
1545    set style(-color) $color
1546    set _style($tag) [array get style]
1547    SetColormap $dataobj $comp
1548}
1549
1550#
1551# SetColormap --
1552#
1553itcl::body Rappture::VtkStreamlinesViewer::SetColormap { dataobj comp } {
1554    array set style {
1555        -color BCGYR
1556    }
1557    set tag $dataobj-$comp
1558    if { ![info exists _initialStyle($tag)] } {
1559        # Save the initial component style.
1560        set _initialStyle($tag) [$dataobj style $comp]
1561    }
1562
1563    # Override defaults with initial style defined in xml.
1564    array set style $_initialStyle($tag)
1565
1566    if { ![info exists _style($tag)] } {
1567        set _style($tag) [array get style]
1568    }
1569    # Override initial style with current style.
1570    array set style $_style($tag)
1571
1572    set name "$style(-color)"
1573    if { ![info exists _colormaps($name)] } {
1574        BuildColormap $name [array get style]
1575        set _colormaps($name) 1
1576    }
1577    if { ![info exists _dataset2style($tag)] ||
1578         $_dataset2style($tag) != $name } {
1579        SendCmd "streamlines colormap $name $tag"
1580        SendCmd "cutplane colormap $name $tag"
1581        set _dataset2style($tag) $name
1582    }
1583}
1584
1585#
1586# BuildColormap --
1587#
1588itcl::body Rappture::VtkStreamlinesViewer::BuildColormap { name styles } {
1589    array set style $styles
1590    set cmap [ColorsToColormap $style(-color)]
1591    if { [llength $cmap] == 0 } {
1592        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
1593    }
1594    set amap "0.0 1.0 1.0 1.0"
1595    SendCmd "colormap add $name { $cmap } { $amap }"
1596}
1597
1598# ----------------------------------------------------------------------
1599# CONFIGURATION OPTION: -plotbackground
1600# ----------------------------------------------------------------------
1601itcl::configbody Rappture::VtkStreamlinesViewer::plotbackground {
1602    if { [isconnected] } {
1603        set rgb [Color2RGB $itk_option(-plotbackground)]
1604        SendCmd "screen bgcolor $rgb"
1605    }
1606}
1607
1608# ----------------------------------------------------------------------
1609# CONFIGURATION OPTION: -plotforeground
1610# ----------------------------------------------------------------------
1611itcl::configbody Rappture::VtkStreamlinesViewer::plotforeground {
1612    if { [isconnected] } {
1613        set rgb [Color2RGB $itk_option(-plotforeground)]
1614        SendCmd "axis color all $rgb"
1615        SendCmd "outline color $rgb"
1616        SendCmd "cutplane color $rgb"
1617    }
1618}
1619
1620itcl::body Rappture::VtkStreamlinesViewer::BuildSurfaceTab {} {
1621
1622    set fg [option get $itk_component(hull) font Font]
1623    #set bfg [option get $itk_component(hull) boldFont Font]
1624
1625    set inner [$itk_component(main) insert end \
1626        -title "Boundary Surface Settings" \
1627        -icon [Rappture::icon volume-on]]
1628    $inner configure -borderwidth 4
1629
1630    checkbutton $inner.surface \
1631        -text "Show Surface" \
1632        -variable [itcl::scope _settings(-surfacevisible)] \
1633        -command [itcl::code $this AdjustSetting -surfacevisible] \
1634        -font "Arial 9"
1635
1636    checkbutton $inner.wireframe \
1637        -text "Show Wireframe" \
1638        -variable [itcl::scope _settings(-surfacewireframe)] \
1639        -command [itcl::code $this AdjustSetting -surfacewireframe] \
1640        -font "Arial 9"
1641
1642    checkbutton $inner.lighting \
1643        -text "Enable Lighting" \
1644        -variable [itcl::scope _settings(-surfacelighting)] \
1645        -command [itcl::code $this AdjustSetting -surfacelighting] \
1646        -font "Arial 9"
1647
1648    checkbutton $inner.edges \
1649        -text "Show Edges" \
1650        -variable [itcl::scope _settings(-surfaceedges)] \
1651        -command [itcl::code $this AdjustSetting -surfaceedges] \
1652        -font "Arial 9"
1653
1654    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1655    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1656        -variable [itcl::scope _settings(-surfaceopacity)] \
1657        -width 10 \
1658        -showvalue off \
1659        -command [itcl::code $this AdjustSetting -surfaceopacity]
1660
1661    blt::table $inner \
1662        0,0 $inner.wireframe -anchor w -pady 2 -cspan 2 \
1663        1,0 $inner.lighting  -anchor w -pady 2 -cspan 2 \
1664        2,0 $inner.edges     -anchor w -pady 2 -cspan 3 \
1665        3,0 $inner.opacity_l -anchor w -pady 2 \
1666        3,1 $inner.opacity   -fill x   -pady 2
1667
1668    blt::table configure $inner r* c* -resize none
1669    blt::table configure $inner r4 c1 -resize expand
1670}
1671
1672itcl::body Rappture::VtkStreamlinesViewer::BuildStreamsTab {} {
1673
1674    set fg [option get $itk_component(hull) font Font]
1675    #set bfg [option get $itk_component(hull) boldFont Font]
1676
1677    set inner [$itk_component(main) insert end \
1678        -title "Streams Settings" \
1679        -icon [Rappture::icon streamlines-on]]
1680    $inner configure -borderwidth 4
1681
1682    checkbutton $inner.streamlines \
1683        -text "Show Streamlines" \
1684        -variable [itcl::scope _settings(-streamlinesvisible)] \
1685        -command [itcl::code $this AdjustSetting -streamlinesvisible] \
1686        -font "Arial 9"
1687
1688    checkbutton $inner.lighting \
1689        -text "Enable Lighting" \
1690        -variable [itcl::scope _settings(-streamlineslighting)] \
1691        -command [itcl::code $this AdjustSetting -streamlineslighting] \
1692        -font "Arial 9"
1693
1694    checkbutton $inner.seeds \
1695        -text "Show Seeds" \
1696        -variable [itcl::scope _settings(-streamlinesseedsvisible)] \
1697        -command [itcl::code $this AdjustSetting -streamlinesseedsvisible] \
1698        -font "Arial 9"
1699
1700    label $inner.mode_l -text "Mode" -font "Arial 9"
1701    itk_component add streammode {
1702        Rappture::Combobox $inner.mode -width 10 -editable no
1703    }
1704    $inner.mode choices insert end \
1705        "lines"    "lines" \
1706        "ribbons"   "ribbons" \
1707        "tubes"     "tubes"
1708    $itk_component(streammode) value $_settings(-streamlinesmode)
1709    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting -streamlinesmode]
1710
1711    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1712    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1713        -variable [itcl::scope _settings(-streamlinesopacity)] \
1714        -width 10 \
1715        -showvalue off \
1716        -command [itcl::code $this AdjustSetting -streamlinesopacity]
1717
1718    label $inner.density_l -text "No. Seeds" -font "Arial 9"
1719    ::scale $inner.density -from 1 -to 1000 -orient horizontal \
1720        -variable [itcl::scope _settings(-streamlinesnumseeds)] \
1721        -width 10 \
1722        -showvalue on \
1723        -command [itcl::code $this AdjustSetting -streamlinesnumseeds]
1724
1725    label $inner.scale_l -text "Length" -font "Arial 9"
1726    ::scale $inner.scale -from 1 -to 100 -orient horizontal \
1727        -variable [itcl::scope _settings(-streamlineslength)] \
1728        -width 10 \
1729        -showvalue off \
1730        -command [itcl::code $this AdjustSetting -streamlineslength]
1731
1732    label $inner.field_l -text "Color by" -font "Arial 9"
1733    itk_component add field {
1734        Rappture::Combobox $inner.field -width 10 -editable no
1735    }
1736    bind $inner.field <<Value>> \
1737        [itcl::code $this AdjustSetting -field]
1738
1739    label $inner.colormap_l -text "Colormap" -font "Arial 9"
1740    itk_component add colormap {
1741        Rappture::Combobox $inner.colormap -width 10 -editable no
1742    }
1743    $inner.colormap choices insert end [GetColormapList]
1744
1745    $itk_component(colormap) value "BCGYR"
1746    bind $inner.colormap <<Value>> \
1747        [itcl::code $this AdjustSetting -streamlinescolormap]
1748
1749    blt::table $inner \
1750        0,0 $inner.field_l     -anchor w -pady 2  \
1751        0,1 $inner.field       -fill x   -pady 2  \
1752        1,0 $inner.colormap_l  -anchor w -pady 2  \
1753        1,1 $inner.colormap    -fill x   -pady 2  \
1754        2,0 $inner.mode_l      -anchor w -pady 2  \
1755        2,1 $inner.mode        -fill x   -pady 2  \
1756        3,0 $inner.scale_l     -anchor w -pady 2  \
1757        3,1 $inner.scale       -fill x   -pady 2  \
1758        4,0 $inner.opacity_l   -anchor w -pady 2  \
1759        4,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 BCGYR
2145        -constcolor white
2146        -edgecolor black
2147        -edges 0
2148        -lighting 1
2149        -linewidth 1.0
2150        -mode lines
2151        -numseeds 200
2152        -opacity 1.0
2153        -seeds 1
2154        -seedcolor white
2155        -streamlineslength 0.7
2156        -surfacecolor white
2157        -surfaceedgecolor black
2158        -surfaceedges 0
2159        -surfacelighting 1
2160        -surfaceopacity 0.4
2161        -surfacevisible 1
2162        -surfacewireframe 0
2163        -visible 1
2164    }
2165    if { $dataobj != $_first } {
2166        set settings(-opacity) 1
2167    }
2168    array set settings $style
2169    StartBufferingCommands
2170    SendCmd "streamlines add $tag"
2171    SendCmd "streamlines color [Color2RGB $settings(-constcolor)] $tag"
2172    SendCmd "streamlines edges $settings(-edges) $tag"
2173    SendCmd "streamlines linecolor [Color2RGB $settings(-edgecolor)] $tag"
2174    SendCmd "streamlines linewidth $settings(-linewidth) $tag"
2175    SendCmd "streamlines lighting $settings(-lighting) $tag"
2176    SendCmd "streamlines opacity $settings(-opacity) $tag"
2177    SendCmd "streamlines seed color [Color2RGB $settings(-seedcolor)] $tag"
2178    SendCmd "streamlines seed visible $settings(-seeds) $tag"
2179    SendCmd "streamlines visible $settings(-visible) $tag"
2180    set seeds [$dataobj hints seeds]
2181    if { $seeds != "" && ![info exists _seeds($dataobj)] } {
2182        set length [string length $seeds]
2183        SendCmd "streamlines seed fmesh $settings(-numseeds) data follows $length $tag"
2184        SendData $seeds
2185        set _seeds($dataobj) 1
2186    }
2187    set _settings(-streamlineslighting) $settings(-lighting)
2188    $itk_component(streammode) value $settings(-mode)
2189    AdjustSetting -streamlinesmode
2190    set _settings(-streamlinesnumseeds) $settings(-numseeds)
2191    set _settings(-streamlinesopacity) [expr $settings(-opacity) * 100.0]
2192    set _settings(-streamlineslength) [expr $settings(-streamlineslength) * 100.0]
2193    set _settings(-streamlinesseedsvisible) $settings(-seeds)
2194    set _settings(-streamlinesvisible) $settings(-visible)
2195
2196    SendCmd "cutplane add $tag"
2197
2198    SendCmd "polydata add $tag"
2199    SendCmd "polydata color [Color2RGB $settings(-surfacecolor)] $tag"
2200    SendCmd "polydata colormode constant {} $tag"
2201    SendCmd "polydata edges $settings(-surfaceedges) $tag"
2202    SendCmd "polydata linecolor [Color2RGB $settings(-surfaceedgecolor)] $tag"
2203    SendCmd "polydata lighting $settings(-surfacelighting) $tag"
2204    SendCmd "polydata opacity $settings(-surfaceopacity) $tag"
2205    SendCmd "polydata wireframe $settings(-surfacewireframe) $tag"
2206    SendCmd "polydata visible $settings(-surfacevisible) $tag"
2207    set _settings(-surfaceedges) $settings(-surfaceedges)
2208    set _settings(-surfacelighting) $settings(-surfacelighting)
2209    set _settings(-surfaceopacity) [expr $settings(-surfaceopacity) * 100.0]
2210    set _settings(-surfacewireframe) $settings(-surfacewireframe)
2211    set _settings(-surfacevisible) $settings(-surfacevisible)
2212    StopBufferingCommands
2213    SetColormap $dataobj $comp
2214    $itk_component(colormap) value $settings(-color)
2215}
2216
2217itcl::body Rappture::VtkStreamlinesViewer::IsValidObject { dataobj } {
2218    if {[catch {$dataobj isa Rappture::Field} valid] != 0 || !$valid} {
2219        return 0
2220    }
2221    return 1
2222}
2223
2224# ----------------------------------------------------------------------
2225# USAGE: ReceiveLegend <colormap> <title> <vmin> <vmax> <size>
2226#
2227# Invoked automatically whenever the "legend" command comes in from
2228# the rendering server.  Indicates that binary image data with the
2229# specified <size> will follow.
2230# ----------------------------------------------------------------------
2231itcl::body Rappture::VtkStreamlinesViewer::ReceiveLegend { colormap title vmin vmax size } {
2232    set _legendPending 0
2233    set _title $title
2234    regsub {\(mag\)} $title "" _title
2235    if { [IsConnected] } {
2236        set bytes [ReceiveBytes $size]
2237        if { ![info exists _image(legend)] } {
2238            set _image(legend) [image create photo]
2239        }
2240        $_image(legend) configure -data $bytes
2241        #puts stderr "read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
2242        if { [catch {DrawLegend} errs] != 0 } {
2243            puts stderr errs=$errs
2244        }
2245    }
2246}
2247
2248#
2249# DrawLegend --
2250#
2251#       Draws the legend in it's own canvas which resides to the right
2252#       of the contour plot area.
2253#
2254itcl::body Rappture::VtkStreamlinesViewer::DrawLegend {} {
2255    set fname $_curFldName
2256    set c $itk_component(view)
2257    set w [winfo width $c]
2258    set h [winfo height $c]
2259    set font "Arial 8"
2260    set lineht [font metrics $font -linespace]
2261
2262    if { [info exists _fields($fname)] } {
2263        foreach { title units } $_fields($fname) break
2264        if { $units != "" } {
2265            set title [format "%s (%s)" $title $units]
2266        }
2267    } else {
2268        set title $fname
2269    }
2270    if { $_settings(-legendvisible) } {
2271        set x [expr $w - 2]
2272        if { [$c find withtag "legend"] == "" } {
2273            set y 2
2274            $c create text $x $y \
2275                -anchor ne \
2276                -fill $itk_option(-plotforeground) -tags "title legend" \
2277                -font $font
2278            incr y $lineht
2279            $c create text $x $y \
2280                -anchor ne \
2281                -fill $itk_option(-plotforeground) -tags "vmax legend" \
2282                -font $font
2283            incr y $lineht
2284            $c create image $x $y \
2285                -anchor ne \
2286                -image $_image(legend) -tags "colormap legend"
2287            $c create text $x [expr {$h-2}] \
2288                -anchor se \
2289                -fill $itk_option(-plotforeground) -tags "vmin legend" \
2290                -font $font
2291            #$c bind colormap <Enter> [itcl::code $this EnterLegend %x %y]
2292            $c bind colormap <Leave> [itcl::code $this LeaveLegend]
2293            $c bind colormap <Motion> [itcl::code $this MotionLegend %x %y]
2294        }
2295        $c bind title <ButtonPress> [itcl::code $this Combo post]
2296        $c bind title <Enter> [itcl::code $this Combo activate]
2297        $c bind title <Leave> [itcl::code $this Combo deactivate]
2298        # Reset the item coordinates according the current size of the plot.
2299        $c itemconfigure title -text $title
2300        if { [info exists _limits($_curFldName)] } {
2301            foreach {vmin vmax} $_limits($_curFldName) break
2302            $c itemconfigure vmin -text [format %g $vmin]
2303            $c itemconfigure vmax -text [format %g $vmax]
2304        }
2305        set y 2
2306        $c coords title $x $y
2307        incr y $lineht
2308        $c coords vmax $x $y
2309        incr y $lineht
2310        $c coords colormap $x $y
2311        $c coords vmin $x [expr {$h - 2}]
2312    }
2313}
2314
2315#
2316# EnterLegend --
2317#
2318itcl::body Rappture::VtkStreamlinesViewer::EnterLegend { x y } {
2319    SetLegendTip $x $y
2320}
2321
2322#
2323# MotionLegend --
2324#
2325itcl::body Rappture::VtkStreamlinesViewer::MotionLegend { x y } {
2326    Rappture::Tooltip::tooltip cancel
2327    set c $itk_component(view)
2328    SetLegendTip $x $y
2329}
2330
2331#
2332# LeaveLegend --
2333#
2334itcl::body Rappture::VtkStreamlinesViewer::LeaveLegend { } {
2335    Rappture::Tooltip::tooltip cancel
2336    .rappturetooltip configure -icon ""
2337}
2338
2339#
2340# SetLegendTip --
2341#
2342itcl::body Rappture::VtkStreamlinesViewer::SetLegendTip { x y } {
2343    set c $itk_component(view)
2344    set w [winfo width $c]
2345    set h [winfo height $c]
2346    set font "Arial 8"
2347    set lineht [font metrics $font -linespace]
2348
2349    set imgHeight [image height $_image(legend)]
2350    set coords [$c coords colormap]
2351    set imgX [expr $w - [image width $_image(legend)] - 2]
2352    set imgY [expr $y - 2 * ($lineht + 2)]
2353
2354    if { [info exists _fields($_title)] } {
2355        foreach { title units } $_fields($_title) break
2356        if { $units != "" } {
2357            set title [format "%s (%s)" $title $units]
2358        }
2359    } else {
2360        set title $_title
2361    }
2362    # Make a swatch of the selected color
2363    if { [catch { $_image(legend) get 10 $imgY } pixel] != 0 } {
2364        #puts stderr "out of range: $imgY"
2365        return
2366    }
2367    if { ![info exists _image(swatch)] } {
2368        set _image(swatch) [image create photo -width 24 -height 24]
2369    }
2370    set color [eval format "\#%02x%02x%02x" $pixel]
2371    $_image(swatch) put black  -to 0 0 23 23
2372    $_image(swatch) put $color -to 1 1 22 22
2373    .rappturetooltip configure -icon $_image(swatch)
2374
2375    # Compute the value of the point
2376    if { [info exists _limits($_curFldName)] } {
2377        foreach {vmin vmax} $_limits($_curFldName) break
2378        set t [expr 1.0 - (double($imgY) / double($imgHeight-1))]
2379        set value [expr $t * ($vmax - $vmin) + $vmin]
2380    } else {
2381        set value 0.0
2382    }
2383    set tipx [expr $x + 15]
2384    set tipy [expr $y - 5]
2385    Rappture::Tooltip::text $c "$title $value"
2386    Rappture::Tooltip::tooltip show $c +$tipx,+$tipy
2387}
2388
2389# ----------------------------------------------------------------------
2390# USAGE: Slice move x|y|z <newval>
2391#
2392# Called automatically when the user drags the slider to move the
2393# cut plane that slices 3D data.  Gets the current value from the
2394# slider and moves the cut plane to the appropriate point in the
2395# data set.
2396# ----------------------------------------------------------------------
2397itcl::body Rappture::VtkStreamlinesViewer::Slice {option args} {
2398    switch -- $option {
2399        "move" {
2400            set axis [lindex $args 0]
2401            set newval [lindex $args 1]
2402            if {[llength $args] != 2} {
2403                error "wrong # args: should be \"Slice move x|y|z newval\""
2404            }
2405            set newpos [expr {0.01*$newval}]
2406            SendCmd "cutplane slice $axis $newpos"
2407        }
2408        "tooltip" {
2409            set axis [lindex $args 0]
2410            set val [$itk_component(${axis}CutScale) get]
2411            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
2412        }
2413        default {
2414            error "bad option \"$option\": should be axis, move, or tooltip"
2415        }
2416    }
2417}
2418
2419# ----------------------------------------------------------------------
2420# USAGE: _dropdown post
2421# USAGE: _dropdown unpost
2422# USAGE: _dropdown select
2423#
2424# Used internally to handle the dropdown list for this combobox.  The
2425# post/unpost options are invoked when the list is posted or unposted
2426# to manage the relief of the controlling button.  The select option
2427# is invoked whenever there is a selection from the list, to assign
2428# the value back to the gauge.
2429# ----------------------------------------------------------------------
2430itcl::body Rappture::VtkStreamlinesViewer::Combo {option} {
2431    set c $itk_component(view)
2432    switch -- $option {
2433        post {
2434            foreach { x1 y1 x2 y2 } [$c bbox title] break
2435            set x1 [expr [winfo width $itk_component(view)] - [winfo reqwidth $itk_component(fieldmenu)]]
2436            set x [expr $x1 + [winfo rootx $itk_component(view)]]
2437            set y [expr $y2 + [winfo rooty $itk_component(view)]]
2438            tk_popup $itk_component(fieldmenu) $x $y
2439        }
2440        activate {
2441            $c itemconfigure title -fill red
2442        }
2443        deactivate {
2444            $c itemconfigure title -fill white
2445        }
2446        invoke {
2447            $itk_component(field) value $_curFldLabel
2448            AdjustSetting -field
2449        }
2450        default {
2451            error "bad option \"$option\": should be post, unpost, select"
2452        }
2453    }
2454}
2455
2456itcl::body Rappture::VtkStreamlinesViewer::SetOrientation { side } {
2457    array set positions {
2458        front "1 0 0 0"
2459        back  "0 0 1 0"
2460        left  "0.707107 0 -0.707107 0"
2461        right "0.707107 0 0.707107 0"
2462        top   "0.707107 -0.707107 0 0"
2463        bottom "0.707107 0.707107 0 0"
2464    }
2465    foreach name { -qw -qx -qy -qz } value $positions($side) {
2466        set _view($name) $value
2467    }
2468    set q [ViewToQuaternion]
2469    $_arcball quaternion $q
2470    SendCmd "camera orient $q"
2471    SendCmd "camera reset"
2472    set _view(-xpan) 0
2473    set _view(-ypan) 0
2474    set _view(-zoom) 1.0
2475}
Note: See TracBrowser for help on using the repository browser.