source: trunk/gui/apps/vtkcontour-test @ 2789

Last change on this file since 2789 was 2789, checked in by gah, 12 years ago

change $* to $@ in bash scripts

  • Property svn:executable set to *
File size: 15.4 KB
Line 
1#!/bin/sh
2# -*- mode: Tcl -*-
3# ----------------------------------------------------------------------
4#  TEST PROGRAM for VtkContourViewer
5#
6#  This program is a test harness for the VtkVis visualization
7#  engine.  It allows you to monitor the commands being sent back
8#  and forth between a standard Rappture application and the VtkVis
9#  server.  You can also send your own commands to the server, to
10#  debug new features.
11#
12# ======================================================================
13#  AUTHOR:  Michael McLennan, Purdue University
14#  Copyright (c) 2004-2007  Purdue Research Foundation
15#
16#  See the file "license.terms" for information on usage and
17#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
18# ======================================================================
19#\
20bindir=`dirname $0` ; \
21exec $bindir/wish "$0" "$@"
22# ----------------------------------------------------------------------
23# wish executes everything from here on...
24
25set installdir [file root $argv0]
26set libdir [file join $installdir "lib"]
27
28lappend auto_path $libdir $libdir/vtk $libdir/vtk/tcl
29
30package require Itcl
31package require Rappture
32package require RapptureGUI
33package require vtk
34
35option add *comm.font -*-courier-medium-r-normal-*-*-120-*
36option add *Menu.tearOff off
37
38option add *Tooltip.background white
39option add *Editor.background white
40option add *Gauge.textBackground white
41option add *TemperatureGauge.textBackground white
42option add *Switch.textBackground white
43option add *Progress.barColor #ffffcc
44option add *Balloon.titleBackground #6666cc
45option add *Balloon.titleForeground white
46option add *Balloon*Label.font -*-helvetica-medium-r-normal-*-*-120-*
47option add *Balloon*Radiobutton.font -*-helvetica-medium-r-normal-*-*-120-*
48option add *Balloon*Checkbutton.font -*-helvetica-medium-r-normal-*-*-120-*
49option add *ResultSet.controlbarBackground #6666cc
50option add *ResultSet.controlbarForeground white
51option add *ResultSet.activeControlBackground #ccccff
52option add *ResultSet.activeControlForeground black
53option add *Radiodial.length 3i
54option add *BugReport*banner*foreground white
55option add *BugReport*banner*background #a9a9a9
56option add *BugReport*banner*highlightBackground #a9a9a9
57option add *BugReport*banner*font -*-helvetica-bold-r-normal-*-*-180-*
58
59# fix the "grab" command to support a stack of grab windows
60#Rappture::grab::init
61
62# ----------------------------------------------------------------------
63# LOAD RESOURCE SETTINGS
64#
65# Try to load the $SESSIONDIR/resources file, which contains
66# middleware settings, such as the application name and the
67# filexfer settings.
68# ----------------------------------------------------------------------
69Rappture::resources::load
70
71# ----------------------------------------------------------------------
72# Fake data object for sending VTK data file...
73# ----------------------------------------------------------------------
74itcl::class visData {
75    constructor {args} {
76        set _reader [vtkDataSetReader $this-xvtkDataSetReader]
77        $_reader SetFileName [lindex $args 0]
78        $_reader Update
79        set _data [$_reader GetOutput]
80    }
81    destructor {
82        rename _data ""
83        rename _reader ""
84    }
85    public method components {args} {
86        if {[llength $args] == 0} {
87            return "one"
88        }
89        return ""
90    }
91    public method mesh {args} {
92        switch -- [$_data GetClassName] {
93            vtkPolyData {
94                if {[$_data GetNumberOfCells] > 0} {
95                    return $_data
96                } else {
97                    return [$_data GetPoints]
98                }
99            }
100            vtkStructuredPoints -
101            vtkUniformGrid -
102            vtkImageData {
103                foreach { x1 y1 z1 } [$_data GetOrigin] break
104                foreach { xN yN zN } [$_data GetDimensions] break
105                foreach { xS yS zS } [$_data GetSpacing] break
106                set x2 [expr {$x1 + $xN * $xS}]
107                set y2 [expr {$x1 + $yN * $yS}]
108                set z2 [expr {$x1 + $zN * $zS}]
109                if {$zN == 1} {
110                    lappend out $x1 $x2 $xN $y1 $y2 $yN
111                } else {
112                    lappend out $x1 $x2 $xN $y1 $y2 $yN $z1 $z2 $zN
113                }
114                return $out
115            }
116            default {
117                return $_data
118            }
119        }
120    }
121    public method isunirect2d {args} {
122        if {[$_data GetClassName] != "vtkImageData" &&
123            [$_data GetClassName] != "vtkStructuredPoints" &&
124            [$_data GetClassName] != "vtkUniformGrid"} {
125            return 0
126        }
127        foreach { xN yN zN } [$_data GetDimensions] break
128        if {$zN == 1} {
129            return 1
130        } else {
131            return 0
132        }
133    }
134    public method isunirect3d {args} {
135        if {[$_data GetClassName] != "vtkImageData" &&
136            [$_data GetClassName] != "vtkStructuredPoints" &&
137            [$_data GetClassName] != "vtkUniformGrid"} {
138            return 0
139        }
140        foreach { xN yN zN } [$_data GetDimensions] break
141        if {$zN > 1} {
142            return 1
143        } else {
144            return 0
145        }
146    }
147    public method data {args} {
148        return [values $args]
149    }
150    public method values {args} {
151        set dataAttrs [$_data GetPointData]
152        if {"" == $dataAttrs} {
153            puts stderr "No point data"
154            return ""
155        }
156        set scalarArr [$dataAttrs GetScalars]
157        if {"" == $scalarArr} {
158            for {set i 0} {$i < [$dataAttrs GetNumberOfArrays]} {incr i} {
159                if {[[$dataAttrs GetArray $i] GetNumberOfComponents] == 1} {
160                    $dataAttrs SetActiveScalars [$dataAttrs GetArrayName $i]
161                    puts stderr "Set scalars to '[$dataAttrs GetArrayName $i]'"
162                    break
163                }
164            }
165            set scalarArr [$dataAttrs GetScalars]
166            if {"" == $scalarArr} {
167                puts stderr "No scalar point data"
168                return ""
169            }
170        }
171        if {[isunirect2d] || [isunirect3d]} {
172            for {set i 0} {$i < [$scalarArr GetNumberOfTuples]} {incr i} {
173                lappend out [$scalarArr GetComponent $i 0]
174            }
175            return $out
176        }
177        return $scalarArr
178    }
179    public method limits {which} {
180        foreach { xMin xMax yMin yMax zMin zMax} [$_data GetBounds] break
181        set min ""
182        set max ""
183
184        switch -- $which {
185            x - xlin - xlog {
186                set min $xMin
187                set max $xMax
188                set axis "xaxis"
189            }
190            y - ylin - ylog {
191                set min $yMin
192                set max $yMax
193                set axis "yaxis"
194            }
195            z - zlin - zlog {
196                set min $zMin
197                set max $zMax
198                set axis "zaxis"
199            }
200            v - vlin - vlog {
201                foreach { min max } [$_data GetScalarRange] break
202                set axis "vaxis"
203            }
204            default {
205                error "unknown axis description \"$which\""
206            }
207        }
208        return [list $min $max]
209    }
210    public method hints {args} {
211        return ""
212    }
213
214    private variable _data ""
215    private variable _reader ""
216}
217
218# ----------------------------------------------------------------------
219# USAGE: send_file
220#
221# Prompts the user for a text file, and then sends the text within
222# that file along to the rendering widget.
223# ----------------------------------------------------------------------
224proc send_file {} {
225    global widgets
226
227    set file [tk_getOpenFile -title "Open VTK File"]
228    if {"" != $file} {
229        set obj [visData #auto $file]
230        $widgets(vtkcontourviewer) add $obj
231    }
232}
233
234# ----------------------------------------------------------------------
235# USAGE: load_script
236#
237# Prompts the user for a text file, and then sends the text within
238# that file along to the rendering widget.
239# ----------------------------------------------------------------------
240proc load_script {} {
241    global widgets
242
243    set file [tk_getOpenFile -title "Open Command File"]
244    if {"" != $file && [catch {
245            set fid [open $file r]
246            fconfigure $fid -translation binary -encoding binary
247            set info [read $fid]
248            close $fid
249          }] == 0} {
250
251        $widgets(command) insert 0 $info
252        send_command
253    }
254}
255
256# ----------------------------------------------------------------------
257# USAGE: send_command
258#
259# Invoked automatically whenever the user enters a command and
260# presses <Return>.  Sends the command along to the rendering
261# widget.
262# ----------------------------------------------------------------------
263proc send_command {} {
264    global widgets
265    global last_command
266
267    set cmd [$widgets(command) get]
268
269    if {[string length $cmd] > 0} {
270        set last_command $cmd
271    } else {
272        set cmd $last_command
273    }
274    namespace eval Rappture::VtkContourViewer [list $widgets(vtkcontourviewer) SendCmd $cmd]
275    $widgets(command) delete 0 end
276}
277
278# ----------------------------------------------------------------------
279# USAGE: reset
280#
281# Used internally to reset the connection to the rendering server.
282# Discards all data and resets the widget connection to the server.
283# ----------------------------------------------------------------------
284proc reset {} {
285    global widgets
286    $widgets(vtkcontourviewer) delete
287    $widgets(vtkcontourviewer) disconnect
288    $widgets(comm) configure -state normal
289    $widgets(comm) delete 1.0 end
290    $widgets(comm) configure -state disabled
291}
292
293# ----------------------------------------------------------------------
294# USAGE: show_comm <channel> <data>
295#
296# Invoked automatically whenever there is communication between
297# the rendering widget and the server.  Eavesdrops on the communication
298# and posts the commands in a text viewer.
299# ----------------------------------------------------------------------
300proc show_comm {channel {data ""}} {
301    global widgets
302
303    $widgets(comm) configure -state normal
304    switch -- $channel {
305        closed {
306            $widgets(comm) insert end "--CLOSED--\n" error
307        }
308        <<line {
309            $widgets(comm) insert end $data incoming "\n" incoming
310            images_refresh
311        }
312        >>line {
313            $widgets(comm) insert end $data outgoing "\n" outgoing
314        }
315        error {
316            $widgets(comm) insert end $data error "\n" error
317        }
318        default {
319            $widgets(comm) insert end "$data\n"
320        }
321    }
322    $widgets(comm) configure -state disabled
323    $widgets(comm) see end
324}
325
326# ----------------------------------------------------------------------
327# USAGE: activate_flow
328#
329# ----------------------------------------------------------------------
330proc activate_flow {} {
331    global widgets
332    # global img_storage_dir
333    # "flow capture 117 $img_storage_dir"
334
335    set info {flow vectorid 0
336              flow particle visible on
337              flow lic on
338              flow capture 100}
339
340    $widgets(command) insert 0 $info
341    send_command
342
343}
344
345# ----------------------------------------------------------------------
346# TOPLEVEL FOR IMAGES
347# ----------------------------------------------------------------------
348# USAGE: images_save
349#
350# Invoked when the user presses the "Save As..." button on the
351# images panel.  Saves the current image in a file, which can be
352# examined by some external program.
353# ----------------------------------------------------------------------
354proc images_save {} {
355    global widgets images
356
357    set imh [$widgets(vtkcontourviewer) get -image $images(which)]
358
359    set file [tk_getSaveFile -title "Save Image File" \
360        -defaultextension .jpg -filetypes {{{JPEG files} .jpg} {{All Files} *}}]
361
362    if {"" != $file} {
363        set cmds {
364            $imh write $file -format jpeg
365        }
366        if {[catch $cmds err]} {
367            tk_messageBox -icon error -message "Oops!  Save failed:\n$err"
368        }
369    }
370}
371
372# ----------------------------------------------------------------------
373# USAGE: images_refresh
374#
375# Invoked automatically whenever there is a change in the view/legend
376# controls on the images panel.  Updates the image being shown based
377# on the current selection.
378# ----------------------------------------------------------------------
379proc images_refresh {} {
380    global widgets images
381    set c $widgets(viewer)
382
383    set w [winfo width $c]
384    set h [winfo height $c]
385
386    set imh [$widgets(vtkcontourviewer) get -image $images(which)]
387    set iw [image width $imh]
388    set ih [image height $imh]
389
390    $c coords image [expr {$w/2}] [expr {$h/2}]
391    $c itemconfigure image -image $imh
392    $c coords outline [expr {$w/2-$iw/2}] [expr {$h/2-$ih/2}] \
393        [expr {$w/2+$iw/2}] [expr {$h/2+$ih/2}]
394}
395
396toplevel .images
397wm title .images "VtkContourViewer: Images"
398wm withdraw .images
399wm protocol .images WM_DELETE_WINDOW {wm withdraw .images}
400
401frame .images.cntls
402pack .images.cntls -side bottom -fill x
403button .images.cntls.save -text "Save As..." -command images_save
404pack .images.cntls.save -side right -padx 4
405radiobutton .images.cntls.view -text "3D View" -variable images(which) \
406    -value "view" -command images_refresh
407pack .images.cntls.view -side top -anchor w
408radiobutton .images.cntls.legend -text "Legend" -variable images(which) \
409    -value "legend" -command images_refresh
410pack .images.cntls.legend -side top -anchor w
411set images(which) "view"
412
413canvas .images.viewer -background black -width 500 -height 500
414pack .images.viewer -expand yes -fill both
415bind .images.viewer <Configure> images_refresh
416set widgets(viewer) .images.viewer
417
418$widgets(viewer) create image 0 0 -anchor c \
419    -image [image create photo] -tags image
420$widgets(viewer) create rectangle 0 0 1 1 -width 2 -outline red -fill "" \
421    -tags outline
422
423
424# ----------------------------------------------------------------------
425# MAIN WINDOW
426# ----------------------------------------------------------------------
427menu .mbar
428menu .mbar.file
429.mbar.file add command -label "Send VTK File..." -underline 0 -command send_file
430.mbar.file add command -label "Load script..." -underline 0 -command load_script
431.mbar.file add command -label "Reset" -underline 0 -command reset
432.mbar.file add separator
433.mbar.file add command -label "Exit" -underline 1 -command exit
434.mbar add cascade -label "File" -underline 0 -menu .mbar.file
435
436menu .mbar.view
437.mbar.view add command -label "Images..." -underline 0 \
438    -command {wm deiconify .images}
439.mbar add cascade -label "View" -underline 0 -menu .mbar.view
440
441. configure -menu .mbar
442
443
444Rappture::Panes .main -sashwidth 4 -sashrelief raised -sashpadding 4 \
445    -width 6i -height 4i
446pack .main -expand yes -fill both
447
448set f [.main pane 0]
449set servers [Rappture::VisViewer::GetServerList "vtkvis"]
450Rappture::VtkContourViewer $f.viewer $servers
451pack $f.viewer -expand yes -fill both
452set widgets(vtkcontourviewer) $f.viewer
453
454$f.viewer configure \
455    -sendcommand show_comm \
456    -receivecommand show_comm
457
458set f [.main insert end -fraction 0.5]
459frame $f.send
460pack $f.send -side bottom -fill x
461label $f.send.l -text "Send:"
462pack $f.send.l -side left
463set widgets(command) [entry $f.send.e]
464pack $f.send.e -side left -expand yes -fill x
465bind $f.send.e <KeyPress-Return> send_command
466
467scrollbar $f.sb -orient vertical -command "$f.comm yview"
468pack $f.sb -side right -fill y
469text $f.comm -wrap char -yscrollcommand "$f.sb set"
470pack $f.comm -expand yes -fill both
471set widgets(comm) $f.comm
472
473$widgets(comm) tag configure error -foreground red \
474    -font -*-courier-medium-o-normal-*-*-120-*
475$widgets(comm) tag configure incoming -foreground blue
Note: See TracBrowser for help on using the repository browser.