source: branches/1.7/gui/scripts/vtkstreamlinesviewer.tcl @ 6306

Last change on this file since 6306 was 6306, checked in by ldelgass, 6 years ago

merge fixes from trunk

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