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

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

merge r5187 from 1.4 branch

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