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

Last change on this file since 5347 was 5347, checked in by ldelgass, 5 years ago

set _downloadPopup to private access

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