source: branches/1.3/gui/scripts/vtkmeshviewer.tcl @ 4759

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

merge r4756:4758 from trunk

File size: 54.6 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_title"    [$dataobj hints tooltitle]
852                lappend info "tool_command"  [$dataobj hints toolcommand]
853                lappend info "tool_revision" [$dataobj hints toolrevision]
854                lappend info "dataset_label" [$dataobj hints label]
855                lappend info "dataset_size"  $length
856                lappend info "dataset_tag"   $tag
857                SendCmd [list "clientinfo" $info]
858            }
859            SendCmd "dataset add $tag data follows $length"
860            append _outbuf $bytes
861            set _datasets($tag) 1
862            SetObjectStyle $dataobj
863        }
864        lappend _obj2datasets($dataobj) $tag
865        if { [info exists _obj2ovride($dataobj-raise)] } {
866            SendCmd "dataset visible 1 $tag"
867            EventuallySetPolydataOpacity
868        }
869    }
870    if {"" != $_first} {
871        set location [$_first hints camera]
872        if { $location != "" } {
873            array set view $location
874        }
875
876        foreach axis { x y z } {
877            set label [$_first label ${axis}]
878            if { $label != "" } {
879                SendCmd [list axis name $axis $label]
880            }
881            set units [$_first units ${axis}]
882            if { $units != "" } {
883                SendCmd [list axis units $axis $units]
884            }
885        }
886    }
887    InitSettings -outline
888    if { $_reset } {
889        # These are settings that rely on a dataset being loaded.
890        InitSettings -polydataedges -polydatalighting -polydataopacity \
891            -polydatavisible -polydatawireframe
892 
893        #SendCmd "axis lformat all %g"
894
895        set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
896        $_arcball quaternion $q
897        SendCmd "camera reset"
898        if { $_view(ortho)} {
899            SendCmd "camera mode ortho"
900        } else {
901            SendCmd "camera mode persp"
902        }
903        DoRotate
904        PanCamera
905        Zoom reset
906    }
907
908    set _reset 0
909    global readyForNextFrame
910    set readyForNextFrame 0;            # Don't advance to the next frame
911                                        # until we get an image.
912
913    # Actually write the commands to the server socket.  If it fails, we don't
914    # care.  We're finished here.
915    blt::busy hold $itk_component(hull)
916    StopBufferingCommands
917    blt::busy release $itk_component(hull)
918}
919
920# ----------------------------------------------------------------------
921# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
922#
923# Returns a list of server IDs for the current datasets being displayed.  This
924# is normally a single ID, but it might be a list of IDs if the current data
925# object has multiple components.
926# ----------------------------------------------------------------------
927itcl::body Rappture::VtkMeshViewer::CurrentDatasets {args} {
928    set flag [lindex $args 0]
929    switch -- $flag {
930        "-all" {
931            if { [llength $args] > 1 } {
932                error "CurrentDatasets: can't specify dataobj after \"-all\""
933            }
934            set dlist [get -objects]
935        }
936        "-visible" {
937            if { [llength $args] > 1 } {
938                set dlist {}
939                set args [lrange $args 1 end]
940                foreach dataobj $args {
941                    if { [info exists _obj2ovride($dataobj-raise)] } {
942                        lappend dlist $dataobj
943                    }
944                }
945            } else {
946                set dlist [get -visible]
947            }
948        }           
949        default {
950            set dlist $args
951        }
952    }
953    set rlist ""
954    foreach tag $dlist {
955        if { [info exists _datasets($tag)] && $_datasets($tag) } {
956            lappend rlist $tag
957        }
958    }
959    return $rlist
960}
961
962# ----------------------------------------------------------------------
963# USAGE: Zoom in
964# USAGE: Zoom out
965# USAGE: Zoom reset
966#
967# Called automatically when the user clicks on one of the zoom
968# controls for this widget.  Changes the zoom for the current view.
969# ----------------------------------------------------------------------
970itcl::body Rappture::VtkMeshViewer::Zoom {option} {
971    switch -- $option {
972        "in" {
973            set _view(zoom) [expr {$_view(zoom)*1.25}]
974            SendCmd "camera zoom $_view(zoom)"
975        }
976        "out" {
977            set _view(zoom) [expr {$_view(zoom)*0.8}]
978            SendCmd "camera zoom $_view(zoom)"
979        }
980        "reset" {
981            array set _view {
982                qw      0.853553
983                qx      -0.353553
984                qy      0.353553
985                qz      0.146447
986                zoom    1.0
987                xpan    0
988                ypan    0
989            }
990            if { $_first != "" } {
991                set location [$_first hints camera]
992                if { $location != "" } {
993                    array set _view $location
994                }
995            }
996            set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
997            $_arcball quaternion $q
998            DoRotate
999            SendCmd "camera reset"
1000        }
1001    }
1002}
1003
1004itcl::body Rappture::VtkMeshViewer::PanCamera {} {
1005    set x $_view(xpan)
1006    set y $_view(ypan)
1007    SendCmd "camera pan $x $y"
1008}
1009
1010# ----------------------------------------------------------------------
1011# USAGE: Rotate click <x> <y>
1012# USAGE: Rotate drag <x> <y>
1013# USAGE: Rotate release <x> <y>
1014#
1015# Called automatically when the user clicks/drags/releases in the
1016# plot area.  Moves the plot according to the user's actions.
1017# ----------------------------------------------------------------------
1018itcl::body Rappture::VtkMeshViewer::Rotate {option x y} {
1019    switch -- $option {
1020        "click" {
1021            $itk_component(view) configure -cursor fleur
1022            set _click(x) $x
1023            set _click(y) $y
1024        }
1025        "drag" {
1026            if {[array size _click] == 0} {
1027                Rotate click $x $y
1028            } else {
1029                set w [winfo width $itk_component(view)]
1030                set h [winfo height $itk_component(view)]
1031                if {$w <= 0 || $h <= 0} {
1032                    return
1033                }
1034
1035                if {[catch {
1036                    # this fails sometimes for no apparent reason
1037                    set dx [expr {double($x-$_click(x))/$w}]
1038                    set dy [expr {double($y-$_click(y))/$h}]
1039                }]} {
1040                    return
1041                }
1042                if { $dx == 0 && $dy == 0 } {
1043                    return
1044                }
1045                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1046                EventuallyRotate $q
1047                set _click(x) $x
1048                set _click(y) $y
1049            }
1050        }
1051        "release" {
1052            Rotate drag $x $y
1053            $itk_component(view) configure -cursor ""
1054            catch {unset _click}
1055        }
1056        default {
1057            error "bad option \"$option\": should be click, drag, release"
1058        }
1059    }
1060}
1061
1062itcl::body Rappture::VtkMeshViewer::Pick {x y} {
1063    foreach tag [CurrentDatasets -visible] {
1064        SendCmd "dataset getscalar pixel $x $y $tag"
1065    }
1066}
1067
1068# ----------------------------------------------------------------------
1069# USAGE: $this Pan click x y
1070#        $this Pan drag x y
1071#        $this Pan release x y
1072#
1073# Called automatically when the user clicks on one of the zoom
1074# controls for this widget.  Changes the zoom for the current view.
1075# ----------------------------------------------------------------------
1076itcl::body Rappture::VtkMeshViewer::Pan {option x y} {
1077    switch -- $option {
1078        "set" {
1079            set w [winfo width $itk_component(view)]
1080            set h [winfo height $itk_component(view)]
1081            set x [expr $x / double($w)]
1082            set y [expr $y / double($h)]
1083            set _view(xpan) [expr $_view(xpan) + $x]
1084            set _view(ypan) [expr $_view(ypan) + $y]
1085            PanCamera
1086            return
1087        }
1088        "click" {
1089            set _click(x) $x
1090            set _click(y) $y
1091            $itk_component(view) configure -cursor hand1
1092        }
1093        "drag" {
1094            if { ![info exists _click(x)] } {
1095                set _click(x) $x
1096            }
1097            if { ![info exists _click(y)] } {
1098                set _click(y) $y
1099            }
1100            set w [winfo width $itk_component(view)]
1101            set h [winfo height $itk_component(view)]
1102            set dx [expr ($_click(x) - $x)/double($w)]
1103            set dy [expr ($_click(y) - $y)/double($h)]
1104            set _click(x) $x
1105            set _click(y) $y
1106            set _view(xpan) [expr $_view(xpan) - $dx]
1107            set _view(ypan) [expr $_view(ypan) - $dy]
1108            PanCamera
1109        }
1110        "release" {
1111            Pan drag $x $y
1112            $itk_component(view) configure -cursor ""
1113        }
1114        default {
1115            error "unknown option \"$option\": should set, click, drag, or release"
1116        }
1117    }
1118}
1119
1120# ----------------------------------------------------------------------
1121# USAGE: InitSettings <what> ?<value>?
1122#
1123# Used internally to update rendering settings whenever parameters
1124# change in the popup settings panel.  Sends the new settings off
1125# to the back end.
1126# ----------------------------------------------------------------------
1127itcl::body Rappture::VtkMeshViewer::InitSettings { args } {
1128    foreach setting $args {
1129        AdjustSetting $setting
1130    }
1131}
1132
1133#
1134# AdjustSetting --
1135#
1136#       Changes/updates a specific setting in the widget.  There are
1137#       usually user-setable option.  Commands are sent to the render
1138#       server.
1139#
1140itcl::body Rappture::VtkMeshViewer::AdjustSetting {what {value ""}} {
1141    if { ![isconnected] } {
1142        return
1143    }
1144    switch -- $what {
1145        "-outline" {
1146            set bool $_settings($what)
1147            # Only display a outline for the currently visible sets.
1148            SendCmd "outline visible 0"
1149            foreach dataset [CurrentDatasets -visible] {
1150                SendCmd "outline visible $bool $dataset"
1151            }
1152        }
1153        "-polydataopacity" {
1154            set _settings($what) [expr $_widget($what) * 0.01]
1155            EventuallySetPolydataOpacity
1156        }
1157        "-polydatawireframe" {
1158            set bool $_settings($what)
1159            SendCmd "polydata wireframe $bool"
1160        }
1161        "-polydatavisible" {
1162            set bool $_settings($what)
1163            # Only change visibility of data sets marked "visible".
1164            foreach dataset [CurrentDatasets -visible] {
1165                SendCmd "polydata visible $bool $dataset"
1166            }
1167        }
1168        "-polydatalighting" {
1169            set bool $_settings($what)
1170            SendCmd "polydata lighting $bool"
1171        }
1172        "-polydataedges" {
1173            set bool $_settings($what)
1174            SendCmd "polydata edges $bool"
1175        }
1176        "-axesvisible" {
1177            set bool $_settings($what)
1178            SendCmd "axis visible all $bool"
1179        }
1180        "-axislabels" {
1181            set bool $_settings($what)
1182            SendCmd "axis labels all $bool"
1183        }
1184        "-axisminorticks" {
1185            set bool $_settings($what)
1186            SendCmd "axis minticks all $bool"
1187        }
1188        "-xgrid" {
1189            set bool $_settings($what)
1190            SendCmd "axis grid x $bool"
1191        }
1192        "-ygrid" {
1193            set bool $_settings($what)
1194            SendCmd "axis grid y $bool"
1195        }
1196        "-zgrid" {
1197            set bool $_settings($what)
1198            SendCmd "axis grid z $bool"
1199        }
1200        "-axismode" {
1201            set mode [$itk_component(axismode) value]
1202            set mode [$itk_component(axismode) translate $mode]
1203            SendCmd "axis flymode $mode"
1204        }
1205        default {
1206            error "don't know how to fix $what"
1207        }
1208    }
1209}
1210
1211# ----------------------------------------------------------------------
1212# CONFIGURATION OPTION: -plotbackground
1213# ----------------------------------------------------------------------
1214itcl::configbody Rappture::VtkMeshViewer::plotbackground {
1215    if { [isconnected] } {
1216        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1217        SendCmd "screen bgcolor $r $g $b"
1218    }
1219}
1220
1221# ----------------------------------------------------------------------
1222# CONFIGURATION OPTION: -plotforeground
1223# ----------------------------------------------------------------------
1224itcl::configbody Rappture::VtkMeshViewer::plotforeground {
1225    if { [isconnected] } {
1226        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1227        #fix this!
1228        #SendCmd "color background $r $g $b"
1229    }
1230}
1231
1232itcl::body Rappture::VtkMeshViewer::limits { dataobj } {
1233    set tag $dataobj
1234    if { ![info exists _limits($tag)] } {
1235        set data [$dataobj vtkdata -full]
1236        if { $data == "" } {
1237            continue
1238        }
1239        set tmpfile file[pid].vtk
1240        set f [open "$tmpfile" "w"]
1241        fconfigure $f -translation binary -encoding binary
1242        puts $f $data
1243        close $f
1244        set reader [vtkDataSetReader $tag-xvtkDataSetReader]
1245        $reader SetFileName $tmpfile
1246        $reader Update
1247        file delete $tmpfile
1248        set output [$reader GetOutput]
1249        set _limits($tag) [$output GetBounds]
1250        rename $output ""
1251        rename $reader ""
1252    }
1253    foreach { xMin xMax yMin yMax zMin zMax} $_limits($tag) break
1254    if {![info exists limits(xmin)] || $limits(xmin) > $xMin} {
1255        set limits(xmin) $xMin
1256    }
1257    if {![info exists limits(xmax)] || $limits(xmax) < $xMax} {
1258        set limits(xmax) $xMax
1259    }
1260    if {![info exists limits(ymin)] || $limits(ymin) > $yMin} {
1261        set limits(ymin) $xMin
1262    }
1263    if {![info exists limits(ymax)] || $limits(ymax) < $yMax} {
1264        set limits(ymax) $yMax
1265    }
1266    if {![info exists limits(zmin)] || $limits(zmin) > $zMin} {
1267        set limits(zmin) $zMin
1268    }
1269    if {![info exists limits(zmax)] || $limits(zmax) < $zMax} {
1270        set limits(zmax) $zMax
1271    }
1272
1273    return [array get limits]
1274}
1275
1276itcl::body Rappture::VtkMeshViewer::BuildPolydataTab {} {
1277
1278    set fg [option get $itk_component(hull) font Font]
1279    #set bfg [option get $itk_component(hull) boldFont Font]
1280
1281    set inner [$itk_component(main) insert end \
1282        -title "Mesh Settings" \
1283        -icon [Rappture::icon mesh]]
1284    $inner configure -borderwidth 4
1285
1286    checkbutton $inner.mesh \
1287        -text "Show Mesh" \
1288        -variable [itcl::scope _settings(-polydatavisible)] \
1289        -command [itcl::code $this AdjustSetting -polydatavisible] \
1290        -font "Arial 9" -anchor w
1291
1292    checkbutton $inner.outline \
1293        -text "Show Outline" \
1294        -variable [itcl::scope _settings(-outline)] \
1295        -command [itcl::code $this AdjustSetting -outline] \
1296        -font "Arial 9" -anchor w
1297
1298    checkbutton $inner.wireframe \
1299        -text "Show Wireframe" \
1300        -variable [itcl::scope _settings(-polydatawireframe)] \
1301        -command [itcl::code $this AdjustSetting -polydatawireframe] \
1302        -font "Arial 9" -anchor w
1303
1304    checkbutton $inner.lighting \
1305        -text "Enable Lighting" \
1306        -variable [itcl::scope _settings(-polydatalighting)] \
1307        -command [itcl::code $this AdjustSetting -polydatalighting] \
1308        -font "Arial 9" -anchor w
1309
1310    checkbutton $inner.edges \
1311        -text "Show Edges" \
1312        -variable [itcl::scope _settings(-polydataedges)] \
1313        -command [itcl::code $this AdjustSetting -polydataedges] \
1314        -font "Arial 9" -anchor w
1315
1316    itk_component add field_l {
1317        label $inner.field_l -text "Field" -font "Arial 9"
1318    } {
1319        ignore -font
1320    }
1321    itk_component add field {
1322        Rappture::Combobox $inner.field -width 10 -editable no
1323    }
1324    bind $inner.field <<Value>> \
1325        [itcl::code $this AdjustSetting -field]
1326
1327    label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w
1328    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1329        -variable [itcl::scope _widget(-polydataopacity)] \
1330        -width 10 \
1331        -showvalue off \
1332        -command [itcl::code $this AdjustSetting -polydataopacity]
1333    $inner.opacity set [expr $_settings(-polydataopacity) * 100.0]
1334
1335    blt::table $inner \
1336        0,0 $inner.mesh      -cspan 2  -anchor w -pady 2 \
1337        1,0 $inner.outline   -cspan 2  -anchor w -pady 2 \
1338        2,0 $inner.wireframe -cspan 2  -anchor w -pady 2 \
1339        3,0 $inner.lighting  -cspan 2  -anchor w -pady 2 \
1340        4,0 $inner.edges     -cspan 2  -anchor w -pady 2 \
1341        5,0 $inner.opacity_l -anchor w -pady 2 \
1342        5,1 $inner.opacity   -fill x   -pady 2
1343
1344    blt::table configure $inner r* c* -resize none
1345    blt::table configure $inner r7 c1 -resize expand
1346}
1347
1348itcl::body Rappture::VtkMeshViewer::BuildAxisTab {} {
1349
1350    set fg [option get $itk_component(hull) font Font]
1351    #set bfg [option get $itk_component(hull) boldFont Font]
1352
1353    set inner [$itk_component(main) insert end \
1354        -title "Axis Settings" \
1355        -icon [Rappture::icon axis2]]
1356    $inner configure -borderwidth 4
1357
1358    checkbutton $inner.visible \
1359        -text "Axes" \
1360        -variable [itcl::scope _settings(-axesvisible)] \
1361        -command [itcl::code $this AdjustSetting -axesvisible] \
1362        -font "Arial 9"
1363
1364    checkbutton $inner.labels \
1365        -text "Axis Labels" \
1366        -variable [itcl::scope _settings(-axislabels)] \
1367        -command [itcl::code $this AdjustSetting -axislabels] \
1368        -font "Arial 9"
1369    label $inner.grid_l -text "Grid" -font "Arial 9"
1370    checkbutton $inner.xgrid \
1371        -text "X" \
1372        -variable [itcl::scope _settings(-xgrid)] \
1373        -command [itcl::code $this AdjustSetting -xgrid] \
1374        -font "Arial 9"
1375    checkbutton $inner.ygrid \
1376        -text "Y" \
1377        -variable [itcl::scope _settings(-ygrid)] \
1378        -command [itcl::code $this AdjustSetting -ygrid] \
1379        -font "Arial 9"
1380    checkbutton $inner.zgrid \
1381        -text "Z" \
1382        -variable [itcl::scope _settings(-zgrid)] \
1383        -command [itcl::code $this AdjustSetting -zgrid] \
1384        -font "Arial 9"
1385    checkbutton $inner.minorticks \
1386        -text "Minor Ticks" \
1387        -variable [itcl::scope _settings(-axisminorticks)] \
1388        -command [itcl::code $this AdjustSetting -axisminorticks] \
1389        -font "Arial 9"
1390
1391    label $inner.mode_l -text "Mode" -font "Arial 9"
1392
1393    itk_component add axismode {
1394        Rappture::Combobox $inner.mode -width 10 -editable no
1395    }
1396    $inner.mode choices insert end \
1397        "static_triad"    "static" \
1398        "closest_triad"   "closest" \
1399        "furthest_triad"  "farthest" \
1400        "outer_edges"     "outer"         
1401    $itk_component(axismode) value "static"
1402    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting -axismode]
1403
1404    blt::table $inner \
1405        0,0 $inner.visible -anchor w -cspan 4 \
1406        1,0 $inner.labels  -anchor w -cspan 4 \
1407        2,0 $inner.minorticks  -anchor w -cspan 4 \
1408        4,0 $inner.grid_l  -anchor w \
1409        4,1 $inner.xgrid   -anchor w \
1410        4,2 $inner.ygrid   -anchor w \
1411        4,3 $inner.zgrid   -anchor w \
1412        5,0 $inner.mode_l  -anchor w -padx { 2 0 } \
1413        5,1 $inner.mode    -fill x   -cspan 3
1414
1415    blt::table configure $inner r* c* -resize none
1416    blt::table configure $inner r7 c6 -resize expand
1417    blt::table configure $inner r3 -height 0.125i
1418}
1419
1420itcl::body Rappture::VtkMeshViewer::BuildCameraTab {} {
1421    set inner [$itk_component(main) insert end \
1422        -title "Camera Settings" \
1423        -icon [Rappture::icon camera]]
1424    $inner configure -borderwidth 4
1425
1426    label $inner.view_l -text "view" -font "Arial 9"
1427    set f [frame $inner.view]
1428    foreach side { front back left right top bottom } {
1429        button $f.$side  -image [Rappture::icon view$side] \
1430            -command [itcl::code $this SetOrientation $side]
1431        Rappture::Tooltip::for $f.$side "Change the view to $side"
1432        pack $f.$side -side left
1433    }
1434
1435    blt::table $inner \
1436        0,0 $inner.view_l -anchor e -pady 2 \
1437        0,1 $inner.view -anchor w -pady 2
1438
1439    set labels { qx qy qz qw xpan ypan zoom }
1440    set row 1
1441    foreach tag $labels {
1442        label $inner.${tag}label -text $tag -font "Arial 9"
1443        entry $inner.${tag} -font "Arial 9"  -bg white \
1444            -textvariable [itcl::scope _view($tag)]
1445        bind $inner.${tag} <Return> \
1446            [itcl::code $this camera set ${tag}]
1447        bind $inner.${tag} <KP_Enter> \
1448            [itcl::code $this camera set ${tag}]
1449        blt::table $inner \
1450            $row,0 $inner.${tag}label -anchor e -pady 2 \
1451            $row,1 $inner.${tag} -anchor w -pady 2
1452        blt::table configure $inner r$row -resize none
1453        incr row
1454    }
1455    checkbutton $inner.ortho \
1456        -text "Orthographic Projection" \
1457        -variable [itcl::scope _view(ortho)] \
1458        -command [itcl::code $this camera set ortho] \
1459        -font "Arial 9"
1460    blt::table $inner \
1461            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1462    blt::table configure $inner r$row -resize none
1463    incr row
1464
1465    blt::table configure $inner c* r* -resize none
1466    blt::table configure $inner c2 -resize expand
1467    blt::table configure $inner r$row -resize expand
1468}
1469
1470#
1471#  camera --
1472#
1473itcl::body Rappture::VtkMeshViewer::camera {option args} {
1474    switch -- $option {
1475        "show" {
1476            puts [array get _view]
1477        }
1478        "set" {
1479            set who [lindex $args 0]
1480            set x $_view($who)
1481            set code [catch { string is double $x } result]
1482            if { $code != 0 || !$result } {
1483                return
1484            }
1485            switch -- $who {
1486                "ortho" {
1487                    if {$_view(ortho)} {
1488                        SendCmd "camera mode ortho"
1489                    } else {
1490                        SendCmd "camera mode persp"
1491                    }
1492                }
1493                "xpan" - "ypan" {
1494                    PanCamera
1495                }
1496                "qx" - "qy" - "qz" - "qw" {
1497                    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1498                    $_arcball quaternion $q
1499                    EventuallyRotate $q
1500                }
1501                "zoom" {
1502                    SendCmd "camera zoom $_view(zoom)"
1503                }
1504            }
1505        }
1506    }
1507}
1508
1509itcl::body Rappture::VtkMeshViewer::GetVtkData { args } {
1510    set bytes ""
1511    foreach dataobj [get] {
1512        set contents [$dataobj vtkdata -full]
1513        append bytes "$contents\n"
1514    }
1515    return [list .vtk $bytes]
1516}
1517
1518itcl::body Rappture::VtkMeshViewer::GetImage { args } {
1519    if { [image width $_image(download)] > 0 &&
1520         [image height $_image(download)] > 0 } {
1521        set bytes [$_image(download) data -format "jpeg -quality 100"]
1522        set bytes [Rappture::encoding::decode -as b64 $bytes]
1523        return [list .jpg $bytes]
1524    }
1525    return ""
1526}
1527
1528itcl::body Rappture::VtkMeshViewer::BuildDownloadPopup { popup command } {
1529    Rappture::Balloon $popup \
1530        -title "[Rappture::filexfer::label downloadWord] as..."
1531    set inner [$popup component inner]
1532    label $inner.summary -text "" -anchor w
1533    radiobutton $inner.vtk_button -text "VTK data file" \
1534        -variable [itcl::scope _downloadPopup(format)] \
1535        -font "Helvetica 9 " \
1536        -value vtk 
1537    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
1538    radiobutton $inner.image_button -text "Image File" \
1539        -variable [itcl::scope _downloadPopup(format)] \
1540        -value image
1541    Rappture::Tooltip::for $inner.image_button \
1542        "Save as digital image."
1543
1544    button $inner.ok -text "Save" \
1545        -highlightthickness 0 -pady 2 -padx 3 \
1546        -command $command \
1547        -compound left \
1548        -image [Rappture::icon download]
1549
1550    button $inner.cancel -text "Cancel" \
1551        -highlightthickness 0 -pady 2 -padx 3 \
1552        -command [list $popup deactivate] \
1553        -compound left \
1554        -image [Rappture::icon cancel]
1555
1556    blt::table $inner \
1557        0,0 $inner.summary -cspan 2  \
1558        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
1559        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
1560        4,1 $inner.cancel -width .9i -fill y \
1561        4,0 $inner.ok -padx 2 -width .9i -fill y
1562    blt::table configure $inner r3 -height 4
1563    blt::table configure $inner r4 -pady 4
1564    raise $inner.image_button
1565    $inner.vtk_button invoke
1566    return $inner
1567}
1568
1569itcl::body Rappture::VtkMeshViewer::SetObjectStyle { dataobj } {
1570    # Parse style string.
1571    set tag $dataobj
1572    set type [$dataobj type]
1573
1574    array set style {
1575        -cloudstyle mesh
1576        -color white
1577        -edgecolor black
1578        -edges 1
1579        -lighting 1
1580        -linewidth 1.0
1581        -opacity 1.0
1582        -outline 0
1583        -visible 1
1584        -wireframe 0
1585    }
1586    if { $dataobj != $_first } {
1587        set style(-wireframe) 1
1588    }
1589    if {$type == "cloud"} {
1590        set style(-cloudstyle) points
1591        set style(-edges) 0
1592        set style(-edgecolor) white
1593    }
1594    array set style [$dataobj hints style]
1595
1596    if {[$dataobj hints color] != ""} {
1597        set style(-color) [$dataobj hints color]
1598    }
1599    SendCmd "outline add $tag"
1600    SendCmd "outline color [Color2RGB $style(-color)] $tag"
1601    SendCmd "outline visible $style(-outline) $tag"
1602    set _settings(-outline) $style(-outline)
1603
1604    SendCmd "polydata add $tag"
1605    SendCmd "polydata visible $style(-visible) $tag"
1606    set _settings(-polydatavisible) $style(-visible)
1607    SendCmd "polydata cloudstyle $style(-cloudstyle) $tag"
1608    SendCmd "polydata edges $style(-edges) $tag"
1609    set _settings(-polydataedges) $style(-edges)
1610    SendCmd "polydata color [Color2RGB $style(-color)] $tag"
1611    SendCmd "polydata lighting $style(-lighting) $tag"
1612    set _settings(-polydatalighting) $style(-lighting)
1613    SendCmd "polydata linecolor [Color2RGB $style(-edgecolor)] $tag"
1614    SendCmd "polydata linewidth $style(-linewidth) $tag"
1615    SendCmd "polydata opacity $style(-opacity) $tag"
1616    set _settings(-polydataopacity) $style(-opacity)
1617    set _widget(-polydataopacity) [expr 100.0 * $style(-opacity)]
1618    SendCmd "polydata wireframe $style(-wireframe) $tag"
1619    set _settings(-polydatawireframe) $style(-wireframe)
1620    set havePolyData 1
1621}
1622
1623itcl::body Rappture::VtkMeshViewer::IsValidObject { dataobj } {
1624    if {[catch {$dataobj isa Rappture::Mesh} valid] != 0 || !$valid} {
1625        return 0
1626    }
1627    return 1
1628}
1629
1630itcl::body Rappture::VtkMeshViewer::SetOrientation { side } {
1631    array set positions {
1632        front "1 0 0 0"
1633        back  "0 0 1 0"
1634        left  "0.707107 0 -0.707107 0"
1635        right "0.707107 0 0.707107 0"
1636        top   "0.707107 -0.707107 0 0"
1637        bottom "0.707107 0.707107 0 0"
1638    }
1639    foreach name { qw qx qy qz } value $positions($side) {
1640        set _view($name) $value
1641    }
1642    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1643    $_arcball quaternion $q
1644    SendCmd "camera orient $q"
1645    SendCmd "camera reset"
1646    set _view(xpan) 0
1647    set _view(ypan) 0
1648    set _view(zoom) 1.0
1649}
Note: See TracBrowser for help on using the repository browser.