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

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

merge fixes from trunk

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