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

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

Enable the wait animation in all nanovis and vtkvis viewers

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