source: branches/uq/gui/scripts/vtkmeshviewer.tcl @ 4797

Last change on this file since 4797 was 4797, checked in by gah, 6 years ago

sync with 1.4-pre

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