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

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

fix drawing default coordinates, rewrite of xyresult scaling, sync with trunk

File size: 83.7 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    foreach {opt val} $settings {
564        if {![info exists params($opt)]} {
565            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
566        }
567        set params($opt) $val
568    }
569    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
570        # can't handle -autocolors yet
571        set params(-color) black
572    }
573    set pos [lsearch -exact $dataobj $_dlist]
574    if {$pos < 0} {
575        lappend _dlist $dataobj
576    }
577    set _obj2ovride($dataobj-color) $params(-color)
578    set _obj2ovride($dataobj-width) $params(-width)
579    set _obj2ovride($dataobj-raise) $params(-raise)
580    $_dispatcher event -idle !rebuild
581}
582
583
584# ----------------------------------------------------------------------
585# USAGE: delete ?<dataobj1> <dataobj2> ...?
586#
587#       Clients use this to delete a dataobj from the plot.  If no dataobjs
588#       are specified, then all dataobjs are deleted.  No data objects are
589#       deleted.  They are only removed from the display list.
590#
591# ----------------------------------------------------------------------
592itcl::body Rappture::VtkStreamlinesViewer::delete {args} {
593    if { [llength $args] == 0} {
594        set args $_dlist
595    }
596    # Delete all specified dataobjs
597    set changed 0
598    foreach dataobj $args {
599        set pos [lsearch -exact $_dlist $dataobj]
600        if { $pos < 0 } {
601            continue;                   # Don't know anything about it.
602        }
603        # Remove it from the dataobj list.
604        set _dlist [lreplace $_dlist $pos $pos]
605        array unset _obj2ovride $dataobj-*
606        array unset _settings $dataobj-*
607        # Append to the end of the dataobj list.
608        lappend _dlist $dataobj
609        set changed 1
610    }
611    # If anything changed, then rebuild the plot
612    if { $changed } {
613        $_dispatcher event -idle !rebuild
614    }
615}
616
617# ----------------------------------------------------------------------
618# USAGE: get ?-objects?
619# USAGE: get ?-visible?
620# USAGE: get ?-image view?
621#
622# Clients use this to query the list of objects being plotted, in
623# order from bottom to top of this result.  The optional "-image"
624# flag can also request the internal images being shown.
625# ----------------------------------------------------------------------
626itcl::body Rappture::VtkStreamlinesViewer::get {args} {
627    if {[llength $args] == 0} {
628        set args "-objects"
629    }
630
631    set op [lindex $args 0]
632    switch -- $op {
633        "-objects" {
634            # put the dataobj list in order according to -raise options
635            set dlist {}
636            foreach dataobj $_dlist {
637                if { ![IsValidObject $dataobj] } {
638                    continue
639                }
640                if {[info exists _obj2ovride($dataobj-raise)] &&
641                    $_obj2ovride($dataobj-raise)} {
642                    set dlist [linsert $dlist 0 $dataobj]
643                } else {
644                    lappend dlist $dataobj
645                }
646            }
647            return $dlist
648        }
649        "-visible" {
650            set dlist {}
651            foreach dataobj $_dlist {
652                if { ![IsValidObject $dataobj] } {
653                    continue
654                }
655                if { ![info exists _obj2ovride($dataobj-raise)] } {
656                    # No setting indicates that the object isn't visible.
657                    continue
658                }
659                # Otherwise use the -raise parameter to put the object to
660                # the front of the list.
661                if { $_obj2ovride($dataobj-raise) } {
662                    set dlist [linsert $dlist 0 $dataobj]
663                } else {
664                    lappend dlist $dataobj
665                }
666            }
667            return $dlist
668        }           
669        -image {
670            if {[llength $args] != 2} {
671                error "wrong # args: should be \"get -image view\""
672            }
673            switch -- [lindex $args end] {
674                view {
675                    return $_image(plot)
676                }
677                default {
678                    error "bad image name \"[lindex $args end]\": should be view"
679                }
680            }
681        }
682        default {
683            error "bad option \"$op\": should be -objects or -image"
684        }
685    }
686}
687
688# ----------------------------------------------------------------------
689# USAGE: scale ?<data1> <data2> ...?
690#
691# Sets the default limits for the overall plot according to the
692# limits of the data for all of the given <data> objects.  This
693# accounts for all objects--even those not showing on the screen.
694# Because of this, the limits are appropriate for all objects as
695# the user scans through data in the ResultSet viewer.
696# ----------------------------------------------------------------------
697itcl::body Rappture::VtkStreamlinesViewer::scale {args} {
698    foreach dataobj $args {
699        foreach axis { x y z } {
700            set lim [$dataobj limits $axis]
701            if { ![info exists _limits($axis)] } {
702                set _limits($axis) $lim
703                continue
704            }
705            foreach {min max} $lim break
706            foreach {amin amax} $_limits($axis) break
707            if { $amin > $min } {
708                set amin $min
709            }
710            if { $amax < $max } {
711                set amax $max
712            }
713            set _limits($axis) [list $amin $amax]
714        }
715        foreach { fname lim } [$dataobj fieldlimits] {
716            if { ![info exists _limits($fname)] } {
717                set _limits($fname) $lim
718                continue
719            }
720            foreach {min max} $lim break
721            foreach {fmin fmax} $_limits($fname) break
722            if { $fmin > $min } {
723                set fmin $min
724            }
725            if { $fmax < $max } {
726                set fmax $max
727            }
728            set _limits($fname) [list $fmin $fmax]
729        }
730    }
731}
732
733# ----------------------------------------------------------------------
734# USAGE: download coming
735# USAGE: download controls <downloadCommand>
736# USAGE: download now
737#
738# Clients use this method to create a downloadable representation
739# of the plot.  Returns a list of the form {ext string}, where
740# "ext" is the file extension (indicating the type of data) and
741# "string" is the data itself.
742# ----------------------------------------------------------------------
743itcl::body Rappture::VtkStreamlinesViewer::download {option args} {
744    switch $option {
745        coming {
746            if {[catch {
747                blt::winop snap $itk_component(plotarea) $_image(download)
748            }]} {
749                $_image(download) configure -width 1 -height 1
750                $_image(download) put #000000
751            }
752        }
753        controls {
754            set popup .vtkviewerdownload
755            if { ![winfo exists .vtkviewerdownload] } {
756                set inner [BuildDownloadPopup $popup [lindex $args 0]]
757            } else {
758                set inner [$popup component inner]
759            }
760            set _downloadPopup(image_controls) $inner.image_frame
761            set num [llength [get]]
762            set num [expr {($num == 1) ? "1 result" : "$num results"}]
763            set word [Rappture::filexfer::label downloadWord]
764            $inner.summary configure -text "$word $num in the following format:"
765            update idletasks            ;# Fix initial sizes
766            return $popup
767        }
768        now {
769            set popup .vtkviewerdownload
770            if {[winfo exists .vtkviewerdownload]} {
771                $popup deactivate
772            }
773            switch -- $_downloadPopup(format) {
774                "image" {
775                    return [$this GetImage [lindex $args 0]]
776                }
777                "vtk" {
778                    return [$this GetVtkData [lindex $args 0]]
779                }
780            }
781            return ""
782        }
783        default {
784            error "bad option \"$option\": should be coming, controls, now"
785        }
786    }
787}
788
789# ----------------------------------------------------------------------
790# USAGE: Connect ?<host:port>,<host:port>...?
791#
792# Clients use this method to establish a connection to a new
793# server, or to reestablish a connection to the previous server.
794# Any existing connection is automatically closed.
795# ----------------------------------------------------------------------
796itcl::body Rappture::VtkStreamlinesViewer::Connect {} {
797    set _hosts [GetServerList "vtkvis"]
798    if { "" == $_hosts } {
799        return 0
800    }
801    set result [VisViewer::Connect $_hosts]
802    if { $result } {
803        if { $_reportClientInfo }  {
804            # Tell the server the viewer, hub, user and session.
805            # Do this immediately on connect before buffing any commands
806            global env
807
808            set info {}
809            set user "???"
810            if { [info exists env(USER)] } {
811                set user $env(USER)
812            }
813            set session "???"
814            if { [info exists env(SESSION)] } {
815                set session $env(SESSION)
816            }
817            lappend info "hub" [exec hostname]
818            lappend info "client" "vtkstreamlinesviewer"
819            lappend info "user" $user
820            lappend info "session" $session
821            SendCmd "clientinfo [list $info]"
822        }
823
824        set w [winfo width $itk_component(view)]
825        set h [winfo height $itk_component(view)]
826        EventuallyResize $w $h
827    }
828    return $result
829}
830
831#
832# isconnected --
833#
834#       Indicates if we are currently connected to the visualization server.
835#
836itcl::body Rappture::VtkStreamlinesViewer::isconnected {} {
837    return [VisViewer::IsConnected]
838}
839
840#
841# disconnect --
842#
843itcl::body Rappture::VtkStreamlinesViewer::disconnect {} {
844    Disconnect
845    set _reset 1
846}
847
848#
849# Disconnect --
850#
851#       Clients use this method to disconnect from the current rendering
852#       server.
853#
854itcl::body Rappture::VtkStreamlinesViewer::Disconnect {} {
855    VisViewer::Disconnect
856
857    $_dispatcher cancel !rebuild
858    $_dispatcher cancel !resize
859    $_dispatcher cancel !reseed
860    $_dispatcher cancel !rotate
861    $_dispatcher cancel !xcutplane
862    $_dispatcher cancel !ycutplane
863    $_dispatcher cancel !zcutplane
864    $_dispatcher cancel !legend
865    # disconnected -- no more data sitting on server
866    array unset _datasets
867    array unset _data
868    array unset _colormaps
869    array unset _seeds
870    array unset _dataset2style
871    array unset _obj2datasets
872}
873
874# ----------------------------------------------------------------------
875# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
876#
877# Invoked automatically whenever the "image" command comes in from
878# the rendering server.  Indicates that binary image data with the
879# specified <size> will follow.
880# ----------------------------------------------------------------------
881itcl::body Rappture::VtkStreamlinesViewer::ReceiveImage { args } {
882    array set info {
883        -token "???"
884        -bytes 0
885        -type image
886    }
887    array set info $args
888    set bytes [ReceiveBytes $info(-bytes)]
889    if { $info(-type) == "image" } {
890        if 0 {
891            set f [open "last.ppm" "w"]
892            puts $f $bytes
893            close $f
894        }
895        $_image(plot) configure -data $bytes
896        set time [clock seconds]
897        set date [clock format $time]
898        #puts stderr "$date: received image [image width $_image(plot)]x[image height $_image(plot)] image>"       
899        if { $_start > 0 } {
900            set finish [clock clicks -milliseconds]
901            #puts stderr "round trip time [expr $finish -$_start] milliseconds"
902            set _start 0
903        }
904    } elseif { $info(type) == "print" } {
905        set tag $this-print-$info(-token)
906        set _hardcopy($tag) $bytes
907    }
908    if { $_legendPending } {
909        RequestLegend
910    }
911}
912
913#
914# ReceiveDataset --
915#
916itcl::body Rappture::VtkStreamlinesViewer::ReceiveDataset { args } {
917    if { ![isconnected] } {
918        return
919    }
920    set option [lindex $args 0]
921    switch -- $option {
922        "scalar" {
923            set option [lindex $args 1]
924            switch -- $option {
925                "world" {
926                    foreach { x y z value tag } [lrange $args 2 end] break
927                }
928                "pixel" {
929                    foreach { x y value tag } [lrange $args 2 end] break
930                }
931            }
932        }
933        "vector" {
934            set option [lindex $args 1]
935            switch -- $option {
936                "world" {
937                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
938                }
939                "pixel" {
940                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
941                }
942            }
943        }
944        "names" {
945            foreach { name } [lindex $args 1] {
946                #puts stderr "Dataset: $name"
947            }
948        }
949        default {
950            error "unknown dataset option \"$option\" from server"
951        }
952    }
953}
954
955# ----------------------------------------------------------------------
956# USAGE: Rebuild
957#
958# Called automatically whenever something changes that affects the
959# data in the widget.  Clears any existing data and rebuilds the
960# widget to display new data.
961# ----------------------------------------------------------------------
962itcl::body Rappture::VtkStreamlinesViewer::Rebuild {} {
963    update
964    set w [winfo width $itk_component(view)]
965    set h [winfo height $itk_component(view)]
966    if { $w < 2 || $h < 2 } {
967        $_dispatcher event -idle !rebuild
968        return
969    }
970
971    # Turn on buffering of commands to the server.  We don't want to
972    # be preempted by a server disconnect/reconnect (which automatically
973    # generates a new call to Rebuild).
974    StartBufferingCommands
975    set _legendPending 1
976
977    set _first ""
978    if { $_reset } {
979        set _width $w
980        set _height $h
981        $_arcball resize $w $h
982        DoResize
983        InitSettings axisXGrid axisYGrid axisZGrid axis-mode \
984            axesVisible axisLabelsVisible
985        # This "imgflush" is to force an image returned before vtkvis starts
986        # reading a (big) dataset.  This will display an empty plot with axes
987        # at the correct image size.
988        SendCmd "imgflush"
989    }
990
991    set _first ""
992    SendCmd "dataset visible 0"
993    foreach dataobj [get -objects] {
994        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
995            set _first $dataobj
996        }
997        set _obj2datasets($dataobj) ""
998        foreach comp [$dataobj components] {
999            set tag $dataobj-$comp
1000            if { ![info exists _datasets($tag)] } {
1001                set bytes [$dataobj vtkdata $comp]
1002                set length [string length $bytes]
1003                if 0 {
1004                    set f [open /tmp/vtkstreamlines.vtk "w"]
1005                    fconfigure $f -translation binary -encoding binary
1006                    puts $f $bytes
1007                    close $f
1008                }
1009                if { $_reportClientInfo }  {
1010                    set info {}
1011                    lappend info "tool_id"       [$dataobj hints toolId]
1012                    lappend info "tool_name"     [$dataobj hints toolName]
1013                    lappend info "tool_version"  [$dataobj hints toolRevision]
1014                    lappend info "tool_title"    [$dataobj hints toolTitle]
1015                    lappend info "dataset_label" [$dataobj hints label]
1016                    lappend info "dataset_size"  $length
1017                    lappend info "dataset_tag"   $tag
1018                    SendCmd "clientinfo [list $info]"
1019                }
1020                SendCmd "dataset add $tag data follows $length"
1021                append _outbuf $bytes
1022                set _datasets($tag) 1
1023                SetObjectStyle $dataobj $comp
1024            }
1025            lappend _obj2datasets($dataobj) $tag
1026            if { [info exists _obj2ovride($dataobj-raise)] } {
1027                SendCmd "dataset visible 1 $tag"
1028            }
1029        }
1030    }
1031    if {"" != $_first} {
1032        set location [$_first hints camera]
1033        if { $location != "" } {
1034            array set view $location
1035        }
1036        foreach axis { x y z } {
1037            set label [$_first hints ${axis}label]
1038            if { $label != "" } {
1039                SendCmd "axis name $axis $label"
1040            }
1041            set units [$_first hints ${axis}units]
1042            if { $units != "" } {
1043                SendCmd "axis units $axis $units"
1044            }
1045        }
1046        $itk_component(field) choices delete 0 end
1047        $itk_component(fieldmenu) delete 0 end
1048        array unset _fields
1049        set _curFldName ""
1050        foreach cname [$_first components] {
1051            foreach fname [$_first fieldnames $cname] {
1052                if { [info exists _fields($fname)] } {
1053                    continue
1054                }
1055                foreach { label units components } \
1056                    [$_first fieldinfo $fname] break
1057                $itk_component(field) choices insert end "$fname" "$label"
1058                $itk_component(fieldmenu) add radiobutton -label "$label" \
1059                    -value $label -variable [itcl::scope _curFldLabel] \
1060                    -selectcolor red \
1061                    -activebackground $itk_option(-plotbackground) \
1062                    -activeforeground $itk_option(-plotforeground) \
1063                    -font "Arial 8" \
1064                    -command [itcl::code $this Combo invoke]
1065                set _fields($fname) [list $label $units $components]
1066                if { $_curFldName == "" } {
1067                    set _curFldName $fname
1068                    set _curFldLabel $label
1069                }
1070            }
1071        }
1072        $itk_component(field) value $_curFldLabel
1073    }
1074
1075    if { $_reset } {
1076        InitSettings streamlinesSeedsVisible streamlinesOpacity \
1077            streamlinesVisible streamlinesColormap \
1078            streamlinesLighting \
1079            streamlinesColormap field \
1080            volumeVisible volumeEdges volumeLighting volumeOpacity \
1081            volumeWireframe \
1082            cutplaneVisible \
1083            cutplaneXPosition cutplaneYPosition cutplaneZPosition \
1084            cutplaneXVisible cutplaneYVisible cutplaneZVisible
1085
1086        # Reset the camera and other view parameters
1087        set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1088        $_arcball quaternion $q
1089        if {$_view(ortho)} {
1090            SendCmd "camera mode ortho"
1091        } else {
1092            SendCmd "camera mode persp"
1093        }
1094        DoRotate
1095        PanCamera
1096        Zoom reset
1097        SendCmd "camera reset"
1098        set _reset 0
1099    }
1100    # Actually write the commands to the server socket.  If it fails, we don't
1101    # care.  We're finished here.
1102    blt::busy hold $itk_component(hull)
1103    StopBufferingCommands
1104    blt::busy release $itk_component(hull)
1105}
1106
1107# ----------------------------------------------------------------------
1108# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
1109#
1110# Returns a list of server IDs for the current datasets being displayed.  This
1111# is normally a single ID, but it might be a list of IDs if the current data
1112# object has multiple components.
1113# ----------------------------------------------------------------------
1114itcl::body Rappture::VtkStreamlinesViewer::CurrentDatasets {args} {
1115    set flag [lindex $args 0]
1116    switch -- $flag {
1117        "-all" {
1118            if { [llength $args] > 1 } {
1119                error "CurrentDatasets: can't specify dataobj after \"-all\""
1120            }
1121            set dlist [get -objects]
1122        }
1123        "-visible" {
1124            if { [llength $args] > 1 } {
1125                set dlist {}
1126                set args [lrange $args 1 end]
1127                foreach dataobj $args {
1128                    if { [info exists _obj2ovride($dataobj-raise)] } {
1129                        lappend dlist $dataobj
1130                    }
1131                }
1132            } else {
1133                set dlist [get -visible]
1134            }
1135        }           
1136        default {
1137            set dlist $args
1138        }
1139    }
1140    set rlist ""
1141    foreach dataobj $dlist {
1142        foreach comp [$dataobj components] {
1143            set tag $dataobj-$comp
1144            if { [info exists _datasets($tag)] && $_datasets($tag) } {
1145                lappend rlist $tag
1146            }
1147        }
1148    }
1149    return $rlist
1150}
1151
1152# ----------------------------------------------------------------------
1153# USAGE: Zoom in
1154# USAGE: Zoom out
1155# USAGE: Zoom reset
1156#
1157# Called automatically when the user clicks on one of the zoom
1158# controls for this widget.  Changes the zoom for the current view.
1159# ----------------------------------------------------------------------
1160itcl::body Rappture::VtkStreamlinesViewer::Zoom {option} {
1161    switch -- $option {
1162        "in" {
1163            set _view(zoom) [expr {$_view(zoom)*1.25}]
1164            SendCmd "camera zoom $_view(zoom)"
1165        }
1166        "out" {
1167            set _view(zoom) [expr {$_view(zoom)*0.8}]
1168            SendCmd "camera zoom $_view(zoom)"
1169        }
1170        "reset" {
1171            array set _view {
1172                qw      0.853553
1173                qx      -0.353553
1174                qy      0.353553
1175                qz      0.146447
1176                zoom    1.0
1177                xpan    0
1178                ypan    0
1179            }
1180            if { $_first != "" } {
1181                set location [$_first hints camera]
1182                if { $location != "" } {
1183                    array set _view $location
1184                }
1185            }
1186            set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1187            $_arcball quaternion $q
1188            DoRotate
1189            SendCmd "camera reset"
1190        }
1191    }
1192}
1193
1194itcl::body Rappture::VtkStreamlinesViewer::PanCamera {} {
1195    set x $_view(xpan)
1196    set y $_view(ypan)
1197    SendCmd "camera pan $x $y"
1198}
1199
1200
1201# ----------------------------------------------------------------------
1202# USAGE: Rotate click <x> <y>
1203# USAGE: Rotate drag <x> <y>
1204# USAGE: Rotate release <x> <y>
1205#
1206# Called automatically when the user clicks/drags/releases in the
1207# plot area.  Moves the plot according to the user's actions.
1208# ----------------------------------------------------------------------
1209itcl::body Rappture::VtkStreamlinesViewer::Rotate {option x y} {
1210    switch -- $option {
1211        "click" {
1212            $itk_component(view) configure -cursor fleur
1213            set _click(x) $x
1214            set _click(y) $y
1215        }
1216        "drag" {
1217            if {[array size _click] == 0} {
1218                Rotate click $x $y
1219            } else {
1220                set w [winfo width $itk_component(view)]
1221                set h [winfo height $itk_component(view)]
1222                if {$w <= 0 || $h <= 0} {
1223                    return
1224                }
1225
1226                if {[catch {
1227                    # this fails sometimes for no apparent reason
1228                    set dx [expr {double($x-$_click(x))/$w}]
1229                    set dy [expr {double($y-$_click(y))/$h}]
1230                }]} {
1231                    return
1232                }
1233                if { $dx == 0 && $dy == 0 } {
1234                    return
1235                }
1236                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1237                EventuallyRotate $q
1238                set _click(x) $x
1239                set _click(y) $y
1240            }
1241        }
1242        "release" {
1243            Rotate drag $x $y
1244            $itk_component(view) configure -cursor ""
1245            catch {unset _click}
1246        }
1247        default {
1248            error "bad option \"$option\": should be click, drag, release"
1249        }
1250    }
1251}
1252
1253itcl::body Rappture::VtkStreamlinesViewer::Pick {x y} {
1254    foreach tag [CurrentDatasets -visible] {
1255        SendCmdNoWait "dataset getscalar pixel $x $y $tag"
1256    }
1257}
1258
1259# ----------------------------------------------------------------------
1260# USAGE: $this Pan click x y
1261#        $this Pan drag x y
1262#        $this Pan release x y
1263#
1264# Called automatically when the user clicks on one of the zoom
1265# controls for this widget.  Changes the zoom for the current view.
1266# ----------------------------------------------------------------------
1267itcl::body Rappture::VtkStreamlinesViewer::Pan {option x y} {
1268    switch -- $option {
1269        "set" {
1270            set w [winfo width $itk_component(view)]
1271            set h [winfo height $itk_component(view)]
1272            set x [expr $x / double($w)]
1273            set y [expr $y / double($h)]
1274            set _view(xpan) [expr $_view(xpan) + $x]
1275            set _view(ypan) [expr $_view(ypan) + $y]
1276            PanCamera
1277            return
1278        }
1279        "click" {
1280            set _click(x) $x
1281            set _click(y) $y
1282            $itk_component(view) configure -cursor hand1
1283        }
1284        "drag" {
1285            if { ![info exists _click(x)] } {
1286                set _click(x) $x
1287            }
1288            if { ![info exists _click(y)] } {
1289                set _click(y) $y
1290            }
1291            set w [winfo width $itk_component(view)]
1292            set h [winfo height $itk_component(view)]
1293            set dx [expr ($_click(x) - $x)/double($w)]
1294            set dy [expr ($_click(y) - $y)/double($h)]
1295            set _click(x) $x
1296            set _click(y) $y
1297            set _view(xpan) [expr $_view(xpan) - $dx]
1298            set _view(ypan) [expr $_view(ypan) - $dy]
1299            PanCamera
1300        }
1301        "release" {
1302            Pan drag $x $y
1303            $itk_component(view) configure -cursor ""
1304        }
1305        default {
1306            error "unknown option \"$option\": should set, click, drag, or release"
1307        }
1308    }
1309}
1310
1311# ----------------------------------------------------------------------
1312# USAGE: InitSettings <what> ?<value>?
1313#
1314# Used internally to update rendering settings whenever parameters
1315# change in the popup settings panel.  Sends the new settings off
1316# to the back end.
1317# ----------------------------------------------------------------------
1318itcl::body Rappture::VtkStreamlinesViewer::InitSettings { args } {
1319    foreach spec $args {
1320        if { [info exists _settings($_first-$spec)] } {
1321            # Reset global setting with dataobj specific setting
1322            set _settings($spec) $_settings($_first-$spec)
1323        }
1324        AdjustSetting $spec
1325    }
1326}
1327
1328#
1329# AdjustSetting --
1330#
1331#       Changes/updates a specific setting in the widget.  There are
1332#       usually user-setable option.  Commands are sent to the render
1333#       server.
1334#
1335itcl::body Rappture::VtkStreamlinesViewer::AdjustSetting {what {value ""}} {
1336    if { ![isconnected] } {
1337        return
1338    }
1339    switch -- $what {
1340        "volumeOpacity" {
1341            set val $_settings(volumeOpacity)
1342            set sval [expr { 0.01 * double($val) }]
1343            SendCmd "polydata opacity $sval"
1344        }
1345        "volumeWireframe" {
1346            set bool $_settings(volumeWireframe)
1347            SendCmd "polydata wireframe $bool"
1348        }
1349        "volumeVisible" {
1350            set bool $_settings(volumeVisible)
1351            SendCmd "polydata visible $bool"
1352            if { $bool } {
1353                Rappture::Tooltip::for $itk_component(volume) \
1354                    "Hide the volume"
1355            } else {
1356                Rappture::Tooltip::for $itk_component(volume) \
1357                    "Show the volume"
1358            }
1359        }
1360        "volumeLighting" {
1361            set bool $_settings(volumeLighting)
1362            SendCmd "polydata lighting $bool"
1363        }
1364        "volumeEdges" {
1365            set bool $_settings(volumeEdges)
1366            SendCmd "polydata edges $bool"
1367        }
1368        "axesVisible" {
1369            set bool $_settings(axesVisible)
1370            SendCmd "axis visible all $bool"
1371        }
1372        "axisLabelsVisible" {
1373            set bool $_settings(axisLabelsVisible)
1374            SendCmd "axis labels all $bool"
1375        }
1376        "axisXGrid" - "axisYGrid" - "axisZGrid" {
1377            set axis [string tolower [string range $what 4 4]]
1378            set bool $_settings($what)
1379            SendCmd "axis grid $axis $bool"
1380        }
1381        "axis-mode" {
1382            set mode [$itk_component(axismode) value]
1383            set mode [$itk_component(axismode) translate $mode]
1384            set _settings($what) $mode
1385            SendCmd "axis flymode $mode"
1386        }
1387        "cutplaneEdges" {
1388            set bool $_settings($what)
1389            SendCmd "cutplane edges $bool"
1390        }
1391        "cutplaneVisible" {
1392            set bool $_settings($what)
1393            SendCmd "cutplane visible $bool"
1394        }
1395        "cutplaneWireframe" {
1396            set bool $_settings($what)
1397            SendCmd "cutplane wireframe $bool"
1398        }
1399        "cutplaneLighting" {
1400            set bool $_settings($what)
1401            SendCmd "cutplane lighting $bool"
1402        }
1403        "cutplaneOpacity" {
1404            set val $_settings($what)
1405            set sval [expr { 0.01 * double($val) }]
1406            SendCmd "cutplane opacity $sval"
1407        }
1408        "cutplaneXVisible" - "cutplaneYVisible" - "cutplaneZVisible" {
1409            set axis [string tolower [string range $what 8 8]]
1410            set bool $_settings($what)
1411            if { $bool } {
1412                $itk_component(${axis}CutScale) configure -state normal \
1413                    -troughcolor white
1414            } else {
1415                $itk_component(${axis}CutScale) configure -state disabled \
1416                    -troughcolor grey82
1417            }
1418            SendCmd "cutplane axis $axis $bool"
1419        }
1420        "cutplaneXPosition" - "cutplaneYPosition" - "cutplaneZPosition" {
1421            set axis [string tolower [string range $what 8 8]]
1422            set pos [expr $_settings($what) * 0.01]
1423            SendCmd "cutplane slice ${axis} ${pos}"
1424            set _cutplanePending 0
1425        }
1426        "streamlinesSeedsVisible" {
1427            set bool $_settings($what)
1428            SendCmd "streamlines seed visible $bool"
1429        }
1430        "streamlinesNumSeeds" {
1431            set density $_settings($what)
1432            EventuallyReseed $density
1433        }
1434        "streamlinesVisible" {
1435            set bool $_settings($what)
1436            SendCmd "streamlines visible $bool"
1437            if { $bool } {
1438                Rappture::Tooltip::for $itk_component(streamlines) \
1439                    "Hide the streamlines"
1440            } else {
1441                Rappture::Tooltip::for $itk_component(streamlines) \
1442                    "Show the streamlines"
1443            }
1444        }
1445        "streamlinesMode" {
1446            set mode [$itk_component(streammode) value]
1447            set _settings(streamlinesMode) $mode
1448            switch -- $mode {
1449                "lines" {
1450                    SendCmd "streamlines lines"
1451                }
1452                "ribbons" {
1453                    SendCmd "streamlines ribbons 3 0"
1454                }
1455                "tubes" {
1456                    SendCmd "streamlines tubes 5 3"
1457                }
1458            }
1459        }
1460        "streamlinesColormap" {
1461            set colormap [$itk_component(colormap) value]
1462            set _settings(streamlinesColormap) $colormap
1463            foreach dataset [CurrentDatasets -visible $_first] {
1464                foreach {dataobj comp} [split $dataset -] break
1465                ChangeColormap $dataobj $comp $colormap
1466            }
1467            set _legendPending 1
1468        }
1469        "streamlinesOpacity" {
1470            set val $_settings(streamlinesOpacity)
1471            set sval [expr { 0.01 * double($val) }]
1472            SendCmd "streamlines opacity $sval"
1473        }
1474        "streamlinesScale" {
1475            set val $_settings(streamlinesScale)
1476            set sval [expr { 0.01 * double($val) }]
1477            SendCmd "streamlines scale $sval $sval $sval"
1478        }
1479        "streamlinesLighting" {
1480            set bool $_settings(streamlinesLighting)
1481            SendCmd "streamlines lighting $bool"
1482        }
1483        "field" {
1484            set label [$itk_component(field) value]
1485            set fname [$itk_component(field) translate $label]
1486            set _settings(field) $fname
1487            if { [info exists _fields($fname)] } {
1488                foreach { label units components } $_fields($fname) break
1489                if { $components > 1 } {
1490                    set _colorMode vmag
1491                } else {
1492                    set _colorMode scalar
1493                }
1494                set _curFldName $fname
1495                set _curFldLabel $label
1496            } else {
1497                puts stderr "unknown field \"$fname\""
1498                return
1499            }
1500            # Get the new limits because the field changed.
1501            SendCmd "dataset maprange explicit $_limits($_curFldName) $_curFldName"
1502            SendCmd "streamlines colormode $_colorMode $_curFldName"
1503            SendCmd "cutplane colormode $_colorMode $_curFldName"
1504            DrawLegend
1505        }
1506        default {
1507            error "don't know how to fix $what"
1508        }
1509    }
1510}
1511
1512#
1513# RequestLegend --
1514#
1515#       Request a new legend from the server.  The size of the legend
1516#       is determined from the height of the canvas.  It will be rotated
1517#       to be vertical when drawn.
1518#
1519itcl::body Rappture::VtkStreamlinesViewer::RequestLegend {} {
1520    set font "Arial 8"
1521    set lineht [font metrics $font -linespace]
1522    set c $itk_component(legend)
1523    set w 12
1524    set h [expr {$_height - 3 * ($lineht + 2)}]
1525    if { $h < 1} {
1526        return
1527    }
1528    # Set the legend on the first streamlines dataset.
1529    foreach dataset [CurrentDatasets -visible $_first] {
1530        foreach {dataobj comp} [split $dataset -] break
1531        if { [info exists _dataset2style($dataset)] } {
1532            SendCmdNoWait \
1533                "legend $_dataset2style($dataset) $_colorMode $_curFldName {} $w $h 0"
1534            break;
1535        }
1536    }
1537}
1538
1539#
1540# ChangeColormap --
1541#
1542itcl::body Rappture::VtkStreamlinesViewer::ChangeColormap {dataobj comp color} {
1543    set tag $dataobj-$comp
1544    if { ![info exist _style($tag)] } {
1545        error "no initial colormap"
1546    }
1547    array set style $_style($tag)
1548    set style(-color) $color
1549    set _style($tag) [array get style]
1550    SetColormap $dataobj $comp
1551}
1552
1553#
1554# SetColormap --
1555#
1556itcl::body Rappture::VtkStreamlinesViewer::SetColormap { dataobj comp } {
1557    array set style {
1558        -color BCGYR
1559        -levels 6
1560        -opacity 1.0
1561    }
1562    set tag $dataobj-$comp
1563    if { ![info exists _initialStyle($tag)] } {
1564        # Save the initial component style.
1565        set _initialStyle($tag) [$dataobj style $comp]
1566    }
1567
1568    # Override defaults with initial style defined in xml.
1569    array set style $_initialStyle($tag)
1570
1571    if { ![info exists _style($tag)] } {
1572        set _style($tag) [array get style]
1573    }
1574    # Override initial style with current style.
1575    array set style $_style($tag)
1576
1577    set name "$style(-color):$style(-levels):$style(-opacity)"
1578    if { ![info exists _colormaps($name)] } {
1579        BuildColormap $name [array get style]
1580        set _colormaps($name) 1
1581    }
1582    if { ![info exists _dataset2style($tag)] ||
1583         $_dataset2style($tag) != $name } {
1584        SendCmd "streamlines colormap $name $tag"
1585        SendCmd "cutplane colormap $name $tag"
1586        set _dataset2style($tag) $name
1587    }
1588}
1589
1590
1591#
1592# BuildColormap --
1593#
1594itcl::body Rappture::VtkStreamlinesViewer::BuildColormap { name styles } {
1595    array set style $styles
1596    set cmap [ColorsToColormap $style(-color)]
1597    if { [llength $cmap] == 0 } {
1598        set cmap "0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0"
1599    }
1600    if { ![info exists _settings(volumeOpacity)] } {
1601        set _settings(volumeOpacity) $style(-opacity)
1602    }
1603    set max $_settings(volumeOpacity)
1604
1605    set wmap "0.0 1.0 1.0 1.0"
1606    SendCmd "colormap add $name { $cmap } { $wmap }"
1607}
1608
1609# ----------------------------------------------------------------------
1610# CONFIGURATION OPTION: -plotbackground
1611# ----------------------------------------------------------------------
1612itcl::configbody Rappture::VtkStreamlinesViewer::plotbackground {
1613    if { [isconnected] } {
1614        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1615        SendCmd "screen bgcolor $r $g $b"
1616    }
1617}
1618
1619# ----------------------------------------------------------------------
1620# CONFIGURATION OPTION: -plotforeground
1621# ----------------------------------------------------------------------
1622itcl::configbody Rappture::VtkStreamlinesViewer::plotforeground {
1623    if { [isconnected] } {
1624        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1625        #fix this!
1626        #SendCmd "color background $r $g $b"
1627    }
1628}
1629
1630itcl::body Rappture::VtkStreamlinesViewer::BuildVolumeTab {} {
1631
1632    set fg [option get $itk_component(hull) font Font]
1633    #set bfg [option get $itk_component(hull) boldFont Font]
1634
1635    set inner [$itk_component(main) insert end \
1636        -title "Volume Settings" \
1637        -icon [Rappture::icon volume-on]]
1638    $inner configure -borderwidth 4
1639
1640    checkbutton $inner.volume \
1641        -text "Show Volume" \
1642        -variable [itcl::scope _settings(volumeVisible)] \
1643        -command [itcl::code $this AdjustSetting volumeVisible] \
1644        -font "Arial 9"
1645
1646    checkbutton $inner.wireframe \
1647        -text "Show Wireframe" \
1648        -variable [itcl::scope _settings(volumeWireframe)] \
1649        -command [itcl::code $this AdjustSetting volumeWireframe] \
1650        -font "Arial 9"
1651
1652    checkbutton $inner.lighting \
1653        -text "Enable Lighting" \
1654        -variable [itcl::scope _settings(volumeLighting)] \
1655        -command [itcl::code $this AdjustSetting volumeLighting] \
1656        -font "Arial 9"
1657
1658    checkbutton $inner.edges \
1659        -text "Show Edges" \
1660        -variable [itcl::scope _settings(volumeEdges)] \
1661        -command [itcl::code $this AdjustSetting volumeEdges] \
1662        -font "Arial 9"
1663
1664    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1665    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1666        -variable [itcl::scope _settings(volumeOpacity)] \
1667        -width 10 \
1668        -showvalue off \
1669        -command [itcl::code $this AdjustSetting volumeOpacity]
1670
1671    blt::table $inner \
1672        0,0 $inner.wireframe -anchor w -pady 2 -cspan 2 \
1673        1,0 $inner.lighting  -anchor w -pady 2 -cspan 2 \
1674        2,0 $inner.edges     -anchor w -pady 2 -cspan 3 \
1675        3,0 $inner.opacity_l -anchor w -pady 2 \
1676        3,1 $inner.opacity   -fill x   -pady 2
1677
1678    blt::table configure $inner r* c* -resize none
1679    blt::table configure $inner r4 c1 -resize expand
1680}
1681
1682
1683itcl::body Rappture::VtkStreamlinesViewer::BuildStreamsTab {} {
1684
1685    set fg [option get $itk_component(hull) font Font]
1686    #set bfg [option get $itk_component(hull) boldFont Font]
1687
1688    set inner [$itk_component(main) insert end \
1689        -title "Streams Settings" \
1690        -icon [Rappture::icon streamlines-on]]
1691    $inner configure -borderwidth 4
1692
1693    checkbutton $inner.streamlines \
1694        -text "Show Streamlines" \
1695        -variable [itcl::scope _settings(streamlinesVisible)] \
1696        -command [itcl::code $this AdjustSetting streamlinesVisible] \
1697        -font "Arial 9"
1698   
1699    checkbutton $inner.lighting \
1700        -text "Enable Lighting" \
1701        -variable [itcl::scope _settings(streamlinesLighting)] \
1702        -command [itcl::code $this AdjustSetting streamlinesLighting] \
1703        -font "Arial 9"
1704
1705    checkbutton $inner.seeds \
1706        -text "Show Seeds" \
1707        -variable [itcl::scope _settings(streamlinesSeedsVisible)] \
1708        -command [itcl::code $this AdjustSetting streamlinesSeedsVisible] \
1709        -font "Arial 9"
1710
1711    label $inner.mode_l -text "Mode" -font "Arial 9"
1712    itk_component add streammode {
1713        Rappture::Combobox $inner.mode -width 10 -editable no
1714    }
1715    $inner.mode choices insert end \
1716        "lines"    "lines" \
1717        "ribbons"   "ribbons" \
1718        "tubes"     "tubes"
1719    $itk_component(streammode) value $_settings(streamlinesMode)
1720    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting streamlinesMode]
1721
1722    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1723    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1724        -variable [itcl::scope _settings(streamlinesOpacity)] \
1725        -width 10 \
1726        -showvalue off \
1727        -command [itcl::code $this AdjustSetting streamlinesOpacity]
1728
1729    label $inner.density_l -text "No. Seeds" -font "Arial 9"
1730    ::scale $inner.density -from 1 -to 1000 -orient horizontal \
1731        -variable [itcl::scope _settings(streamlinesNumSeeds)] \
1732        -width 10 \
1733        -showvalue on \
1734        -command [itcl::code $this AdjustSetting streamlinesNumSeeds]
1735
1736    label $inner.scale_l -text "Scale" -font "Arial 9"
1737    ::scale $inner.scale -from 1 -to 100 -orient horizontal \
1738        -variable [itcl::scope _settings(streamlinesScale)] \
1739        -width 10 \
1740        -showvalue off \
1741        -command [itcl::code $this AdjustSetting streamlinesScale]
1742
1743    label $inner.field_l -text "Color by" -font "Arial 9"
1744    itk_component add field {
1745        Rappture::Combobox $inner.field -width 10 -editable no
1746    }
1747    bind $inner.field <<Value>> \
1748        [itcl::code $this AdjustSetting field]
1749
1750    label $inner.colormap_l -text "Colormap" -font "Arial 9"
1751    itk_component add colormap {
1752        Rappture::Combobox $inner.colormap -width 10 -editable no
1753    }
1754    $inner.colormap choices insert end \
1755        "BCGYR"              "BCGYR"            \
1756        "BGYOR"              "BGYOR"            \
1757        "blue"               "blue"             \
1758        "blue-to-brown"      "blue-to-brown"    \
1759        "blue-to-orange"     "blue-to-orange"   \
1760        "blue-to-grey"       "blue-to-grey"     \
1761        "green-to-magenta"   "green-to-magenta" \
1762        "greyscale"          "greyscale"        \
1763        "nanohub"            "nanohub"          \
1764        "rainbow"            "rainbow"          \
1765        "spectral"           "spectral"         \
1766        "ROYGB"              "ROYGB"            \
1767        "RYGCB"              "RYGCB"            \
1768        "brown-to-blue"      "brown-to-blue"    \
1769        "grey-to-blue"       "grey-to-blue"     \
1770        "orange-to-blue"     "orange-to-blue"   
1771
1772    $itk_component(colormap) value "BCGYR"
1773    bind $inner.colormap <<Value>> \
1774        [itcl::code $this AdjustSetting streamlinesColormap]
1775
1776    blt::table $inner \
1777        0,0 $inner.field_l     -anchor w -pady 2  \
1778        0,1 $inner.field       -fill x   -pady 2  \
1779        1,0 $inner.colormap_l  -anchor w -pady 2  \
1780        1,1 $inner.colormap    -fill x   -pady 2  \
1781        2,0 $inner.mode_l      -anchor w -pady 2  \
1782        2,1 $inner.mode        -fill x   -pady 2  \
1783        3,0 $inner.opacity_l   -anchor w -pady 2  \
1784        3,1 $inner.opacity     -fill x -pady 2 \
1785        5,0 $inner.lighting    -anchor w -pady 2 -cspan 2 \
1786        6,0 $inner.seeds       -anchor w -pady 2 -cspan 2 \
1787        7,0 $inner.density_l   -anchor w -pady 2  \
1788        7,1 $inner.density     -fill x   -pady 2  \
1789
1790    blt::table configure $inner r* c* -resize none
1791    blt::table configure $inner r10 c1 c2 -resize expand
1792}
1793
1794itcl::body Rappture::VtkStreamlinesViewer::BuildAxisTab {} {
1795
1796    set fg [option get $itk_component(hull) font Font]
1797    #set bfg [option get $itk_component(hull) boldFont Font]
1798
1799    set inner [$itk_component(main) insert end \
1800        -title "Axis Settings" \
1801        -icon [Rappture::icon axis1]]
1802    $inner configure -borderwidth 4
1803
1804    checkbutton $inner.visible \
1805        -text "Show Axes" \
1806        -variable [itcl::scope _settings(axesVisible)] \
1807        -command [itcl::code $this AdjustSetting axesVisible] \
1808        -font "Arial 9"
1809
1810    checkbutton $inner.labels \
1811        -text "Show Axis Labels" \
1812        -variable [itcl::scope _settings(axisLabelsVisible)] \
1813        -command [itcl::code $this AdjustSetting axisLabelsVisible] \
1814        -font "Arial 9"
1815
1816    checkbutton $inner.xgrid \
1817        -text "Show X Grid" \
1818        -variable [itcl::scope _settings(axisXGrid)] \
1819        -command [itcl::code $this AdjustSetting axisXGrid] \
1820        -font "Arial 9"
1821    checkbutton $inner.ygrid \
1822        -text "Show Y Grid" \
1823        -variable [itcl::scope _settings(axisYGrid)] \
1824        -command [itcl::code $this AdjustSetting axisYGrid] \
1825        -font "Arial 9"
1826    checkbutton $inner.zgrid \
1827        -text "Show Z Grid" \
1828        -variable [itcl::scope _settings(axisZGrid)] \
1829        -command [itcl::code $this AdjustSetting axisZGrid] \
1830        -font "Arial 9"
1831
1832    label $inner.mode_l -text "Mode" -font "Arial 9"
1833
1834    itk_component add axismode {
1835        Rappture::Combobox $inner.mode -width 10 -editable no
1836    }
1837    $inner.mode choices insert end \
1838        "static_triad"    "static" \
1839        "closest_triad"   "closest" \
1840        "furthest_triad"  "furthest" \
1841        "outer_edges"     "outer"         
1842    $itk_component(axismode) value "static"
1843    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting axis-mode]
1844
1845    blt::table $inner \
1846        0,0 $inner.visible -anchor w -cspan 2 \
1847        1,0 $inner.labels  -anchor w -cspan 2 \
1848        2,0 $inner.xgrid   -anchor w -cspan 2 \
1849        3,0 $inner.ygrid   -anchor w -cspan 2 \
1850        4,0 $inner.zgrid   -anchor w -cspan 2 \
1851        5,0 $inner.mode_l  -anchor w -cspan 2 -padx { 2 0 } \
1852        6,0 $inner.mode    -fill x   -cspan 2
1853
1854    blt::table configure $inner r* c* -resize none
1855    blt::table configure $inner r7 c1 -resize expand
1856}
1857
1858
1859itcl::body Rappture::VtkStreamlinesViewer::BuildCameraTab {} {
1860    set inner [$itk_component(main) insert end \
1861        -title "Camera Settings" \
1862        -icon [Rappture::icon camera]]
1863    $inner configure -borderwidth 4
1864
1865    label $inner.view_l -text "view" -font "Arial 9"
1866    set f [frame $inner.view]
1867    foreach side { front back left right top bottom } {
1868        button $f.$side  -image [Rappture::icon view$side] \
1869            -command [itcl::code $this SetOrientation $side]
1870        Rappture::Tooltip::for $f.$side "Change the view to $side"
1871        pack $f.$side -side left
1872    }
1873
1874    blt::table $inner \
1875        0,0 $inner.view_l -anchor e -pady 2 \
1876        0,1 $inner.view -anchor w -pady 2
1877
1878    set labels { qx qy qz qw xpan ypan zoom }
1879    set row 1
1880    foreach tag $labels {
1881        label $inner.${tag}label -text $tag -font "Arial 9"
1882        entry $inner.${tag} -font "Arial 9"  -bg white \
1883            -textvariable [itcl::scope _view($tag)]
1884        bind $inner.${tag} <KeyPress-Return> \
1885            [itcl::code $this camera set ${tag}]
1886        blt::table $inner \
1887            $row,0 $inner.${tag}label -anchor e -pady 2 \
1888            $row,1 $inner.${tag} -anchor w -pady 2
1889        blt::table configure $inner r$row -resize none
1890        incr row
1891    }
1892    checkbutton $inner.ortho \
1893        -text "Orthographic Projection" \
1894        -variable [itcl::scope _view(ortho)] \
1895        -command [itcl::code $this camera set ortho] \
1896        -font "Arial 9"
1897    blt::table $inner \
1898            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1899    blt::table configure $inner r$row -resize none
1900    incr row
1901
1902    blt::table configure $inner c* r* -resize none
1903    blt::table configure $inner c2 -resize expand
1904    blt::table configure $inner r$row -resize expand
1905}
1906
1907itcl::body Rappture::VtkStreamlinesViewer::BuildCutplaneTab {} {
1908
1909    set fg [option get $itk_component(hull) font Font]
1910   
1911    set inner [$itk_component(main) insert end \
1912        -title "Cutplane Settings" \
1913        -icon [Rappture::icon cutbutton]]
1914
1915    $inner configure -borderwidth 4
1916
1917    checkbutton $inner.visible \
1918        -text "Show Cutplanes" \
1919        -variable [itcl::scope _settings(cutplaneVisible)] \
1920        -command [itcl::code $this AdjustSetting cutplaneVisible] \
1921        -font "Arial 9"
1922
1923    checkbutton $inner.wireframe \
1924        -text "Show Wireframe" \
1925        -variable [itcl::scope _settings(cutplaneWireframe)] \
1926        -command [itcl::code $this AdjustSetting cutplaneWireframe] \
1927        -font "Arial 9"
1928
1929    checkbutton $inner.lighting \
1930        -text "Enable Lighting" \
1931        -variable [itcl::scope _settings(cutplaneLighting)] \
1932        -command [itcl::code $this AdjustSetting cutplaneLighting] \
1933        -font "Arial 9"
1934
1935    checkbutton $inner.edges \
1936        -text "Show Edges" \
1937        -variable [itcl::scope _settings(cutplaneEdges)] \
1938        -command [itcl::code $this AdjustSetting cutplaneEdges] \
1939        -font "Arial 9"
1940
1941    label $inner.opacity_l -text "Opacity" -font "Arial 9"
1942    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1943        -variable [itcl::scope _settings(cutplaneOpacity)] \
1944        -width 10 \
1945        -showvalue off \
1946        -command [itcl::code $this AdjustSetting cutplaneOpacity]
1947    $inner.opacity set $_settings(cutplaneOpacity)
1948
1949    # X-value slicer...
1950    itk_component add xCutButton {
1951        Rappture::PushButton $inner.xbutton \
1952            -onimage [Rappture::icon x-cutplane-red] \
1953            -offimage [Rappture::icon x-cutplane-red] \
1954            -command [itcl::code $this AdjustSetting cutplaneXVisible] \
1955            -variable [itcl::scope _settings(cutplaneXVisible)]
1956    }
1957    Rappture::Tooltip::for $itk_component(xCutButton) \
1958        "Toggle the X-axis cutplane on/off"
1959    $itk_component(xCutButton) select
1960
1961    itk_component add xCutScale {
1962        ::scale $inner.xval -from 100 -to 0 \
1963            -width 10 -orient vertical -showvalue yes \
1964            -borderwidth 1 -highlightthickness 0 \
1965            -command [itcl::code $this EventuallySetCutplane x] \
1966            -variable [itcl::scope _settings(cutplaneXPosition)] \
1967            -foreground red3 -font "Arial 9 bold"
1968    } {
1969        usual
1970        ignore -borderwidth -highlightthickness -foreground -font
1971    }
1972    # Set the default cutplane value before disabling the scale.
1973    $itk_component(xCutScale) set 50
1974    $itk_component(xCutScale) configure -state disabled
1975    Rappture::Tooltip::for $itk_component(xCutScale) \
1976        "@[itcl::code $this Slice tooltip x]"
1977
1978    # Y-value slicer...
1979    itk_component add yCutButton {
1980        Rappture::PushButton $inner.ybutton \
1981            -onimage [Rappture::icon y-cutplane-green] \
1982            -offimage [Rappture::icon y-cutplane-green] \
1983            -command [itcl::code $this AdjustSetting cutplaneYVisible] \
1984            -variable [itcl::scope _settings(cutplaneYVisible)]
1985    }
1986    Rappture::Tooltip::for $itk_component(yCutButton) \
1987        "Toggle the Y-axis cutplane on/off"
1988    $itk_component(yCutButton) select
1989
1990    itk_component add yCutScale {
1991        ::scale $inner.yval -from 100 -to 0 \
1992            -width 10 -orient vertical -showvalue yes \
1993            -borderwidth 1 -highlightthickness 0 \
1994            -command [itcl::code $this EventuallySetCutplane y] \
1995            -variable [itcl::scope _settings(cutplaneYPosition)] \
1996            -foreground green3 -font "Arial 9 bold"
1997    } {
1998        usual
1999        ignore -borderwidth -highlightthickness -foreground -font
2000    }
2001    Rappture::Tooltip::for $itk_component(yCutScale) \
2002        "@[itcl::code $this Slice tooltip y]"
2003    # Set the default cutplane value before disabling the scale.
2004    $itk_component(yCutScale) set 50
2005    $itk_component(yCutScale) configure -state disabled
2006
2007    # Z-value slicer...
2008    itk_component add zCutButton {
2009        Rappture::PushButton $inner.zbutton \
2010            -onimage [Rappture::icon z-cutplane-blue] \
2011            -offimage [Rappture::icon z-cutplane-blue] \
2012            -command [itcl::code $this AdjustSetting cutplaneZVisible] \
2013            -variable [itcl::scope _settings(cutplaneZVisible)]
2014    }
2015    Rappture::Tooltip::for $itk_component(zCutButton) \
2016        "Toggle the Z-axis cutplane on/off"
2017    $itk_component(zCutButton) select
2018
2019    itk_component add zCutScale {
2020        ::scale $inner.zval -from 100 -to 0 \
2021            -width 10 -orient vertical -showvalue yes \
2022            -borderwidth 1 -highlightthickness 0 \
2023            -command [itcl::code $this EventuallySetCutplane z] \
2024            -variable [itcl::scope _settings(cutplaneZPosition)] \
2025            -foreground blue3 -font "Arial 9 bold"
2026    } {
2027        usual
2028        ignore -borderwidth -highlightthickness -foreground -font
2029    }
2030    $itk_component(zCutScale) set 50
2031    $itk_component(zCutScale) configure -state disabled
2032    Rappture::Tooltip::for $itk_component(zCutScale) \
2033        "@[itcl::code $this Slice tooltip z]"
2034
2035    blt::table $inner \
2036        0,0 $inner.lighting             -anchor w -pady 2 -cspan 4 \
2037        1,0 $inner.wireframe            -anchor w -pady 2 -cspan 4 \
2038        2,0 $inner.edges                -anchor w -pady 2 -cspan 4 \
2039        3,0 $inner.opacity_l            -anchor w -pady 2 -cspan 1 \
2040        3,1 $inner.opacity              -fill x   -pady 2 -cspan 3 \
2041        4,0 $itk_component(xCutButton)  -anchor w -padx 2 -pady 2 \
2042        5,0 $itk_component(yCutButton)  -anchor w -padx 2 -pady 2 \
2043        6,0 $itk_component(zCutButton)  -anchor w -padx 2 -pady 2 \
2044        4,1 $itk_component(xCutScale)   -fill y -rspan 4 \
2045        4,2 $itk_component(yCutScale)   -fill y -rspan 4 \
2046        4,3 $itk_component(zCutScale)   -fill y -rspan 4 \
2047
2048    blt::table configure $inner r* c* -resize none
2049    blt::table configure $inner r7 c4 -resize expand
2050}
2051
2052#
2053#  camera --
2054#
2055itcl::body Rappture::VtkStreamlinesViewer::camera {option args} {
2056    switch -- $option {
2057        "show" {
2058            puts [array get _view]
2059        }
2060        "set" {
2061            set who [lindex $args 0]
2062            set x $_view($who)
2063            set code [catch { string is double $x } result]
2064            if { $code != 0 || !$result } {
2065                return
2066            }
2067            switch -- $who {
2068                "ortho" {
2069                    if {$_view(ortho)} {
2070                        SendCmd "camera mode ortho"
2071                    } else {
2072                        SendCmd "camera mode persp"
2073                    }
2074                }
2075                "xpan" - "ypan" {
2076                    PanCamera
2077                }
2078                "qx" - "qy" - "qz" - "qw" {
2079                    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
2080                    $_arcball quaternion $q
2081                    EventuallyRotate $q
2082                }
2083                "zoom" {
2084                    SendCmd "camera zoom $_view(zoom)"
2085                }
2086            }
2087        }
2088    }
2089}
2090
2091itcl::body Rappture::VtkStreamlinesViewer::GetVtkData { args } {
2092    set bytes ""
2093    foreach dataobj [get] {
2094        foreach comp [$dataobj components] {
2095            set tag $dataobj-$comp
2096            set contents [$dataobj vtkdata $comp]
2097            append bytes "$contents\n"
2098        }
2099    }
2100    return [list .vtk $bytes]
2101}
2102
2103itcl::body Rappture::VtkStreamlinesViewer::GetImage { args } {
2104    if { [image width $_image(download)] > 0 &&
2105         [image height $_image(download)] > 0 } {
2106        set bytes [$_image(download) data -format "jpeg -quality 100"]
2107        set bytes [Rappture::encoding::decode -as b64 $bytes]
2108        return [list .jpg $bytes]
2109    }
2110    return ""
2111}
2112
2113itcl::body Rappture::VtkStreamlinesViewer::BuildDownloadPopup { popup command } {
2114    Rappture::Balloon $popup \
2115        -title "[Rappture::filexfer::label downloadWord] as..."
2116    set inner [$popup component inner]
2117    label $inner.summary -text "" -anchor w
2118    radiobutton $inner.vtk_button -text "VTK data file" \
2119        -variable [itcl::scope _downloadPopup(format)] \
2120        -font "Helvetica 9 " \
2121        -value vtk 
2122    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
2123    radiobutton $inner.image_button -text "Image File" \
2124        -variable [itcl::scope _downloadPopup(format)] \
2125        -value image
2126    Rappture::Tooltip::for $inner.image_button \
2127        "Save as digital image."
2128
2129    button $inner.ok -text "Save" \
2130        -highlightthickness 0 -pady 2 -padx 3 \
2131        -command $command \
2132        -compound left \
2133        -image [Rappture::icon download]
2134
2135    button $inner.cancel -text "Cancel" \
2136        -highlightthickness 0 -pady 2 -padx 3 \
2137        -command [list $popup deactivate] \
2138        -compound left \
2139        -image [Rappture::icon cancel]
2140
2141    blt::table $inner \
2142        0,0 $inner.summary -cspan 2  \
2143        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
2144        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
2145        4,1 $inner.cancel -width .9i -fill y \
2146        4,0 $inner.ok -padx 2 -width .9i -fill y
2147    blt::table configure $inner r3 -height 4
2148    blt::table configure $inner r4 -pady 4
2149    raise $inner.image_button
2150    $inner.vtk_button invoke
2151    return $inner
2152}
2153
2154itcl::body Rappture::VtkStreamlinesViewer::SetObjectStyle { dataobj comp } {
2155    # Parse style string.
2156    set tag $dataobj-$comp
2157    set style [$dataobj style $comp]
2158    array set settings {
2159        -color \#808080
2160        -edges 0
2161        -edgecolor black
2162        -linewidth 1.0
2163        -opacity 0.4
2164        -wireframe 0
2165        -lighting 1
2166        -seeds 1
2167        -seedcolor white
2168        -visible 1
2169    }
2170    if { $dataobj != $_first } {
2171        set settings(-opacity) 1
2172    }
2173    array set settings $style
2174    SendCmd "streamlines add $tag"
2175    SendCmd "streamlines seed visible off $tag"
2176    set seeds [$dataobj hints seeds]
2177    if { $seeds != "" && ![info exists _seeds($dataobj)] } {
2178        set length [string length $seeds]
2179        SendCmd "streamlines seed fmesh 200 data follows $length $tag"
2180        SendCmd "$seeds"
2181        set _seeds($dataobj) 1
2182    }
2183    SendCmd "cutplane add $tag"
2184    SendCmd "polydata add $tag"
2185    #SendCmd "polydata colormode ccolor {} $tag"
2186    set _settings(volumeEdges) $settings(-edges)
2187    set _settings(volumeLighting) $settings(-lighting)
2188    set _settings(volumeOpacity) $settings(-opacity)
2189    set _settings(volumeWireframe) $settings(-wireframe)
2190    set _settings(volumeOpacity) [expr $settings(-opacity) * 100.0]
2191    SetColormap $dataobj $comp
2192}
2193
2194itcl::body Rappture::VtkStreamlinesViewer::IsValidObject { dataobj } {
2195    if {[catch {$dataobj isa Rappture::Field} valid] != 0 || !$valid} {
2196        return 0
2197    }
2198    return 1
2199}
2200
2201# ----------------------------------------------------------------------
2202# USAGE: ReceiveLegend <colormap> <title> <vmin> <vmax> <size>
2203#
2204# Invoked automatically whenever the "legend" command comes in from
2205# the rendering server.  Indicates that binary image data with the
2206# specified <size> will follow.
2207# ----------------------------------------------------------------------
2208itcl::body Rappture::VtkStreamlinesViewer::ReceiveLegend { colormap title vmin vmax size } {
2209    set _legendPending 0
2210    set _title $title
2211    regsub {\(mag\)} $title "" _title
2212    if { [IsConnected] } {
2213        set bytes [ReceiveBytes $size]
2214        if { ![info exists _image(legend)] } {
2215            set _image(legend) [image create photo]
2216        }
2217        $_image(legend) configure -data $bytes
2218        #puts stderr "read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
2219        if { [catch {DrawLegend} errs] != 0 } {
2220            puts stderr errs=$errs
2221        }
2222    }
2223}
2224
2225#
2226# DrawLegend --
2227#
2228#       Draws the legend in it's own canvas which resides to the right
2229#       of the contour plot area.
2230#
2231itcl::body Rappture::VtkStreamlinesViewer::DrawLegend {} {
2232    set fname $_curFldName
2233    set c $itk_component(view)
2234    set w [winfo width $c]
2235    set h [winfo height $c]
2236    set font "Arial 8"
2237    set lineht [font metrics $font -linespace]
2238   
2239    if { [info exists _fields($fname)] } {
2240        foreach { title units } $_fields($fname) break
2241        if { $units != "" } {
2242            set title [format "%s (%s)" $title $units]
2243        }
2244    } else {
2245        set title $fname
2246    }
2247    if { $_settings(legendVisible) } {
2248        set x [expr $w - 2]
2249        if { [$c find withtag "legend"] == "" } {
2250            set y 2
2251            $c create text $x $y \
2252                -anchor ne \
2253                -fill $itk_option(-plotforeground) -tags "title legend" \
2254                -font $font
2255            incr y $lineht
2256            $c create text $x $y \
2257                -anchor ne \
2258                -fill $itk_option(-plotforeground) -tags "vmax legend" \
2259                -font $font
2260            incr y $lineht
2261            $c create image $x $y \
2262                -anchor ne \
2263                -image $_image(legend) -tags "colormap legend"
2264            $c create text $x [expr {$h-2}] \
2265                -anchor se \
2266                -fill $itk_option(-plotforeground) -tags "vmin legend" \
2267                -font $font
2268            #$c bind colormap <Enter> [itcl::code $this EnterLegend %x %y]
2269            $c bind colormap <Leave> [itcl::code $this LeaveLegend]
2270            $c bind colormap <Motion> [itcl::code $this MotionLegend %x %y]
2271        }
2272        $c bind title <ButtonPress> [itcl::code $this Combo post]
2273        $c bind title <Enter> [itcl::code $this Combo activate]
2274        $c bind title <Leave> [itcl::code $this Combo deactivate]
2275        # Reset the item coordinates according the current size of the plot.
2276        $c itemconfigure title -text $title
2277        if { [info exists _limits($_curFldName)] } {
2278            foreach {vmin vmax} $_limits($_curFldName) break
2279            $c itemconfigure vmin -text [format %g $vmin]
2280            $c itemconfigure vmax -text [format %g $vmax]
2281        }
2282        set y 2
2283        $c coords title $x $y
2284        incr y $lineht
2285        $c coords vmax $x $y
2286        incr y $lineht
2287        $c coords colormap $x $y
2288        $c coords vmin $x [expr {$h - 2}]
2289    }
2290}
2291
2292#
2293# EnterLegend --
2294#
2295itcl::body Rappture::VtkStreamlinesViewer::EnterLegend { x y } {
2296    SetLegendTip $x $y
2297}
2298
2299#
2300# MotionLegend --
2301#
2302itcl::body Rappture::VtkStreamlinesViewer::MotionLegend { x y } {
2303    Rappture::Tooltip::tooltip cancel
2304    set c $itk_component(view)
2305    SetLegendTip $x $y
2306}
2307
2308#
2309# LeaveLegend --
2310#
2311itcl::body Rappture::VtkStreamlinesViewer::LeaveLegend { } {
2312    Rappture::Tooltip::tooltip cancel
2313    .rappturetooltip configure -icon ""
2314}
2315
2316#
2317# SetLegendTip --
2318#
2319itcl::body Rappture::VtkStreamlinesViewer::SetLegendTip { x y } {
2320    set c $itk_component(view)
2321    set w [winfo width $c]
2322    set h [winfo height $c]
2323    set font "Arial 8"
2324    set lineht [font metrics $font -linespace]
2325   
2326    set imgHeight [image height $_image(legend)]
2327    set coords [$c coords colormap]
2328    set imgX [expr $w - [image width $_image(legend)] - 2]
2329    set imgY [expr $y - 2 * ($lineht + 2)]
2330
2331    if { [info exists _fields($_title)] } {
2332        foreach { title units } $_fields($_title) break
2333        if { $units != "" } {
2334            set title [format "%s (%s)" $title $units]
2335        }
2336    } else {
2337        set title $_title
2338    }
2339    # Make a swatch of the selected color
2340    if { [catch { $_image(legend) get 10 $imgY } pixel] != 0 } {
2341        #puts stderr "out of range: $imgY"
2342        return
2343    }
2344    if { ![info exists _image(swatch)] } {
2345        set _image(swatch) [image create photo -width 24 -height 24]
2346    }
2347    set color [eval format "\#%02x%02x%02x" $pixel]
2348    $_image(swatch) put black  -to 0 0 23 23
2349    $_image(swatch) put $color -to 1 1 22 22
2350    .rappturetooltip configure -icon $_image(swatch)
2351
2352    # Compute the value of the point
2353    if { [info exists _limits($_curFldName)] } {
2354        foreach {vmin vmax} $_limits($_curFldName) break
2355        set t [expr 1.0 - (double($imgY) / double($imgHeight-1))]
2356        set value [expr $t * ($vmax - $vmin) + $vmin]
2357    } else {
2358        set value 0.0
2359    }
2360    set tipx [expr $x + 15]
2361    set tipy [expr $y - 5]
2362    Rappture::Tooltip::text $c "$title $value"
2363    Rappture::Tooltip::tooltip show $c +$tipx,+$tipy   
2364}
2365
2366# ----------------------------------------------------------------------
2367# USAGE: Slice move x|y|z <newval>
2368#
2369# Called automatically when the user drags the slider to move the
2370# cut plane that slices 3D data.  Gets the current value from the
2371# slider and moves the cut plane to the appropriate point in the
2372# data set.
2373# ----------------------------------------------------------------------
2374itcl::body Rappture::VtkStreamlinesViewer::Slice {option args} {
2375    switch -- $option {
2376        "move" {
2377            set axis [lindex $args 0]
2378            set oldval $_settings(axis-${axis}position)
2379            set newval [lindex $args 1]
2380            if {[llength $args] != 2} {
2381                error "wrong # args: should be \"Slice move x|y|z newval\""
2382            }
2383            set newpos [expr {0.01*$newval}]
2384            SendCmd "cutplane slice $axis $newpos"
2385        }
2386        "tooltip" {
2387            set axis [lindex $args 0]
2388            set val [$itk_component(${axis}CutScale) get]
2389            return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val%"
2390        }
2391        default {
2392            error "bad option \"$option\": should be axis, move, or tooltip"
2393        }
2394    }
2395}
2396
2397# ----------------------------------------------------------------------
2398# USAGE: _dropdown post
2399# USAGE: _dropdown unpost
2400# USAGE: _dropdown select
2401#
2402# Used internally to handle the dropdown list for this combobox.  The
2403# post/unpost options are invoked when the list is posted or unposted
2404# to manage the relief of the controlling button.  The select option
2405# is invoked whenever there is a selection from the list, to assign
2406# the value back to the gauge.
2407# ----------------------------------------------------------------------
2408itcl::body Rappture::VtkStreamlinesViewer::Combo {option} {
2409    set c $itk_component(view)
2410    switch -- $option {
2411        post {
2412            foreach { x1 y1 x2 y2 } [$c bbox title] break
2413            set x1 [expr [winfo width $itk_component(view)] - [winfo reqwidth $itk_component(fieldmenu)]]
2414            set x [expr $x1 + [winfo rootx $itk_component(view)]]
2415            set y [expr $y2 + [winfo rooty $itk_component(view)]]
2416            tk_popup $itk_component(fieldmenu) $x $y
2417        }
2418        activate {
2419            $c itemconfigure title -fill red
2420        }
2421        deactivate {
2422            $c itemconfigure title -fill white
2423        }
2424        invoke {
2425            $itk_component(field) value $_curFldLabel
2426            AdjustSetting field
2427        }
2428        default {
2429            error "bad option \"$option\": should be post, unpost, select"
2430        }
2431    }
2432}
2433
2434itcl::body Rappture::VtkStreamlinesViewer::SetOrientation { side } {
2435    array set positions {
2436        front "1 0 0 0"
2437        back  "0 0 1 0"
2438        left  "0.707107 0 -0.707107 0"
2439        right "0.707107 0 0.707107 0"
2440        top   "0.707107 -0.707107 0 0"
2441        bottom "0.707107 0.707107 0 0"
2442    }
2443    foreach name { qw qx qy qz } value $positions($side) {
2444        set _view($name) $value
2445    }
2446    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
2447    $_arcball quaternion $q
2448    SendCmd "camera orient $q"
2449    SendCmd "camera reset"
2450    set _view(xpan) 0
2451    set _view(ypan) 0
2452    set _view(zoom) 1.0
2453}
Note: See TracBrowser for help on using the repository browser.