source: trunk/gui/scripts/vtkmeshviewer.tcl @ 5347

Last change on this file since 5347 was 5347, checked in by ldelgass, 5 years ago

set _downloadPopup to private access

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