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

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

merge r4749 from trunk

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