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

Last change on this file since 6142 was 6142, checked in by ldelgass, 9 years ago

minor cleanups

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