[2126] | 1 | #!/bin/sh |
---|
[2415] | 2 | # -*- mode: Tcl -*- |
---|
[2126] | 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 |
---|
[3177] | 14 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[2126] | 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 | #\ |
---|
| 20 | bindir=`dirname $0` ; \ |
---|
[2789] | 21 | exec $bindir/wish "$0" "$@" |
---|
[2126] | 22 | # ---------------------------------------------------------------------- |
---|
| 23 | # wish executes everything from here on... |
---|
| 24 | |
---|
| 25 | set installdir [file root $argv0] |
---|
| 26 | set libdir [file join $installdir "lib"] |
---|
| 27 | |
---|
| 28 | lappend auto_path $libdir $libdir/vtk $libdir/vtk/tcl |
---|
| 29 | |
---|
| 30 | package require Itcl |
---|
| 31 | package require Rappture |
---|
| 32 | package require RapptureGUI |
---|
[2424] | 33 | package require vtk |
---|
[2126] | 34 | |
---|
| 35 | option add *comm.font -*-courier-medium-r-normal-*-*-120-* |
---|
| 36 | option add *Menu.tearOff off |
---|
| 37 | |
---|
| 38 | option add *Tooltip.background white |
---|
| 39 | option add *Editor.background white |
---|
| 40 | option add *Gauge.textBackground white |
---|
| 41 | option add *TemperatureGauge.textBackground white |
---|
| 42 | option add *Switch.textBackground white |
---|
| 43 | option add *Progress.barColor #ffffcc |
---|
| 44 | option add *Balloon.titleBackground #6666cc |
---|
| 45 | option add *Balloon.titleForeground white |
---|
| 46 | option add *Balloon*Label.font -*-helvetica-medium-r-normal-*-*-120-* |
---|
| 47 | option add *Balloon*Radiobutton.font -*-helvetica-medium-r-normal-*-*-120-* |
---|
| 48 | option add *Balloon*Checkbutton.font -*-helvetica-medium-r-normal-*-*-120-* |
---|
| 49 | option add *ResultSet.controlbarBackground #6666cc |
---|
| 50 | option add *ResultSet.controlbarForeground white |
---|
| 51 | option add *ResultSet.activeControlBackground #ccccff |
---|
| 52 | option add *ResultSet.activeControlForeground black |
---|
| 53 | option add *Radiodial.length 3i |
---|
| 54 | option add *BugReport*banner*foreground white |
---|
| 55 | option add *BugReport*banner*background #a9a9a9 |
---|
| 56 | option add *BugReport*banner*highlightBackground #a9a9a9 |
---|
| 57 | option 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 | # ---------------------------------------------------------------------- |
---|
| 69 | Rappture::resources::load |
---|
| 70 | |
---|
| 71 | # ---------------------------------------------------------------------- |
---|
[2293] | 72 | # Fake data object for sending VTK data file... |
---|
[2126] | 73 | # ---------------------------------------------------------------------- |
---|
| 74 | itcl::class visData { |
---|
| 75 | constructor {args} { |
---|
[2424] | 76 | set _reader [vtkDataSetReader $this-xvtkDataSetReader] |
---|
| 77 | $_reader SetFileName [lindex $args 0] |
---|
| 78 | $_reader Update |
---|
| 79 | set _data [$_reader GetOutput] |
---|
[2126] | 80 | } |
---|
[2424] | 81 | destructor { |
---|
| 82 | rename _data "" |
---|
| 83 | rename _reader "" |
---|
| 84 | } |
---|
[2126] | 85 | public method components {args} { |
---|
| 86 | if {[llength $args] == 0} { |
---|
| 87 | return "one" |
---|
| 88 | } |
---|
| 89 | return "" |
---|
| 90 | } |
---|
[2424] | 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 | } |
---|
[2293] | 147 | public method data {args} { |
---|
[2424] | 148 | return [values $args] |
---|
[2293] | 149 | } |
---|
[2126] | 150 | public method values {args} { |
---|
[2424] | 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 |
---|
[2126] | 178 | } |
---|
[2424] | 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 | } |
---|
[2126] | 210 | public method hints {args} { |
---|
| 211 | return "" |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | private variable _data "" |
---|
[2424] | 215 | private variable _reader "" |
---|
[2126] | 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 | # ---------------------------------------------------------------------- |
---|
| 224 | proc send_file {} { |
---|
| 225 | global widgets |
---|
| 226 | |
---|
[2293] | 227 | set file [tk_getOpenFile -title "Open VTK File"] |
---|
[2424] | 228 | if {"" != $file} { |
---|
| 229 | set obj [visData #auto $file] |
---|
[2126] | 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 | # ---------------------------------------------------------------------- |
---|
| 240 | proc 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] |
---|
[2687] | 246 | fconfigure $fid -translation binary -encoding binary |
---|
[2126] | 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 | # ---------------------------------------------------------------------- |
---|
| 263 | proc 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 | # ---------------------------------------------------------------------- |
---|
| 284 | proc 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 | # ---------------------------------------------------------------------- |
---|
| 300 | proc 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 | # ---------------------------------------------------------------------- |
---|
| 330 | proc 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 | # ---------------------------------------------------------------------- |
---|
| 354 | proc 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 | # ---------------------------------------------------------------------- |
---|
| 379 | proc 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 | |
---|
| 396 | toplevel .images |
---|
| 397 | wm title .images "VtkContourViewer: Images" |
---|
| 398 | wm withdraw .images |
---|
| 399 | wm protocol .images WM_DELETE_WINDOW {wm withdraw .images} |
---|
| 400 | |
---|
| 401 | frame .images.cntls |
---|
| 402 | pack .images.cntls -side bottom -fill x |
---|
| 403 | button .images.cntls.save -text "Save As..." -command images_save |
---|
| 404 | pack .images.cntls.save -side right -padx 4 |
---|
| 405 | radiobutton .images.cntls.view -text "3D View" -variable images(which) \ |
---|
| 406 | -value "view" -command images_refresh |
---|
| 407 | pack .images.cntls.view -side top -anchor w |
---|
| 408 | radiobutton .images.cntls.legend -text "Legend" -variable images(which) \ |
---|
| 409 | -value "legend" -command images_refresh |
---|
| 410 | pack .images.cntls.legend -side top -anchor w |
---|
| 411 | set images(which) "view" |
---|
| 412 | |
---|
| 413 | canvas .images.viewer -background black -width 500 -height 500 |
---|
| 414 | pack .images.viewer -expand yes -fill both |
---|
| 415 | bind .images.viewer <Configure> images_refresh |
---|
| 416 | set 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 | # ---------------------------------------------------------------------- |
---|
| 427 | menu .mbar |
---|
| 428 | menu .mbar.file |
---|
[2293] | 429 | .mbar.file add command -label "Send VTK File..." -underline 0 -command send_file |
---|
[2126] | 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 | |
---|
| 436 | menu .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 | |
---|
| 444 | Rappture::Panes .main -sashwidth 4 -sashrelief raised -sashpadding 4 \ |
---|
| 445 | -width 6i -height 4i |
---|
| 446 | pack .main -expand yes -fill both |
---|
| 447 | |
---|
| 448 | set f [.main pane 0] |
---|
[2166] | 449 | set servers [Rappture::VisViewer::GetServerList "vtkvis"] |
---|
[2144] | 450 | Rappture::VtkContourViewer $f.viewer $servers |
---|
[2126] | 451 | pack $f.viewer -expand yes -fill both |
---|
| 452 | set widgets(vtkcontourviewer) $f.viewer |
---|
| 453 | |
---|
| 454 | $f.viewer configure \ |
---|
| 455 | -sendcommand show_comm \ |
---|
| 456 | -receivecommand show_comm |
---|
| 457 | |
---|
| 458 | set f [.main insert end -fraction 0.5] |
---|
| 459 | frame $f.send |
---|
| 460 | pack $f.send -side bottom -fill x |
---|
| 461 | label $f.send.l -text "Send:" |
---|
| 462 | pack $f.send.l -side left |
---|
| 463 | set widgets(command) [entry $f.send.e] |
---|
| 464 | pack $f.send.e -side left -expand yes -fill x |
---|
| 465 | bind $f.send.e <KeyPress-Return> send_command |
---|
| 466 | |
---|
| 467 | scrollbar $f.sb -orient vertical -command "$f.comm yview" |
---|
| 468 | pack $f.sb -side right -fill y |
---|
| 469 | text $f.comm -wrap char -yscrollcommand "$f.sb set" |
---|
| 470 | pack $f.comm -expand yes -fill both |
---|
| 471 | set 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 |
---|