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

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

Axis fixes, removed currently unused clip-plane code

File size: 54.6 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: vtkmeshviewer - Vtk mesh viewer
4#
5#  It connects to the Vtk server running on a rendering farm,
6#  transmits data, and displays the results.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2014  HUBzero Foundation, LLC
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15package require BLT
16#package require Img
17
18option add *VtkMeshViewer.width 4i widgetDefault
19option add *VtkMeshViewer*cursor crosshair widgetDefault
20option add *VtkMeshViewer.height 4i widgetDefault
21option add *VtkMeshViewer.foreground black widgetDefault
22option add *VtkMeshViewer.controlBackground gray widgetDefault
23option add *VtkMeshViewer.controlDarkBackground #999999 widgetDefault
24option add *VtkMeshViewer.plotBackground black widgetDefault
25option add *VtkMeshViewer.plotForeground white widgetDefault
26option add *VtkMeshViewer.font \
27    -*-helvetica-medium-r-normal-*-12-* widgetDefault
28
29# must use this name -- plugs into Rappture::resources::load
30proc VtkMeshViewer_init_resources {} {
31    Rappture::resources::register \
32        vtkvis_server Rappture::VtkMeshViewer::SetServerList
33}
34
35itcl::class Rappture::VtkMeshViewer {
36    inherit Rappture::VisViewer
37
38    itk_option define -plotforeground plotForeground Foreground ""
39    itk_option define -plotbackground plotBackground Background ""
40
41    constructor { hostlist args } {
42        Rappture::VisViewer::constructor $hostlist
43    } {
44        # defined below
45    }
46    destructor {
47        # defined below
48    }
49    public proc SetServerList { namelist } {
50        Rappture::VisViewer::SetServerList "vtkvis" $namelist
51    }
52    public method add {dataobj {settings ""}}
53    public method camera {option args}
54    public method delete {args}
55    public method disconnect {}
56    public method download {option args}
57    public method get {args}
58    public method isconnected {}
59    public method limits { colormap }
60    public method parameters {title args} {
61        # do nothing
62    }
63    public method scale {args}
64
65    protected method Connect {}
66    protected method CurrentDatasets {args}
67    protected method Disconnect {}
68    protected method DoResize {}
69    protected method DoRotate {}
70    protected method AdjustSetting {what {value ""}}
71    protected method InitSettings { args  }
72    protected method Pan {option x y}
73    protected method Pick {x y}
74    protected method Rebuild {}
75    protected method ReceiveDataset { args }
76    protected method ReceiveImage { args }
77    protected method Rotate {option x y}
78    protected method Zoom {option}
79
80    # The following methods are only used by this class.
81    private method BuildAxisTab {}
82    private method BuildCameraTab {}
83    private method BuildCutawayTab {}
84    private method BuildDownloadPopup { widget command }
85    private method BuildPolydataTab {}
86    private method EventuallyResize { w h }
87    private method EventuallyRotate { q }
88    private method EventuallySetPolydataOpacity {}
89    private method GetImage { args }
90    private method GetVtkData { args }
91    private method IsValidObject { dataobj }
92    private method PanCamera {}
93    private method SetObjectStyle { dataobj }
94    private method SetOrientation { side }
95    private method SetPolydataOpacity {}
96
97    private variable _arcball ""
98    private variable _dlist "";         # list of data objects
99    private variable _obj2datasets
100    private variable _obj2ovride;       # maps dataobj => style override
101    private variable _datasets;         # contains all the dataobj-component
102                                        # datasets in the server
103    private variable _colormaps;        # contains all the colormaps
104                                        # in the server.
105    private variable _dataset2style;    # maps dataobj-component to transfunc
106    private variable _style2datasets;   # maps tf back to list of
107                                        # dataobj-components using the tf.
108    private variable _click;            # info used for rotate operations
109    private variable _limits;           # autoscale min/max for all axes
110    private variable _view;             # view params for 3D view
111    private variable _settings
112    private variable _widget
113    private variable _style;            # Array of current component styles.
114    private variable _initialStyle;     # Array of initial component styles.
115    private variable _reset 1;          # Indicates that server was reset and
116                                        # needs to be reinitialized.
117
118    private variable _first "";         # This is the topmost dataset.
119    private variable _start 0
120    private variable _title ""
121
122    common _downloadPopup;              # download options from popup
123    private common _hardcopy
124    private variable _width 0
125    private variable _height 0
126    private variable _resizePending 0
127    private variable _rotatePending 0
128    private variable _polydataOpacityPending 0
129    private variable _rotateDelay 150
130    private variable _opacityDelay 150
131}
132
133itk::usual VtkMeshViewer {
134    keep -background -foreground -cursor -font
135    keep -plotbackground -plotforeground
136}
137
138# ----------------------------------------------------------------------
139# CONSTRUCTOR
140# ----------------------------------------------------------------------
141itcl::body Rappture::VtkMeshViewer::constructor {hostlist args} {
142    package require vtk
143    set _serverType "vtkvis"
144
145    # Rebuild event
146    $_dispatcher register !rebuild
147    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
148
149    # Resize event
150    $_dispatcher register !resize
151    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
152
153    # Rotate event
154    $_dispatcher register !rotate
155    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
156
157    # Polydata opacity event
158    $_dispatcher register !polydataOpacity
159    $_dispatcher dispatch $this !polydataOpacity \
160        "[itcl::code $this SetPolydataOpacity]; list"
161    #
162    # Populate parser with commands handle incoming requests
163    #
164    $_parser alias image    [itcl::code $this ReceiveImage]
165    $_parser alias dataset  [itcl::code $this ReceiveDataset]
166
167    # Initialize the view to some default parameters.
168    array set _view {
169        qw              0.853553
170        qx              -0.353553
171        qy              0.353553
172        qz              0.146447
173        zoom            1.0
174        xpan            0
175        ypan            0
176        ortho           0
177    }
178    set _arcball [blt::arcball create 100 100]
179    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
180    $_arcball quaternion $q
181
182    set _limits(zmin) 0.0
183    set _limits(zmax) 1.0
184
185    array set _settings {
186        -axesvisible            1
187        -axislabels             1
188        -axisminorticks         1
189        -outline                0
190        -polydataedges          0
191        -polydatalighting       1
192        -polydataopacity        1.0
193        -polydatavisible        1
194        -polydatawireframe      0
195        -xgrid                  0
196        -ygrid                  0
197        -zgrid                  0
198    }
199    array set _widget {
200        -polydataopacity        100
201    }       
202    itk_component add view {
203        canvas $itk_component(plotarea).view \
204            -highlightthickness 0 -borderwidth 0
205    } {
206        usual
207        ignore -highlightthickness -borderwidth  -background
208    }
209
210    itk_component add fieldmenu {
211        menu $itk_component(plotarea).menu -bg black -fg white -relief flat \
212            -tearoff no
213    } {
214        usual
215        ignore -background -foreground -relief -tearoff
216    }
217
218    set c $itk_component(view)
219    bind $c <Configure> [itcl::code $this EventuallyResize %w %h]
220    bind $c <4> [itcl::code $this Zoom in 0.25]
221    bind $c <5> [itcl::code $this Zoom out 0.25]
222    bind $c <KeyPress-Left>  [list %W xview scroll 10 units]
223    bind $c <KeyPress-Right> [list %W xview scroll -10 units]
224    bind $c <KeyPress-Up>    [list %W yview scroll 10 units]
225    bind $c <KeyPress-Down>  [list %W yview scroll -10 units]
226    bind $c <Enter> "focus %W"
227    bind $c <Control-F1> [itcl::code $this ToggleConsole]
228
229    # Fix the scrollregion in case we go off screen
230    $c configure -scrollregion [$c bbox all]
231
232    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
233    set _map(cwidth) -1
234    set _map(cheight) -1
235    set _map(zoom) 1.0
236    set _map(original) ""
237
238    set f [$itk_component(main) component controls]
239    itk_component add reset {
240        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
241            -highlightthickness 0 \
242            -image [Rappture::icon reset-view] \
243            -command [itcl::code $this Zoom reset]
244    } {
245        usual
246        ignore -highlightthickness
247    }
248    pack $itk_component(reset) -side top -padx 2 -pady 2
249    Rappture::Tooltip::for $itk_component(reset) \
250        "Reset the view to the default zoom level"
251
252    itk_component add zoomin {
253        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
254            -highlightthickness 0 \
255            -image [Rappture::icon zoom-in] \
256            -command [itcl::code $this Zoom in]
257    } {
258        usual
259        ignore -highlightthickness
260    }
261    pack $itk_component(zoomin) -side top -padx 2 -pady 2
262    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
263
264    itk_component add zoomout {
265        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
266            -highlightthickness 0 \
267            -image [Rappture::icon zoom-out] \
268            -command [itcl::code $this Zoom out]
269    } {
270        usual
271        ignore -highlightthickness
272    }
273    pack $itk_component(zoomout) -side top -padx 2 -pady 2
274    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
275
276    BuildPolydataTab
277    BuildAxisTab
278    BuildCameraTab
279
280    # Hack around the Tk panewindow.  The problem is that the requested
281    # size of the 3d view isn't set until an image is retrieved from
282    # the server.  So the panewindow uses the tiny size.
283    set w 10000
284    pack forget $itk_component(view)
285    blt::table $itk_component(plotarea) \
286        0,0 $itk_component(view) -fill both -reqwidth $w
287    blt::table configure $itk_component(plotarea) c1 -resize none
288
289    # Bindings for rotation via mouse
290    bind $itk_component(view) <ButtonPress-1> \
291        [itcl::code $this Rotate click %x %y]
292    bind $itk_component(view) <B1-Motion> \
293        [itcl::code $this Rotate drag %x %y]
294    bind $itk_component(view) <ButtonRelease-1> \
295        [itcl::code $this Rotate release %x %y]
296    bind $itk_component(view) <Configure> \
297        [itcl::code $this EventuallyResize %w %h]
298
299    # Bindings for panning via mouse
300    bind $itk_component(view) <ButtonPress-2> \
301        [itcl::code $this Pan click %x %y]
302    bind $itk_component(view) <B2-Motion> \
303        [itcl::code $this Pan drag %x %y]
304    bind $itk_component(view) <ButtonRelease-2> \
305        [itcl::code $this Pan release %x %y]
306
307    #bind $itk_component(view) <ButtonRelease-3> \
308    #    [itcl::code $this Pick %x %y]
309
310    # Bindings for panning via keyboard
311    bind $itk_component(view) <KeyPress-Left> \
312        [itcl::code $this Pan set -10 0]
313    bind $itk_component(view) <KeyPress-Right> \
314        [itcl::code $this Pan set 10 0]
315    bind $itk_component(view) <KeyPress-Up> \
316        [itcl::code $this Pan set 0 -10]
317    bind $itk_component(view) <KeyPress-Down> \
318        [itcl::code $this Pan set 0 10]
319    bind $itk_component(view) <Shift-KeyPress-Left> \
320        [itcl::code $this Pan set -2 0]
321    bind $itk_component(view) <Shift-KeyPress-Right> \
322        [itcl::code $this Pan set 2 0]
323    bind $itk_component(view) <Shift-KeyPress-Up> \
324        [itcl::code $this Pan set 0 -2]
325    bind $itk_component(view) <Shift-KeyPress-Down> \
326        [itcl::code $this Pan set 0 2]
327
328    # Bindings for zoom via keyboard
329    bind $itk_component(view) <KeyPress-Prior> \
330        [itcl::code $this Zoom out]
331    bind $itk_component(view) <KeyPress-Next> \
332        [itcl::code $this Zoom in]
333
334    bind $itk_component(view) <Enter> "focus $itk_component(view)"
335
336    if {[string equal "x11" [tk windowingsystem]]} {
337        # Bindings for zoom via mouse
338        bind $itk_component(view) <4> [itcl::code $this Zoom out]
339        bind $itk_component(view) <5> [itcl::code $this Zoom in]
340    }
341
342    set _image(download) [image create photo]
343
344    eval itk_initialize $args
345    Connect
346}
347
348# ----------------------------------------------------------------------
349# DESTRUCTOR
350# ----------------------------------------------------------------------
351itcl::body Rappture::VtkMeshViewer::destructor {} {
352    Disconnect
353    $_dispatcher cancel !rebuild
354    $_dispatcher cancel !resize
355    $_dispatcher cancel !rotate
356    image delete $_image(plot)
357    image delete $_image(download)
358    catch { blt::arcball destroy $_arcball }
359}
360
361itcl::body Rappture::VtkMeshViewer::DoResize {} {
362    if { $_width < 2 } {
363        set _width 500
364    }
365    if { $_height < 2 } {
366        set _height 500
367    }
368    set _start [clock clicks -milliseconds]
369    SendCmd "screen size $_width $_height"
370
371    set _resizePending 0
372}
373
374itcl::body Rappture::VtkMeshViewer::DoRotate {} {
375    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
376    SendCmd "camera orient $q"
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    foreach { _view(qw) _view(qx) _view(qy) _view(qz) } $q break
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    array unset _colormaps
718    global readyForNextFrame
719    set readyForNextFrame 1
720}
721
722# ----------------------------------------------------------------------
723# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
724#
725# Invoked automatically whenever the "image" command comes in from
726# the rendering server.  Indicates that binary image data with the
727# specified <size> will follow.
728# ----------------------------------------------------------------------
729itcl::body Rappture::VtkMeshViewer::ReceiveImage { args } {
730    global readyForNextFrame
731    set readyForNextFrame 1
732    array set info {
733        -token "???"
734        -bytes 0
735        -type image
736    }
737    array set info $args
738    set bytes [ReceiveBytes $info(-bytes)]
739    if { $info(-type) == "image" } {
740        if 0 {
741            set f [open "last.ppm" "w"]
742            fconfigure $f -encoding binary
743            puts -nonewline $f $bytes
744            close $f
745        }
746        $_image(plot) configure -data $bytes
747        set time [clock seconds]
748        set date [clock format $time]
749        if { $_start > 0 } {
750            set finish [clock clicks -milliseconds]
751            set _start 0
752        }
753    } elseif { $info(type) == "print" } {
754        set tag $this-print-$info(-token)
755        set _hardcopy($tag) $bytes
756    }
757}
758
759#
760# ReceiveDataset --
761#
762itcl::body Rappture::VtkMeshViewer::ReceiveDataset { args } {
763    if { ![isconnected] } {
764        return
765    }
766    set option [lindex $args 0]
767    switch -- $option {
768        "scalar" {
769            set option [lindex $args 1]
770            switch -- $option {
771                "world" {
772                    foreach { x y z value tag } [lrange $args 2 end] break
773                }
774                "pixel" {
775                    foreach { x y value tag } [lrange $args 2 end] break
776                }
777            }
778        }
779        "vector" {
780            set option [lindex $args 1]
781            switch -- $option {
782                "world" {
783                    foreach { x y z vx vy vz tag } [lrange $args 2 end] break
784                }
785                "pixel" {
786                    foreach { x y vx vy vz tag } [lrange $args 2 end] break
787                }
788            }
789        }
790        "names" {
791            foreach { name } [lindex $args 1] {
792                #puts stderr "Dataset: $name"
793            }
794        }
795        default {
796            error "unknown dataset option \"$option\" from server"
797        }
798    }
799}
800
801# ----------------------------------------------------------------------
802# USAGE: Rebuild
803#
804# Called automatically whenever something changes that affects the
805# data in the widget.  Clears any existing data and rebuilds the
806# widget to display new data.
807# ----------------------------------------------------------------------
808itcl::body Rappture::VtkMeshViewer::Rebuild {} {
809
810    set w [winfo width $itk_component(view)]
811    set h [winfo height $itk_component(view)]
812    if { $w < 2 || $h < 2 } {
813        $_dispatcher event -idle !rebuild
814        return
815    }
816
817    # Turn on buffering of commands to the server.  We don't want to
818    # be preempted by a server disconnect/reconnect (which automatically
819    # generates a new call to Rebuild).   
820    StartBufferingCommands
821
822    if { $_reset } {
823        set _width $w
824        set _height $h
825        $_arcball resize $w $h
826        DoResize
827        InitSettings -xgrid -ygrid -zgrid -axismode \
828            -axesvisible -axislabels -axisminorticks
829        StopBufferingCommands
830        SendCmd "imgflush"
831        StartBufferingCommands
832    }
833
834    set _limits(zmin) ""
835    set _limits(zmax) ""
836    set _first ""
837    SendCmd "dataset visible 0"
838    set count 0
839    foreach dataobj [get -objects] {
840        if { [info exists _obj2ovride($dataobj-raise)] &&  $_first == "" } {
841            set _first $dataobj
842        }
843        set _obj2datasets($dataobj) ""
844        set tag $dataobj
845        if { ![info exists _datasets($tag)] } {
846            set bytes [$dataobj vtkdata -full]
847            if { $bytes == "" } {
848                continue
849            }
850            set length [string length $bytes]
851            if { $_reportClientInfo }  {
852                set info {}
853                lappend info "tool_id"       [$dataobj hints toolId]
854                lappend info "tool_name"     [$dataobj hints toolName]
855                lappend info "tool_version"  [$dataobj hints toolRevision]
856                lappend info "tool_title"    [$dataobj hints toolTitle]
857                lappend info "dataset_label" [$dataobj hints label]
858                lappend info "dataset_size"  $length
859                lappend info "dataset_tag"   $tag
860                SendCmd [list "clientinfo" $info]
861            }
862            SendCmd "dataset add $tag data follows $length"
863            append _outbuf $bytes
864            set _datasets($tag) 1
865            SetObjectStyle $dataobj
866        }
867        lappend _obj2datasets($dataobj) $tag
868        if { [info exists _obj2ovride($dataobj-raise)] } {
869            SendCmd "dataset visible 1 $tag"
870            EventuallySetPolydataOpacity
871        }
872    }
873    if {"" != $_first} {
874        set location [$_first hints camera]
875        if { $location != "" } {
876            array set view $location
877        }
878
879        foreach axis { x y z } {
880            set label [$_first label ${axis}]
881            if { $label != "" } {
882                SendCmd [list axis name $axis $label]
883            }
884            set units [$_first units ${axis}]
885            if { $units != "" } {
886                SendCmd [list axis units $axis $units]
887            }
888        }
889    }
890    InitSettings -outline
891    if { $_reset } {
892        # These are settings that rely on a dataset being loaded.
893        InitSettings -polydataedges -polydatalighting -polydataopacity \
894            -polydatavisible -polydatawireframe
895 
896        #SendCmd "axis lformat all %g"
897
898        set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
899        $_arcball quaternion $q
900        SendCmd "camera reset"
901        if { $_view(ortho)} {
902            SendCmd "camera mode ortho"
903        } else {
904            SendCmd "camera mode persp"
905        }
906        DoRotate
907        PanCamera
908        Zoom reset
909    }
910
911    set _reset 0
912    global readyForNextFrame
913    set readyForNextFrame 0;            # Don't advance to the next frame
914                                        # until we get an image.
915
916    # Actually write the commands to the server socket.  If it fails, we don't
917    # care.  We're finished here.
918    blt::busy hold $itk_component(hull)
919    StopBufferingCommands
920    blt::busy release $itk_component(hull)
921}
922
923# ----------------------------------------------------------------------
924# USAGE: CurrentDatasets ?-all -visible? ?dataobjs?
925#
926# Returns a list of server IDs for the current datasets being displayed.  This
927# is normally a single ID, but it might be a list of IDs if the current data
928# object has multiple components.
929# ----------------------------------------------------------------------
930itcl::body Rappture::VtkMeshViewer::CurrentDatasets {args} {
931    set flag [lindex $args 0]
932    switch -- $flag {
933        "-all" {
934            if { [llength $args] > 1 } {
935                error "CurrentDatasets: can't specify dataobj after \"-all\""
936            }
937            set dlist [get -objects]
938        }
939        "-visible" {
940            if { [llength $args] > 1 } {
941                set dlist {}
942                set args [lrange $args 1 end]
943                foreach dataobj $args {
944                    if { [info exists _obj2ovride($dataobj-raise)] } {
945                        lappend dlist $dataobj
946                    }
947                }
948            } else {
949                set dlist [get -visible]
950            }
951        }           
952        default {
953            set dlist $args
954        }
955    }
956    set rlist ""
957    foreach tag $dlist {
958        if { [info exists _datasets($tag)] && $_datasets($tag) } {
959            lappend rlist $tag
960        }
961    }
962    return $rlist
963}
964
965# ----------------------------------------------------------------------
966# USAGE: Zoom in
967# USAGE: Zoom out
968# USAGE: Zoom reset
969#
970# Called automatically when the user clicks on one of the zoom
971# controls for this widget.  Changes the zoom for the current view.
972# ----------------------------------------------------------------------
973itcl::body Rappture::VtkMeshViewer::Zoom {option} {
974    switch -- $option {
975        "in" {
976            set _view(zoom) [expr {$_view(zoom)*1.25}]
977            SendCmd "camera zoom $_view(zoom)"
978        }
979        "out" {
980            set _view(zoom) [expr {$_view(zoom)*0.8}]
981            SendCmd "camera zoom $_view(zoom)"
982        }
983        "reset" {
984            array set _view {
985                qw      0.853553
986                qx      -0.353553
987                qy      0.353553
988                qz      0.146447
989                zoom    1.0
990                xpan    0
991                ypan    0
992            }
993            if { $_first != "" } {
994                set location [$_first hints camera]
995                if { $location != "" } {
996                    array set _view $location
997                }
998            }
999            set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1000            $_arcball quaternion $q
1001            DoRotate
1002            SendCmd "camera reset"
1003        }
1004    }
1005}
1006
1007itcl::body Rappture::VtkMeshViewer::PanCamera {} {
1008    set x $_view(xpan)
1009    set y $_view(ypan)
1010    SendCmd "camera pan $x $y"
1011}
1012
1013# ----------------------------------------------------------------------
1014# USAGE: Rotate click <x> <y>
1015# USAGE: Rotate drag <x> <y>
1016# USAGE: Rotate release <x> <y>
1017#
1018# Called automatically when the user clicks/drags/releases in the
1019# plot area.  Moves the plot according to the user's actions.
1020# ----------------------------------------------------------------------
1021itcl::body Rappture::VtkMeshViewer::Rotate {option x y} {
1022    switch -- $option {
1023        "click" {
1024            $itk_component(view) configure -cursor fleur
1025            set _click(x) $x
1026            set _click(y) $y
1027        }
1028        "drag" {
1029            if {[array size _click] == 0} {
1030                Rotate click $x $y
1031            } else {
1032                set w [winfo width $itk_component(view)]
1033                set h [winfo height $itk_component(view)]
1034                if {$w <= 0 || $h <= 0} {
1035                    return
1036                }
1037
1038                if {[catch {
1039                    # this fails sometimes for no apparent reason
1040                    set dx [expr {double($x-$_click(x))/$w}]
1041                    set dy [expr {double($y-$_click(y))/$h}]
1042                }]} {
1043                    return
1044                }
1045                if { $dx == 0 && $dy == 0 } {
1046                    return
1047                }
1048                set q [$_arcball rotate $x $y $_click(x) $_click(y)]
1049                EventuallyRotate $q
1050                set _click(x) $x
1051                set _click(y) $y
1052            }
1053        }
1054        "release" {
1055            Rotate drag $x $y
1056            $itk_component(view) configure -cursor ""
1057            catch {unset _click}
1058        }
1059        default {
1060            error "bad option \"$option\": should be click, drag, release"
1061        }
1062    }
1063}
1064
1065itcl::body Rappture::VtkMeshViewer::Pick {x y} {
1066    foreach tag [CurrentDatasets -visible] {
1067        SendCmd "dataset getscalar pixel $x $y $tag"
1068    }
1069}
1070
1071# ----------------------------------------------------------------------
1072# USAGE: $this Pan click x y
1073#        $this Pan drag x y
1074#        $this Pan release x y
1075#
1076# Called automatically when the user clicks on one of the zoom
1077# controls for this widget.  Changes the zoom for the current view.
1078# ----------------------------------------------------------------------
1079itcl::body Rappture::VtkMeshViewer::Pan {option x y} {
1080    switch -- $option {
1081        "set" {
1082            set w [winfo width $itk_component(view)]
1083            set h [winfo height $itk_component(view)]
1084            set x [expr $x / double($w)]
1085            set y [expr $y / double($h)]
1086            set _view(xpan) [expr $_view(xpan) + $x]
1087            set _view(ypan) [expr $_view(ypan) + $y]
1088            PanCamera
1089            return
1090        }
1091        "click" {
1092            set _click(x) $x
1093            set _click(y) $y
1094            $itk_component(view) configure -cursor hand1
1095        }
1096        "drag" {
1097            if { ![info exists _click(x)] } {
1098                set _click(x) $x
1099            }
1100            if { ![info exists _click(y)] } {
1101                set _click(y) $y
1102            }
1103            set w [winfo width $itk_component(view)]
1104            set h [winfo height $itk_component(view)]
1105            set dx [expr ($_click(x) - $x)/double($w)]
1106            set dy [expr ($_click(y) - $y)/double($h)]
1107            set _click(x) $x
1108            set _click(y) $y
1109            set _view(xpan) [expr $_view(xpan) - $dx]
1110            set _view(ypan) [expr $_view(ypan) - $dy]
1111            PanCamera
1112        }
1113        "release" {
1114            Pan drag $x $y
1115            $itk_component(view) configure -cursor ""
1116        }
1117        default {
1118            error "unknown option \"$option\": should set, click, drag, or release"
1119        }
1120    }
1121}
1122
1123# ----------------------------------------------------------------------
1124# USAGE: InitSettings <what> ?<value>?
1125#
1126# Used internally to update rendering settings whenever parameters
1127# change in the popup settings panel.  Sends the new settings off
1128# to the back end.
1129# ----------------------------------------------------------------------
1130itcl::body Rappture::VtkMeshViewer::InitSettings { args } {
1131    foreach setting $args {
1132        AdjustSetting $setting
1133    }
1134}
1135
1136#
1137# AdjustSetting --
1138#
1139#       Changes/updates a specific setting in the widget.  There are
1140#       usually user-setable option.  Commands are sent to the render
1141#       server.
1142#
1143itcl::body Rappture::VtkMeshViewer::AdjustSetting {what {value ""}} {
1144    if { ![isconnected] } {
1145        return
1146    }
1147    switch -- $what {
1148        "-outline" {
1149            set bool $_settings($what)
1150            # Only display a outline for the currently visible sets.
1151            SendCmd "outline visible 0"
1152            foreach dataset [CurrentDatasets -visible] {
1153                SendCmd "outline visible $bool $dataset"
1154            }
1155        }
1156        "-polydataopacity" {
1157            EventuallySetPolydataOpacity
1158        }
1159        "-polydatawireframe" {
1160            set bool $_settings($what)
1161            SendCmd "polydata wireframe $bool"
1162        }
1163        "-polydatavisible" {
1164            set bool $_settings($what)
1165            # Only change visibility of data sets marked "visible".
1166            foreach dataset [CurrentDatasets -visible] {
1167                SendCmd "polydata visible $bool $dataset"
1168            }
1169        }
1170        "-polydatalighting" {
1171            set bool $_settings($what)
1172            SendCmd "polydata lighting $bool"
1173        }
1174        "-polydataedges" {
1175            set bool $_settings($what)
1176            SendCmd "polydata edges $bool"
1177        }
1178        "-axesvisible" {
1179            set bool $_settings($what)
1180            SendCmd "axis visible all $bool"
1181        }
1182        "-axislabels" {
1183            set bool $_settings($what)
1184            SendCmd "axis labels all $bool"
1185        }
1186        "-axisminorticks" {
1187            set bool $_settings($what)
1188            SendCmd "axis minticks all $bool"
1189        }
1190        "-xgrid" {
1191            set bool $_settings($what)
1192            SendCmd "axis grid x $bool"
1193        }
1194        "-ygrid" {
1195            set bool $_settings($what)
1196            SendCmd "axis grid y $bool"
1197        }
1198        "-zgrid" {
1199            set bool $_settings($what)
1200            SendCmd "axis grid z $bool"
1201        }
1202        "-axismode" {
1203            set mode [$itk_component(axismode) value]
1204            set mode [$itk_component(axismode) translate $mode]
1205            SendCmd "axis flymode $mode"
1206        }
1207        default {
1208            error "don't know how to fix $what"
1209        }
1210    }
1211}
1212
1213# ----------------------------------------------------------------------
1214# CONFIGURATION OPTION: -plotbackground
1215# ----------------------------------------------------------------------
1216itcl::configbody Rappture::VtkMeshViewer::plotbackground {
1217    if { [isconnected] } {
1218        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1219        SendCmd "screen bgcolor $r $g $b"
1220    }
1221}
1222
1223# ----------------------------------------------------------------------
1224# CONFIGURATION OPTION: -plotforeground
1225# ----------------------------------------------------------------------
1226itcl::configbody Rappture::VtkMeshViewer::plotforeground {
1227    if { [isconnected] } {
1228        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1229        #fix this!
1230        #SendCmd "color background $r $g $b"
1231    }
1232}
1233
1234itcl::body Rappture::VtkMeshViewer::limits { dataobj } {
1235    set tag $dataobj
1236    if { ![info exists _limits($tag)] } {
1237        set data [$dataobj vtkdata -full]
1238        if { $data == "" } {
1239            continue
1240        }
1241        set tmpfile file[pid].vtk
1242        set f [open "$tmpfile" "w"]
1243        fconfigure $f -translation binary -encoding binary
1244        puts $f $data
1245        close $f
1246        set reader [vtkDataSetReader $tag-xvtkDataSetReader]
1247        $reader SetFileName $tmpfile
1248        $reader Update
1249        file delete $tmpfile
1250        set output [$reader GetOutput]
1251        set _limits($tag) [$output GetBounds]
1252        rename $output ""
1253        rename $reader ""
1254    }
1255    foreach { xMin xMax yMin yMax zMin zMax} $_limits($tag) break
1256    if {![info exists limits(xmin)] || $limits(xmin) > $xMin} {
1257        set limits(xmin) $xMin
1258    }
1259    if {![info exists limits(xmax)] || $limits(xmax) < $xMax} {
1260        set limits(xmax) $xMax
1261    }
1262    if {![info exists limits(ymin)] || $limits(ymin) > $yMin} {
1263        set limits(ymin) $xMin
1264    }
1265    if {![info exists limits(ymax)] || $limits(ymax) < $yMax} {
1266        set limits(ymax) $yMax
1267    }
1268    if {![info exists limits(zmin)] || $limits(zmin) > $zMin} {
1269        set limits(zmin) $zMin
1270    }
1271    if {![info exists limits(zmax)] || $limits(zmax) < $zMax} {
1272        set limits(zmax) $zMax
1273    }
1274
1275    return [array get limits]
1276}
1277
1278itcl::body Rappture::VtkMeshViewer::BuildPolydataTab {} {
1279
1280    set fg [option get $itk_component(hull) font Font]
1281    #set bfg [option get $itk_component(hull) boldFont Font]
1282
1283    set inner [$itk_component(main) insert end \
1284        -title "Mesh Settings" \
1285        -icon [Rappture::icon mesh]]
1286    $inner configure -borderwidth 4
1287
1288    checkbutton $inner.mesh \
1289        -text "Show Mesh" \
1290        -variable [itcl::scope _settings(-polydatavisible)] \
1291        -command [itcl::code $this AdjustSetting -polydatavisible] \
1292        -font "Arial 9" -anchor w
1293
1294    checkbutton $inner.outline \
1295        -text "Show Outline" \
1296        -variable [itcl::scope _settings(-outline)] \
1297        -command [itcl::code $this AdjustSetting -outline] \
1298        -font "Arial 9" -anchor w
1299
1300    checkbutton $inner.wireframe \
1301        -text "Show Wireframe" \
1302        -variable [itcl::scope _settings(-polydatawireframe)] \
1303        -command [itcl::code $this AdjustSetting -polydatawireframe] \
1304        -font "Arial 9" -anchor w
1305
1306    checkbutton $inner.lighting \
1307        -text "Enable Lighting" \
1308        -variable [itcl::scope _settings(-polydatalighting)] \
1309        -command [itcl::code $this AdjustSetting -polydatalighting] \
1310        -font "Arial 9" -anchor w
1311
1312    checkbutton $inner.edges \
1313        -text "Show Edges" \
1314        -variable [itcl::scope _settings(-polydataedges)] \
1315        -command [itcl::code $this AdjustSetting -polydataedges] \
1316        -font "Arial 9" -anchor w
1317
1318    itk_component add field_l {
1319        label $inner.field_l -text "Field" -font "Arial 9"
1320    } {
1321        ignore -font
1322    }
1323    itk_component add field {
1324        Rappture::Combobox $inner.field -width 10 -editable no
1325    }
1326    bind $inner.field <<Value>> \
1327        [itcl::code $this AdjustSetting -field]
1328
1329    label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w
1330    ::scale $inner.opacity -from 0 -to 100 -orient horizontal \
1331        -variable [itcl::scope _widget(-polydataopacity)] \
1332        -width 10 \
1333        -showvalue off \
1334        -command [itcl::code $this AdjustSetting -polydataopacity]
1335    $inner.opacity set [expr $_settings(-polydataopacity) * 100.0]
1336
1337    blt::table $inner \
1338        0,0 $inner.mesh      -cspan 2  -anchor w -pady 2 \
1339        1,0 $inner.outline   -cspan 2  -anchor w -pady 2 \
1340        2,0 $inner.wireframe -cspan 2  -anchor w -pady 2 \
1341        3,0 $inner.lighting  -cspan 2  -anchor w -pady 2 \
1342        4,0 $inner.edges     -cspan 2  -anchor w -pady 2 \
1343        5,0 $inner.opacity_l -anchor w -pady 2 \
1344        5,1 $inner.opacity   -fill x   -pady 2
1345
1346    blt::table configure $inner r* c* -resize none
1347    blt::table configure $inner r7 c1 -resize expand
1348}
1349
1350itcl::body Rappture::VtkMeshViewer::BuildAxisTab {} {
1351
1352    set fg [option get $itk_component(hull) font Font]
1353    #set bfg [option get $itk_component(hull) boldFont Font]
1354
1355    set inner [$itk_component(main) insert end \
1356        -title "Axis Settings" \
1357        -icon [Rappture::icon axis2]]
1358    $inner configure -borderwidth 4
1359
1360    checkbutton $inner.visible \
1361        -text "Axes" \
1362        -variable [itcl::scope _settings(-axesvisible)] \
1363        -command [itcl::code $this AdjustSetting -axesvisible] \
1364        -font "Arial 9"
1365
1366    checkbutton $inner.labels \
1367        -text "Axis Labels" \
1368        -variable [itcl::scope _settings(-axislabels)] \
1369        -command [itcl::code $this AdjustSetting -axislabels] \
1370        -font "Arial 9"
1371    label $inner.grid_l -text "Grid" -font "Arial 9"
1372    checkbutton $inner.xgrid \
1373        -text "X" \
1374        -variable [itcl::scope _settings(-xgrid)] \
1375        -command [itcl::code $this AdjustSetting -xgrid] \
1376        -font "Arial 9"
1377    checkbutton $inner.ygrid \
1378        -text "Y" \
1379        -variable [itcl::scope _settings(-ygrid)] \
1380        -command [itcl::code $this AdjustSetting -ygrid] \
1381        -font "Arial 9"
1382    checkbutton $inner.zgrid \
1383        -text "Z" \
1384        -variable [itcl::scope _settings(-zgrid)] \
1385        -command [itcl::code $this AdjustSetting -zgrid] \
1386        -font "Arial 9"
1387    checkbutton $inner.minorticks \
1388        -text "Minor Ticks" \
1389        -variable [itcl::scope _settings(-axisminorticks)] \
1390        -command [itcl::code $this AdjustSetting -axisminorticks] \
1391        -font "Arial 9"
1392
1393    label $inner.mode_l -text "Mode" -font "Arial 9"
1394
1395    itk_component add axismode {
1396        Rappture::Combobox $inner.mode -width 10 -editable no
1397    }
1398    $inner.mode choices insert end \
1399        "static_triad"    "static" \
1400        "closest_triad"   "closest" \
1401        "furthest_triad"  "farthest" \
1402        "outer_edges"     "outer"         
1403    $itk_component(axismode) value "static"
1404    bind $inner.mode <<Value>> [itcl::code $this AdjustSetting -axismode]
1405
1406    blt::table $inner \
1407        0,0 $inner.visible -anchor w -cspan 4 \
1408        1,0 $inner.labels  -anchor w -cspan 4 \
1409        2,0 $inner.minorticks  -anchor w -cspan 4 \
1410        4,0 $inner.grid_l  -anchor w \
1411        4,1 $inner.xgrid   -anchor w \
1412        4,2 $inner.ygrid   -anchor w \
1413        4,3 $inner.zgrid   -anchor w \
1414        5,0 $inner.mode_l  -anchor w -padx { 2 0 } \
1415        5,1 $inner.mode    -fill x   -cspan 3
1416
1417    blt::table configure $inner r* c* -resize none
1418    blt::table configure $inner r7 c6 -resize expand
1419    blt::table configure $inner r3 -height 0.125i
1420}
1421
1422itcl::body Rappture::VtkMeshViewer::BuildCameraTab {} {
1423    set inner [$itk_component(main) insert end \
1424        -title "Camera Settings" \
1425        -icon [Rappture::icon camera]]
1426    $inner configure -borderwidth 4
1427
1428    label $inner.view_l -text "view" -font "Arial 9"
1429    set f [frame $inner.view]
1430    foreach side { front back left right top bottom } {
1431        button $f.$side  -image [Rappture::icon view$side] \
1432            -command [itcl::code $this SetOrientation $side]
1433        Rappture::Tooltip::for $f.$side "Change the view to $side"
1434        pack $f.$side -side left
1435    }
1436
1437    blt::table $inner \
1438        0,0 $inner.view_l -anchor e -pady 2 \
1439        0,1 $inner.view -anchor w -pady 2
1440
1441    set labels { qx qy qz qw xpan ypan zoom }
1442    set row 1
1443    foreach tag $labels {
1444        label $inner.${tag}label -text $tag -font "Arial 9"
1445        entry $inner.${tag} -font "Arial 9"  -bg white \
1446            -textvariable [itcl::scope _view($tag)]
1447        bind $inner.${tag} <KeyPress-Return> \
1448            [itcl::code $this camera set ${tag}]
1449        blt::table $inner \
1450            $row,0 $inner.${tag}label -anchor e -pady 2 \
1451            $row,1 $inner.${tag} -anchor w -pady 2
1452        blt::table configure $inner r$row -resize none
1453        incr row
1454    }
1455    checkbutton $inner.ortho \
1456        -text "Orthographic Projection" \
1457        -variable [itcl::scope _view(ortho)] \
1458        -command [itcl::code $this camera set ortho] \
1459        -font "Arial 9"
1460    blt::table $inner \
1461            $row,0 $inner.ortho -cspan 2 -anchor w -pady 2
1462    blt::table configure $inner r$row -resize none
1463    incr row
1464
1465    blt::table configure $inner c* r* -resize none
1466    blt::table configure $inner c2 -resize expand
1467    blt::table configure $inner r$row -resize expand
1468}
1469
1470#
1471#  camera --
1472#
1473itcl::body Rappture::VtkMeshViewer::camera {option args} {
1474    switch -- $option {
1475        "show" {
1476            puts [array get _view]
1477        }
1478        "set" {
1479            set who [lindex $args 0]
1480            set x $_view($who)
1481            set code [catch { string is double $x } result]
1482            if { $code != 0 || !$result } {
1483                return
1484            }
1485            switch -- $who {
1486                "ortho" {
1487                    if {$_view(ortho)} {
1488                        SendCmd "camera mode ortho"
1489                    } else {
1490                        SendCmd "camera mode persp"
1491                    }
1492                }
1493                "xpan" - "ypan" {
1494                    PanCamera
1495                }
1496                "qx" - "qy" - "qz" - "qw" {
1497                    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1498                    $_arcball quaternion $q
1499                    EventuallyRotate $q
1500                }
1501                "zoom" {
1502                    SendCmd "camera zoom $_view(zoom)"
1503                }
1504            }
1505        }
1506    }
1507}
1508
1509itcl::body Rappture::VtkMeshViewer::GetVtkData { args } {
1510    set bytes ""
1511    foreach dataobj [get] {
1512        set contents [$dataobj vtkdata -full]
1513        append bytes "$contents\n"
1514    }
1515    return [list .vtk $bytes]
1516}
1517
1518itcl::body Rappture::VtkMeshViewer::GetImage { args } {
1519    if { [image width $_image(download)] > 0 &&
1520         [image height $_image(download)] > 0 } {
1521        set bytes [$_image(download) data -format "jpeg -quality 100"]
1522        set bytes [Rappture::encoding::decode -as b64 $bytes]
1523        return [list .jpg $bytes]
1524    }
1525    return ""
1526}
1527
1528itcl::body Rappture::VtkMeshViewer::BuildDownloadPopup { popup command } {
1529    Rappture::Balloon $popup \
1530        -title "[Rappture::filexfer::label downloadWord] as..."
1531    set inner [$popup component inner]
1532    label $inner.summary -text "" -anchor w
1533    radiobutton $inner.vtk_button -text "VTK data file" \
1534        -variable [itcl::scope _downloadPopup(format)] \
1535        -font "Helvetica 9 " \
1536        -value vtk 
1537    Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file."
1538    radiobutton $inner.image_button -text "Image File" \
1539        -variable [itcl::scope _downloadPopup(format)] \
1540        -value image
1541    Rappture::Tooltip::for $inner.image_button \
1542        "Save as digital image."
1543
1544    button $inner.ok -text "Save" \
1545        -highlightthickness 0 -pady 2 -padx 3 \
1546        -command $command \
1547        -compound left \
1548        -image [Rappture::icon download]
1549
1550    button $inner.cancel -text "Cancel" \
1551        -highlightthickness 0 -pady 2 -padx 3 \
1552        -command [list $popup deactivate] \
1553        -compound left \
1554        -image [Rappture::icon cancel]
1555
1556    blt::table $inner \
1557        0,0 $inner.summary -cspan 2  \
1558        1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \
1559        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
1560        4,1 $inner.cancel -width .9i -fill y \
1561        4,0 $inner.ok -padx 2 -width .9i -fill y
1562    blt::table configure $inner r3 -height 4
1563    blt::table configure $inner r4 -pady 4
1564    raise $inner.image_button
1565    $inner.vtk_button invoke
1566    return $inner
1567}
1568
1569itcl::body Rappture::VtkMeshViewer::SetObjectStyle { dataobj } {
1570    # Parse style string.
1571    set tag $dataobj
1572    set type [$dataobj type]
1573
1574    array set style {
1575        -cloudstyle mesh
1576        -color white
1577        -edgecolor black
1578        -edges 1
1579        -lighting 1
1580        -linewidth 1.0
1581        -opacity 1.0
1582        -outline 0
1583        -visible 1
1584        -wireframe 0
1585    }
1586    if { $dataobj != $_first } {
1587        set style(-wireframe) 1
1588    }
1589    if {$type == "cloud"} {
1590        set style(-cloudstyle) points
1591        set style(-edges) 0
1592        set style(-edgecolor) white
1593    }
1594    array set style [$dataobj hints style]
1595
1596    if {[$dataobj hints color] != ""} {
1597        set style(-color) [$dataobj hints color]
1598    }
1599    SendCmd "outline add $tag"
1600    SendCmd "outline color [Color2RGB $style(-color)] $tag"
1601    SendCmd "outline visible $style(-outline) $tag"
1602    set _settings(-outline) $style(-outline)
1603
1604    SendCmd "polydata add $tag"
1605    SendCmd "polydata visible $style(-visible) $tag"
1606    set _settings(-polydatavisible) $style(-visible)
1607    SendCmd "polydata cloudstyle $style(-cloudstyle) $tag"
1608    SendCmd "polydata edges $style(-edges) $tag"
1609    set _settings(-polydataedges) $style(-edges)
1610    SendCmd "polydata color [Color2RGB $style(-color)] $tag"
1611    SendCmd "polydata lighting $style(-lighting) $tag"
1612    set _settings(-polydatalighting) $style(-lighting)
1613    SendCmd "polydata linecolor [Color2RGB $style(-edgecolor)] $tag"
1614    SendCmd "polydata linewidth $style(-linewidth) $tag"
1615    SendCmd "polydata opacity $style(-opacity) $tag"
1616    set _settings(-polydataopacity) $style(-opacity)
1617    set _widget(-polydataopacity) [expr 100.0 * $style(-opacity)]
1618    SendCmd "polydata wireframe $style(-wireframe) $tag"
1619    set _settings(-polydatawireframe) $style(-wireframe)
1620    set havePolyData 1
1621}
1622
1623itcl::body Rappture::VtkMeshViewer::IsValidObject { dataobj } {
1624    if {[catch {$dataobj isa Rappture::Mesh} valid] != 0 || !$valid} {
1625        return 0
1626    }
1627    return 1
1628}
1629
1630itcl::body Rappture::VtkMeshViewer::SetOrientation { side } {
1631    array set positions {
1632        front "1 0 0 0"
1633        back  "0 0 1 0"
1634        left  "0.707107 0 -0.707107 0"
1635        right "0.707107 0 0.707107 0"
1636        top   "0.707107 -0.707107 0 0"
1637        bottom "0.707107 0.707107 0 0"
1638    }
1639    foreach name { qw qx qy qz } value $positions($side) {
1640        set _view($name) $value
1641    }
1642    set q [list $_view(qw) $_view(qx) $_view(qy) $_view(qz)]
1643    $_arcball quaternion $q
1644    SendCmd "camera orient $q"
1645    SendCmd "camera reset"
1646    set _view(xpan) 0
1647    set _view(ypan) 0
1648    set _view(zoom) 1.0
1649}
Note: See TracBrowser for help on using the repository browser.