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

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

merge r4765 from trunk

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