# -*- mode: tcl; indent-tabs-mode: nil -*- # ---------------------------------------------------------------------- # COMPONENT: vtkmeshviewer - Vtk mesh viewer # # It connects to the Vtkvis server running on a rendering farm, # transmits data, and displays the results. # ====================================================================== # AUTHOR: Michael McLennan, Purdue University # Copyright (c) 2004-2016 HUBzero Foundation, LLC # # See the file "license.terms" for information on usage and # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. # ====================================================================== package require Itk package require BLT #package require Img option add *VtkMeshViewer.width 4i widgetDefault option add *VtkMeshViewer*cursor crosshair widgetDefault option add *VtkMeshViewer.height 4i widgetDefault option add *VtkMeshViewer.foreground black widgetDefault option add *VtkMeshViewer.controlBackground gray widgetDefault option add *VtkMeshViewer.controlDarkBackground #999999 widgetDefault option add *VtkMeshViewer.plotBackground black widgetDefault option add *VtkMeshViewer.plotForeground white widgetDefault option add *VtkMeshViewer.font \ -*-helvetica-medium-r-normal-*-12-* widgetDefault # must use this name -- plugs into Rappture::resources::load proc VtkMeshViewer_init_resources {} { Rappture::resources::register \ vtkvis_server Rappture::VtkMeshViewer::SetServerList } itcl::class Rappture::VtkMeshViewer { inherit Rappture::VisViewer itk_option define -plotforeground plotForeground Foreground "" itk_option define -plotbackground plotBackground Background "" constructor { args } { Rappture::VisViewer::constructor } { # defined below } destructor { # defined below } public proc SetServerList { namelist } { Rappture::VisViewer::SetServerList "vtkvis" $namelist } public method add {dataobj {settings ""}} public method camera {option args} public method delete {args} public method disconnect {} public method download {option args} public method get {args} public method isconnected {} public method parameters {title args} { # do nothing } public method scale {args} # The following methods are only used by this class. private method AdjustSetting {what {value ""}} private method BuildAxisTab {} private method BuildCameraTab {} private method BuildDownloadPopup { widget command } private method BuildPolydataTab {} private method Connect {} private method CurrentDatasets {args} private method Disconnect {} private method DoResize {} private method DoRotate {} private method EventuallyResize { w h } private method EventuallyRotate { q } private method EventuallySetPolydataOpacity {} private method GetImage { args } private method GetVtkData { args } private method InitSettings { args } private method IsValidObject { dataobj } private method Pan {option x y} private method PanCamera {} private method Pick {x y} private method QuaternionToView { q } { foreach { _view(-qw) _view(-qx) _view(-qy) _view(-qz) } $q break } private method Rebuild {} private method ReceiveDataset { args } private method ReceiveImage { args } private method Rotate {option x y} private method SetObjectStyle { dataobj } private method SetOrientation { side } private method SetPolydataOpacity {} private method ViewToQuaternion {} { return [list $_view(-qw) $_view(-qx) $_view(-qy) $_view(-qz)] } private method Zoom {option} private variable _arcball "" private variable _dlist ""; # list of data objects private variable _obj2ovride; # maps dataobj => style override private variable _datasets; # contains all the dataobj-component # datasets in the server private variable _click; # info used for rotate operations private variable _limits; # autoscale min/max for all axes private variable _view; # view params for 3D view private variable _settings private variable _widget private variable _reset 1; # Connection to server has been reset. private variable _first ""; # This is the topmost dataset. private variable _start 0 private variable _title "" private variable _width 0 private variable _height 0 private variable _resizePending 0 private variable _rotatePending 0 private variable _polydataOpacityPending 0 private variable _rotateDelay 150 private variable _opacityDelay 150 private common _downloadPopup; # download options from popup private common _hardcopy } itk::usual VtkMeshViewer { keep -background -foreground -cursor -font keep -plotbackground -plotforeground } # ---------------------------------------------------------------------- # CONSTRUCTOR # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::constructor {args} { set _serverType "vtkvis" #DebugOn EnableWaitDialog 900 # Rebuild event $_dispatcher register !rebuild $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list" # Resize event $_dispatcher register !resize $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list" # Rotate event $_dispatcher register !rotate $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list" # Polydata opacity event $_dispatcher register !polydataOpacity $_dispatcher dispatch $this !polydataOpacity \ "[itcl::code $this SetPolydataOpacity]; list" # # Populate parser with commands handle incoming requests # $_parser alias image [itcl::code $this ReceiveImage] $_parser alias dataset [itcl::code $this ReceiveDataset] # Initialize the view to some default parameters. array set _view { -ortho 0 -qw 0.853553 -qx -0.353553 -qy 0.353553 -qz 0.146447 -xpan 0 -ypan 0 -zoom 1.0 } set _arcball [blt::arcball create 100 100] $_arcball quaternion [ViewToQuaternion] array set _settings { -axesvisible 1 -axislabels 1 -axisminorticks 1 -outline 0 -polydataedges 0 -polydatalighting 1 -polydataopacity 1.0 -polydatavisible 1 -polydatawireframe 0 -xgrid 0 -ygrid 0 -zgrid 0 } array set _widget { -polydataopacity 100 } itk_component add view { canvas $itk_component(plotarea).view \ -highlightthickness 0 -borderwidth 0 } { usual ignore -highlightthickness -borderwidth -background } itk_component add fieldmenu { menu $itk_component(plotarea).menu -bg black -fg white -relief flat \ -tearoff 0 } { usual ignore -background -foreground -relief -tearoff } set c $itk_component(view) bind $c [itcl::code $this EventuallyResize %w %h] bind $c [itcl::code $this ToggleConsole] # Fix the scrollregion in case we go off screen $c configure -scrollregion [$c bbox all] set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)] set _map(cwidth) -1 set _map(cheight) -1 set _map(zoom) 1.0 set _map(original) "" set f [$itk_component(main) component controls] itk_component add reset { button $f.reset -borderwidth 1 -padx 1 -pady 1 \ -highlightthickness 0 \ -image [Rappture::icon reset-view] \ -command [itcl::code $this Zoom reset] } { usual ignore -highlightthickness } pack $itk_component(reset) -side top -padx 2 -pady 2 Rappture::Tooltip::for $itk_component(reset) \ "Reset the view to the default zoom level" itk_component add zoomin { button $f.zin -borderwidth 1 -padx 1 -pady 1 \ -highlightthickness 0 \ -image [Rappture::icon zoom-in] \ -command [itcl::code $this Zoom in] } { usual ignore -highlightthickness } pack $itk_component(zoomin) -side top -padx 2 -pady 2 Rappture::Tooltip::for $itk_component(zoomin) "Zoom in" itk_component add zoomout { button $f.zout -borderwidth 1 -padx 1 -pady 1 \ -highlightthickness 0 \ -image [Rappture::icon zoom-out] \ -command [itcl::code $this Zoom out] } { usual ignore -highlightthickness } pack $itk_component(zoomout) -side top -padx 2 -pady 2 Rappture::Tooltip::for $itk_component(zoomout) "Zoom out" BuildPolydataTab BuildAxisTab BuildCameraTab # Hack around the Tk panewindow. The problem is that the requested # size of the 3d view isn't set until an image is retrieved from # the server. So the panewindow uses the tiny size. set w 10000 pack forget $itk_component(view) blt::table $itk_component(plotarea) \ 0,0 $itk_component(view) -fill both -reqwidth $w blt::table configure $itk_component(plotarea) c1 -resize none # Bindings for rotation via mouse bind $itk_component(view) \ [itcl::code $this Rotate click %x %y] bind $itk_component(view) \ [itcl::code $this Rotate drag %x %y] bind $itk_component(view) \ [itcl::code $this Rotate release %x %y] # Bindings for panning via mouse bind $itk_component(view) \ [itcl::code $this Pan click %x %y] bind $itk_component(view) \ [itcl::code $this Pan drag %x %y] bind $itk_component(view) \ [itcl::code $this Pan release %x %y] #bind $itk_component(view) \ # [itcl::code $this Pick %x %y] # Bindings for panning via keyboard bind $itk_component(view) \ [itcl::code $this Pan set -10 0] bind $itk_component(view) \ [itcl::code $this Pan set 10 0] bind $itk_component(view) \ [itcl::code $this Pan set 0 -10] bind $itk_component(view) \ [itcl::code $this Pan set 0 10] bind $itk_component(view) \ [itcl::code $this Pan set -2 0] bind $itk_component(view) \ [itcl::code $this Pan set 2 0] bind $itk_component(view) \ [itcl::code $this Pan set 0 -2] bind $itk_component(view) \ [itcl::code $this Pan set 0 2] # Bindings for zoom via keyboard bind $itk_component(view) \ [itcl::code $this Zoom out] bind $itk_component(view) \ [itcl::code $this Zoom in] bind $itk_component(view) "focus $itk_component(view)" if {[string equal "x11" [tk windowingsystem]]} { # Bindings for zoom via mouse bind $itk_component(view) <4> [itcl::code $this Zoom out] bind $itk_component(view) <5> [itcl::code $this Zoom in] } set _image(download) [image create photo] eval itk_initialize $args Connect } # ---------------------------------------------------------------------- # DESTRUCTOR # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::destructor {} { Disconnect $_dispatcher cancel !rebuild $_dispatcher cancel !resize $_dispatcher cancel !rotate image delete $_image(plot) image delete $_image(download) catch { blt::arcball destroy $_arcball } } itcl::body Rappture::VtkMeshViewer::DoResize {} { if { $_width < 2 } { set _width 500 } if { $_height < 2 } { set _height 500 } set _start [clock clicks -milliseconds] SendCmd "screen size $_width $_height" set _resizePending 0 } itcl::body Rappture::VtkMeshViewer::DoRotate {} { SendCmd "camera orient [ViewToQuaternion]" set _rotatePending 0 } itcl::body Rappture::VtkMeshViewer::EventuallyResize { w h } { set _width $w set _height $h $_arcball resize $w $h if { !$_resizePending } { set _resizePending 1 $_dispatcher event -after 200 !resize } } itcl::body Rappture::VtkMeshViewer::EventuallyRotate { q } { QuaternionToView $q if { !$_rotatePending } { set _rotatePending 1 $_dispatcher event -after $_rotateDelay !rotate } } itcl::body Rappture::VtkMeshViewer::SetPolydataOpacity {} { set _polydataOpacityPending 0 set val $_settings(-polydataopacity) SendCmd "polydata opacity $val" } itcl::body Rappture::VtkMeshViewer::EventuallySetPolydataOpacity {} { if { !$_polydataOpacityPending } { set _polydataOpacityPending 1 $_dispatcher event -after $_opacityDelay !polydataOpacity } } # ---------------------------------------------------------------------- # USAGE: add ?? # # Clients use this to add a data object to the plot. The optional # are used to configure the plot. Allowed settings are # -color, -brightness, -width, -linestyle, and -raise. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::add {dataobj {settings ""}} { array set params { -color auto -width 1 -linestyle solid -brightness 0 -raise 0 -description "" -param "" -type "" } array set params $settings if {$params(-color) == "auto" || $params(-color) == "autoreset"} { # can't handle -autocolors yet set params(-color) black } set pos [lsearch -exact $_dlist $dataobj] if {$pos < 0} { lappend _dlist $dataobj } set _obj2ovride($dataobj-color) $params(-color) set _obj2ovride($dataobj-width) $params(-width) set _obj2ovride($dataobj-raise) $params(-raise) $_dispatcher event -idle !rebuild } # ---------------------------------------------------------------------- # USAGE: delete ? ...? # # Clients use this to delete a dataobj from the plot. If no dataobjs # are specified, then all dataobjs are deleted. No data objects are # deleted. They are only removed from the display list. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::delete {args} { if { [llength $args] == 0} { set args $_dlist } # Delete all specified dataobjs set changed 0 foreach dataobj $args { set pos [lsearch -exact $_dlist $dataobj] if { $pos < 0 } { continue; # Don't know anything about it. } # Remove it from the dataobj list. set _dlist [lreplace $_dlist $pos $pos] array unset _obj2ovride $dataobj-* set changed 1 } # If anything changed, then rebuild the plot if { $changed } { $_dispatcher event -idle !rebuild } } # ---------------------------------------------------------------------- # USAGE: get ?-objects? # USAGE: get ?-visible? # USAGE: get ?-image view? # # Clients use this to query the list of objects being plotted, in # order from bottom to top of this result. The optional "-image" # flag can also request the internal images being shown. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::get {args} { if {[llength $args] == 0} { set args "-objects" } set op [lindex $args 0] switch -- $op { "-objects" { # put the dataobj list in order according to -raise options set dlist {} foreach dataobj $_dlist { if { ![IsValidObject $dataobj] } { continue } if {[info exists _obj2ovride($dataobj-raise)] && $_obj2ovride($dataobj-raise)} { set dlist [linsert $dlist 0 $dataobj] } else { lappend dlist $dataobj } } return $dlist } "-visible" { set dlist {} foreach dataobj $_dlist { if { ![IsValidObject $dataobj] } { continue } if { ![info exists _obj2ovride($dataobj-raise)] } { # No setting indicates that the object isn't visible. continue } # Otherwise use the -raise parameter to put the object to # the front of the list. if { $_obj2ovride($dataobj-raise) } { set dlist [linsert $dlist 0 $dataobj] } else { lappend dlist $dataobj } } return $dlist } "-image" { if {[llength $args] != 2} { error "wrong # args: should be \"get -image view\"" } switch -- [lindex $args end] { view { return $_image(plot) } default { error "bad image name \"[lindex $args end]\": should be view" } } } default { error "bad option \"$op\": should be -objects, -visible or -image" } } } # ---------------------------------------------------------------------- # USAGE: scale ? ...? # # Sets the default limits for the overall plot according to the # limits of the data for all of the given objects. This # accounts for all objects--even those not showing on the screen. # Because of this, the limits are appropriate for all objects as # the user scans through data in the ResultSet viewer. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::scale {args} { foreach dataobj $args { if { ![$dataobj isvalid] } { continue; # Object doesn't contain valid data. } foreach axis { x y z } { set lim [$dataobj limits $axis] if { ![info exists _limits($axis)] } { set _limits($axis) $lim continue } foreach {min max} $lim break foreach {amin amax} $_limits($axis) break if { $amin > $min } { set amin $min } if { $amax < $max } { set amax $max } set _limits($axis) [list $amin $amax] } } } # ---------------------------------------------------------------------- # USAGE: download coming # USAGE: download controls # USAGE: download now # # Clients use this method to create a downloadable representation # of the plot. Returns a list of the form {ext string}, where # "ext" is the file extension (indicating the type of data) and # "string" is the data itself. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::download {option args} { switch $option { coming { if {[catch { blt::winop snap $itk_component(plotarea) $_image(download) }]} { $_image(download) configure -width 1 -height 1 $_image(download) put #000000 } } controls { set popup .vtkviewerdownload if { ![winfo exists .vtkviewerdownload] } { set inner [BuildDownloadPopup $popup [lindex $args 0]] } else { set inner [$popup component inner] } set _downloadPopup(image_controls) $inner.image_frame set num [llength [get]] set num [expr {($num == 1) ? "1 result" : "$num results"}] set word [Rappture::filexfer::label downloadWord] $inner.summary configure -text "$word $num in the following format:" update idletasks ;# Fix initial sizes return $popup } now { set popup .vtkviewerdownload if {[winfo exists .vtkviewerdownload]} { $popup deactivate } switch -- $_downloadPopup(format) { "image" { return [$this GetImage [lindex $args 0]] } "vtk" { return [$this GetVtkData [lindex $args 0]] } } return "" } default { error "bad option \"$option\": should be coming, controls, now" } } } # ---------------------------------------------------------------------- # USAGE: Connect ?,...? # # Clients use this method to establish a connection to a new # server, or to reestablish a connection to the previous server. # Any existing connection is automatically closed. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::Connect {} { global readyForNextFrame set readyForNextFrame 1 set _hosts [GetServerList "vtkvis"] if { "" == $_hosts } { return 0 } set _reset 1 set result [VisViewer::Connect $_hosts] if { $result } { if { $_reportClientInfo } { # Tell the server the viewer, hub, user and session. # Do this immediately on connect before buffering any commands global env set info {} set user "???" if { [info exists env(USER)] } { set user $env(USER) } set session "???" if { [info exists env(SESSION)] } { set session $env(SESSION) } lappend info "version" "$Rappture::version" lappend info "build" "$Rappture::build" lappend info "svnurl" "$Rappture::svnurl" lappend info "installdir" "$Rappture::installdir" lappend info "hub" [exec hostname] lappend info "client" "vtkmeshviewer" lappend info "user" $user lappend info "session" $session SendCmd "clientinfo [list $info]" } set w [winfo width $itk_component(view)] set h [winfo height $itk_component(view)] EventuallyResize $w $h } return $result } # # isconnected -- # # Indicates if we are currently connected to the visualization server. # itcl::body Rappture::VtkMeshViewer::isconnected {} { return [VisViewer::IsConnected] } # # disconnect -- # itcl::body Rappture::VtkMeshViewer::disconnect {} { Disconnect set _reset 1 } # # Disconnect -- # # Clients use this method to disconnect from the current rendering server. # itcl::body Rappture::VtkMeshViewer::Disconnect {} { VisViewer::Disconnect # disconnected -- no more data sitting on server array unset _datasets global readyForNextFrame set readyForNextFrame 1 } # ---------------------------------------------------------------------- # USAGE: ReceiveImage -bytes -type -token # # Invoked automatically whenever the "image" command comes in from # the rendering server. Indicates that binary image data with the # specified will follow. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::ReceiveImage { args } { global readyForNextFrame set readyForNextFrame 1 array set info { -token "???" -bytes 0 -type image } array set info $args set bytes [ReceiveBytes $info(-bytes)] if { $info(-type) == "image" } { if 0 { set f [open "last.ppm" "w"] fconfigure $f -encoding binary puts -nonewline $f $bytes close $f } $_image(plot) configure -data $bytes set time [clock seconds] set date [clock format $time] if { $_start > 0 } { set finish [clock clicks -milliseconds] set _start 0 } } elseif { $info(type) == "print" } { set tag $this-print-$info(-token) set _hardcopy($tag) $bytes } } # # ReceiveDataset -- # itcl::body Rappture::VtkMeshViewer::ReceiveDataset { args } { if { ![isconnected] } { return } set option [lindex $args 0] switch -- $option { "scalar" { set option [lindex $args 1] switch -- $option { "world" { foreach { x y z value tag } [lrange $args 2 end] break } "pixel" { foreach { x y value tag } [lrange $args 2 end] break } } } "vector" { set option [lindex $args 1] switch -- $option { "world" { foreach { x y z vx vy vz tag } [lrange $args 2 end] break } "pixel" { foreach { x y vx vy vz tag } [lrange $args 2 end] break } } } "names" { foreach { name } [lindex $args 1] { #puts stderr "Dataset: $name" } } default { error "unknown dataset option \"$option\" from server" } } } # ---------------------------------------------------------------------- # USAGE: Rebuild # # Called automatically whenever something changes that affects the # data in the widget. Clears any existing data and rebuilds the # widget to display new data. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::Rebuild {} { set w [winfo width $itk_component(view)] set h [winfo height $itk_component(view)] if { $w < 2 || $h < 2 } { update $_dispatcher event -idle !rebuild return } # Turn on buffering of commands to the server. We don't want to # be preempted by a server disconnect/reconnect (which automatically # generates a new call to Rebuild). StartBufferingCommands if { $_reset } { set _width $w set _height $h $_arcball resize $w $h DoResize InitSettings -xgrid -ygrid -zgrid -axismode \ -axesvisible -axislabels -axisminorticks StopBufferingCommands SendCmd "imgflush" StartBufferingCommands } set _first "" SendCmd "dataset visible 0" set count 0 foreach dataobj [get -objects] { if { [info exists _obj2ovride($dataobj-raise)] && $_first == "" } { set _first $dataobj } set tag $dataobj if { ![info exists _datasets($tag)] } { set bytes [$dataobj vtkdata -full] if { $bytes == "" } { continue } if 0 { set f [open /tmp/vtkmesh.vtk "w"] fconfigure $f -translation binary -encoding binary puts -nonewline $f $bytes close $f } set length [string length $bytes] if { $_reportClientInfo } { set info {} lappend info "tool_id" [$dataobj hints toolid] lappend info "tool_name" [$dataobj hints toolname] lappend info "tool_title" [$dataobj hints tooltitle] lappend info "tool_command" [$dataobj hints toolcommand] lappend info "tool_revision" [$dataobj hints toolrevision] lappend info "dataset_label" [$dataobj hints label] lappend info "dataset_size" $length lappend info "dataset_tag" $tag SendCmd "clientinfo [list $info]" } SendCmd "dataset add $tag data follows $length" SendData $bytes set _datasets($tag) 1 SetObjectStyle $dataobj } if { [info exists _obj2ovride($dataobj-raise)] } { SendCmd "dataset visible 1 $tag" EventuallySetPolydataOpacity } } if {"" != $_first} { foreach axis { x y z } { set label [$_first label ${axis}] if { $label != "" } { SendCmd [list axis name $axis $label] } set units [$_first units ${axis}] if { $units != "" } { SendCmd [list axis units $axis $units] } } } InitSettings -outline if { $_reset } { # These are settings that rely on a dataset being loaded. InitSettings -polydataedges -polydatalighting -polydataopacity \ -polydatavisible -polydatawireframe #SendCmd "axis lformat all %g" $_arcball quaternion [ViewToQuaternion] SendCmd "camera reset" if { $_view(-ortho)} { SendCmd "camera mode ortho" } else { SendCmd "camera mode persp" } DoRotate PanCamera Zoom reset } set _reset 0 global readyForNextFrame set readyForNextFrame 0; # Don't advance to the next frame # until we get an image. # Actually write the commands to the server socket. If it fails, we don't # care. We're finished here. blt::busy hold $itk_component(hull) StopBufferingCommands blt::busy release $itk_component(hull) } # ---------------------------------------------------------------------- # USAGE: CurrentDatasets ?-all -visible? ?dataobjs? # # Returns a list of server IDs for the current datasets being displayed. This # is normally a single ID, but it might be a list of IDs if the current data # object has multiple components. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::CurrentDatasets {args} { set flag [lindex $args 0] switch -- $flag { "-all" { if { [llength $args] > 1 } { error "CurrentDatasets: can't specify dataobj after \"-all\"" } set dlist [get -objects] } "-visible" { if { [llength $args] > 1 } { set dlist {} set args [lrange $args 1 end] foreach dataobj $args { if { [info exists _obj2ovride($dataobj-raise)] } { lappend dlist $dataobj } } } else { set dlist [get -visible] } } default { set dlist $args } } set rlist "" foreach tag $dlist { if { [info exists _datasets($tag)] && $_datasets($tag) } { lappend rlist $tag } } return $rlist } # ---------------------------------------------------------------------- # USAGE: Zoom in # USAGE: Zoom out # USAGE: Zoom reset # # Called automatically when the user clicks on one of the zoom # controls for this widget. Changes the zoom for the current view. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::Zoom {option} { switch -- $option { "in" { set _view(-zoom) [expr {$_view(-zoom)*1.25}] SendCmd "camera zoom $_view(-zoom)" } "out" { set _view(-zoom) [expr {$_view(-zoom)*0.8}] SendCmd "camera zoom $_view(-zoom)" } "reset" { array set _view { -qw 0.853553 -qx -0.353553 -qy 0.353553 -qz 0.146447 -xpan 0 -ypan 0 -zoom 1.0 } if { $_first != "" } { set location [$_first hints camera] if { $location != "" } { array set _view $location } } $_arcball quaternion [ViewToQuaternion] DoRotate SendCmd "camera reset" } } } itcl::body Rappture::VtkMeshViewer::PanCamera {} { set x $_view(-xpan) set y $_view(-ypan) SendCmd "camera pan $x $y" } # ---------------------------------------------------------------------- # USAGE: Rotate click # USAGE: Rotate drag # USAGE: Rotate release # # Called automatically when the user clicks/drags/releases in the # plot area. Moves the plot according to the user's actions. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::Rotate {option x y} { switch -- $option { "click" { $itk_component(view) configure -cursor fleur set _click(x) $x set _click(y) $y } "drag" { if {[array size _click] == 0} { Rotate click $x $y } else { set w [winfo width $itk_component(view)] set h [winfo height $itk_component(view)] if {$w <= 0 || $h <= 0} { return } if {[catch { # this fails sometimes for no apparent reason set dx [expr {double($x-$_click(x))/$w}] set dy [expr {double($y-$_click(y))/$h}] }]} { return } if { $dx == 0 && $dy == 0 } { return } set q [$_arcball rotate $x $y $_click(x) $_click(y)] EventuallyRotate $q set _click(x) $x set _click(y) $y } } "release" { Rotate drag $x $y $itk_component(view) configure -cursor "" catch {unset _click} } default { error "bad option \"$option\": should be click, drag, release" } } } itcl::body Rappture::VtkMeshViewer::Pick {x y} { foreach tag [CurrentDatasets -visible] { SendCmd "dataset getscalar pixel $x $y $tag" } } # ---------------------------------------------------------------------- # USAGE: $this Pan click x y # $this Pan drag x y # $this Pan release x y # # Called automatically when the user clicks on one of the zoom # controls for this widget. Changes the zoom for the current view. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::Pan {option x y} { switch -- $option { "set" { set w [winfo width $itk_component(view)] set h [winfo height $itk_component(view)] set x [expr $x / double($w)] set y [expr $y / double($h)] set _view(-xpan) [expr $_view(-xpan) + $x] set _view(-ypan) [expr $_view(-ypan) + $y] PanCamera return } "click" { set _click(x) $x set _click(y) $y $itk_component(view) configure -cursor hand1 } "drag" { if { ![info exists _click(x)] } { set _click(x) $x } if { ![info exists _click(y)] } { set _click(y) $y } set w [winfo width $itk_component(view)] set h [winfo height $itk_component(view)] set dx [expr ($_click(x) - $x)/double($w)] set dy [expr ($_click(y) - $y)/double($h)] set _click(x) $x set _click(y) $y set _view(-xpan) [expr $_view(-xpan) - $dx] set _view(-ypan) [expr $_view(-ypan) - $dy] PanCamera } "release" { Pan drag $x $y $itk_component(view) configure -cursor "" } default { error "unknown option \"$option\": should set, click, drag, or release" } } } # ---------------------------------------------------------------------- # USAGE: InitSettings ?? # # Used internally to update rendering settings whenever parameters # change in the popup settings panel. Sends the new settings off # to the back end. # ---------------------------------------------------------------------- itcl::body Rappture::VtkMeshViewer::InitSettings { args } { foreach setting $args { AdjustSetting $setting } } # # AdjustSetting -- # # Changes/updates a specific setting in the widget. There are # usually user-setable option. Commands are sent to the render # server. # itcl::body Rappture::VtkMeshViewer::AdjustSetting {what {value ""}} { if { ![isconnected] } { return } switch -- $what { "-outline" { set bool $_settings($what) # Only display a outline for the currently visible sets. SendCmd "outline visible 0" foreach dataset [CurrentDatasets -visible] { SendCmd "outline visible $bool $dataset" } } "-polydataopacity" { set _settings($what) [expr $_widget($what) * 0.01] EventuallySetPolydataOpacity } "-polydatawireframe" { set bool $_settings($what) SendCmd "polydata wireframe $bool" } "-polydatavisible" { set bool $_settings($what) # Only change visibility of data sets marked "visible". foreach dataset [CurrentDatasets -visible] { SendCmd "polydata visible $bool $dataset" } } "-polydatalighting" { set bool $_settings($what) SendCmd "polydata lighting $bool" } "-polydataedges" { set bool $_settings($what) SendCmd "polydata edges $bool" } "-axesvisible" { set bool $_settings($what) SendCmd "axis visible all $bool" } "-axislabels" { set bool $_settings($what) SendCmd "axis labels all $bool" } "-axisminorticks" { set bool $_settings($what) SendCmd "axis minticks all $bool" } "-xgrid" { set bool $_settings($what) SendCmd "axis grid x $bool" } "-ygrid" { set bool $_settings($what) SendCmd "axis grid y $bool" } "-zgrid" { set bool $_settings($what) SendCmd "axis grid z $bool" } "-axismode" { set mode [$itk_component(axismode) value] set mode [$itk_component(axismode) translate $mode] SendCmd "axis flymode $mode" } default { error "don't know how to fix $what" } } } # ---------------------------------------------------------------------- # CONFIGURATION OPTION: -plotbackground # ---------------------------------------------------------------------- itcl::configbody Rappture::VtkMeshViewer::plotbackground { if { [isconnected] } { set rgb [Color2RGB $itk_option(-plotbackground)] SendCmd "screen bgcolor $rgb" } } # ---------------------------------------------------------------------- # CONFIGURATION OPTION: -plotforeground # ---------------------------------------------------------------------- itcl::configbody Rappture::VtkMeshViewer::plotforeground { if { [isconnected] } { set rgb [Color2RGB $itk_option(-plotforeground)] SendCmd "axis color all $rgb" SendCmd "outline color $rgb" } } itcl::body Rappture::VtkMeshViewer::BuildPolydataTab {} { set fg [option get $itk_component(hull) font Font] #set bfg [option get $itk_component(hull) boldFont Font] set inner [$itk_component(main) insert end \ -title "Mesh Settings" \ -icon [Rappture::icon mesh]] $inner configure -borderwidth 4 checkbutton $inner.mesh \ -text "Show Mesh" \ -variable [itcl::scope _settings(-polydatavisible)] \ -command [itcl::code $this AdjustSetting -polydatavisible] \ -font "Arial 9" -anchor w checkbutton $inner.outline \ -text "Show Outline" \ -variable [itcl::scope _settings(-outline)] \ -command [itcl::code $this AdjustSetting -outline] \ -font "Arial 9" -anchor w checkbutton $inner.wireframe \ -text "Show Wireframe" \ -variable [itcl::scope _settings(-polydatawireframe)] \ -command [itcl::code $this AdjustSetting -polydatawireframe] \ -font "Arial 9" -anchor w checkbutton $inner.lighting \ -text "Enable Lighting" \ -variable [itcl::scope _settings(-polydatalighting)] \ -command [itcl::code $this AdjustSetting -polydatalighting] \ -font "Arial 9" -anchor w checkbutton $inner.edges \ -text "Show Edges" \ -variable [itcl::scope _settings(-polydataedges)] \ -command [itcl::code $this AdjustSetting -polydataedges] \ -font "Arial 9" -anchor w itk_component add field_l { label $inner.field_l -text "Field" -font "Arial 9" } { ignore -font } itk_component add field { Rappture::Combobox $inner.field -width 10 -editable 0 } bind $inner.field <> \ [itcl::code $this AdjustSetting -field] label $inner.opacity_l -text "Opacity" -font "Arial 9" -anchor w ::scale $inner.opacity -from 0 -to 100 -orient horizontal \ -variable [itcl::scope _widget(-polydataopacity)] \ -width 10 \ -showvalue off \ -command [itcl::code $this AdjustSetting -polydataopacity] $inner.opacity set [expr $_settings(-polydataopacity) * 100.0] blt::table $inner \ 0,0 $inner.mesh -cspan 2 -anchor w -pady 2 \ 1,0 $inner.outline -cspan 2 -anchor w -pady 2 \ 2,0 $inner.wireframe -cspan 2 -anchor w -pady 2 \ 3,0 $inner.lighting -cspan 2 -anchor w -pady 2 \ 4,0 $inner.edges -cspan 2 -anchor w -pady 2 \ 5,0 $inner.opacity_l -anchor w -pady 2 \ 5,1 $inner.opacity -fill x -pady 2 blt::table configure $inner r* c* -resize none blt::table configure $inner r7 c1 -resize expand } itcl::body Rappture::VtkMeshViewer::BuildAxisTab {} { set fg [option get $itk_component(hull) font Font] #set bfg [option get $itk_component(hull) boldFont Font] set inner [$itk_component(main) insert end \ -title "Axis Settings" \ -icon [Rappture::icon axis2]] $inner configure -borderwidth 4 checkbutton $inner.visible \ -text "Axes" \ -variable [itcl::scope _settings(-axesvisible)] \ -command [itcl::code $this AdjustSetting -axesvisible] \ -font "Arial 9" checkbutton $inner.labels \ -text "Axis Labels" \ -variable [itcl::scope _settings(-axislabels)] \ -command [itcl::code $this AdjustSetting -axislabels] \ -font "Arial 9" label $inner.grid_l -text "Grid" -font "Arial 9" checkbutton $inner.xgrid \ -text "X" \ -variable [itcl::scope _settings(-xgrid)] \ -command [itcl::code $this AdjustSetting -xgrid] \ -font "Arial 9" checkbutton $inner.ygrid \ -text "Y" \ -variable [itcl::scope _settings(-ygrid)] \ -command [itcl::code $this AdjustSetting -ygrid] \ -font "Arial 9" checkbutton $inner.zgrid \ -text "Z" \ -variable [itcl::scope _settings(-zgrid)] \ -command [itcl::code $this AdjustSetting -zgrid] \ -font "Arial 9" checkbutton $inner.minorticks \ -text "Minor Ticks" \ -variable [itcl::scope _settings(-axisminorticks)] \ -command [itcl::code $this AdjustSetting -axisminorticks] \ -font "Arial 9" label $inner.mode_l -text "Mode" -font "Arial 9" itk_component add axismode { Rappture::Combobox $inner.mode -width 10 -editable 0 } $inner.mode choices insert end \ "static_triad" "static" \ "closest_triad" "closest" \ "furthest_triad" "farthest" \ "outer_edges" "outer" $itk_component(axismode) value "static" bind $inner.mode <> [itcl::code $this AdjustSetting -axismode] blt::table $inner \ 0,0 $inner.visible -anchor w -cspan 4 \ 1,0 $inner.labels -anchor w -cspan 4 \ 2,0 $inner.minorticks -anchor w -cspan 4 \ 4,0 $inner.grid_l -anchor w \ 4,1 $inner.xgrid -anchor w \ 4,2 $inner.ygrid -anchor w \ 4,3 $inner.zgrid -anchor w \ 5,0 $inner.mode_l -anchor w -padx { 2 0 } \ 5,1 $inner.mode -fill x -cspan 3 blt::table configure $inner r* c* -resize none blt::table configure $inner r7 c6 -resize expand blt::table configure $inner r3 -height 0.125i } itcl::body Rappture::VtkMeshViewer::BuildCameraTab {} { set inner [$itk_component(main) insert end \ -title "Camera Settings" \ -icon [Rappture::icon camera]] $inner configure -borderwidth 4 label $inner.view_l -text "view" -font "Arial 9" set f [frame $inner.view] foreach side { front back left right top bottom } { button $f.$side -image [Rappture::icon view$side] \ -command [itcl::code $this SetOrientation $side] Rappture::Tooltip::for $f.$side "Change the view to $side" pack $f.$side -side left } blt::table $inner \ 0,0 $inner.view_l -anchor e -pady 2 \ 0,1 $inner.view -anchor w -pady 2 blt::table configure $inner r0 -resize none set labels { qx qy qz qw xpan ypan zoom } set row 1 foreach tag $labels { label $inner.${tag}label -text $tag -font "Arial 9" entry $inner.${tag} -font "Arial 9" -bg white \ -textvariable [itcl::scope _view(-$tag)] bind $inner.${tag} \ [itcl::code $this camera set -${tag}] bind $inner.${tag} \ [itcl::code $this camera set -${tag}] blt::table $inner \ $row,0 $inner.${tag}label -anchor e -pady 2 \ $row,1 $inner.${tag} -anchor w -pady 2 blt::table configure $inner r$row -resize none incr row } checkbutton $inner.ortho \ -text "Orthographic Projection" \ -variable [itcl::scope _view(-ortho)] \ -command [itcl::code $this camera set -ortho] \ -font "Arial 9" blt::table $inner \ $row,0 $inner.ortho -cspan 2 -anchor w -pady 2 blt::table configure $inner r$row -resize none incr row blt::table configure $inner c* -resize none blt::table configure $inner c2 -resize expand blt::table configure $inner r$row -resize expand } # # camera -- # itcl::body Rappture::VtkMeshViewer::camera {option args} { switch -- $option { "show" { puts [array get _view] } "set" { set what [lindex $args 0] set x $_view($what) set code [catch { string is double $x } result] if { $code != 0 || !$result } { return } switch -- $what { "-ortho" { if {$_view($what)} { SendCmd "camera mode ortho" } else { SendCmd "camera mode persp" } } "-xpan" - "-ypan" { PanCamera } "-qx" - "-qy" - "-qz" - "-qw" { set q [ViewToQuaternion] $_arcball quaternion $q EventuallyRotate $q } "-zoom" { SendCmd "camera zoom $_view($what)" } } } } } itcl::body Rappture::VtkMeshViewer::GetVtkData { args } { set bytes "" foreach dataobj [get] { set contents [$dataobj vtkdata -full] append bytes "$contents\n" } return [list .vtk $bytes] } itcl::body Rappture::VtkMeshViewer::GetImage { args } { if { [image width $_image(download)] > 0 && [image height $_image(download)] > 0 } { set bytes [$_image(download) data -format "jpeg -quality 100"] set bytes [Rappture::encoding::decode -as b64 $bytes] return [list .jpg $bytes] } return "" } itcl::body Rappture::VtkMeshViewer::BuildDownloadPopup { popup command } { Rappture::Balloon $popup \ -title "[Rappture::filexfer::label downloadWord] as..." set inner [$popup component inner] label $inner.summary -text "" -anchor w radiobutton $inner.vtk_button -text "VTK data file" \ -variable [itcl::scope _downloadPopup(format)] \ -font "Arial 9" \ -value vtk Rappture::Tooltip::for $inner.vtk_button "Save as VTK data file." radiobutton $inner.image_button -text "Image File" \ -variable [itcl::scope _downloadPopup(format)] \ -value image Rappture::Tooltip::for $inner.image_button \ "Save as digital image." button $inner.ok -text "Save" \ -highlightthickness 0 -pady 2 -padx 3 \ -command $command \ -compound left \ -image [Rappture::icon download] button $inner.cancel -text "Cancel" \ -highlightthickness 0 -pady 2 -padx 3 \ -command [list $popup deactivate] \ -compound left \ -image [Rappture::icon cancel] blt::table $inner \ 0,0 $inner.summary -cspan 2 \ 1,0 $inner.vtk_button -anchor w -cspan 2 -padx { 4 0 } \ 2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \ 4,1 $inner.cancel -width .9i -fill y \ 4,0 $inner.ok -padx 2 -width .9i -fill y blt::table configure $inner r3 -height 4 blt::table configure $inner r4 -pady 4 raise $inner.image_button $inner.vtk_button invoke return $inner } itcl::body Rappture::VtkMeshViewer::SetObjectStyle { dataobj } { # Parse style string. set tag $dataobj set type [$dataobj type] array set style { -cloudstyle mesh -color white -edgecolor black -edges 1 -lighting 1 -linewidth 1.0 -opacity 1.0 -outline 0 -visible 1 -wireframe 0 } if {$type == "cloud"} { set style(-cloudstyle) points set style(-edges) 0 set style(-edgecolor) white } array set style [$dataobj hints style] if {[$dataobj hints color] != ""} { set style(-color) [$dataobj hints color] } SendCmd "outline add $tag" SendCmd "outline color [Color2RGB $style(-color)] $tag" SendCmd "outline visible $style(-outline) $tag" set _settings(-outline) $style(-outline) SendCmd "polydata add $tag" SendCmd "polydata visible $style(-visible) $tag" set _settings(-polydatavisible) $style(-visible) SendCmd "polydata cloudstyle $style(-cloudstyle) $tag" SendCmd "polydata edges $style(-edges) $tag" set _settings(-polydataedges) $style(-edges) SendCmd "polydata color [Color2RGB $style(-color)] $tag" SendCmd "polydata lighting $style(-lighting) $tag" set _settings(-polydatalighting) $style(-lighting) SendCmd "polydata linecolor [Color2RGB $style(-edgecolor)] $tag" SendCmd "polydata linewidth $style(-linewidth) $tag" SendCmd "polydata opacity $style(-opacity) $tag" set _settings(-polydataopacity) $style(-opacity) set _widget(-polydataopacity) [expr 100.0 * $style(-opacity)] SendCmd "polydata wireframe $style(-wireframe) $tag" set _settings(-polydatawireframe) $style(-wireframe) set havePolyData 1 } itcl::body Rappture::VtkMeshViewer::IsValidObject { dataobj } { if {[catch {$dataobj isa Rappture::Mesh} valid] != 0 || !$valid} { return 0 } return 1 } itcl::body Rappture::VtkMeshViewer::SetOrientation { side } { array set positions { front "1 0 0 0" back "0 0 1 0" left "0.707107 0 -0.707107 0" right "0.707107 0 0.707107 0" top "0.707107 -0.707107 0 0" bottom "0.707107 0.707107 0 0" } foreach name { -qw -qx -qy -qz } value $positions($side) { set _view($name) $value } set q [ViewToQuaternion] $_arcball quaternion $q SendCmd "camera orient $q" SendCmd "camera reset" set _view(-xpan) 0 set _view(-ypan) 0 set _view(-zoom) 1.0 }