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

Last change on this file since 6155 was 6155, checked in by ldelgass, 8 years ago

merge viewer cleanups from trunk

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