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

Last change on this file since 5334 was 5334, checked in by ldelgass, 10 years ago

In VTK mesh viewer, get bounds/limits from mesh object -- don't need to parse
the VTK file.

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