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

Last change on this file since 2632 was 2632, checked in by ldelgass, 13 years ago

Write temp VTK file as binary in VtkViewer::limits

File size: 84.8 KB
Line 
1
2# ----------------------------------------------------------------------
3#  COMPONENT: vtkviewer - Vtk drawing 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-2005  Purdue Research Foundation
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
16#package require Img
17package require vtk
18
19option add *VtkStreamlinesViewer.width 4i widgetDefault
20option add *VtkStreamlinesViewer*cursor crosshair widgetDefault
21option add *VtkStreamlinesViewer.height 4i widgetDefault
22option add *VtkStreamlinesViewer.foreground black widgetDefault
23option add *VtkStreamlinesViewer.controlBackground gray widgetDefault
24option add *VtkStreamlinesViewer.controlDarkBackground #999999 widgetDefault
25option add *VtkStreamlinesViewer.plotBackground black widgetDefault
26option add *VtkStreamlinesViewer.plotForeground white widgetDefault
27option add *VtkStreamlinesViewer.font \
28    -*-helvetica-medium-r-normal-*-12-* widgetDefault
29
30# must use this name -- plugs into Rappture::resources::load
31proc VtkStreamlinesViewer_init_resources {} {
32    Rappture::resources::register \
33        vtkvis_server Rappture::VtkStreamlinesViewer::SetServerList
34}
35
36itcl::class Rappture::VtkStreamlinesViewer {
37    inherit Rappture::VisViewer
38
39    itk_option define -plotforeground plotForeground Foreground ""
40    itk_option define -plotbackground plotBackground Background ""
41
42    constructor { hostlist args } {
43        Rappture::VisViewer::constructor $hostlist
44    } {
45        # defined below
46    }
47    destructor {
48        # defined below
49    }
50    public proc SetServerList { namelist } {
51        Rappture::VisViewer::SetServerList "vtkvis" $namelist
52    }
53    public method add {dataobj {settings ""}}
54    public method camera {option args}
55    public method delete {args}
56    public method disconnect {}
57    public method download {option args}
58    public method get {args}
59    public method isconnected {}
60    public method limits { colormap }
61    public method sendto { string }
62    public method parameters {title args} {
63        # do nothing
64    }
65    public method scale {args}
66
67    protected method Connect {}
68    protected method CurrentDatasets {args}
69    protected method Disconnect {}
70    protected method DoResize {}
71    protected method DoReseed {}
72    protected method DoRotate {}
73    protected method AdjustSetting {what {value ""}}
74    protected method InitSettings { args  }
75    protected method Pan {option x y}
76    protected method Pick {x y}
77    protected method Rebuild {}
78    protected method ReceiveDataset { args }
79    protected method ReceiveImage { args }
80    protected method ReceiveLegend { colormap title vmin vmax size }
81    protected method Rotate {option x y}
82    protected method SendCmd {string}
83    protected method Zoom {option}
84
85    # The following methods are only used by this class.
86    private method BuildAxisTab {}
87    private method BuildCameraTab {}
88    private method BuildColormap { name colors }
89    private method BuildCutplaneTab {}
90    private method BuildDownloadPopup { widget command }
91    private method BuildStreamsTab {}
92    private method BuildVolumeTab {}
93    private method ConvertToVtkData { dataobj comp }
94    private method DrawLegend { title }
95    private method EnterLegend { x y }
96    private method EventuallyResize { w h }
97    private method EventuallyReseed { numPoints }
98    private method EventuallyRotate { q }
99    private method EventuallySetCutplane { axis args }
100    private method GetImage { args }
101    private method GetVtkData { args }
102    private method IsValidObject { dataobj }
103    private method LeaveLegend {}
104    private method MotionLegend { x y }
105    private method PanCamera {}
106    private method RequestLegend {}
107    private method SetColormap { dataobj comp }
108    private method ChangeColormap { dataobj comp color }
109    private method ColorsToColormap { color }
110    private method SetLegendTip { x y }
111    private method SetObjectStyle { dataobj comp }
112    private method Slice {option args}
113
114    private variable _arcball ""
115    private variable _outbuf       ;    # buffer for outgoing commands
116
117    private variable _dlist ""     ;    # list of data objects
118    private variable _obj2datasets
119    private variable _obj2ovride   ;    # maps dataobj => style override
120    private variable _datasets     ;    # contains all the dataobj-component
121                                   ;    # datasets in the server
122    private variable _colormaps    ;    # contains all the colormaps
123                                   ;    # in the server.
124    private variable _dataset2style    ;# maps dataobj-component to transfunc
125
126    private variable _click        ;    # info used for rotate operations
127    private variable _limits       ;    # autoscale min/max for all axes
128    private variable _view         ;    # view params for 3D view
129    private variable _settings
130    private variable _style;            # Array of current component styles.
131    private variable _initialStyle;     # Array of initial component styles.
132    private variable _reset 1;          # indicates if camera needs to be reset
133                                        # to starting position.
134
135    private variable _first ""     ;    # This is the topmost dataset.
136    private variable _start 0
137    private variable _buffering 0
138    private variable _title ""
139    private variable _seeds
140
141    common _downloadPopup;              # download options from popup
142    private common _hardcopy
143    private variable _width 0
144    private variable _height 0
145    private variable _resizePending 0
146    private variable _reseedPending 0
147    private variable _rotatePending 0
148    private variable _cutplanePending 0
149    private variable _outline
150    private variable _vectorFields
151    private variable _scalarFields
152    private variable _fields
153    private variable _currentField ""
154    private variable _numSeeds 200
155    private variable _colorMode "vmag";#  Mode of colormap (vmag or scalar)
156}
157
158itk::usual VtkStreamlinesViewer {
159    keep -background -foreground -cursor -font
160    keep -plotbackground -plotforeground
161}
162
163# ----------------------------------------------------------------------
164# CONSTRUCTOR
165# ----------------------------------------------------------------------
166itcl::body Rappture::VtkStreamlinesViewer::constructor {hostlist args} {
167    # Rebuild event
168    $_dispatcher register !rebuild
169    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
170
171    # Resize event
172    $_dispatcher register !resize
173    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
174
175    # Reseed event
176    $_dispatcher register !reseed
177    $_dispatcher dispatch $this !reseed "[itcl::code $this DoReseed]; list"
178
179    # Rotate event
180    $_dispatcher register !rotate
181    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
182
183    # X-Cutplane event
184    $_dispatcher register !xcutplane
185    $_dispatcher dispatch $this !xcutplane \
186        "[itcl::code $this AdjustSetting cutplane-xposition]; list"
187
188    # Y-Cutplane event
189    $_dispatcher register !ycutplane
190    $_dispatcher dispatch $this !ycutplane \
191        "[itcl::code $this AdjustSetting cutplane-yposition]; list"
192
193    # Z-Cutplane event
194    $_dispatcher register !zcutplane
195    $_dispatcher dispatch $this !zcutplane \
196        "[itcl::code $this AdjustSetting cutplane-zposition]; list"
197
198    #
199    # Populate parser with commands handle incoming requests
200    #
201    $_parser alias image [itcl::code $this ReceiveImage]
202    $_parser alias dataset [itcl::code $this ReceiveDataset]
203    $_parser alias legend [itcl::code $this ReceiveLegend]
204
205    array set _outline {
206        id -1
207        afterId -1
208        x1 -1
209        y1 -1
210        x2 -1
211        y2 -1
212    }
213    # Initialize the view to some default parameters.
214    array set _view {
215        qw              1
216        qx              0
217        qy              0
218        qz              0
219        zoom            1.0
220        xpan            0
221        ypan            0
222        ortho           0
223    }
224    set _arcball [blt::arcball create 100 100]
225    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
226    $_arcball quaternion $q
227
228    set _limits(zmin) 0.0
229    set _limits(zmax) 1.0
230
231    array set _settings [subst {
232        axis-xgrid              0
233        axis-ygrid              0
234        axis-zgrid              0
235        axis-xcutplane          0
236        axis-ycutplane          0
237        axis-zcutplane          0
238        axis-xposition          0
239        axis-yposition          0
240        axis-zposition          0
241        axis-visible            1
242        axis-labels             1
243        cutplane-edges          0
244        cutplane-xvisible       0
245        cutplane-yvisible       0
246        cutplane-zvisible       0
247        cutplane-xposition      50
248        cutplane-yposition      50
249        cutplane-zposition      50
250        cutplane-visible        1
251        cutplane-wireframe      0
252        cutplane-opacity        100
253        volume-edges            0
254        volume-lighting         1
255        volume-opacity          40
256        volume-visible          1
257        volume-wireframe        0
258        streamlines-seeds       0
259        streamlines-visible     1
260        streamlines-opacity     100
261        streamlines-numpoints   200
262        streamlines-lighting    1
263        streamlines-scale       1
264        legend-visible          1
265    }]
266
267    itk_component add view {
268        canvas $itk_component(plotarea).view \
269            -highlightthickness 0 -borderwidth 0
270    } {
271        usual
272        ignore -highlightthickness -borderwidth  -background
273    }
274
275    set c $itk_component(view)
276    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
277    bind $c <4> [itcl::code $this Zoom in 0.25]
278    bind $c <5> [itcl::code $this Zoom out 0.25]
279    bind $c <KeyPress-Left>  [list %W xview scroll 10 units]
280    bind $c <KeyPress-Right> [list %W xview scroll -10 units]
281    bind $c <KeyPress-Up>    [list %W yview scroll 10 units]
282    bind $c <KeyPress-Down>  [list %W yview scroll -10 units]
283    bind $c <Enter> "focus %W"
284
285    # Fix the scrollregion in case we go off screen
286    $c configure -scrollregion [$c bbox all]
287
288    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
289    set _map(cwidth) -1
290    set _map(cheight) -1
291    set _map(zoom) 1.0
292    set _map(original) ""
293
294    set f [$itk_component(main) component controls]
295    itk_component add reset {
296        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
297            -highlightthickness 0 \
298            -image [Rappture::icon reset-view] \
299            -command [itcl::code $this Zoom reset]
300    } {
301        usual
302        ignore -highlightthickness
303    }
304    pack $itk_component(reset) -side top -padx 2 -pady 2
305    Rappture::Tooltip::for $itk_component(reset) "Reset the view to the default zoom level"
306
307    itk_component add zoomin {
308        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
309            -highlightthickness 0 \
310            -image [Rappture::icon zoom-in] \
311            -command [itcl::code $this Zoom in]
312    } {
313        usual
314        ignore -highlightthickness
315    }
316    pack $itk_component(zoomin) -side top -padx 2 -pady 2
317    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
318
319    itk_component add zoomout {
320        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
321            -highlightthickness 0 \
322            -image [Rappture::icon zoom-out] \
323            -command [itcl::code $this Zoom out]
324    } {
325        usual
326        ignore -highlightthickness
327    }
328    pack $itk_component(zoomout) -side top -padx 2 -pady 2
329    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
330
331    itk_component add volume {
332        Rappture::PushButton $f.volume \
333            -onimage [Rappture::icon volume-on] \
334            -offimage [Rappture::icon volume-off] \
335            -variable [itcl::scope _settings(volume-visible)] \
336            -command [itcl::code $this AdjustSetting volume-visible]
337    }
338    $itk_component(volume) select
339    Rappture::Tooltip::for $itk_component(volume) \
340        "Don't display the volume"
341    pack $itk_component(volume) -padx 2 -pady 2
342
343    itk_component add streamlines {
344        Rappture::PushButton $f.streamlines \
345            -onimage [Rappture::icon streamlines-on] \
346            -offimage [Rappture::icon streamlines-off] \
347            -variable [itcl::scope _settings(streamlines-visible)] \
348            -command [itcl::code $this AdjustSetting streamlines-visible] \
349    }
350    $itk_component(streamlines) select
351    Rappture::Tooltip::for $itk_component(streamlines) \
352        "Toggle the streamlines on/off"
353    pack $itk_component(streamlines) -padx 2 -pady 2
354
355    itk_component add cutplane {
356        Rappture::PushButton $f.cutplane \
357            -onimage [Rappture::icon cutbutton] \
358            -offimage [Rappture::icon cutbutton] \
359            -variable [itcl::scope _settings(cutplane-visible)] \
360            -command [itcl::code $this AdjustSetting cutplane-visible]
361    }
362    $itk_component(cutplane) select
363    Rappture::Tooltip::for $itk_component(cutplane) \
364        "Show/Hide cutplanes"
365    pack $itk_component(cutplane) -padx 2 -pady 2
366
367
368    if { [catch {
369        BuildVolumeTab
370        BuildStreamsTab
371        BuildCutplaneTab
372        BuildAxisTab
373        BuildCameraTab
374    } errs] != 0 } {
375        puts stderr errs=$errs
376    }
377    # Legend
378
379    set _image(legend) [image create photo]
380    itk_component add legend {
381        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
382    } {
383        usual
384        ignore -highlightthickness
385        rename -background -plotbackground plotBackground Background
386    }
387
388    # Hack around the Tk panewindow.  The problem is that the requested
389    # size of the 3d view isn't set until an image is retrieved from
390    # the server.  So the panewindow uses the tiny size.
391    set w 10000
392    pack forget $itk_component(view)
393    blt::table $itk_component(plotarea) \
394        0,0 $itk_component(view) -fill both -reqwidth $w
395    blt::table configure $itk_component(plotarea) c1 -resize none
396
397    # Bindings for rotation via mouse
398    bind $itk_component(view) <ButtonPress-1> \
399        [itcl::code $this Rotate click %x %y]
400    bind $itk_component(view) <B1-Motion> \
401        [itcl::code $this Rotate drag %x %y]
402    bind $itk_component(view) <ButtonRelease-1> \
403        [itcl::code $this Rotate release %x %y]
404    bind $itk_component(view) <Configure> \
405        [itcl::code $this EventuallyResize %w %h]
406
407    if 0 {
408    bind $itk_component(view) <Configure> \
409        [itcl::code $this EventuallyResize %w %h]
410    }
411    # Bindings for panning via mouse
412    bind $itk_component(view) <ButtonPress-2> \
413        [itcl::code $this Pan click %x %y]
414    bind $itk_component(view) <B2-Motion> \
415        [itcl::code $this Pan drag %x %y]
416    bind $itk_component(view) <ButtonRelease-2> \
417        [itcl::code $this Pan release %x %y]
418
419    bind $itk_component(view) <ButtonRelease-3> \
420        [itcl::code $this Pick %x %y]
421
422    # Bindings for panning via keyboard
423    bind $itk_component(view) <KeyPress-Left> \
424        [itcl::code $this Pan set -10 0]
425    bind $itk_component(view) <KeyPress-Right> \
426        [itcl::code $this Pan set 10 0]
427    bind $itk_component(view) <KeyPress-Up> \
428        [itcl::code $this Pan set 0 -10]
429    bind $itk_component(view) <KeyPress-Down> \
430        [itcl::code $this Pan set 0 10]
431    bind $itk_component(view) <Shift-KeyPress-Left> \
432        [itcl::code $this Pan set -2 0]
433    bind $itk_component(view) <Shift-KeyPress-Right> \
434        [itcl::code $this Pan set 2 0]
435    bind $itk_component(view) <Shift-KeyPress-Up> \
436        [itcl::code $this Pan set 0 -2]
437    bind $itk_component(view) <Shift-KeyPress-Down> \
438        [itcl::code $this Pan set 0 2]
439
440    # Bindings for zoom via keyboard
441    bind $itk_component(view) <KeyPress-Prior> \
442        [itcl::code $this Zoom out]
443    bind $itk_component(view) <KeyPress-Next> \
444        [itcl::code $this Zoom in]
445
446    bind $itk_component(view) <Enter> "focus $itk_component(view)"
447
448    if {[string equal "x11" [tk windowingsystem]]} {
449        # Bindings for zoom via mouse
450        bind $itk_component(view) <4> [itcl::code $this Zoom out]
451        bind $itk_component(view) <5> [itcl::code $this Zoom in]
452    }
453
454    set _image(download) [image create photo]
455
456    eval itk_initialize $args
457    Connect
458    update
459}
460
461# ----------------------------------------------------------------------
462# DESTRUCTOR
463# ----------------------------------------------------------------------
464itcl::body Rappture::VtkStreamlinesViewer::destructor {} {
465    Disconnect
466    $_dispatcher cancel !rebuild
467    $_dispatcher cancel !resize
468    $_dispatcher cancel !reseed
469    $_dispatcher cancel !rotate
470    $_dispatcher cancel !xcutplane
471    $_dispatcher cancel !ycutplane
472    $_dispatcher cancel !zcutplane
473    image delete $_image(plot)
474    image delete $_image(download)
475    catch { blt::arcball destroy $_arcball }
476}
477
478itcl::body Rappture::VtkStreamlinesViewer::DoResize {} {
479    if { $_width < 2 } {
480        set _width 500
481    }
482    if { $_height < 2 } {
483        set _height 500
484    }
485    set _start [clock clicks -milliseconds]
486    SendCmd "screen size $_width $_height"
487    RequestLegend
488
489    #SendCmd "imgflush"
490
491    # Must reset camera to have object scaling to take effect.
492    #SendCmd "camera reset"
493    #SendCmd "camera zoom $_view(zoom)"
494    set _resizePending 0
495}
496
497itcl::body Rappture::VtkStreamlinesViewer::DoRotate {} {
498    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
499    SendCmd "camera orient $q"
500    set _rotatePending 0
501}
502
503itcl::body Rappture::VtkStreamlinesViewer::DoReseed {} {
504    foreach dataset [CurrentDatasets -visible] {
505        foreach {dataobj comp} [split $dataset -] break
506        # This command works for either random or fmesh seeds
507        SendCmd "streamlines seed numpts $_numSeeds $dataset"
508    }
509    set _reseedPending 0
510}
511
512itcl::body Rappture::VtkStreamlinesViewer::EventuallyResize { w h } {
513    set _width $w
514    set _height $h
515    $_arcball resize $w $h
516    if { !$_resizePending } {
517        set _resizePending 1
518        $_dispatcher event -after 400 !resize
519    }
520}
521
522itcl::body Rappture::VtkStreamlinesViewer::EventuallyReseed { numPoints } {
523    set _numSeeds $numPoints
524    if { !$_reseedPending } {
525        set _reseedPending 1
526        $_dispatcher event -after 600 !reseed
527    }
528}
529
530set rotate_delay 100
531
532itcl::body Rappture::VtkStreamlinesViewer::EventuallyRotate { q } {
533    foreach { _view(qw) _view(qx) _view(qy) _view(qz) } $q break
534    if { !$_rotatePending } {
535        set _rotatePending 1
536        global rotate_delay
537        $_dispatcher event -after $rotate_delay !rotate
538    }
539}
540
541itcl::body Rappture::VtkStreamlinesViewer::EventuallySetCutplane { axis args } {
542    if { !$_cutplanePending } {
543        set _cutplanePending 1
544        $_dispatcher event -after 100 !${axis}cutplane
545    }
546}
547
548# ----------------------------------------------------------------------
549# USAGE: add <dataobj> ?<settings>?
550#
551# Clients use this to add a data object to the plot.  The optional
552# <settings> are used to configure the plot.  Allowed settings are
553# -color, -brightness, -width, -linestyle, and -raise.
554# ----------------------------------------------------------------------
555itcl::body Rappture::VtkStreamlinesViewer::add {dataobj {settings ""}} {
556    array set params {
557        -color auto
558        -width 1
559        -linestyle solid
560        -brightness 0
561        -raise 0
562        -description ""
563        -param ""
564        -type ""
565    }
566    array set params $settings
567    set params(-description) ""
568    set params(-param) ""
569    foreach {opt val} $settings {
570        if {![info exists params($opt)]} {
571            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
572        }
573        set params($opt) $val
574    }
575    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
576        # can't handle -autocolors yet
577        set params(-color) black
578    }
579    set pos [lsearch -exact $dataobj $_dlist]
580    if {$pos < 0} {
581        lappend _dlist $dataobj
582    }
583    set _obj2ovride($dataobj-color) $params(-color)
584    set _obj2ovride($dataobj-width) $params(-width)
585    set _obj2ovride($dataobj-raise) $params(-raise)
586    $_dispatcher event -idle !rebuild
587}
588
589
590# ----------------------------------------------------------------------
591# USAGE: delete ?<dataobj1> <dataobj2> ...?
592#
593#       Clients use this to delete a dataobj from the plot.  If no dataobjs
594#       are specified, then all dataobjs are deleted.  No data objects are
595#       deleted.  They are only removed from the display list.
596#
597# ----------------------------------------------------------------------
598itcl::body Rappture::VtkStreamlinesViewer::delete {args} {
599    if { [llength $args] == 0} {
600        set args $_dlist
601    }
602    # Delete all specified dataobjs
603    set changed 0
604    foreach dataobj $args {
605        set pos [lsearch -exact $_dlist $dataobj]
606        if { $pos < 0 } {
607            continue;                   # Don't know anything about it.
608        }
609        # Remove it from the dataobj list.
610        set _dlist [lreplace $_dlist $pos $pos]
611        SendCmd "dataset visible 0"
612        array unset _obj2ovride $dataobj-*
613        array unset _settings $dataobj-*
614        # Append to the end of the dataobj list.
615        lappend _dlist $dataobj
616        set changed 1
617    }
618    # If anything changed, then rebuild the plot
619    if { $changed } {
620        $_dispatcher event -idle !rebuild
621    }
622}
623
624# ----------------------------------------------------------------------
625# USAGE: get ?-objects?
626# USAGE: get ?-visible?
627# USAGE: get ?-image view?
628#
629# Clients use this to query the list of objects being plotted, in
630# order from bottom to top of this result.  The optional "-image"
631# flag can also request the internal images being shown.
632# ----------------------------------------------------------------------
633itcl::body Rappture::VtkStreamlinesViewer::get {args} {
634    if {[llength $args] == 0} {
635        set args "-objects"
636    }
637
638    set op [lindex $args 0]
639    switch -- $op {
640        "-objects" {
641            # put the dataobj list in order according to -raise options
642            set dlist {}
643            foreach dataobj $_dlist {
644                if { ![IsValidObject $dataobj] } {
645                    continue
646                }
647                if {[info exists _obj2ovride($dataobj-raise)] &&
648                    $_obj2ovride($dataobj-raise)} {
649                    set dlist [linsert $dlist 0 $dataobj]
650                } else {
651                    lappend dlist $dataobj
652                }
653            }
654            return $dlist
655        }
656        "-visible" {
657            set dlist {}
658            foreach dataobj $_dlist {
659                if { ![IsValidObject $dataobj] } {
660                    continue
661                }
662                if { ![info exists _obj2ovride($dataobj-raise)] } {
663                    # No setting indicates that the object isn't visible.
664                    continue
665                }
666                # Otherwise use the -raise parameter to put the object to
667                # the front of the list.
668                if { $_obj2ovride($dataobj-raise) } {
669                    set dlist [linsert $dlist 0 $dataobj]
670                } else {
671                    lappend dlist $dataobj
672                }
673            }
674            return $dlist
675        }           
676        -image {
677            if {[llength $args] != 2} {
678                error "wrong # args: should be \"get -image view\""
679            }
680            switch -- [lindex $args end] {
681                view {
682                    return $_image(plot)
683                }
684                default {
685                    error "bad image name \"[lindex $args end]\": should be view"
686                }
687            }
688        }
689        default {
690            error "bad option \"$op\": should be -objects or -image"
691        }
692    }
693}
694
695# ----------------------------------------------------------------------
696# USAGE: scale ?<data1> <data2> ...?
697#
698# Sets the default limits for the overall plot according to the
699# limits of the data for all of the given <data> objects.  This
700# accounts for all objects--even those not showing on the screen.
701# Because of this, the limits are appropriate for all objects as
702# the user scans through data in the ResultSet viewer.
703# ----------------------------------------------------------------------
704itcl::body Rappture::VtkStreamlinesViewer::scale {args} {
705    array unset _limits
706    foreach dataobj $args {
707        set string [limits $dataobj]
708        if { $string == "" } {
709            continue
710        }
711        array set bounds $string
712        if {![info exists _limits(xmin)] || $_limits(xmin) > $bounds(xmin)} {
713            set _limits(xmin) $bounds(xmin)
714        }
715        if {![info exists _limits(xmax)] || $_limits(xmax) < $bounds(xmax)} {
716            set _limits(xmax) $bounds(xmax)
717        }
718
719        if {![info exists _limits(ymin)] || $_limits(ymin) > $bounds(ymin)} {
720            set _limits(ymin) $bounds(ymin)
721        }
722        if {![info exists _limits(ymax)] || $_limits(ymax) < $bounds(ymax)} {
723            set _limits(ymax) $bounds(ymax)
724        }
725
726        if {![info exists _limits(zmin)] || $_limits(zmin) > $bounds(zmin)} {
727            set _limits(zmin) $bounds(zmin)
728        }
729        if {![info exists _limits(zmax)] || $_limits(zmax) < $bounds(zmax)} {
730            set _limits(zmax) $bounds(zmax)
731        }
732    }
733}
734
735# ----------------------------------------------------------------------
736# USAGE: download coming
737# USAGE: download controls <downloadCommand>
738# USAGE: download now
739#
740# Clients use this method to create a downloadable representation
741# of the plot.  Returns a list of the form {ext string}, where
742# "ext" is the file extension (indicating the type of data) and
743# "string" is the data itself.
744# ----------------------------------------------------------------------
745itcl::body Rappture::VtkStreamlinesViewer::download {option args} {
746    switch $option {
747        coming {
748            if {[catch {
749                blt::winop snap $itk_component(plotarea) $_image(download)
750            }]} {
751                $_image(download) configure -width 1 -height 1
752                $_image(download) put #000000
753            }
754        }
755        controls {
756            set popup .vtkviewerdownload
757            if { ![winfo exists .vtkviewerdownload] } {
758                set inner [BuildDownloadPopup $popup [lindex $args 0]]
759            } else {
760                set inner [$popup component inner]
761            }
762            set _downloadPopup(image_controls) $inner.image_frame
763            set num [llength [get]]
764            set num [expr {($num == 1) ? "1 result" : "$num results"}]
765            set word [Rappture::filexfer::label downloadWord]
766            $inner.summary configure -text "$word $num in the following format:"
767            update idletasks            ;# Fix initial sizes
768            return $popup
769        }
770        now {
771            set popup .vtkviewerdownload
772            if {[winfo exists .vtkviewerdownload]} {
773                $popup deactivate
774            }
775            switch -- $_downloadPopup(format) {
776                "image" {
777                    return [$this GetImage [lindex $args 0]]
778                }
779                "vtk" {
780                    return [$this GetVtkData [lindex $args 0]]
781                }
782            }
783            return ""
784        }
785        default {
786            error "bad option \"$option\": should be coming, controls, now"
787        }
788    }
789}
790
791# ----------------------------------------------------------------------
792# USAGE: Connect ?<host:port>,<host:port>...?
793#
794# Clients use this method to establish a connection to a new
795# server, or to reestablish a connection to the previous server.
796# Any existing connection is automatically closed.
797# ----------------------------------------------------------------------
798itcl::body Rappture::VtkStreamlinesViewer::Connect {} {
799    set _hosts [GetServerList "vtkvis"]
800    if { "" == $_hosts } {
801        return 0
802    }
803    set result [VisViewer::Connect $_hosts]
804    if { $result } {
805        set w [winfo width $itk_component(view)]
806        set h [winfo height $itk_component(view)]
807        EventuallyResize $w $h
808    }
809    return $result
810}
811
812#
813# isconnected --
814#
815#       Indicates if we are currently connected to the visualization server.
816#
817itcl::body Rappture::VtkStreamlinesViewer::isconnected {} {
818    return [VisViewer::IsConnected]
819}
820
821#
822# disconnect --
823#
824itcl::body Rappture::VtkStreamlinesViewer::disconnect {} {
825    Disconnect
826    set _reset 1
827}
828
829#
830# Disconnect --
831#
832#       Clients use this method to disconnect from the current rendering
833#       server.
834#
835itcl::body Rappture::VtkStreamlinesViewer::Disconnect {} {
836    VisViewer::Disconnect
837
838    # disconnected -- no more data sitting on server
839    set _outbuf ""
840    array unset _datasets
841    array unset _data
842    array unset _colormaps
843    array unset _seeds
844    array unset _dataset2style
845    array unset _obj2datasets
846}
847
848#
849# sendto --
850#
851itcl::body Rappture::VtkStreamlinesViewer::sendto { bytes } {
852    SendBytes "$bytes\n"
853}
854
855#
856# SendCmd
857#
858#       Send commands off to the rendering server.  If we're currently
859#       sending data objects to the server, buffer the commands to be
860#       sent later.
861#
862itcl::body Rappture::VtkStreamlinesViewer::SendCmd {string} {
863    if { $_buffering } {
864        append _outbuf $string "\n"
865    } else {
866        SendBytes "$string\n"
867    }
868}
869
870# ----------------------------------------------------------------------
871# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
872#
873# Invoked automatically whenever the "image" command comes in from
874# the rendering server.  Indicates that binary image data with the
875# specified <size> will follow.
876# ----------------------------------------------------------------------
877itcl::body Rappture::VtkStreamlinesViewer::ReceiveImage { args } {
878    array set info {
879        -token "???"
880        -bytes 0
881        -type image
882    }
883    array set info $args
884    set bytes [ReceiveBytes $info(-bytes)]
885    if { $info(-type) == "image" } {
886        if 0 {
887            set f [open "last.ppm" "w"]
888            puts $f $bytes
889            close $f
890        }
891        $_image(plot) configure -data $bytes
892        set time [clock seconds]
893        set date [clock format $time]
894        #puts stderr "$date: received image [image width $_image(plot)]x[image height $_image(plot)] image>"       
895        if { $_start > 0 } {
896            set finish [clock clicks -milliseconds]
897            #puts stderr "round trip time [expr $finish -$_start] milliseconds"
898            set _start 0
899        }
900    } elseif { $info(type) == "print" } {
901        set tag $this-print-$info(-token)
902        set _hardcopy($tag) $bytes
903    }
904}
905
906#
907# ReceiveDataset --
908#
909itcl::body Rappture::VtkStreamlinesViewer::ReceiveDataset { args } {
910    if { ![isconnected] } {
911        return
912    }
913    set option [lindex $args 0]
914    switch -- $option {
915        "scalar" {
916            set option [lindex $args 1]
917            switch -- $option {
918                "world" {
919                    foreach { x y z value tag } [lrange $args 2 end] break
920                }
921                "pixel" {
922                    foreach { x y value tag } [lrange $args 2 end] break
923                }
924            }
925        }
926        "vector" {
927            set option [lindex $args 1]
928            switch -- $option {
929                "world" {
930                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
931                }
932                "pixel" {
933                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
934                }
935            }
936        }
937        "names" {
938            foreach { name } [lindex $args 1] {
939                #puts stderr "Dataset: $name"
940            }
941        }
942        default {
943            error "unknown dataset option \"$option\" from server"
944        }
945    }
946}
947
948# ----------------------------------------------------------------------
949# USAGE: Rebuild
950#
951# Called automatically whenever something changes that affects the
952# data in the widget.  Clears any existing data and rebuilds the
953# widget to display new data.
954# ----------------------------------------------------------------------
955itcl::body Rappture::VtkStreamlinesViewer::Rebuild {} {
956    update
957    set w [winfo width $itk_component(view)]
958    set h [winfo height $itk_component(view)]
959    if { $w < 2 || $h < 2 } {
960        $_dispatcher event -idle !rebuild
961        return
962    }
963
964    # Turn on buffering of commands to the server.  We don't want to
965    # be preempted by a server disconnect/reconnect (which automatically
966    # generates a new call to Rebuild).   
967    set _buffering 1
968
969    set _width $w
970    set _height $h
971    $_arcball resize $w $h
972    DoResize
973    #
974    # Reset the camera and other view parameters
975    #
976    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
977    $_arcball quaternion $q
978    if {$_view(ortho)} {
979        SendCmd "camera mode ortho"
980    } else {
981        SendCmd "camera mode persp"
982    }
983    DoRotate
984    PanCamera
985    set _first ""
986    InitSettings axis-xgrid axis-ygrid axis-zgrid axis-mode \
987        axis-visible axis-labels cutplane-visible \
988        cutplane-xposition cutplane-yposition cutplane-zposition \
989        cutplane-xvisible cutplane-yvisible cutplane-zvisible
990
991    SendCmd "imgflush"
992
993    set _limits(zmin) ""
994    set _limits(zmax) ""
995    set _first ""
996    foreach dataobj [get -objects] {
997        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
998            set _first $dataobj
999        }
1000        set _obj2datasets($dataobj) ""
1001        foreach comp [$dataobj components] {
1002            set tag $dataobj-$comp
1003            if { ![info exists _datasets($tag)] } {
1004                set bytes [$dataobj blob $comp]
1005                set length [string length $bytes]
1006                append _outbuf "dataset add $tag data follows $length\n"
1007                append _outbuf $bytes
1008                set _datasets($tag) 1
1009                SetObjectStyle $dataobj $comp
1010            }
1011            lappend _obj2datasets($dataobj) $tag
1012            if { [info exists _obj2ovride($dataobj-raise)] } {
1013                SendCmd "dataset visible 1 $tag"
1014            } else {
1015                SendCmd "dataset visible 0 $tag"
1016            }
1017        }
1018    }
1019    if {"" != $_first} {
1020        set location [$_first hints camera]
1021        if { $location != "" } {
1022            array set view $location
1023        }
1024
1025        foreach axis { x y z } {
1026            set label [$_first hints ${axis}label]
1027            if { $label != "" } {
1028                SendCmd "axis name $axis $label"
1029            }
1030            set units [$_first hints ${axis}units]
1031            if { $units != "" } {
1032                SendCmd "axis units $axis $units"
1033            }
1034        }
1035        array unset _scalarFields
1036        array unset _vectorFields
1037        set _currentField [$_first hints default]
1038        $itk_component(field) choices delete 0 end
1039        array unset _fields
1040        foreach { name title units } [$_first hints vectors] {
1041            set _vectorFields($title) $name
1042            $itk_component(field) choices insert end "$name" "$title"
1043            set _fields($name) [list $title $units]
1044        }
1045        foreach { name title units } [$_first hints scalars] {
1046            set _scalarFields($title) $name
1047            $itk_component(field) choices insert end "$name" "$title"
1048            set _fields($name) [list $title $units]
1049        }
1050        $itk_component(field) value $_currentField
1051    }
1052
1053    InitSettings streamlines-visible streamlines-palette volume-visible
1054
1055    if { $_reset } {
1056        InitSettings streamlines-seeds streamlines-opacity \
1057            streamlines-numpoints streamlines-lighting \
1058            streamlines-palette streamlines-field \
1059            volume-edges volume-lighting volume-opacity volume-wireframe
1060        Zoom reset
1061        set _reset 0
1062    }
1063       
1064    set _buffering 0;                        # Turn off buffering.
1065
1066    # Actually write the commands to the server socket.  If it fails, we don't
1067    # care.  We're finished here.
1068    blt::busy hold $itk_component(hull)
1069    SendBytes $_outbuf;                       
1070    blt::busy release $itk_component(hull)
1071    set _outbuf "";                        # Clear the buffer.               
1072}
1073
1074# ----------------------------------------------------------------------
1075# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
1076#
1077# Returns a list of server IDs for the current datasets being displayed.  This
1078# is normally a single ID, but it might be a list of IDs if the current data
1079# object has multiple components.
1080# ----------------------------------------------------------------------
1081itcl::body Rappture::VtkStreamlinesViewer::CurrentDatasets {args} {
1082    set flag [lindex $args 0]
1083    switch -- $flag {
1084        "-all" {
1085            if { [llength $args] > 1 } {
1086                error "CurrentDatasets: can't specify dataobj after \"-all\""
1087            }
1088            set dlist [get -objects]
1089        }
1090        "-visible" {
1091            if { [llength $args] > 1 } {
1092                set dlist {}
1093                set args [lrange $args 1 end]
1094                foreach dataobj $args {
1095                    if { [info exists _obj2ovride($dataobj-raise)] } {
1096                        lappend dlist $dataobj
1097                    }
1098                }
1099            } else {
1100                set dlist [get -visible]
1101            }
1102        }           
1103        default {
1104            set dlist $args
1105        }
1106    }
1107    set rlist ""
1108    foreach dataobj $dlist {
1109        foreach comp [$dataobj components] {
1110            set tag $dataobj-$comp
1111            if { [info exists _datasets($tag)] && $_datasets($tag) } {
1112                lappend rlist $tag
1113            }
1114        }
1115    }
1116    return $rlist
1117}
1118
1119# ----------------------------------------------------------------------
1120# USAGE: Zoom in
1121# USAGE: Zoom out
1122# USAGE: Zoom reset
1123#
1124# Called automatically when the user clicks on one of the zoom
1125# controls for this widget.  Changes the zoom for the current view.
1126# ----------------------------------------------------------------------
1127itcl::body Rappture::VtkStreamlinesViewer::Zoom {option} {
1128    switch -- $option {
1129        "in" {
1130            set _view(zoom) [expr {$_view(zoom)*1.25}]
1131            SendCmd "camera zoom $_view(zoom)"
1132        }
1133        "out" {
1134            set _view(zoom) [expr {$_view(zoom)*0.8}]
1135            SendCmd "camera zoom $_view(zoom)"
1136        }
1137        "reset" {
1138            array set _view {
1139                qw      1
1140                qx      0
1141                qy      0
1142                qz      0
1143                zoom    1.0
1144                xpan   0
1145                ypan   0
1146            }
1147            SendCmd "camera reset all"
1148            if { $_first != "" } {
1149                set location [$_first hints camera]
1150                if { $location != "" } {
1151                    array set _view $location
1152                }
1153            }
1154            set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1155            $_arcball quaternion $q
1156            DoRotate
1157        }
1158    }
1159}
1160
1161itcl::body Rappture::VtkStreamlinesViewer::PanCamera {} {
1162    set x $_view(xpan)
1163    set y $_view(ypan)
1164    SendCmd "camera pan $x $y"
1165}
1166
1167
1168# ----------------------------------------------------------------------
1169# USAGE: Rotate click <x> <y>
1170# USAGE: Rotate drag <x> <y>
1171# USAGE: Rotate release <x> <y>
1172#
1173# Called automatically when the user clicks/drags/releases in the
1174# plot area.  Moves the plot according to the user's actions.
1175# ----------------------------------------------------------------------
1176itcl::body Rappture::VtkStreamlinesViewer::Rotate {option x y} {
1177    switch -- $option {
1178        "click" {
1179            $itk_component(view) configure -cursor fleur
1180            set _click(x) $x
1181            set _click(y) $y
1182        }
1183        "drag" {
1184            if {[array size _click] == 0} {
1185                Rotate click $x $y
1186            } else {
1187                set w [winfo width $itk_component(view)]
1188                set h [winfo height $itk_component(view)]
1189                if {$w <= 0 || $h <= 0} {
1190                    return
1191                }
1192
1193                if {[catch {
1194                    # this fails sometimes for no apparent reason
1195                    set dx [expr {double($x-$_click(x))/$w}]
1196                    set dy [expr {double($y-$_click(y))/$h}]
1197                }]} {
1198                    return
1199                }
1200                if { $dx == 0 && $dy == 0 } {
1201                    return
1202                }
1203                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1204                EventuallyRotate $q
1205                set _click(x) $x
1206                set _click(y) $y
1207            }
1208        }
1209        "release" {
1210            Rotate drag $x $y
1211            $itk_component(view) configure -cursor ""
1212            catch {unset _click}
1213        }
1214        default {
1215            error "bad option \"$option\": should be click, drag, release"
1216        }
1217    }
1218}
1219
1220itcl::body Rappture::VtkStreamlinesViewer::Pick {x y} {
1221    foreach tag [CurrentDatasets -visible] {
1222        SendCmd "dataset getscalar pixel $x $y $tag"
1223    }
1224}
1225
1226# ----------------------------------------------------------------------
1227# USAGE: $this Pan click x y
1228#        $this Pan drag x y
1229#        $this Pan release x y
1230#
1231# Called automatically when the user clicks on one of the zoom
1232# controls for this widget.  Changes the zoom for the current view.
1233# ----------------------------------------------------------------------
1234itcl::body Rappture::VtkStreamlinesViewer::Pan {option x y} {
1235    switch -- $option {
1236        "set" {
1237            set w [winfo width $itk_component(view)]
1238            set h [winfo height $itk_component(view)]
1239            set x [expr $x / double($w)]
1240            set y [expr $y / double($h)]
1241            set _view(xpan) [expr $_view(xpan) + $x]
1242            set _view(ypan) [expr $_view(ypan) + $y]
1243            PanCamera
1244            return
1245        }
1246        "click" {
1247            set _click(x) $x
1248            set _click(y) $y
1249            $itk_component(view) configure -cursor hand1
1250        }
1251        "drag" {
1252            if { ![info exists _click(x)] } {
1253                set _click(x) $x
1254            }
1255            if { ![info exists _click(y)] } {
1256                set _click(y) $y
1257            }
1258            set w [winfo width $itk_component(view)]
1259            set h [winfo height $itk_component(view)]
1260            set dx [expr ($_click(x) - $x)/double($w)]
1261            set dy [expr ($_click(y) - $y)/double($h)]
1262            set _click(x) $x
1263            set _click(y) $y
1264            set _view(xpan) [expr $_view(xpan) - $dx]
1265            set _view(ypan) [expr $_view(ypan) - $dy]
1266            PanCamera
1267        }
1268        "release" {
1269            Pan drag $x $y
1270            $itk_component(view) configure -cursor ""
1271        }
1272        default {
1273            error "unknown option \"$option\": should set, click, drag, or release"
1274        }
1275    }
1276}
1277
1278# ----------------------------------------------------------------------
1279# USAGE: InitSettings <what> ?<value>?
1280#
1281# Used internally to update rendering settings whenever parameters
1282# change in the popup settings panel.  Sends the new settings off
1283# to the back end.
1284# ----------------------------------------------------------------------
1285itcl::body Rappture::VtkStreamlinesViewer::InitSettings { args } {
1286    foreach spec $args {
1287        if { [info exists _settings($_first-$spec)] } {
1288            # Reset global setting with dataobj specific setting
1289            set _settings($spec) $_settings($_first-$spec)
1290        }
1291        AdjustSetting $spec
1292    }
1293}
1294
1295#
1296# AdjustSetting --
1297#
1298#       Changes/updates a specific setting in the widget.  There are
1299#       usually user-setable option.  Commands are sent to the render
1300#       server.
1301#
1302itcl::body Rappture::VtkStreamlinesViewer::AdjustSetting {what {value ""}} {
1303    if { ![isconnected] } {
1304        return
1305    }
1306    switch -- $what {
1307        "volume-opacity" {
1308            set val $_settings(volume-opacity)
1309            set sval [expr { 0.01 * double($val) }]
1310            foreach dataset [CurrentDatasets -visible] {
1311                SendCmd "polydata opacity $sval $dataset"
1312            }
1313        }
1314        "volume-wireframe" {
1315            set bool $_settings(volume-wireframe)
1316            foreach dataset [CurrentDatasets -visible] {
1317                SendCmd "polydata wireframe $bool $dataset"
1318            }
1319        }
1320        "volume-visible" {
1321            set bool $_settings(volume-visible)
1322            foreach dataset [CurrentDatasets -visible] {
1323                SendCmd "polydata visible $bool $dataset"
1324            }
1325            if { $bool } {
1326                Rappture::Tooltip::for $itk_component(volume) \
1327                    "Hide the volume"
1328            } else {
1329                Rappture::Tooltip::for $itk_component(volume) \
1330                    "Show the volume"
1331            }
1332        }
1333        "volume-lighting" {
1334            set bool $_settings(volume-lighting)
1335            foreach dataset [CurrentDatasets -visible] {
1336                SendCmd "polydata lighting $bool $dataset"
1337            }
1338        }
1339        "volume-edges" {
1340            set bool $_settings(volume-edges)
1341            foreach dataset [CurrentDatasets -visible] {
1342                SendCmd "polydata edges $bool $dataset"
1343            }
1344        }
1345        "axis-visible" {
1346            set bool $_settings(axis-visible)
1347            SendCmd "axis visible all $bool"
1348        }
1349        "axis-labels" {
1350            set bool $_settings(axis-labels)
1351            SendCmd "axis labels all $bool"
1352        }
1353        "axis-xgrid" - "axis-ygrid" - "axis-zgrid" {
1354            set axis [string range $what 5 5]
1355            set bool $_settings($what)
1356            SendCmd "axis grid $axis $bool"
1357        }
1358        "axis-mode" {
1359            set mode [$itk_component(axismode) value]
1360            set mode [$itk_component(axismode) translate $mode]
1361            set _settings($what) $mode
1362            SendCmd "axis flymode $mode"
1363        }
1364        "cutplane-edges" {
1365            set bool $_settings($what)
1366            foreach dataset [CurrentDatasets -visible] {
1367                SendCmd "cutplane edges $bool $dataset"
1368            }
1369        }
1370        "cutplane-visible" {
1371            set bool $_settings($what)
1372            foreach dataset [CurrentDatasets -visible] {
1373                SendCmd "cutplane visible $bool $dataset"
1374            }
1375        }
1376        "cutplane-wireframe" {
1377            set bool $_settings($what)
1378            foreach dataset [CurrentDatasets -visible] {
1379                SendCmd "cutplane wireframe $bool $dataset"
1380            }
1381        }
1382        "cutplane-lighting" {
1383            set bool $_settings($what)
1384            foreach dataset [CurrentDatasets -visible] {
1385                SendCmd "cutplane lighting $bool $dataset"
1386            }
1387        }
1388        "cutplane-opacity" {
1389            set val $_settings($what)
1390            set sval [expr { 0.01 * double($val) }]
1391            foreach dataset [CurrentDatasets -visible] {
1392                SendCmd "cutplane opacity $sval $dataset"
1393            }
1394        }
1395        "cutplane-xvisible" - "cutplane-yvisible" - "cutplane-zvisible" {
1396            set axis [string range $what 9 9]
1397            set bool $_settings($what)
1398            if { $bool } {
1399                $itk_component(${axis}CutScale) configure -state normal \
1400                    -troughcolor white
1401            } else {
1402                $itk_component(${axis}CutScale) configure -state disabled \
1403                    -troughcolor grey82
1404            }
1405            SendCmd "cutplane axis $axis $bool"
1406        }
1407        "cutplane-xposition" - "cutplane-yposition" - "cutplane-zposition" {
1408            set axis [string range $what 9 9]
1409            set pos [expr $_settings($what) * 0.01]
1410            SendCmd "cutplane slice ${axis} ${pos}"
1411            set _cutplanePending 0
1412        }
1413        "streamlines-seeds" {
1414            set bool $_settings($what)
1415            foreach dataset [CurrentDatasets -visible] {
1416                SendCmd "streamlines seed visible $bool $dataset"
1417            }
1418        }
1419        "streamlines-numpoints" {
1420            set density $_settings($what)
1421            EventuallyReseed $density
1422        }
1423        "streamlines-visible" {
1424            set bool $_settings($what)
1425            foreach dataset [CurrentDatasets -visible] {
1426                SendCmd "streamlines visible $bool $dataset"
1427            }
1428            if { $bool } {
1429                Rappture::Tooltip::for $itk_component(streamlines) \
1430                    "Hide the streamlines"
1431            } else {
1432                Rappture::Tooltip::for $itk_component(streamlines) \
1433                    "Show the streamlines"
1434            }
1435        }
1436        "streamlines-mode" {
1437            set mode [$itk_component(streammode) value]
1438            set _settings(streamlines-mode) $mode
1439            foreach dataset [CurrentDatasets -visible] {
1440                switch -- $mode {
1441                    "lines" {
1442                        SendCmd "streamlines lines $dataset"
1443                    }
1444                    "ribbons" {
1445                        SendCmd "streamlines ribbons 3 0 $dataset"
1446                    }
1447                    "tubes" {
1448                        SendCmd "streamlines tubes 5 3 $dataset"
1449                    }
1450                }
1451            }
1452        }
1453        "streamlines-palette" {
1454            set palette [$itk_component(palette) value]
1455            set _settings(streamlines-palette) $palette
1456            foreach dataset [CurrentDatasets -visible $_first] {
1457                foreach {dataobj comp} [split $dataset -] break
1458                ChangeColormap $dataobj $comp $palette
1459            }
1460            RequestLegend
1461        }
1462        "streamlines-opacity" {
1463            set val $_settings(streamlines-opacity)
1464            set sval [expr { 0.01 * double($val) }]
1465            foreach dataset [CurrentDatasets -visible $_first] {
1466                SendCmd "streamlines opacity $sval $dataset"
1467            }
1468        }
1469        "streamlines-scale" {
1470            set val $_settings(streamlines-scale)
1471            set sval [expr { 0.01 * double($val) }]
1472            foreach dataset [CurrentDatasets -visible $_first] {
1473                SendCmd "streamlines scale $sval $sval $sval $dataset"
1474            }
1475        }
1476        "streamlines-lighting" {
1477            set bool $_settings(streamlines-lighting)
1478            foreach dataset [CurrentDatasets -visible $_first] {
1479                SendCmd "streamlines lighting $bool $dataset"
1480            }
1481        }
1482        "streamlines-field" {
1483            set new [$itk_component(field) value]
1484            set value [$itk_component(field) translate $new]
1485            set _settings(streamlines-field) $value
1486            if { [info exists _scalarFields($new)] } {
1487                set name $_scalarFields($new)
1488                set _colorMode scalar
1489                set _currentField $new
1490            } elseif { [info exists _vectorFields($new)] } {
1491                set name $_vectorFields($new)
1492                set _colorMode vmag
1493                set _currentField $new
1494            } else {
1495                puts stderr "unknown field \"$new\""
1496                return
1497            }
1498            foreach dataset [CurrentDatasets -visible] {
1499                SendCmd "streamlines colormode $_colorMode ${name} $dataset"
1500                SendCmd "cutplane colormode $_colorMode ${name} $dataset"
1501            }
1502            RequestLegend
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 c $itk_component(legend)
1521    set w 12
1522    set h [expr {$_height - 3 * ($lineht + 2)}]
1523    if { $h < 1} {
1524        return
1525    }
1526    if { [info exists _scalarFields($_currentField)] } {
1527        set name $_scalarFields($_currentField)
1528    } elseif { [info exists _vectorFields($_currentField)] } {
1529        set name $_vectorFields($_currentField)
1530    } else {
1531        return
1532    }
1533    # Set the legend on the first streamlines dataset.
1534    foreach dataset [CurrentDatasets -visible $_first] {
1535        foreach {dataobj comp} [split $dataset -] break
1536        if { [info exists _dataset2style($dataset)] } {
1537            SendCmd "legend $_dataset2style($dataset) $_colorMode $name {} $w $h 0"
1538            break;
1539        }
1540    }
1541}
1542
1543#
1544# ChangeColormap --
1545#
1546itcl::body Rappture::VtkStreamlinesViewer::ChangeColormap {dataobj comp color} {
1547    set tag $dataobj-$comp
1548    if { ![info exist _style($tag)] } {
1549        error "no initial colormap"
1550    }
1551    array set style $_style($tag)
1552    set style(-color) $color
1553    set _style($tag) [array get style]
1554    SetColormap $dataobj $comp
1555}
1556
1557#
1558# SetColormap --
1559#
1560itcl::body Rappture::VtkStreamlinesViewer::SetColormap { dataobj comp } {
1561    array set style {
1562        -color BGYOR
1563        -levels 6
1564        -opacity 1.0
1565    }
1566    set tag $dataobj-$comp
1567    if { ![info exists _initialStyle($tag)] } {
1568        # Save the initial component style.
1569        set _initialStyle($tag) [$dataobj style $comp]
1570    }
1571
1572    # Override defaults with initial style defined in xml.
1573    array set style $_initialStyle($tag)
1574
1575    if { ![info exists _style($tag)] } {
1576        set _style($tag) [array get style]
1577    }
1578    # Override initial style with current style.
1579    array set style $_style($tag)
1580
1581    set name "$style(-color):$style(-levels):$style(-opacity)"
1582    if { ![info exists _colormaps($name)] } {
1583        BuildColormap $name [array get style]
1584        set _colormaps($name) 1
1585    }
1586    if { ![info exists _dataset2style($tag)] ||
1587         $_dataset2style($tag) != $name } {
1588        SendCmd "streamlines colormap $name $tag"
1589        SendCmd "cutplane colormap $name $tag"
1590        set _dataset2style($tag) $name
1591    }
1592}
1593
1594itcl::body Rappture::VtkStreamlinesViewer::ColorsToColormap { colors } {
1595    switch -- $colors {
1596        "grey-to-blue" {
1597            return {
1598                0.0                      0.200 0.200 0.200
1599                0.14285714285714285      0.400 0.400 0.400
1600                0.2857142857142857       0.600 0.600 0.600
1601                0.42857142857142855      0.900 0.900 0.900
1602                0.5714285714285714       0.800 1.000 1.000
1603                0.7142857142857143       0.600 1.000 1.000
1604                0.8571428571428571       0.400 0.900 1.000
1605                1.0                      0.000 0.600 0.800
1606            }
1607        }
1608        "blue-to-grey" {
1609            return {
1610                0.0                     0.000 0.600 0.800
1611                0.14285714285714285     0.400 0.900 1.000
1612                0.2857142857142857      0.600 1.000 1.000
1613                0.42857142857142855     0.800 1.000 1.000
1614                0.5714285714285714      0.900 0.900 0.900
1615                0.7142857142857143      0.600 0.600 0.600
1616                0.8571428571428571      0.400 0.400 0.400
1617                1.0                     0.200 0.200 0.200
1618            }
1619        }
1620        "blue" {
1621            return {
1622                0.0                     0.900 1.000 1.000
1623                0.1111111111111111      0.800 0.983 1.000
1624                0.2222222222222222      0.700 0.950 1.000
1625                0.3333333333333333      0.600 0.900 1.000
1626                0.4444444444444444      0.500 0.833 1.000
1627                0.5555555555555556      0.400 0.750 1.000
1628                0.6666666666666666      0.300 0.650 1.000
1629                0.7777777777777778      0.200 0.533 1.000
1630                0.8888888888888888      0.100 0.400 1.000
1631                1.0                     0.000 0.250 1.000
1632            }
1633        }
1634        "brown-to-blue" {
1635            return {
1636                0.0                             0.200   0.100   0.000
1637                0.09090909090909091             0.400   0.187   0.000
1638                0.18181818181818182             0.600   0.379   0.210
1639                0.2727272727272727              0.800   0.608   0.480
1640                0.36363636363636365             0.850   0.688   0.595
1641                0.45454545454545453             0.950   0.855   0.808
1642                0.5454545454545454              0.800   0.993   1.000
1643                0.6363636363636364              0.600   0.973   1.000
1644                0.7272727272727273              0.400   0.940   1.000
1645                0.8181818181818182              0.200   0.893   1.000
1646                0.9090909090909091              0.000   0.667   0.800
1647                1.0                             0.000   0.480   0.600
1648            }
1649        }
1650        "blue-to-brown" {
1651            return {
1652                0.0                             0.000   0.480   0.600
1653                0.09090909090909091             0.000   0.667   0.800
1654                0.18181818181818182             0.200   0.893   1.000
1655                0.2727272727272727              0.400   0.940   1.000
1656                0.36363636363636365             0.600   0.973   1.000
1657                0.45454545454545453             0.800   0.993   1.000
1658                0.5454545454545454              0.950   0.855   0.808
1659                0.6363636363636364              0.850   0.688   0.595
1660                0.7272727272727273              0.800   0.608   0.480
1661                0.8181818181818182              0.600   0.379   0.210
1662                0.9090909090909091              0.400   0.187   0.000
1663                1.0                             0.200   0.100   0.000
1664            }
1665        }
1666        "blue-to-orange" {
1667            return {
1668                0.0                             0.000   0.167   1.000
1669                0.09090909090909091             0.100   0.400   1.000
1670                0.18181818181818182             0.200   0.600   1.000
1671                0.2727272727272727              0.400   0.800   1.000
1672                0.36363636363636365             0.600   0.933   1.000
1673                0.45454545454545453             0.800   1.000   1.000
1674                0.5454545454545454              1.000   1.000   0.800
1675                0.6363636363636364              1.000   0.933   0.600
1676                0.7272727272727273              1.000   0.800   0.400
1677                0.8181818181818182              1.000   0.600   0.200
1678                0.9090909090909091              1.000   0.400   0.100
1679                1.0                             1.000   0.167   0.000
1680            }
1681        }
1682        "orange-to-blue" {
1683            return {
1684                0.0                             1.000   0.167   0.000
1685                0.09090909090909091             1.000   0.400   0.100
1686                0.18181818181818182             1.000   0.600   0.200
1687                0.2727272727272727              1.000   0.800   0.400
1688                0.36363636363636365             1.000   0.933   0.600
1689                0.45454545454545453             1.000   1.000   0.800
1690                0.5454545454545454              0.800   1.000   1.000
1691                0.6363636363636364              0.600   0.933   1.000
1692                0.7272727272727273              0.400   0.800   1.000
1693                0.8181818181818182              0.200   0.600   1.000
1694                0.9090909090909091              0.100   0.400   1.000
1695                1.0                             0.000   0.167   1.000
1696            }
1697        }
1698        "rainbow" {
1699            set clist {
1700                "#EE82EE"
1701                "#4B0082"
1702                "blue"
1703                "#008000"
1704                "yellow"
1705                "#FFA500"
1706                "red"
1707            }
1708        }
1709        "BGYOR" {
1710            set clist {
1711                "blue"
1712                "#008000"
1713                "yellow"
1714                "#FFA500"
1715                "red"
1716            }
1717        }
1718        "ROYGB" {
1719            set clist {
1720                "red"
1721                "#FFA500"
1722                "yellow"
1723                "#008000"
1724                "blue"
1725            }
1726        }
1727        "RYGCB" {
1728            set clist {
1729                "red"
1730                "yellow"
1731                "green"
1732                "cyan"
1733                "blue"
1734            }
1735        }
1736        "BCGYR" {
1737            set clist {
1738                "blue"
1739                "cyan"
1740                "green"
1741                "yellow"
1742                "red"
1743            }
1744        }
1745        "spectral" {
1746            return {
1747                0.0 0.150 0.300 1.000
1748                0.1 0.250 0.630 1.000
1749                0.2 0.450 0.850 1.000
1750                0.3 0.670 0.970 1.000
1751                0.4 0.880 1.000 1.000
1752                0.5 1.000 1.000 0.750
1753                0.6 1.000 0.880 0.600
1754                0.7 1.000 0.680 0.450
1755                0.8 0.970 0.430 0.370
1756                0.9 0.850 0.150 0.196
1757                1.0 0.650 0.000 0.130
1758            }
1759        }
1760        "green-to-magenta" {
1761            return {
1762                0.0 0.000 0.316 0.000
1763                0.06666666666666667 0.000 0.526 0.000
1764                0.13333333333333333 0.000 0.737 0.000
1765                0.2 0.000 0.947 0.000
1766                0.26666666666666666 0.316 1.000 0.316
1767                0.3333333333333333 0.526 1.000 0.526
1768                0.4 0.737 1.000 0.737
1769                0.4666666666666667 1.000 1.000 1.000
1770                0.5333333333333333 1.000 0.947 1.000
1771                0.6 1.000 0.737 1.000
1772                0.6666666666666666 1.000 0.526 1.000
1773                0.7333333333333333 1.000 0.316 1.000
1774                0.8 0.947 0.000 0.947
1775                0.8666666666666667 0.737 0.000 0.737
1776                0.9333333333333333 0.526 0.000 0.526
1777                1.0 0.316 0.000 0.316
1778            }
1779        }
1780        "greyscale" {
1781            return {
1782                0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
1783            }
1784        }
1785        "nanohub" {
1786            set clist "white yellow green cyan blue magenta"
1787        }
1788        default {
1789            set clist $colors
1790        }
1791    }
1792    set cmap {}
1793    for {set i 0} {$i < [llength $clist]} {incr i} {
1794        set x [expr {double($i)/([llength $clist]-1)}]
1795        set color [lindex $clist $i]
1796        append cmap "$x [Color2RGB $color] "
1797    }
1798    return $cmap
1799}
1800
1801#
1802# BuildColormap --
1803#
1804itcl::body Rappture::VtkStreamlinesViewer::BuildColormap { name styles } {
1805    array set style $styles
1806    set cmap [ColorsToColormap $style(-color)]
1807    if { [llength $cmap] == 0 } {
1808        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
1809    }
1810    if { ![info exists _settings(volume-opacity)] } {
1811        set _settings(volume-opacity) $style(-opacity)
1812    }
1813    set max $_settings(volume-opacity)
1814
1815    set wmap "0.0 1.0 1.0 1.0"
1816    SendCmd "colormap add $name { $cmap } { $wmap }"
1817}
1818
1819# ----------------------------------------------------------------------
1820# CONFIGURATION OPTION: -plotbackground
1821# ----------------------------------------------------------------------
1822itcl::configbody Rappture::VtkStreamlinesViewer::plotbackground {
1823    if { [isconnected] } {
1824        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1825        SendCmd "screen bgcolor $r $g $b"
1826    }
1827}
1828
1829# ----------------------------------------------------------------------
1830# CONFIGURATION OPTION: -plotforeground
1831# ----------------------------------------------------------------------
1832itcl::configbody Rappture::VtkStreamlinesViewer::plotforeground {
1833    if { [isconnected] } {
1834        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1835        #fix this!
1836        #SendCmd "color background $r $g $b"
1837    }
1838}
1839
1840itcl::body Rappture::VtkStreamlinesViewer::limits { dataobj } {
1841    return
1842    array unset _limits $dataobj-*
1843    foreach comp [$dataobj components] {
1844        set tag $dataobj-$comp
1845        if { ![info exists _limits($tag)] } {
1846            set data [$dataobj blob $comp]
1847            set tmpfile file[pid].vtk
1848            set f [open "$tmpfile" "w"]
1849            fconfigure $f -translation binary -encoding binary
1850            puts $f $data
1851            close $f
1852            set reader [vtkDataSetReader $tag-xvtkDataSetReader]
1853            $reader SetFileName $tmpfile
1854            $reader ReadAllScalarsOn
1855            $reader ReadAllVectorsOn
1856            $reader ReadAllFieldsOn
1857            $reader Update
1858            set output [$reader GetOutput]
1859            set _limits($tag) [$output GetBounds]
1860            set pointData [$output GetPointData]
1861            puts stderr "\#scalars=[$reader GetNumberOfScalarsInFile]"
1862            puts stderr "\#fielddata=[$reader GetNumberOfFieldDataInFile]"
1863            puts stderr "fielddataname=[$reader GetFieldDataNameInFile 0]"
1864            set fieldData [$output GetFieldData]
1865            set pointData [$output GetPointData]
1866            puts stderr "field \#arrays=[$fieldData GetNumberOfArrays]"
1867            for { set i 0 } { $i < [$fieldData GetNumberOfArrays] } { incr i } {
1868                puts stderr [$fieldData GetArrayName $i]
1869            }
1870            puts stderr "point \#arrays=[$pointData GetNumberOfArrays]"
1871            for { set i 0 } { $i < [$pointData GetNumberOfArrays] } { incr i } {
1872                set name [$pointData GetArrayName $i]
1873                if { ![info exists _fields($name)] } {
1874                    $itk_component(field) choices insert end "$name" "$name"
1875                    set _fields($name) 1
1876                }
1877            }
1878            puts stderr "field \#components=[$fieldData GetNumberOfComponents]"
1879            puts stderr "point \#components=[$pointData GetNumberOfComponents]"
1880            puts stderr "field \#tuples=[$fieldData GetNumberOfTuples]"
1881            puts stderr "point \#tuples=[$pointData GetNumberOfTuples]"
1882            puts stderr "point \#scalars=[$pointData GetScalars]"
1883            puts stderr vectors=[$pointData GetVectors]
1884            rename $output ""
1885            rename $reader ""
1886            file delete $tmpfile
1887        }
1888        foreach { xMin xMax yMin yMax zMin zMax} $_limits($tag) break
1889        if {![info exists limits(xmin)] || $limits(xmin) > $xMin} {
1890            set limits(xmin) $xMin
1891        }
1892        if {![info exists limits(xmax)] || $limits(xmax) < $xMax} {
1893            set limits(xmax) $xMax
1894        }
1895        if {![info exists limits(ymin)] || $limits(ymin) > $yMin} {
1896            set limits(ymin) $xMin
1897        }
1898        if {![info exists limits(ymax)] || $limits(ymax) < $yMax} {
1899            set limits(ymax) $yMax
1900        }
1901        if {![info exists limits(zmin)] || $limits(zmin) > $zMin} {
1902            set limits(zmin) $zMin
1903        }
1904        if {![info exists limits(zmax)] || $limits(zmax) < $zMax} {
1905            set limits(zmax) $zMax
1906        }
1907    }
1908    return [array get limits]
1909}
1910
1911itcl::body Rappture::VtkStreamlinesViewer::BuildVolumeTab {} {
1912
1913    set fg [option get $itk_component(hull) font Font]
1914    #set bfg [option get $itk_component(hull) boldFont Font]
1915
1916    set inner [$itk_component(main) insert end \
1917        -title "Volume Settings" \
1918        -icon [Rappture::icon volume-on]]
1919    $inner configure -borderwidth 4
1920
1921    checkbutton $inner.volume \
1922        -text "Show Volume" \
1923        -variable [itcl::scope _settings(volume-visible)] \
1924        -command [itcl::code $this AdjustSetting volume-visible] \
1925        -font "Arial 9"
1926
1927    checkbutton $inner.wireframe \
1928        -text "Show Wireframe" \
1929        -variable [itcl::scope _settings(volume-wireframe)] \
1930        -command [itcl::code $this AdjustSetting volume-wireframe] \
1931        -font "Arial 9"
1932
1933    checkbutton $inner.lighting \
1934        -text "Enable Lighting" \
1935        -variable [itcl::scope _settings(volume-lighting)] \
1936        -command [itcl::code $this AdjustSetting volume-lighting] \
1937        -font "Arial 9"
1938
1939    checkbutton $inner.edges \
1940        -text "Show Edges" \
1941        -variable [itcl::scope _settings(volume-edges)] \
1942        -command [itcl::code $this AdjustSetting volume-edges] \
1943        -font "Arial 9"
1944
1945    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1946    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1947        -variable [itcl::scope _settings(volume-opacity)] \
1948        -width 10 \
1949        -showvalue off \
1950        -command [itcl::code $this AdjustSetting volume-opacity]
1951
1952    blt::table $inner \
1953        0,0 $inner.volume    -anchor w -pady 2 \
1954        1,0 $inner.wireframe -anchor w -pady 2 \
1955        2,0 $inner.lighting  -anchor w -pady 2 \
1956        3,0 $inner.edges     -anchor w -pady 2 \
1957        4,0 $inner.opacity_l -anchor w -pady 2 \
1958        5,0 $inner.opacity   -fill x   -pady 2
1959
1960    blt::table configure $inner r* c* -resize none
1961    blt::table configure $inner r6 c1 -resize expand
1962}
1963
1964
1965itcl::body Rappture::VtkStreamlinesViewer::BuildStreamsTab {} {
1966
1967    set fg [option get $itk_component(hull) font Font]
1968    #set bfg [option get $itk_component(hull) boldFont Font]
1969
1970    set inner [$itk_component(main) insert end \
1971        -title "Streams Settings" \
1972        -icon [Rappture::icon streamlines-on]]
1973    $inner configure -borderwidth 4
1974
1975    checkbutton $inner.streamlines \
1976        -text "Show Streamlines" \
1977        -variable [itcl::scope _settings(streamlines-visible)] \
1978        -command [itcl::code $this AdjustSetting streamlines-visible] \
1979        -font "Arial 9"
1980   
1981    checkbutton $inner.lighting \
1982        -text "Enable Lighting" \
1983        -variable [itcl::scope _settings(streamlines-lighting)] \
1984        -command [itcl::code $this AdjustSetting streamlines-lighting] \
1985        -font "Arial 9"
1986
1987    checkbutton $inner.seeds \
1988        -text "Show Seeds" \
1989        -variable [itcl::scope _settings(streamlines-seeds)] \
1990        -command [itcl::code $this AdjustSetting streamlines-seeds] \
1991        -font "Arial 9"
1992
1993    label $inner.mode_l -text "Mode" -font "Arial 9"
1994    itk_component add streammode {
1995        Rappture::Combobox $inner.mode -width 10 -editable no
1996    }
1997    $inner.mode choices insert end \
1998        "lines"    "lines" \
1999        "ribbons"   "ribbons" \
2000        "tubes"     "tubes"
2001    $itk_component(streammode) value "lines"
2002    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting streamlines-mode]
2003
2004    label $inner.opacity_l -text "Opacity" -font "Arial 9"
2005    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2006        -variable [itcl::scope _settings(streamlines-opacity)] \
2007        -width 10 \
2008        -showvalue off \
2009        -command [itcl::code $this AdjustSetting streamlines-opacity]
2010
2011    label $inner.density_l -text "Number of Seeds" -font "Arial 9"
2012    ::scale $inner.density -from 1 -to 1000 -orient horizontal \
2013        -variable [itcl::scope _settings(streamlines-numpoints)] \
2014        -width 10 \
2015        -showvalue on \
2016        -command [itcl::code $this AdjustSetting streamlines-numpoints]
2017
2018    label $inner.scale_l -text "Scale" -font "Arial 9"
2019    ::scale $inner.scale -from 1 -to 100 -orient horizontal \
2020        -variable [itcl::scope _settings(streamlines-scale)] \
2021        -width 10 \
2022        -showvalue off \
2023        -command [itcl::code $this AdjustSetting streamlines-scale]
2024
2025    label $inner.field_l -text "Field" -font "Arial 9"
2026    itk_component add field {
2027        Rappture::Combobox $inner.field -width 10 -editable no
2028    }
2029    bind $inner.field <<Value>> \
2030        [itcl::code $this AdjustSetting streamlines-field]
2031
2032    label $inner.palette_l -text "Palette" -font "Arial 9"
2033    itk_component add palette {
2034        Rappture::Combobox $inner.palette -width 10 -editable no
2035    }
2036    $inner.palette choices insert end \
2037        "BCGYR"              "BCGYR"            \
2038        "BGYOR"              "BGYOR"            \
2039        "blue"               "blue"             \
2040        "blue-to-brown"      "blue-to-brown"    \
2041        "blue-to-orange"     "blue-to-orange"   \
2042        "blue-to-grey"       "blue-to-grey"     \
2043        "green-to-magenta"   "green-to-magenta" \
2044        "greyscale"          "greyscale"        \
2045        "nanohub"            "nanohub"          \
2046        "rainbow"            "rainbow"          \
2047        "spectral"           "spectral"         \
2048        "ROYGB"              "ROYGB"            \
2049        "RYGCB"              "RYGCB"            \
2050        "brown-to-blue"      "brown-to-blue"    \
2051        "grey-to-blue"       "grey-to-blue"     \
2052        "orange-to-blue"     "orange-to-blue"   
2053
2054    $itk_component(palette) value "BCGYR"
2055    bind $inner.palette <<Value>> \
2056        [itcl::code $this AdjustSetting streamlines-palette]
2057
2058    blt::table $inner \
2059        0,0 $inner.streamlines -anchor w -pady 2 -cspan 2 \
2060        1,0 $inner.lighting    -anchor w -pady 2 -cspan 2 \
2061        2,0 $inner.seeds       -anchor w -pady 2 -cspan 2 \
2062        3,0 $inner.density_l   -anchor w -pady 2 -cspan 2 \
2063        4,0 $inner.density     -fill x   -pady 2 -cspan 2 \
2064        5,0 $inner.mode_l      -anchor w -pady 2  \
2065        5,1 $inner.mode        -anchor w -pady 2  \
2066        6,0 $inner.opacity_l   -anchor w -pady 2 -cspan 2 \
2067        7,0 $inner.opacity     -fill x   -pady 2 -cspan 2 \
2068        8,0 $inner.field_l     -anchor w -pady 2  \
2069        8,1 $inner.field       -anchor w -pady 2  \
2070        9,0 $inner.palette_l   -anchor w -pady 2  \
2071        9,1 $inner.palette     -anchor w -pady 2  \
2072       
2073
2074    blt::table configure $inner r* c* -resize none
2075    blt::table configure $inner r10 c1 c2 -resize expand
2076}
2077
2078itcl::body Rappture::VtkStreamlinesViewer::BuildAxisTab {} {
2079
2080    set fg [option get $itk_component(hull) font Font]
2081    #set bfg [option get $itk_component(hull) boldFont Font]
2082
2083    set inner [$itk_component(main) insert end \
2084        -title "Axis Settings" \
2085        -icon [Rappture::icon axis1]]
2086    $inner configure -borderwidth 4
2087
2088    checkbutton $inner.visible \
2089        -text "Show Axes" \
2090        -variable [itcl::scope _settings(axis-visible)] \
2091        -command [itcl::code $this AdjustSetting axis-visible] \
2092        -font "Arial 9"
2093
2094    checkbutton $inner.labels \
2095        -text "Show Axis Labels" \
2096        -variable [itcl::scope _settings(axis-labels)] \
2097        -command [itcl::code $this AdjustSetting axis-labels] \
2098        -font "Arial 9"
2099
2100    checkbutton $inner.gridx \
2101        -text "Show X Grid" \
2102        -variable [itcl::scope _settings(axis-xgrid)] \
2103        -command [itcl::code $this AdjustSetting axis-xgrid] \
2104        -font "Arial 9"
2105    checkbutton $inner.gridy \
2106        -text "Show Y Grid" \
2107        -variable [itcl::scope _settings(axis-ygrid)] \
2108        -command [itcl::code $this AdjustSetting axis-ygrid] \
2109        -font "Arial 9"
2110    checkbutton $inner.gridz \
2111        -text "Show Z Grid" \
2112        -variable [itcl::scope _settings(axis-zgrid)] \
2113        -command [itcl::code $this AdjustSetting axis-zgrid] \
2114        -font "Arial 9"
2115
2116    label $inner.mode_l -text "Mode" -font "Arial 9"
2117
2118    itk_component add axismode {
2119        Rappture::Combobox $inner.mode -width 10 -editable no
2120    }
2121    $inner.mode choices insert end \
2122        "static_triad"    "static" \
2123        "closest_triad"   "closest" \
2124        "furthest_triad"  "furthest" \
2125        "outer_edges"     "outer"         
2126    $itk_component(axismode) value "static"
2127    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting axis-mode]
2128
2129    blt::table $inner \
2130        0,0 $inner.visible -anchor w -cspan 2 \
2131        1,0 $inner.labels  -anchor w -cspan 2 \
2132        2,0 $inner.gridx   -anchor w -cspan 2 \
2133        3,0 $inner.gridy   -anchor w -cspan 2 \
2134        4,0 $inner.gridz   -anchor w -cspan 2 \
2135        5,0 $inner.mode_l  -anchor w -cspan 2 -padx { 2 0 } \
2136        6,0 $inner.mode    -fill x   -cspan 2
2137
2138    blt::table configure $inner r* c* -resize none
2139    blt::table configure $inner r7 c1 -resize expand
2140}
2141
2142
2143itcl::body Rappture::VtkStreamlinesViewer::BuildCameraTab {} {
2144    set inner [$itk_component(main) insert end \
2145        -title "Camera Settings" \
2146        -icon [Rappture::icon camera]]
2147    $inner configure -borderwidth 4
2148
2149    set labels { qx qy qz qw xpan ypan zoom }
2150    set row 0
2151    foreach tag $labels {
2152        label $inner.${tag}label -text $tag -font "Arial 9"
2153        entry $inner.${tag} -font "Arial 9"  -bg white \
2154            -textvariable [itcl::scope _view($tag)]
2155        bind $inner.${tag} <KeyPress-Return> \
2156            [itcl::code $this camera set ${tag}]
2157        blt::table $inner \
2158            $row,0 $inner.${tag}label -anchor e -pady 2 \
2159            $row,1 $inner.${tag} -anchor w -pady 2
2160        blt::table configure $inner r$row -resize none
2161        incr row
2162    }
2163    checkbutton $inner.ortho \
2164        -text "Orthographic Projection" \
2165        -variable [itcl::scope _view(ortho)] \
2166        -command [itcl::code $this camera set ortho] \
2167        -font "Arial 9"
2168    blt::table $inner \
2169            $row,0 $inner.ortho -columnspan 2 -anchor w -pady 2
2170    blt::table configure $inner r$row -resize none
2171    incr row
2172
2173    blt::table configure $inner c0 c1 -resize none
2174    blt::table configure $inner c2 -resize expand
2175    blt::table configure $inner r$row -resize expand
2176}
2177
2178itcl::body Rappture::VtkStreamlinesViewer::BuildCutplaneTab {} {
2179
2180    set fg [option get $itk_component(hull) font Font]
2181   
2182    set inner [$itk_component(main) insert end \
2183        -title "Cutplane Settings" \
2184        -icon [Rappture::icon cutbutton]]
2185
2186    $inner configure -borderwidth 4
2187
2188    checkbutton $inner.visible \
2189        -text "Show Cutplanes" \
2190        -variable [itcl::scope _settings(cutplane-visible)] \
2191        -command [itcl::code $this AdjustSetting cutplane-visible] \
2192        -font "Arial 9"
2193
2194    checkbutton $inner.wireframe \
2195        -text "Show Wireframe" \
2196        -variable [itcl::scope _settings(cutplane-wireframe)] \
2197        -command [itcl::code $this AdjustSetting cutplane-wireframe] \
2198        -font "Arial 9"
2199
2200    checkbutton $inner.lighting \
2201        -text "Enable Lighting" \
2202        -variable [itcl::scope _settings(cutplane-lighting)] \
2203        -command [itcl::code $this AdjustSetting cutplane-lighting] \
2204        -font "Arial 9"
2205
2206    checkbutton $inner.edges \
2207        -text "Show Edges" \
2208        -variable [itcl::scope _settings(cutplane-edges)] \
2209        -command [itcl::code $this AdjustSetting cutplane-edges] \
2210        -font "Arial 9"
2211
2212    label $inner.opacity_l -text "Opacity" -font "Arial 9"
2213    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
2214        -variable [itcl::scope _settings(cutplane-opacity)] \
2215        -width 10 \
2216        -showvalue off \
2217        -command [itcl::code $this AdjustSetting cutplane-opacity]
2218    $inner.opacity set $_settings(cutplane-opacity)
2219
2220    # X-value slicer...
2221    itk_component add xCutButton {
2222        Rappture::PushButton $inner.xbutton \
2223            -onimage [Rappture::icon x-cutplane] \
2224            -offimage [Rappture::icon x-cutplane] \
2225            -command [itcl::code $this AdjustSetting cutplane-xvisible] \
2226            -variable [itcl::scope _settings(cutplane-xvisible)]
2227    }
2228    Rappture::Tooltip::for $itk_component(xCutButton) \
2229        "Toggle the X-axis cutplane on/off"
2230
2231    itk_component add xCutScale {
2232        ::scale $inner.xval -from 100 -to 1 \
2233            -width 10 -orient vertical -showvalue yes \
2234            -borderwidth 1 -highlightthickness 0 \
2235            -command [itcl::code $this EventuallySetCutplane x] \
2236            -variable [itcl::scope _settings(cutplane-xposition)]
2237    } {
2238        usual
2239        ignore -borderwidth -highlightthickness
2240    }
2241    # Set the default cutplane value before disabling the scale.
2242    $itk_component(xCutScale) set 50
2243    $itk_component(xCutScale) configure -state disabled
2244    Rappture::Tooltip::for $itk_component(xCutScale) \
2245        "@[itcl::code $this Slice tooltip x]"
2246
2247    # Y-value slicer...
2248    itk_component add yCutButton {
2249        Rappture::PushButton $inner.ybutton \
2250            -onimage [Rappture::icon y-cutplane] \
2251            -offimage [Rappture::icon y-cutplane] \
2252            -command [itcl::code $this AdjustSetting cutplane-yvisible] \
2253            -variable [itcl::scope _settings(cutplane-yvisible)]
2254    }
2255    Rappture::Tooltip::for $itk_component(yCutButton) \
2256        "Toggle the Y-axis cutplane on/off"
2257
2258    itk_component add yCutScale {
2259        ::scale $inner.yval -from 100 -to 1 \
2260            -width 10 -orient vertical -showvalue yes \
2261            -borderwidth 1 -highlightthickness 0 \
2262            -command [itcl::code $this EventuallySetCutplane y] \
2263            -variable [itcl::scope _settings(cutplane-yposition)]
2264    } {
2265        usual
2266        ignore -borderwidth -highlightthickness
2267    }
2268    Rappture::Tooltip::for $itk_component(yCutScale) \
2269        "@[itcl::code $this Slice tooltip y]"
2270    # Set the default cutplane value before disabling the scale.
2271    $itk_component(yCutScale) set 50
2272    $itk_component(yCutScale) configure -state disabled
2273
2274    # Z-value slicer...
2275    itk_component add zCutButton {
2276        Rappture::PushButton $inner.zbutton \
2277            -onimage [Rappture::icon z-cutplane] \
2278            -offimage [Rappture::icon z-cutplane] \
2279            -command [itcl::code $this AdjustSetting cutplane-zvisible] \
2280            -variable [itcl::scope _settings(cutplane-zvisible)]
2281    }
2282    Rappture::Tooltip::for $itk_component(zCutButton) \
2283        "Toggle the Z-axis cutplane on/off"
2284
2285    itk_component add zCutScale {
2286        ::scale $inner.zval -from 100 -to 1 \
2287            -width 10 -orient vertical -showvalue yes \
2288            -borderwidth 1 -highlightthickness 0 \
2289            -command [itcl::code $this EventuallySetCutplane z] \
2290            -variable [itcl::scope _settings(cutplane-zposition)]
2291    } {
2292        usual
2293        ignore -borderwidth -highlightthickness
2294    }
2295    $itk_component(zCutScale) set 50
2296    $itk_component(zCutScale) configure -state disabled
2297    #$itk_component(zCutScale) configure -state disabled
2298    Rappture::Tooltip::for $itk_component(zCutScale) \
2299        "@[itcl::code $this Slice tooltip z]"
2300
2301    blt::table $inner \
2302        0,0 $inner.visible              -anchor w -pady 2 -cspan 4 \
2303        1,0 $inner.lighting             -anchor w -pady 2 -cspan 4 \
2304        2,0 $inner.wireframe            -anchor w -pady 2 -cspan 4 \
2305        3,0 $inner.edges                -anchor w -pady 2 -cspan 4 \
2306        4,0 $inner.opacity_l            -anchor w -pady 2 -cspan 3 \
2307        5,0 $inner.opacity              -fill x   -pady 2 -cspan 3 \
2308        6,0 $itk_component(xCutButton)  -anchor e -padx 2 -pady 2 \
2309        7,0 $itk_component(xCutScale)   -fill y \
2310        6,1 $itk_component(yCutButton)  -anchor e -padx 2 -pady 2 \
2311        7,1 $itk_component(yCutScale)   -fill y \
2312        6,2 $itk_component(zCutButton)  -anchor e -padx 2 -pady 2 \
2313        7,2 $itk_component(zCutScale)   -fill y \
2314
2315    blt::table configure $inner r* c* -resize none
2316    blt::table configure $inner r7 c3 -resize expand
2317}
2318
2319
2320
2321#
2322#  camera --
2323#
2324itcl::body Rappture::VtkStreamlinesViewer::camera {option args} {
2325    switch -- $option {
2326        "show" {
2327            puts [array get _view]
2328        }
2329        "set" {
2330            set who [lindex $args 0]
2331            set x $_view($who)
2332            set code [catch { string is double $x } result]
2333            if { $code != 0 || !$result } {
2334                return
2335            }
2336            switch -- $who {
2337                "ortho" {
2338                    if {$_view(ortho)} {
2339                        SendCmd "camera mode ortho"
2340                    } else {
2341                        SendCmd "camera mode persp"
2342                    }
2343                }
2344                "xpan" - "ypan" {
2345                    PanCamera
2346                }
2347                "qx" - "qy" - "qz" - "qw" {
2348                    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
2349                    $_arcball quaternion $q
2350                    EventuallyRotate $q
2351                }
2352                "zoom" {
2353                    SendCmd "camera zoom $_view(zoom)"
2354                }
2355            }
2356        }
2357    }
2358}
2359
2360itcl::body Rappture::VtkStreamlinesViewer::ConvertToVtkData { dataobj comp } {
2361    foreach { x1 x2 xN y1 y2 yN } [$dataobj mesh $comp] break
2362    set values [$dataobj values $comp]
2363    append out "# vtk DataFile Version 2.0 \n"
2364    append out "Test data \n"
2365    append out "ASCII \n"
2366    append out "DATASET STRUCTURED_POINTS \n"
2367    append out "DIMENSIONS $xN $yN 1 \n"
2368    append out "ORIGIN 0 0 0 \n"
2369    append out "SPACING 1 1 1 \n"
2370    append out "POINT_DATA [expr $xN * $yN] \n"
2371    append out "SCALARS field float 1 \n"
2372    append out "LOOKUP_TABLE default \n"
2373    append out [join $values "\n"]
2374    append out "\n"
2375    return $out
2376}
2377
2378
2379itcl::body Rappture::VtkStreamlinesViewer::GetVtkData { args } {
2380    set bytes ""
2381    foreach dataobj [get] {
2382        foreach comp [$dataobj components] {
2383            set tag $dataobj-$comp
2384            #set contents [ConvertToVtkData $dataobj $comp]
2385            set contents [$dataobj blob $comp]
2386            append bytes "$contents\n\n"
2387        }
2388    }
2389    return [list .vtk $bytes]
2390}
2391
2392itcl::body Rappture::VtkStreamlinesViewer::GetImage { args } {
2393    if { [image width $_image(download)] > 0 &&
2394         [image height $_image(download)] > 0 } {
2395        set bytes [$_image(download) data -format "jpeg -quality 100"]
2396        set bytes [Rappture::encoding::decode -as b64 $bytes]
2397        return [list .jpg $bytes]
2398    }
2399    return ""
2400}
2401
2402itcl::body Rappture::VtkStreamlinesViewer::BuildDownloadPopup { popup command } {
2403    Rappture::Balloon $popup \
2404        -title "[Rappture::filexfer::label downloadWord] as..."
2405    set inner [$popup component inner]
2406    label $inner.summary -text "" -anchor w
2407    radiobutton $inner.vtk_button -text "VTK data file" \
2408        -variable [itcl::scope _downloadPopup(format)] \
2409        -font "Helvetica 9 " \
2410        -value vtk 
2411    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2412    radiobutton $inner.image_button -text "Image File" \
2413        -variable [itcl::scope _downloadPopup(format)] \
2414        -value image
2415    Rappture::Tooltip::for $inner.image_button \
2416        "Save as digital image."
2417
2418    button $inner.ok -text "Save" \
2419        -highlightthickness 0 -pady 2 -padx 3 \
2420        -command $command \
2421        -compound left \
2422        -image [Rappture::icon download]
2423
2424    button $inner.cancel -text "Cancel" \
2425        -highlightthickness 0 -pady 2 -padx 3 \
2426        -command [list $popup deactivate] \
2427        -compound left \
2428        -image [Rappture::icon cancel]
2429
2430    blt::table $inner \
2431        0,0 $inner.summary -cspan 2  \
2432        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2433        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2434        4,1 $inner.cancel -width .9i -fill y \
2435        4,0 $inner.ok -padx 2 -width .9i -fill y
2436    blt::table configure $inner r3 -height 4
2437    blt::table configure $inner r4 -pady 4
2438    raise $inner.image_button
2439    $inner.vtk_button invoke
2440    return $inner
2441}
2442
2443itcl::body Rappture::VtkStreamlinesViewer::SetObjectStyle { dataobj comp } {
2444    # Parse style string.
2445    set tag $dataobj-$comp
2446    set style [$dataobj style $comp]
2447    array set settings {
2448        -color \#808080
2449        -edges 0
2450        -edgecolor black
2451        -linewidth 1.0
2452        -opacity 0.4
2453        -wireframe 0
2454        -lighting 1
2455        -seeds 1
2456        -seedcolor white
2457        -visible 1
2458    }
2459    if { $dataobj != $_first } {
2460        set settings(-opacity) 1
2461    }
2462    array set settings $style
2463    SendCmd "streamlines add $tag"
2464    SendCmd "streamlines seed visible off $tag"
2465    set seeds [$dataobj hints seeds]
2466    if { $seeds != "" && ![info exists _seeds($dataobj)] } {
2467        set length [string length $seeds]
2468        SendCmd "streamlines seed fmesh 200 data follows $length $tag"
2469        SendCmd "$seeds"
2470        set _seeds($dataobj) 1
2471    }
2472    SendCmd "cutplane add $tag"
2473    SendCmd "cutplane edges 0 $tag"
2474    SendCmd "cutplane wireframe 0 $tag"
2475    SendCmd "cutplane lighting 1 $tag"
2476    SendCmd "cutplane linewidth 1 $tag"
2477    #SendCmd "cutplane linecolor 1 1 1 $tag"
2478    #SendCmd "cutplane visible $tag"
2479    foreach axis { x y z } {
2480        SendCmd "cutplane slice $axis 1.0 $tag"
2481        SendCmd "cutplane axis $axis 0 $tag"
2482    }
2483
2484    SendCmd "polydata add $tag"
2485    SendCmd "polydata edges $settings(-edges) $tag"
2486    set _settings(volume-edges) $settings(-edges)
2487    SendCmd "polydata color [Color2RGB $settings(-color)] $tag"
2488    SendCmd "polydata lighting $settings(-lighting) $tag"
2489    set _settings(volume-lighting) $settings(-lighting)
2490    SendCmd "polydata linecolor [Color2RGB $settings(-edgecolor)] $tag"
2491    SendCmd "polydata linewidth $settings(-linewidth) $tag"
2492    SendCmd "polydata opacity $settings(-opacity) $tag"
2493    set _settings(volume-opacity) $settings(-opacity)
2494    SendCmd "polydata wireframe $settings(-wireframe) $tag"
2495    set _settings(volume-wireframe) $settings(-wireframe)
2496    set _settings(volume-opacity) [expr $settings(-opacity) * 100.0]
2497    SetColormap $dataobj $comp
2498}
2499
2500itcl::body Rappture::VtkStreamlinesViewer::IsValidObject { dataobj } {
2501    if {[catch {$dataobj isa Rappture::Field} valid] != 0 || !$valid} {
2502        return 0
2503    }
2504    return 1
2505}
2506
2507# ----------------------------------------------------------------------
2508# USAGE: ReceiveLegend <colormap> <title> <vmin> <vmax> <size>
2509#
2510# Invoked automatically whenever the "legend" command comes in from
2511# the rendering server.  Indicates that binary image data with the
2512# specified <size> will follow.
2513# ----------------------------------------------------------------------
2514itcl::body Rappture::VtkStreamlinesViewer::ReceiveLegend { colormap title vmin vmax size } {
2515    #puts stderr "ReceiveLegend colormap=$colormap title=$title range=$vmin,$vmax size=$size"
2516    set _limits(vmin) $vmin
2517    set _limits(vmax) $vmax
2518    set _title $title
2519    regsub {\(mag\)} $title "" _title
2520    if { [IsConnected] } {
2521        set bytes [ReceiveBytes $size]
2522        if { ![info exists _image(legend)] } {
2523            set _image(legend) [image create photo]
2524        }
2525        $_image(legend) configure -data $bytes
2526        #puts stderr "read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
2527        DrawLegend $_title
2528    }
2529}
2530
2531#
2532# DrawLegend --
2533#
2534#       Draws the legend in it's own canvas which resides to the right
2535#       of the contour plot area.
2536#
2537itcl::body Rappture::VtkStreamlinesViewer::DrawLegend { name } {
2538    set c $itk_component(view)
2539    set w [winfo width $c]
2540    set h [winfo height $c]
2541    set font "Arial 8"
2542    set lineht [font metrics $font -linespace]
2543   
2544    if { [info exists _fields($name)] } {
2545        foreach { title units } $_fields($name) break
2546        if { $units != "" } {
2547            set title [format "%s (%s)" $title $units]
2548        }
2549    } else {
2550        set title $name
2551    }
2552    if { $_settings(legend-visible) } {
2553        set x [expr $w - 2]
2554        if { [$c find withtag "legend"] == "" } {
2555            set y 2
2556            $c create text $x $y \
2557                -anchor ne \
2558                -fill $itk_option(-plotforeground) -tags "title legend" \
2559                -font $font
2560            incr y $lineht
2561            $c create text $x $y \
2562                -anchor ne \
2563                -fill $itk_option(-plotforeground) -tags "vmax legend" \
2564                -font $font
2565            incr y $lineht
2566            $c create image $x $y \
2567                -anchor ne \
2568                -image $_image(legend) -tags "colormap legend"
2569            $c create text $x [expr {$h-2}] \
2570                -anchor se \
2571                -fill $itk_option(-plotforeground) -tags "vmin legend" \
2572                -font $font
2573            #$c bind colormap <Enter> [itcl::code $this EnterLegend %x %y]
2574            $c bind colormap <Leave> [itcl::code $this LeaveLegend]
2575            $c bind colormap <Motion> [itcl::code $this MotionLegend %x %y]
2576        }
2577        # Reset the item coordinates according the current size of the plot.
2578        $c itemconfigure title -text $title
2579        if { $_limits(vmin) != "" } {
2580            $c itemconfigure vmin -text [format %g $_limits(vmin)]
2581        }
2582        if { $_limits(vmax) != "" } {
2583            $c itemconfigure vmax -text [format %g $_limits(vmax)]
2584        }
2585        set y 2
2586        $c coords title $x $y
2587        incr y $lineht
2588        $c coords vmax $x $y
2589        incr y $lineht
2590        $c coords colormap $x $y
2591        $c coords vmin $x [expr {$h - 2}]
2592    }
2593}
2594
2595#
2596# EnterLegend --
2597#
2598itcl::body Rappture::VtkStreamlinesViewer::EnterLegend { x y } {
2599    SetLegendTip $x $y
2600}
2601
2602#
2603# MotionLegend --
2604#
2605itcl::body Rappture::VtkStreamlinesViewer::MotionLegend { x y } {
2606    Rappture::Tooltip::tooltip cancel
2607    set c $itk_component(view)
2608    SetLegendTip $x $y
2609}
2610
2611#
2612# LeaveLegend --
2613#
2614itcl::body Rappture::VtkStreamlinesViewer::LeaveLegend { } {
2615    Rappture::Tooltip::tooltip cancel
2616    .rappturetooltip configure -icon ""
2617}
2618
2619#
2620# SetLegendTip --
2621#
2622itcl::body Rappture::VtkStreamlinesViewer::SetLegendTip { x y } {
2623    set c $itk_component(view)
2624    set w [winfo width $c]
2625    set h [winfo height $c]
2626    set font "Arial 8"
2627    set lineht [font metrics $font -linespace]
2628   
2629    set imgHeight [image height $_image(legend)]
2630    set coords [$c coords colormap]
2631    set imgX [expr $w - [image width $_image(legend)] - 2]
2632    set imgY [expr $y - 2 * ($lineht + 2)]
2633
2634    if { [info exists _fields($_title)] } {
2635        foreach { title units } $_fields($_title) break
2636        if { $units != "" } {
2637            set title [format "%s (%s)" $title $units]
2638        }
2639    } else {
2640        set title $_title
2641    }
2642    # Make a swatch of the selected color
2643    if { [catch { $_image(legend) get 10 $imgY } pixel] != 0 } {
2644        #puts stderr "out of range: $imgY"
2645        return
2646    }
2647    if { ![info exists _image(swatch)] } {
2648        set _image(swatch) [image create photo -width 24 -height 24]
2649    }
2650    set color [eval format "\#%02x%02x%02x" $pixel]
2651    $_image(swatch) put black  -to 0 0 23 23
2652    $_image(swatch) put $color -to 1 1 22 22
2653    .rappturetooltip configure -icon $_image(swatch)
2654
2655    # Compute the value of the point
2656    if { [info exists _limits(vmax)] && [info exists _limits(vmin)] } {
2657        set t [expr 1.0 - (double($imgY) / double($imgHeight-1))]
2658        set value [expr $t * ($_limits(vmax) - $_limits(vmin)) + $_limits(vmin)]
2659    } else {
2660        set value 0.0
2661    }
2662    set tipx [expr $x + 15]
2663    set tipy [expr $y - 5]
2664    Rappture::Tooltip::text $c "$title $value"
2665    Rappture::Tooltip::tooltip show $c +$tipx,+$tipy   
2666}
2667
2668
2669# ----------------------------------------------------------------------
2670# USAGE: Slice move x|y|z <newval>
2671#
2672# Called automatically when the user drags the slider to move the
2673# cut plane that slices 3D data.  Gets the current value from the
2674# slider and moves the cut plane to the appropriate point in the
2675# data set.
2676# ----------------------------------------------------------------------
2677itcl::body Rappture::VtkStreamlinesViewer::Slice {option args} {
2678    switch -- $option {
2679        "move" {
2680            set axis [lindex $args 0]
2681            set oldval $_settings(axis-${axis}position)
2682            set newval [lindex $args 1]
2683            if {[llength $args] != 2} {
2684                error "wrong # args: should be \"Slice move x|y|z newval\""
2685            }
2686            set newpos [expr {0.01*$newval}]
2687            SendCmd "cutplane slice $axis $newpos"
2688        }
2689        "tooltip" {
2690            set axis [lindex $args 0]
2691            set val [$itk_component(${axis}CutScale) get]
2692            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
2693        }
2694        default {
2695            error "bad option \"$option\": should be axis, move, or tooltip"
2696        }
2697    }
2698}
Note: See TracBrowser for help on using the repository browser.