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

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

Merge fix from r4454 from trunk

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