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

Last change on this file since 3800 was 3800, checked in by gah, 11 years ago

add -simulation to plotadd calls

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