source: branches/1.6/gui/scripts/vtkmeshviewer.tcl @ 6212

Last change on this file since 6212 was 6212, checked in by ldelgass, 7 years ago

merge viewer cleanups from trunk

File size: 51.8 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: vtkmeshviewer - Vtk mesh 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
16#package require Img
17
18option add *VtkMeshViewer.width 4i widgetDefault
19option add *VtkMeshViewer*cursor crosshair widgetDefault
20option add *VtkMeshViewer.height 4i widgetDefault
21option add *VtkMeshViewer.foreground black widgetDefault
22option add *VtkMeshViewer.controlBackground gray widgetDefault
23option add *VtkMeshViewer.controlDarkBackground #999999 widgetDefault
24option add *VtkMeshViewer.plotBackground black widgetDefault
25option add *VtkMeshViewer.plotForeground white widgetDefault
26option add *VtkMeshViewer.font \
27    -*-helvetica-medium-r-normal-*-12-* widgetDefault
28
29# must use this name -- plugs into Rappture::resources::load
30proc VtkMeshViewer_init_resources {} {
31    Rappture::resources::register \
32        vtkvis_server Rappture::VtkMeshViewer::SetServerList
33}
34
35itcl::class Rappture::VtkMeshViewer {
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 BuildDownloadPopup { widget command }
69    private method BuildPolydataTab {}
70    private method Connect {}
71    private method CurrentDatasets {args}
72    private method Disconnect {}
73    private method DoResize {}
74    private method DoRotate {}
75    private method EventuallyResize { w h }
76    private method EventuallyRotate { q }
77    private method EventuallySetPolydataOpacity {}
78    private method GetImage { args }
79    private method GetVtkData { args }
80    private method InitSettings { args }
81    private method IsValidObject { dataobj }
82    private method Pan {option x y}
83    private method PanCamera {}
84    private method Pick {x y}
85    private method QuaternionToView { q } {
86        foreach { _view(-qw) _view(-qx) _view(-qy) _view(-qz) } $q break
87    }
88    private method Rebuild {}
89    private method ReceiveDataset { args }
90    private method ReceiveImage { args }
91    private method Rotate {option x y}
92    private method SetObjectStyle { dataobj }
93    private method SetOrientation { side }
94    private method SetPolydataOpacity {}
95    private method ViewToQuaternion {} {
96        return [list $_view(-qw) $_view(-qx) $_view(-qy) $_view(-qz)]
97    }
98    private method Zoom {option}
99
100    private variable _arcball ""
101    private variable _dlist "";         # list of data objects
102    private variable _obj2ovride;       # maps dataobj => style override
103    private variable _datasets;         # contains all the dataobj-component
104                                        # datasets in the server
105    private variable _click;            # info used for rotate operations
106    private variable _limits;           # autoscale min/max for all axes
107    private variable _view;             # view params for 3D view
108    private variable _settings
109    private variable _widget
110    private variable _reset 1;          # Connection to server has been reset.
111
112    private variable _first "";         # This is the topmost dataset.
113    private variable _start 0
114    private variable _title ""
115    private variable _width 0
116    private variable _height 0
117    private variable _resizePending 0
118    private variable _rotatePending 0
119    private variable _polydataOpacityPending 0
120    private variable _rotateDelay 150
121    private variable _opacityDelay 150
122
123    private common _downloadPopup;      # download options from popup
124    private common _hardcopy
125}
126
127itk::usual VtkMeshViewer {
128    keep -background -foreground -cursor -font
129    keep -plotbackground -plotforeground
130}
131
132# ----------------------------------------------------------------------
133# CONSTRUCTOR
134# ----------------------------------------------------------------------
135itcl::body Rappture::VtkMeshViewer::constructor {args} {
136    set _serverType "vtkvis"
137
138    #DebugOn
139    EnableWaitDialog 900
140
141    # Rebuild event
142    $_dispatcher register !rebuild
143    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
144
145    # Resize event
146    $_dispatcher register !resize
147    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
148
149    # Rotate event
150    $_dispatcher register !rotate
151    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
152
153    # Polydata opacity event
154    $_dispatcher register !polydataOpacity
155    $_dispatcher dispatch $this !polydataOpacity \
156        "[itcl::code $this SetPolydataOpacity]; list"
157    #
158    # Populate parser with commands handle incoming requests
159    #
160    $_parser alias image [itcl::code $this ReceiveImage]
161    $_parser alias dataset [itcl::code $this ReceiveDataset]
162
163    # Initialize the view to some default parameters.
164    array set _view {
165        -ortho    0
166        -qw       0.853553
167        -qx       -0.353553
168        -qy       0.353553
169        -qz       0.146447
170        -xpan     0
171        -ypan     0
172        -zoom     1.0
173    }
174    set _arcball [blt::arcball create 100 100]
175    $_arcball quaternion [ViewToQuaternion]
176
177    array set _settings {
178        -axesvisible            1
179        -axislabels             1
180        -axisminorticks         1
181        -outline                0
182        -polydataedges          0
183        -polydatalighting       1
184        -polydataopacity        1.0
185        -polydatavisible        1
186        -polydatawireframe      0
187        -xgrid                  0
188        -ygrid                  0
189        -zgrid                  0
190    }
191    array set _widget {
192        -polydataopacity        100
193    }
194    itk_component add view {
195        canvas $itk_component(plotarea).view \
196            -highlightthickness 0 -borderwidth 0
197    } {
198        usual
199        ignore -highlightthickness -borderwidth -background
200    }
201
202    itk_component add fieldmenu {
203        menu $itk_component(plotarea).menu -bg black -fg white -relief flat \
204            -tearoff 0
205    } {
206        usual
207        ignore -background -foreground -relief -tearoff
208    }
209
210    set c $itk_component(view)
211    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
212    bind $c <Control-F1> [itcl::code $this ToggleConsole]
213
214    # Fix the scrollregion in case we go off screen
215    $c configure -scrollregion [$c bbox all]
216
217    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
218    set _map(cwidth) -1
219    set _map(cheight) -1
220    set _map(zoom) 1.0
221    set _map(original) ""
222
223    set f [$itk_component(main) component controls]
224    itk_component add reset {
225        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
226            -highlightthickness 0 \
227            -image [Rappture::icon reset-view] \
228            -command [itcl::code $this Zoom reset]
229    } {
230        usual
231        ignore -highlightthickness
232    }
233    pack $itk_component(reset) -side top -padx 2 -pady 2
234    Rappture::Tooltip::for $itk_component(reset) \
235        "Reset the view to the default zoom level"
236
237    itk_component add zoomin {
238        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
239            -highlightthickness 0 \
240            -image [Rappture::icon zoom-in] \
241            -command [itcl::code $this Zoom in]
242    } {
243        usual
244        ignore -highlightthickness
245    }
246    pack $itk_component(zoomin) -side top -padx 2 -pady 2
247    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
248
249    itk_component add zoomout {
250        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
251            -highlightthickness 0 \
252            -image [Rappture::icon zoom-out] \
253            -command [itcl::code $this Zoom out]
254    } {
255        usual
256        ignore -highlightthickness
257    }
258    pack $itk_component(zoomout) -side top -padx 2 -pady 2
259    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
260
261    BuildPolydataTab
262    BuildAxisTab
263    BuildCameraTab
264
265    # Hack around the Tk panewindow.  The problem is that the requested
266    # size of the 3d view isn't set until an image is retrieved from
267    # the server.  So the panewindow uses the tiny size.
268    set w 10000
269    pack forget $itk_component(view)
270    blt::table $itk_component(plotarea) \
271        0,0 $itk_component(view) -fill both -reqwidth $w
272    blt::table configure $itk_component(plotarea) c1 -resize none
273
274    # Bindings for rotation via mouse
275    bind $itk_component(view) <ButtonPress-1> \
276        [itcl::code $this Rotate click %x %y]
277    bind $itk_component(view) <B1-Motion> \
278        [itcl::code $this Rotate drag %x %y]
279    bind $itk_component(view) <ButtonRelease-1> \
280        [itcl::code $this Rotate release %x %y]
281
282    # Bindings for panning via mouse
283    bind $itk_component(view) <ButtonPress-2> \
284        [itcl::code $this Pan click %x %y]
285    bind $itk_component(view) <B2-Motion> \
286        [itcl::code $this Pan drag %x %y]
287    bind $itk_component(view) <ButtonRelease-2> \
288        [itcl::code $this Pan release %x %y]
289
290    #bind $itk_component(view) <ButtonRelease-3> \
291    #    [itcl::code $this Pick %x %y]
292
293    # Bindings for panning via keyboard
294    bind $itk_component(view) <KeyPress-Left> \
295        [itcl::code $this Pan set -10 0]
296    bind $itk_component(view) <KeyPress-Right> \
297        [itcl::code $this Pan set 10 0]
298    bind $itk_component(view) <KeyPress-Up> \
299        [itcl::code $this Pan set 0 -10]
300    bind $itk_component(view) <KeyPress-Down> \
301        [itcl::code $this Pan set 0 10]
302    bind $itk_component(view) <Shift-KeyPress-Left> \
303        [itcl::code $this Pan set -2 0]
304    bind $itk_component(view) <Shift-KeyPress-Right> \
305        [itcl::code $this Pan set 2 0]
306    bind $itk_component(view) <Shift-KeyPress-Up> \
307        [itcl::code $this Pan set 0 -2]
308    bind $itk_component(view) <Shift-KeyPress-Down> \
309        [itcl::code $this Pan set 0 2]
310
311    # Bindings for zoom via keyboard
312    bind $itk_component(view) <KeyPress-Prior> \
313        [itcl::code $this Zoom out]
314    bind $itk_component(view) <KeyPress-Next> \
315        [itcl::code $this Zoom in]
316
317    bind $itk_component(view) <Enter> "focus $itk_component(view)"
318
319    if {[string equal "x11" [tk windowingsystem]]} {
320        # Bindings for zoom via mouse
321        bind $itk_component(view) <4> [itcl::code $this Zoom out]
322        bind $itk_component(view) <5> [itcl::code $this Zoom in]
323    }
324
325    set _image(download) [image create photo]
326
327    eval itk_initialize $args
328
329    Connect
330}
331
332# ----------------------------------------------------------------------
333# DESTRUCTOR
334# ----------------------------------------------------------------------
335itcl::body Rappture::VtkMeshViewer::destructor {} {
336    Disconnect
337    $_dispatcher cancel !rebuild
338    $_dispatcher cancel !resize
339    $_dispatcher cancel !rotate
340    image delete $_image(plot)
341    image delete $_image(download)
342    catch { blt::arcball destroy $_arcball }
343}
344
345itcl::body Rappture::VtkMeshViewer::DoResize {} {
346    if { $_width < 2 } {
347        set _width 500
348    }
349    if { $_height < 2 } {
350        set _height 500
351    }
352    set _start [clock clicks -milliseconds]
353    SendCmd "screen size $_width $_height"
354
355    set _resizePending 0
356}
357
358itcl::body Rappture::VtkMeshViewer::DoRotate {} {
359    SendCmd "camera orient [ViewToQuaternion]"
360    set _rotatePending 0
361}
362
363itcl::body Rappture::VtkMeshViewer::EventuallyResize { w h } {
364    set _width $w
365    set _height $h
366    $_arcball resize $w $h
367    if { !$_resizePending } {
368        set _resizePending 1
369        $_dispatcher event -after 200 !resize
370    }
371}
372
373itcl::body Rappture::VtkMeshViewer::EventuallyRotate { q } {
374    QuaternionToView $q
375    if { !$_rotatePending } {
376        set _rotatePending 1
377        $_dispatcher event -after $_rotateDelay !rotate
378    }
379}
380
381itcl::body Rappture::VtkMeshViewer::SetPolydataOpacity {} {
382    set _polydataOpacityPending 0
383    set val $_settings(-polydataopacity)
384    SendCmd "polydata opacity $val"
385}
386
387itcl::body Rappture::VtkMeshViewer::EventuallySetPolydataOpacity {} {
388    if { !$_polydataOpacityPending } {
389        set _polydataOpacityPending 1
390        $_dispatcher event -after $_opacityDelay !polydataOpacity
391    }
392}
393
394# ----------------------------------------------------------------------
395# USAGE: add <dataobj> ?<settings>?
396#
397# Clients use this to add a data object to the plot.  The optional
398# <settings> are used to configure the plot.  Allowed settings are
399# -color, -brightness, -width, -linestyle, and -raise.
400# ----------------------------------------------------------------------
401itcl::body Rappture::VtkMeshViewer::add {dataobj {settings ""}} {
402    array set params {
403        -color auto
404        -width 1
405        -linestyle solid
406        -brightness 0
407        -raise 0
408        -description ""
409        -param ""
410        -type ""
411    }
412    array set params $settings
413    set params(-description) ""
414    set params(-param) ""
415    array set params $settings
416
417    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
418        # can't handle -autocolors yet
419        set params(-color) black
420    }
421    set pos [lsearch -exact $_dlist $dataobj]
422    if {$pos < 0} {
423        lappend _dlist $dataobj
424    }
425    set _obj2ovride($dataobj-color) $params(-color)
426    set _obj2ovride($dataobj-width) $params(-width)
427    set _obj2ovride($dataobj-raise) $params(-raise)
428    $_dispatcher event -idle !rebuild
429}
430
431# ----------------------------------------------------------------------
432# USAGE: delete ?<dataobj1> <dataobj2> ...?
433#
434# Clients use this to delete a dataobj from the plot.  If no dataobjs
435# are specified, then all dataobjs are deleted.  No data objects are
436# deleted.  They are only removed from the display list.
437# ----------------------------------------------------------------------
438itcl::body Rappture::VtkMeshViewer::delete {args} {
439    if { [llength $args] == 0} {
440        set args $_dlist
441    }
442    # Delete all specified dataobjs
443    set changed 0
444    foreach dataobj $args {
445        set pos [lsearch -exact $_dlist $dataobj]
446        if { $pos < 0 } {
447            continue;                   # Don't know anything about it.
448        }
449        # Remove it from the dataobj list.
450        set _dlist [lreplace $_dlist $pos $pos]
451        array unset _obj2ovride $dataobj-*
452        set changed 1
453    }
454    # If anything changed, then rebuild the plot
455    if { $changed } {
456        $_dispatcher event -idle !rebuild
457    }
458}
459
460# ----------------------------------------------------------------------
461# USAGE: get ?-objects?
462# USAGE: get ?-visible?
463# USAGE: get ?-image view?
464#
465# Clients use this to query the list of objects being plotted, in
466# order from bottom to top of this result.  The optional "-image"
467# flag can also request the internal images being shown.
468# ----------------------------------------------------------------------
469itcl::body Rappture::VtkMeshViewer::get {args} {
470    if {[llength $args] == 0} {
471        set args "-objects"
472    }
473
474    set op [lindex $args 0]
475    switch -- $op {
476        "-objects" {
477            # put the dataobj list in order according to -raise options
478            set dlist {}
479            foreach dataobj $_dlist {
480                if { ![IsValidObject $dataobj] } {
481                    continue
482                }
483                if {[info exists _obj2ovride($dataobj-raise)] &&
484                    $_obj2ovride($dataobj-raise)} {
485                    set dlist [linsert $dlist 0 $dataobj]
486                } else {
487                    lappend dlist $dataobj
488                }
489            }
490            return $dlist
491        }
492        "-visible" {
493            set dlist {}
494            foreach dataobj $_dlist {
495                if { ![IsValidObject $dataobj] } {
496                    continue
497                }
498                if { ![info exists _obj2ovride($dataobj-raise)] } {
499                    # No setting indicates that the object isn't visible.
500                    continue
501                }
502                # Otherwise use the -raise parameter to put the object to
503                # the front of the list.
504                if { $_obj2ovride($dataobj-raise) } {
505                    set dlist [linsert $dlist 0 $dataobj]
506                } else {
507                    lappend dlist $dataobj
508                }
509            }
510            return $dlist
511        }
512        -image {
513            if {[llength $args] != 2} {
514                error "wrong # args: should be \"get -image view\""
515            }
516            switch -- [lindex $args end] {
517                view {
518                    return $_image(plot)
519                }
520                default {
521                    error "bad image name \"[lindex $args end]\": should be view"
522                }
523            }
524        }
525        default {
526            error "bad option \"$op\": should be -objects or -image"
527        }
528    }
529}
530
531# ----------------------------------------------------------------------
532# USAGE: scale ?<data1> <data2> ...?
533#
534# Sets the default limits for the overall plot according to the
535# limits of the data for all of the given <data> objects.  This
536# accounts for all objects--even those not showing on the screen.
537# Because of this, the limits are appropriate for all objects as
538# the user scans through data in the ResultSet viewer.
539# ----------------------------------------------------------------------
540itcl::body Rappture::VtkMeshViewer::scale {args} {
541    foreach dataobj $args {
542        if { ![$dataobj isvalid] } {
543            continue;                   # Object doesn't contain valid data.
544        }
545        foreach axis { x y z } {
546            set lim [$dataobj limits $axis]
547            if { ![info exists _limits($axis)] } {
548                set _limits($axis) $lim
549                continue
550            }
551            foreach {min max} $lim break
552            foreach {amin amax} $_limits($axis) break
553            if { $amin > $min } {
554                set amin $min
555            }
556            if { $amax < $max } {
557                set amax $max
558            }
559            set _limits($axis) [list $amin $amax]
560        }
561    }
562}
563
564# ----------------------------------------------------------------------
565# USAGE: download coming
566# USAGE: download controls <downloadCommand>
567# USAGE: download now
568#
569# Clients use this method to create a downloadable representation
570# of the plot.  Returns a list of the form {ext string}, where
571# "ext" is the file extension (indicating the type of data) and
572# "string" is the data itself.
573# ----------------------------------------------------------------------
574itcl::body Rappture::VtkMeshViewer::download {option args} {
575    switch $option {
576        coming {
577            if {[catch {
578                blt::winop snap $itk_component(plotarea) $_image(download)
579            }]} {
580                $_image(download) configure -width 1 -height 1
581                $_image(download) put #000000
582            }
583        }
584        controls {
585            set popup .vtkviewerdownload
586            if { ![winfo exists .vtkviewerdownload] } {
587                set inner [BuildDownloadPopup $popup [lindex $args 0]]
588            } else {
589                set inner [$popup component inner]
590            }
591            set _downloadPopup(image_controls) $inner.image_frame
592            set num [llength [get]]
593            set num [expr {($num == 1) ? "1 result" : "$num results"}]
594            set word [Rappture::filexfer::label downloadWord]
595            $inner.summary configure -text "$word $num in the following format:"
596            update idletasks            ;# Fix initial sizes
597            return $popup
598        }
599        now {
600            set popup .vtkviewerdownload
601            if {[winfo exists .vtkviewerdownload]} {
602                $popup deactivate
603            }
604            switch -- $_downloadPopup(format) {
605                "image" {
606                    return [$this GetImage [lindex $args 0]]
607                }
608                "vtk" {
609                    return [$this GetVtkData [lindex $args 0]]
610                }
611            }
612            return ""
613        }
614        default {
615            error "bad option \"$option\": should be coming, controls, now"
616        }
617    }
618}
619
620# ----------------------------------------------------------------------
621# USAGE: Connect ?<host:port>,<host:port>...?
622#
623# Clients use this method to establish a connection to a new
624# server, or to reestablish a connection to the previous server.
625# Any existing connection is automatically closed.
626# ----------------------------------------------------------------------
627itcl::body Rappture::VtkMeshViewer::Connect {} {
628    global readyForNextFrame
629    set readyForNextFrame 1
630    set _hosts [GetServerList "vtkvis"]
631    if { "" == $_hosts } {
632        return 0
633    }
634    set _reset 1
635    set result [VisViewer::Connect $_hosts]
636    if { $result } {
637        if { $_reportClientInfo }  {
638            # Tell the server the viewer, hub, user and session.
639            # Do this immediately on connect before buffering any commands
640            global env
641
642            set info {}
643            set user "???"
644            if { [info exists env(USER)] } {
645                set user $env(USER)
646            }
647            set session "???"
648            if { [info exists env(SESSION)] } {
649                set session $env(SESSION)
650            }
651            lappend info "version" "$Rappture::version"
652            lappend info "build" "$Rappture::build"
653            lappend info "svnurl" "$Rappture::svnurl"
654            lappend info "installdir" "$Rappture::installdir"
655            lappend info "hub" [exec hostname]
656            lappend info "client" "vtkmeshviewer"
657            lappend info "user" $user
658            lappend info "session" $session
659            SendCmd "clientinfo [list $info]"
660        }
661
662        set w [winfo width $itk_component(view)]
663        set h [winfo height $itk_component(view)]
664        EventuallyResize $w $h
665    }
666    return $result
667}
668
669#
670# isconnected --
671#
672# Indicates if we are currently connected to the visualization server.
673#
674itcl::body Rappture::VtkMeshViewer::isconnected {} {
675    return [VisViewer::IsConnected]
676}
677
678#
679# disconnect --
680#
681itcl::body Rappture::VtkMeshViewer::disconnect {} {
682    Disconnect
683    set _reset 1
684}
685
686#
687# Disconnect --
688#
689# Clients use this method to disconnect from the current rendering server.
690#
691itcl::body Rappture::VtkMeshViewer::Disconnect {} {
692    VisViewer::Disconnect
693
694    # disconnected -- no more data sitting on server
695    array unset _datasets
696    global readyForNextFrame
697    set readyForNextFrame 1
698}
699
700# ----------------------------------------------------------------------
701# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
702#
703# Invoked automatically whenever the "image" command comes in from
704# the rendering server.  Indicates that binary image data with the
705# specified <size> will follow.
706# ----------------------------------------------------------------------
707itcl::body Rappture::VtkMeshViewer::ReceiveImage { args } {
708    global readyForNextFrame
709    set readyForNextFrame 1
710    array set info {
711        -token "???"
712        -bytes 0
713        -type image
714    }
715    array set info $args
716    set bytes [ReceiveBytes $info(-bytes)]
717    if { $info(-type) == "image" } {
718        if 0 {
719            set f [open "last.ppm" "w"]
720            fconfigure $f -encoding binary
721            puts -nonewline $f $bytes
722            close $f
723        }
724        $_image(plot) configure -data $bytes
725        set time [clock seconds]
726        set date [clock format $time]
727        if { $_start > 0 } {
728            set finish [clock clicks -milliseconds]
729            set _start 0
730        }
731    } elseif { $info(type) == "print" } {
732        set tag $this-print-$info(-token)
733        set _hardcopy($tag) $bytes
734    }
735}
736
737#
738# ReceiveDataset --
739#
740itcl::body Rappture::VtkMeshViewer::ReceiveDataset { args } {
741    if { ![isconnected] } {
742        return
743    }
744    set option [lindex $args 0]
745    switch -- $option {
746        "scalar" {
747            set option [lindex $args 1]
748            switch -- $option {
749                "world" {
750                    foreach { x y z value tag } [lrange $args 2 end] break
751                }
752                "pixel" {
753                    foreach { x y value tag } [lrange $args 2 end] break
754                }
755            }
756        }
757        "vector" {
758            set option [lindex $args 1]
759            switch -- $option {
760                "world" {
761                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
762                }
763                "pixel" {
764                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
765                }
766            }
767        }
768        "names" {
769            foreach { name } [lindex $args 1] {
770                #puts stderr "Dataset: $name"
771            }
772        }
773        default {
774            error "unknown dataset option \"$option\" from server"
775        }
776    }
777}
778
779# ----------------------------------------------------------------------
780# USAGE: Rebuild
781#
782# Called automatically whenever something changes that affects the
783# data in the widget.  Clears any existing data and rebuilds the
784# widget to display new data.
785# ----------------------------------------------------------------------
786itcl::body Rappture::VtkMeshViewer::Rebuild {} {
787    set w [winfo width $itk_component(view)]
788    set h [winfo height $itk_component(view)]
789    if { $w < 2 || $h < 2 } {
790        update
791        $_dispatcher event -idle !rebuild
792        return
793    }
794
795    # Turn on buffering of commands to the server.  We don't want to
796    # be preempted by a server disconnect/reconnect (which automatically
797    # generates a new call to Rebuild).
798    StartBufferingCommands
799
800    if { $_reset } {
801        set _width $w
802        set _height $h
803        $_arcball resize $w $h
804        DoResize
805        InitSettings -xgrid -ygrid -zgrid -axismode \
806            -axesvisible -axislabels -axisminorticks
807        StopBufferingCommands
808        SendCmd "imgflush"
809        StartBufferingCommands
810    }
811
812    set _first ""
813    SendCmd "dataset visible 0"
814    set count 0
815    foreach dataobj [get -objects] {
816        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
817            set _first $dataobj
818        }
819        set tag $dataobj
820        if { ![info exists _datasets($tag)] } {
821            set bytes [$dataobj vtkdata -full]
822            if { $bytes == "" } {
823                continue
824            }
825            if 0 {
826                set f [open /tmp/vtkmesh.vtk "w"]
827                fconfigure $f -translation binary -encoding binary
828                puts -nonewline $f $bytes
829                close $f
830            }
831            set length [string length $bytes]
832            if { $_reportClientInfo }  {
833                set info {}
834                lappend info "tool_id"       [$dataobj hints toolid]
835                lappend info "tool_name"     [$dataobj hints toolname]
836                lappend info "tool_title"    [$dataobj hints tooltitle]
837                lappend info "tool_command"  [$dataobj hints toolcommand]
838                lappend info "tool_revision" [$dataobj hints toolrevision]
839                lappend info "dataset_label" [$dataobj hints label]
840                lappend info "dataset_size"  $length
841                lappend info "dataset_tag"   $tag
842                SendCmd "clientinfo [list $info]"
843            }
844            SendCmd "dataset add $tag data follows $length"
845            SendData $bytes
846            set _datasets($tag) 1
847            SetObjectStyle $dataobj
848        }
849        if { [info exists _obj2ovride($dataobj-raise)] } {
850            SendCmd "dataset visible 1 $tag"
851            EventuallySetPolydataOpacity
852        }
853    }
854    if {"" != $_first} {
855        foreach axis { x y z } {
856            set label [$_first label ${axis}]
857            if { $label != "" } {
858                SendCmd [list axis name $axis $label]
859            }
860            set units [$_first units ${axis}]
861            if { $units != "" } {
862                SendCmd [list axis units $axis $units]
863            }
864        }
865    }
866    InitSettings -outline
867    if { $_reset } {
868        # These are settings that rely on a dataset being loaded.
869        InitSettings -polydataedges -polydatalighting -polydataopacity \
870            -polydatavisible -polydatawireframe
871
872        #SendCmd "axis lformat all %g"
873
874        $_arcball quaternion [ViewToQuaternion]
875        SendCmd "camera reset"
876        if { $_view(-ortho)} {
877            SendCmd "camera mode ortho"
878        } else {
879            SendCmd "camera mode persp"
880        }
881        DoRotate
882        PanCamera
883        Zoom reset
884    }
885
886    set _reset 0
887    global readyForNextFrame
888    set readyForNextFrame 0;            # Don't advance to the next frame
889                                        # until we get an image.
890
891    # Actually write the commands to the server socket.  If it fails, we don't
892    # care.  We're finished here.
893    blt::busy hold $itk_component(hull)
894    StopBufferingCommands
895    blt::busy release $itk_component(hull)
896}
897
898# ----------------------------------------------------------------------
899# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
900#
901# Returns a list of server IDs for the current datasets being displayed.  This
902# is normally a single ID, but it might be a list of IDs if the current data
903# object has multiple components.
904# ----------------------------------------------------------------------
905itcl::body Rappture::VtkMeshViewer::CurrentDatasets {args} {
906    set flag [lindex $args 0]
907    switch -- $flag {
908        "-all" {
909            if { [llength $args] > 1 } {
910                error "CurrentDatasets: can't specify dataobj after \"-all\""
911            }
912            set dlist [get -objects]
913        }
914        "-visible" {
915            if { [llength $args] > 1 } {
916                set dlist {}
917                set args [lrange $args 1 end]
918                foreach dataobj $args {
919                    if { [info exists _obj2ovride($dataobj-raise)] } {
920                        lappend dlist $dataobj
921                    }
922                }
923            } else {
924                set dlist [get -visible]
925            }
926        }
927        default {
928            set dlist $args
929        }
930    }
931    set rlist ""
932    foreach tag $dlist {
933        if { [info exists _datasets($tag)] && $_datasets($tag) } {
934            lappend rlist $tag
935        }
936    }
937    return $rlist
938}
939
940# ----------------------------------------------------------------------
941# USAGE: Zoom in
942# USAGE: Zoom out
943# USAGE: Zoom reset
944#
945# Called automatically when the user clicks on one of the zoom
946# controls for this widget.  Changes the zoom for the current view.
947# ----------------------------------------------------------------------
948itcl::body Rappture::VtkMeshViewer::Zoom {option} {
949    switch -- $option {
950        "in" {
951            set _view(-zoom) [expr {$_view(-zoom)*1.25}]
952            SendCmd "camera zoom $_view(-zoom)"
953        }
954        "out" {
955            set _view(-zoom) [expr {$_view(-zoom)*0.8}]
956            SendCmd "camera zoom $_view(-zoom)"
957        }
958        "reset" {
959            array set _view {
960                -qw      0.853553
961                -qx      -0.353553
962                -qy      0.353553
963                -qz      0.146447
964                -xpan    0
965                -ypan    0
966                -zoom    1.0
967            }
968            if { $_first != "" } {
969                set location [$_first hints camera]
970                if { $location != "" } {
971                    array set _view $location
972                }
973            }
974            $_arcball quaternion [ViewToQuaternion]
975            DoRotate
976            SendCmd "camera reset"
977        }
978    }
979}
980
981itcl::body Rappture::VtkMeshViewer::PanCamera {} {
982    set x $_view(-xpan)
983    set y $_view(-ypan)
984    SendCmd "camera pan $x $y"
985}
986
987# ----------------------------------------------------------------------
988# USAGE: Rotate click <x> <y>
989# USAGE: Rotate drag <x> <y>
990# USAGE: Rotate release <x> <y>
991#
992# Called automatically when the user clicks/drags/releases in the
993# plot area.  Moves the plot according to the user's actions.
994# ----------------------------------------------------------------------
995itcl::body Rappture::VtkMeshViewer::Rotate {option x y} {
996    switch -- $option {
997        "click" {
998            $itk_component(view) configure -cursor fleur
999            set _click(x) $x
1000            set _click(y) $y
1001        }
1002        "drag" {
1003            if {[array size _click] == 0} {
1004                Rotate click $x $y
1005            } else {
1006                set w [winfo width $itk_component(view)]
1007                set h [winfo height $itk_component(view)]
1008                if {$w <= 0 || $h <= 0} {
1009                    return
1010                }
1011
1012                if {[catch {
1013                    # this fails sometimes for no apparent reason
1014                    set dx [expr {double($x-$_click(x))/$w}]
1015                    set dy [expr {double($y-$_click(y))/$h}]
1016                }]} {
1017                    return
1018                }
1019                if { $dx == 0 && $dy == 0 } {
1020                    return
1021                }
1022                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1023                EventuallyRotate $q
1024                set _click(x) $x
1025                set _click(y) $y
1026            }
1027        }
1028        "release" {
1029            Rotate drag $x $y
1030            $itk_component(view) configure -cursor ""
1031            catch {unset _click}
1032        }
1033        default {
1034            error "bad option \"$option\": should be click, drag, release"
1035        }
1036    }
1037}
1038
1039itcl::body Rappture::VtkMeshViewer::Pick {x y} {
1040    foreach tag [CurrentDatasets -visible] {
1041        SendCmd "dataset getscalar pixel $x $y $tag"
1042    }
1043}
1044
1045# ----------------------------------------------------------------------
1046# USAGE: $this Pan click x y
1047#        $this Pan drag x y
1048#        $this Pan release x y
1049#
1050# Called automatically when the user clicks on one of the zoom
1051# controls for this widget.  Changes the zoom for the current view.
1052# ----------------------------------------------------------------------
1053itcl::body Rappture::VtkMeshViewer::Pan {option x y} {
1054    switch -- $option {
1055        "set" {
1056            set w [winfo width $itk_component(view)]
1057            set h [winfo height $itk_component(view)]
1058            set x [expr $x / double($w)]
1059            set y [expr $y / double($h)]
1060            set _view(-xpan) [expr $_view(-xpan) + $x]
1061            set _view(-ypan) [expr $_view(-ypan) + $y]
1062            PanCamera
1063            return
1064        }
1065        "click" {
1066            set _click(x) $x
1067            set _click(y) $y
1068            $itk_component(view) configure -cursor hand1
1069        }
1070        "drag" {
1071            if { ![info exists _click(x)] } {
1072                set _click(x) $x
1073            }
1074            if { ![info exists _click(y)] } {
1075                set _click(y) $y
1076            }
1077            set w [winfo width $itk_component(view)]
1078            set h [winfo height $itk_component(view)]
1079            set dx [expr ($_click(x) - $x)/double($w)]
1080            set dy [expr ($_click(y) - $y)/double($h)]
1081            set _click(x) $x
1082            set _click(y) $y
1083            set _view(-xpan) [expr $_view(-xpan) - $dx]
1084            set _view(-ypan) [expr $_view(-ypan) - $dy]
1085            PanCamera
1086        }
1087        "release" {
1088            Pan drag $x $y
1089            $itk_component(view) configure -cursor ""
1090        }
1091        default {
1092            error "unknown option \"$option\": should set, click, drag, or release"
1093        }
1094    }
1095}
1096
1097# ----------------------------------------------------------------------
1098# USAGE: InitSettings <what> ?<value>?
1099#
1100# Used internally to update rendering settings whenever parameters
1101# change in the popup settings panel.  Sends the new settings off
1102# to the back end.
1103# ----------------------------------------------------------------------
1104itcl::body Rappture::VtkMeshViewer::InitSettings { args } {
1105    foreach setting $args {
1106        AdjustSetting $setting
1107    }
1108}
1109
1110#
1111# AdjustSetting --
1112#
1113# Changes/updates a specific setting in the widget.  There are
1114# usually user-setable option.  Commands are sent to the render
1115# server.
1116#
1117itcl::body Rappture::VtkMeshViewer::AdjustSetting {what {value ""}} {
1118    if { ![isconnected] } {
1119        return
1120    }
1121    switch -- $what {
1122        "-outline" {
1123            set bool $_settings($what)
1124            # Only display a outline for the currently visible sets.
1125            SendCmd "outline visible 0"
1126            foreach dataset [CurrentDatasets -visible] {
1127                SendCmd "outline visible $bool $dataset"
1128            }
1129        }
1130        "-polydataopacity" {
1131            set _settings($what) [expr $_widget($what) * 0.01]
1132            EventuallySetPolydataOpacity
1133        }
1134        "-polydatawireframe" {
1135            set bool $_settings($what)
1136            SendCmd "polydata wireframe $bool"
1137        }
1138        "-polydatavisible" {
1139            set bool $_settings($what)
1140            # Only change visibility of data sets marked "visible".
1141            foreach dataset [CurrentDatasets -visible] {
1142                SendCmd "polydata visible $bool $dataset"
1143            }
1144        }
1145        "-polydatalighting" {
1146            set bool $_settings($what)
1147            SendCmd "polydata lighting $bool"
1148        }
1149        "-polydataedges" {
1150            set bool $_settings($what)
1151            SendCmd "polydata edges $bool"
1152        }
1153        "-axesvisible" {
1154            set bool $_settings($what)
1155            SendCmd "axis visible all $bool"
1156        }
1157        "-axislabels" {
1158            set bool $_settings($what)
1159            SendCmd "axis labels all $bool"
1160        }
1161        "-axisminorticks" {
1162            set bool $_settings($what)
1163            SendCmd "axis minticks all $bool"
1164        }
1165        "-xgrid" {
1166            set bool $_settings($what)
1167            SendCmd "axis grid x $bool"
1168        }
1169        "-ygrid" {
1170            set bool $_settings($what)
1171            SendCmd "axis grid y $bool"
1172        }
1173        "-zgrid" {
1174            set bool $_settings($what)
1175            SendCmd "axis grid z $bool"
1176        }
1177        "-axismode" {
1178            set mode [$itk_component(axismode) value]
1179            set mode [$itk_component(axismode) translate $mode]
1180            SendCmd "axis flymode $mode"
1181        }
1182        default {
1183            error "don't know how to fix $what"
1184        }
1185    }
1186}
1187
1188# ----------------------------------------------------------------------
1189# CONFIGURATION OPTION: -plotbackground
1190# ----------------------------------------------------------------------
1191itcl::configbody Rappture::VtkMeshViewer::plotbackground {
1192    if { [isconnected] } {
1193        set rgb [Color2RGB $itk_option(-plotbackground)]
1194        SendCmd "screen bgcolor $rgb"
1195    }
1196}
1197
1198# ----------------------------------------------------------------------
1199# CONFIGURATION OPTION: -plotforeground
1200# ----------------------------------------------------------------------
1201itcl::configbody Rappture::VtkMeshViewer::plotforeground {
1202    if { [isconnected] } {
1203        set rgb [Color2RGB $itk_option(-plotforeground)]
1204        SendCmd "axis color all $rgb"
1205        SendCmd "outline color $rgb"
1206    }
1207}
1208
1209itcl::body Rappture::VtkMeshViewer::BuildPolydataTab {} {
1210
1211    set fg [option get $itk_component(hull) font Font]
1212    #set bfg [option get $itk_component(hull) boldFont Font]
1213
1214    set inner [$itk_component(main) insert end \
1215        -title "Mesh Settings" \
1216        -icon [Rappture::icon mesh]]
1217    $inner configure -borderwidth 4
1218
1219    checkbutton $inner.mesh \
1220        -text "Show Mesh" \
1221        -variable [itcl::scope _settings(-polydatavisible)] \
1222        -command [itcl::code $this AdjustSetting -polydatavisible] \
1223        -font "Arial 9" -anchor w
1224
1225    checkbutton $inner.outline \
1226        -text "Show Outline" \
1227        -variable [itcl::scope _settings(-outline)] \
1228        -command [itcl::code $this AdjustSetting -outline] \
1229        -font "Arial 9" -anchor w
1230
1231    checkbutton $inner.wireframe \
1232        -text "Show Wireframe" \
1233        -variable [itcl::scope _settings(-polydatawireframe)] \
1234        -command [itcl::code $this AdjustSetting -polydatawireframe] \
1235        -font "Arial 9" -anchor w
1236
1237    checkbutton $inner.lighting \
1238        -text "Enable Lighting" \
1239        -variable [itcl::scope _settings(-polydatalighting)] \
1240        -command [itcl::code $this AdjustSetting -polydatalighting] \
1241        -font "Arial 9" -anchor w
1242
1243    checkbutton $inner.edges \
1244        -text "Show Edges" \
1245        -variable [itcl::scope _settings(-polydataedges)] \
1246        -command [itcl::code $this AdjustSetting -polydataedges] \
1247        -font "Arial 9" -anchor w
1248
1249    itk_component add field_l {
1250        label $inner.field_l -text "Field" -font "Arial 9"
1251    } {
1252        ignore -font
1253    }
1254    itk_component add field {
1255        Rappture::Combobox $inner.field -width 10 -editable 0
1256    }
1257    bind $inner.field <<Value>> \
1258        [itcl::code $this AdjustSetting -field]
1259
1260    label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w
1261    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1262        -variable [itcl::scope _widget(-polydataopacity)] \
1263        -width 10 \
1264        -showvalue off \
1265        -command [itcl::code $this AdjustSetting -polydataopacity]
1266    $inner.opacity set [expr $_settings(-polydataopacity) * 100.0]
1267
1268    blt::table $inner \
1269        0,0 $inner.mesh      -cspan 2  -anchor w -pady 2 \
1270        1,0 $inner.outline   -cspan 2  -anchor w -pady 2 \
1271        2,0 $inner.wireframe -cspan 2  -anchor w -pady 2 \
1272        3,0 $inner.lighting  -cspan 2  -anchor w -pady 2 \
1273        4,0 $inner.edges     -cspan 2  -anchor w -pady 2 \
1274        5,0 $inner.opacity_l -anchor w -pady 2 \
1275        5,1 $inner.opacity   -fill x   -pady 2
1276
1277    blt::table configure $inner r* c* -resize none
1278    blt::table configure $inner r7 c1 -resize expand
1279}
1280
1281itcl::body Rappture::VtkMeshViewer::BuildAxisTab {} {
1282
1283    set fg [option get $itk_component(hull) font Font]
1284    #set bfg [option get $itk_component(hull) boldFont Font]
1285
1286    set inner [$itk_component(main) insert end \
1287        -title "Axis Settings" \
1288        -icon [Rappture::icon axis2]]
1289    $inner configure -borderwidth 4
1290
1291    checkbutton $inner.visible \
1292        -text "Axes" \
1293        -variable [itcl::scope _settings(-axesvisible)] \
1294        -command [itcl::code $this AdjustSetting -axesvisible] \
1295        -font "Arial 9"
1296
1297    checkbutton $inner.labels \
1298        -text "Axis Labels" \
1299        -variable [itcl::scope _settings(-axislabels)] \
1300        -command [itcl::code $this AdjustSetting -axislabels] \
1301        -font "Arial 9"
1302    label $inner.grid_l -text "Grid" -font "Arial 9"
1303    checkbutton $inner.xgrid \
1304        -text "X" \
1305        -variable [itcl::scope _settings(-xgrid)] \
1306        -command [itcl::code $this AdjustSetting -xgrid] \
1307        -font "Arial 9"
1308    checkbutton $inner.ygrid \
1309        -text "Y" \
1310        -variable [itcl::scope _settings(-ygrid)] \
1311        -command [itcl::code $this AdjustSetting -ygrid] \
1312        -font "Arial 9"
1313    checkbutton $inner.zgrid \
1314        -text "Z" \
1315        -variable [itcl::scope _settings(-zgrid)] \
1316        -command [itcl::code $this AdjustSetting -zgrid] \
1317        -font "Arial 9"
1318    checkbutton $inner.minorticks \
1319        -text "Minor Ticks" \
1320        -variable [itcl::scope _settings(-axisminorticks)] \
1321        -command [itcl::code $this AdjustSetting -axisminorticks] \
1322        -font "Arial 9"
1323
1324    label $inner.mode_l -text "Mode" -font "Arial 9"
1325
1326    itk_component add axismode {
1327        Rappture::Combobox $inner.mode -width 10 -editable 0
1328    }
1329    $inner.mode choices insert end \
1330        "static_triad"    "static" \
1331        "closest_triad"   "closest" \
1332        "furthest_triad"  "farthest" \
1333        "outer_edges"     "outer"
1334    $itk_component(axismode) value "static"
1335    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting -axismode]
1336
1337    blt::table $inner \
1338        0,0 $inner.visible -anchor w -cspan 4 \
1339        1,0 $inner.labels  -anchor w -cspan 4 \
1340        2,0 $inner.minorticks  -anchor w -cspan 4 \
1341        4,0 $inner.grid_l  -anchor w \
1342        4,1 $inner.xgrid   -anchor w \
1343        4,2 $inner.ygrid   -anchor w \
1344        4,3 $inner.zgrid   -anchor w \
1345        5,0 $inner.mode_l  -anchor w -padx { 2 0 } \
1346        5,1 $inner.mode    -fill x   -cspan 3
1347
1348    blt::table configure $inner r* c* -resize none
1349    blt::table configure $inner r7 c6 -resize expand
1350    blt::table configure $inner r3 -height 0.125i
1351}
1352
1353itcl::body Rappture::VtkMeshViewer::BuildCameraTab {} {
1354    set inner [$itk_component(main) insert end \
1355        -title "Camera Settings" \
1356        -icon [Rappture::icon camera]]
1357    $inner configure -borderwidth 4
1358
1359    label $inner.view_l -text "view" -font "Arial 9"
1360    set f [frame $inner.view]
1361    foreach side { front back left right top bottom } {
1362        button $f.$side  -image [Rappture::icon view$side] \
1363            -command [itcl::code $this SetOrientation $side]
1364        Rappture::Tooltip::for $f.$side "Change the view to $side"
1365        pack $f.$side -side left
1366    }
1367
1368    blt::table $inner \
1369        0,0 $inner.view_l -anchor e -pady 2 \
1370        0,1 $inner.view -anchor w -pady 2
1371    blt::table configure $inner r0 -resize none
1372
1373    set labels { qx qy qz qw xpan ypan zoom }
1374    set row 1
1375    foreach tag $labels {
1376        label $inner.${tag}label -text $tag -font "Arial 9"
1377        entry $inner.${tag} -font "Arial 9"  -bg white \
1378            -textvariable [itcl::scope _view(-$tag)]
1379        bind $inner.${tag} <Return> \
1380            [itcl::code $this camera set -${tag}]
1381        bind $inner.${tag} <KP_Enter> \
1382            [itcl::code $this camera set -${tag}]
1383        blt::table $inner \
1384            $row,0 $inner.${tag}label -anchor e -pady 2 \
1385            $row,1 $inner.${tag} -anchor w -pady 2
1386        blt::table configure $inner r$row -resize none
1387        incr row
1388    }
1389    checkbutton $inner.ortho \
1390        -text "Orthographic Projection" \
1391        -variable [itcl::scope _view(-ortho)] \
1392        -command [itcl::code $this camera set -ortho] \
1393        -font "Arial 9"
1394    blt::table $inner \
1395            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1396    blt::table configure $inner r$row -resize none
1397    incr row
1398
1399    blt::table configure $inner c* -resize none
1400    blt::table configure $inner c2 -resize expand
1401    blt::table configure $inner r$row -resize expand
1402}
1403
1404#
1405# camera --
1406#
1407itcl::body Rappture::VtkMeshViewer::camera {option args} {
1408    switch -- $option {
1409        "show" {
1410            puts [array get _view]
1411        }
1412        "set" {
1413            set what [lindex $args 0]
1414            set x $_view($what)
1415            set code [catch { string is double $x } result]
1416            if { $code != 0 || !$result } {
1417                return
1418            }
1419            switch -- $what {
1420                "-ortho" {
1421                    if {$_view($what)} {
1422                        SendCmd "camera mode ortho"
1423                    } else {
1424                        SendCmd "camera mode persp"
1425                    }
1426                }
1427                "-xpan" - "-ypan" {
1428                    PanCamera
1429                }
1430                "-qx" - "-qy" - "-qz" - "-qw" {
1431                    set q [ViewToQuaternion]
1432                    $_arcball quaternion $q
1433                    EventuallyRotate $q
1434                }
1435                "-zoom" {
1436                    SendCmd "camera zoom $_view($what)"
1437                }
1438            }
1439        }
1440    }
1441}
1442
1443itcl::body Rappture::VtkMeshViewer::GetVtkData { args } {
1444    set bytes ""
1445    foreach dataobj [get] {
1446        set contents [$dataobj vtkdata -full]
1447        append bytes "$contents\n"
1448    }
1449    return [list .vtk $bytes]
1450}
1451
1452itcl::body Rappture::VtkMeshViewer::GetImage { args } {
1453    if { [image width $_image(download)] > 0 &&
1454         [image height $_image(download)] > 0 } {
1455        set bytes [$_image(download) data -format "jpeg -quality 100"]
1456        set bytes [Rappture::encoding::decode -as b64 $bytes]
1457        return [list .jpg $bytes]
1458    }
1459    return ""
1460}
1461
1462itcl::body Rappture::VtkMeshViewer::BuildDownloadPopup { popup command } {
1463    Rappture::Balloon $popup \
1464        -title "[Rappture::filexfer::label downloadWord] as..."
1465    set inner [$popup component inner]
1466    label $inner.summary -text "" -anchor w
1467    radiobutton $inner.vtk_button -text "VTK data file" \
1468        -variable [itcl::scope _downloadPopup(format)] \
1469        -font "Helvetica 9 " \
1470        -value vtk
1471    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
1472    radiobutton $inner.image_button -text "Image File" \
1473        -variable [itcl::scope _downloadPopup(format)] \
1474        -value image
1475    Rappture::Tooltip::for $inner.image_button \
1476        "Save as digital image."
1477
1478    button $inner.ok -text "Save" \
1479        -highlightthickness 0 -pady 2 -padx 3 \
1480        -command $command \
1481        -compound left \
1482        -image [Rappture::icon download]
1483
1484    button $inner.cancel -text "Cancel" \
1485        -highlightthickness 0 -pady 2 -padx 3 \
1486        -command [list $popup deactivate] \
1487        -compound left \
1488        -image [Rappture::icon cancel]
1489
1490    blt::table $inner \
1491        0,0 $inner.summary -cspan 2  \
1492        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
1493        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
1494        4,1 $inner.cancel -width .9i -fill y \
1495        4,0 $inner.ok -padx 2 -width .9i -fill y
1496    blt::table configure $inner r3 -height 4
1497    blt::table configure $inner r4 -pady 4
1498    raise $inner.image_button
1499    $inner.vtk_button invoke
1500    return $inner
1501}
1502
1503itcl::body Rappture::VtkMeshViewer::SetObjectStyle { dataobj } {
1504    # Parse style string.
1505    set tag $dataobj
1506    set type [$dataobj type]
1507
1508    array set style {
1509        -cloudstyle mesh
1510        -color white
1511        -edgecolor black
1512        -edges 1
1513        -lighting 1
1514        -linewidth 1.0
1515        -opacity 1.0
1516        -outline 0
1517        -visible 1
1518        -wireframe 0
1519    }
1520    if {$type == "cloud"} {
1521        set style(-cloudstyle) points
1522        set style(-edges) 0
1523        set style(-edgecolor) white
1524    }
1525    array set style [$dataobj hints style]
1526
1527    if {[$dataobj hints color] != ""} {
1528        set style(-color) [$dataobj hints color]
1529    }
1530    SendCmd "outline add $tag"
1531    SendCmd "outline color [Color2RGB $style(-color)] $tag"
1532    SendCmd "outline visible $style(-outline) $tag"
1533    set _settings(-outline) $style(-outline)
1534
1535    SendCmd "polydata add $tag"
1536    SendCmd "polydata visible $style(-visible) $tag"
1537    set _settings(-polydatavisible) $style(-visible)
1538    SendCmd "polydata cloudstyle $style(-cloudstyle) $tag"
1539    SendCmd "polydata edges $style(-edges) $tag"
1540    set _settings(-polydataedges) $style(-edges)
1541    SendCmd "polydata color [Color2RGB $style(-color)] $tag"
1542    SendCmd "polydata lighting $style(-lighting) $tag"
1543    set _settings(-polydatalighting) $style(-lighting)
1544    SendCmd "polydata linecolor [Color2RGB $style(-edgecolor)] $tag"
1545    SendCmd "polydata linewidth $style(-linewidth) $tag"
1546    SendCmd "polydata opacity $style(-opacity) $tag"
1547    set _settings(-polydataopacity) $style(-opacity)
1548    set _widget(-polydataopacity) [expr 100.0 * $style(-opacity)]
1549    SendCmd "polydata wireframe $style(-wireframe) $tag"
1550    set _settings(-polydatawireframe) $style(-wireframe)
1551    set havePolyData 1
1552}
1553
1554itcl::body Rappture::VtkMeshViewer::IsValidObject { dataobj } {
1555    if {[catch {$dataobj isa Rappture::Mesh} valid] != 0 || !$valid} {
1556        return 0
1557    }
1558    return 1
1559}
1560
1561itcl::body Rappture::VtkMeshViewer::SetOrientation { side } {
1562    array set positions {
1563        front "1 0 0 0"
1564        back  "0 0 1 0"
1565        left  "0.707107 0 -0.707107 0"
1566        right "0.707107 0 0.707107 0"
1567        top   "0.707107 -0.707107 0 0"
1568        bottom "0.707107 0.707107 0 0"
1569    }
1570    foreach name { -qw -qx -qy -qz } value $positions($side) {
1571        set _view($name) $value
1572    }
1573    set q [ViewToQuaternion]
1574    $_arcball quaternion $q
1575    SendCmd "camera orient $q"
1576    SendCmd "camera reset"
1577    set _view(-xpan) 0
1578    set _view(-ypan) 0
1579    set _view(-zoom) 1.0
1580}
Note: See TracBrowser for help on using the repository browser.