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

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

merge streamlines viewer from trunk

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