[3330] | 1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
[1373] | 2 | |
---|
[839] | 3 | # ---------------------------------------------------------------------- |
---|
| 4 | # COMPONENT: unirect2d - represents a uniform rectangular 2-D mesh. |
---|
| 5 | # |
---|
| 6 | # This object represents one field in an XML description of a device. |
---|
| 7 | # It simplifies the process of extracting data vectors that represent |
---|
| 8 | # the field. |
---|
| 9 | # ====================================================================== |
---|
| 10 | # AUTHOR: Michael McLennan, Purdue University |
---|
[3177] | 11 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[839] | 12 | # |
---|
| 13 | # See the file "license.terms" for information on usage and |
---|
| 14 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 15 | # ====================================================================== |
---|
| 16 | package require Itcl |
---|
| 17 | package require BLT |
---|
| 18 | |
---|
| 19 | namespace eval Rappture { # forward declaration } |
---|
| 20 | |
---|
[1373] | 21 | itcl::class Rappture::Unirect2d { |
---|
[3571] | 22 | private variable _axisOrder "x y" |
---|
| 23 | private variable _xMax 0 |
---|
| 24 | private variable _xMin 0 |
---|
| 25 | private variable _xNum 0 |
---|
| 26 | private variable _yMax 0 |
---|
| 27 | private variable _yMin 0 |
---|
| 28 | private variable _yNum 0 |
---|
| 29 | private variable _hints |
---|
| 30 | private variable _vtkdata "" |
---|
| 31 | private variable _numPoints 0 |
---|
| 32 | |
---|
| 33 | private common _xp2obj ; # used for fetch/release ref counting |
---|
| 34 | private common _obj2ref ; # used for fetch/release ref counting |
---|
| 35 | private variable _isValid 0; # Indicates if the data is valid. |
---|
| 36 | |
---|
[4474] | 37 | private method GetString { obj path varName } |
---|
| 38 | private method GetValue { obj path varName } |
---|
| 39 | private method GetSize { obj path varName } |
---|
| 40 | |
---|
[3330] | 41 | constructor {xmlobj path} { |
---|
| 42 | # defined below |
---|
| 43 | } |
---|
[3571] | 44 | destructor { |
---|
| 45 | # defined below |
---|
| 46 | } |
---|
[3330] | 47 | public proc fetch {xmlobj path} |
---|
| 48 | public proc release {obj} |
---|
[839] | 49 | public method limits {axis} |
---|
[4474] | 50 | public method units { axis } |
---|
| 51 | public method label { axis } |
---|
[839] | 52 | public method blob {} |
---|
[3571] | 53 | public method hints {{keyword ""}} |
---|
[839] | 54 | public method mesh {} |
---|
[3330] | 55 | public method dimensions {} { |
---|
| 56 | return 2 |
---|
| 57 | } |
---|
[3571] | 58 | public method isvalid {} { |
---|
| 59 | return $_isValid |
---|
| 60 | } |
---|
[3330] | 61 | public method numpoints {} { |
---|
| 62 | return $_numPoints |
---|
| 63 | } |
---|
| 64 | public method vtkdata {} { |
---|
| 65 | return $_vtkdata |
---|
| 66 | } |
---|
[839] | 67 | } |
---|
| 68 | |
---|
[3330] | 69 | # |
---|
| 70 | # fetch <xmlobj> <path> |
---|
| 71 | # |
---|
| 72 | # Clients use this instead of a constructor to fetch the Mesh for a |
---|
| 73 | # particular <path> in the <xmlobj>. When the client is done with the mesh, |
---|
| 74 | # he calls "release" to decrement the reference count. When the mesh is no |
---|
| 75 | # longer needed, it is cleaned up automatically. |
---|
| 76 | # |
---|
| 77 | itcl::body Rappture::Unirect2d::fetch {xmlobj path} { |
---|
| 78 | set handle "$xmlobj|$path" |
---|
| 79 | if {[info exists _xp2obj($handle)]} { |
---|
| 80 | set obj $_xp2obj($handle) |
---|
| 81 | incr _obj2ref($obj) |
---|
| 82 | return $obj |
---|
| 83 | } |
---|
| 84 | set obj [Rappture::Unirect2d ::#auto $xmlobj $path] |
---|
| 85 | set _xp2obj($handle) $obj |
---|
| 86 | set _obj2ref($obj) 1 |
---|
| 87 | return $obj |
---|
| 88 | } |
---|
| 89 | |
---|
[839] | 90 | # ---------------------------------------------------------------------- |
---|
[4474] | 91 | # USAGE: Rappture::Unirect2d::release <obj> |
---|
[3330] | 92 | # |
---|
| 93 | # Clients call this when they're no longer using a Mesh fetched |
---|
| 94 | # previously by the "fetch" proc. This decrements the reference |
---|
| 95 | # count for the mesh and destroys the object when it is no longer |
---|
| 96 | # in use. |
---|
| 97 | # ---------------------------------------------------------------------- |
---|
| 98 | itcl::body Rappture::Unirect2d::release { obj } { |
---|
| 99 | if { ![info exists _obj2ref($obj)] } { |
---|
| 100 | error "can't find reference count for $obj" |
---|
| 101 | } |
---|
| 102 | incr _obj2ref($obj) -1 |
---|
| 103 | if {$_obj2ref($obj) <= 0} { |
---|
| 104 | unset _obj2ref($obj) |
---|
| 105 | foreach handle [array names _xp2obj] { |
---|
| 106 | if {$_xp2obj($handle) == $obj} { |
---|
| 107 | unset _xp2obj($handle) |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | itcl::delete object $obj |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | # ---------------------------------------------------------------------- |
---|
[839] | 115 | # Constructor |
---|
| 116 | # ---------------------------------------------------------------------- |
---|
[3330] | 117 | itcl::body Rappture::Unirect2d::constructor {xmlobj path} { |
---|
[839] | 118 | if {![Rappture::library isvalid $xmlobj]} { |
---|
[1929] | 119 | error "bad value \"$xmlobj\": should be Rappture::library" |
---|
[839] | 120 | } |
---|
[1463] | 121 | set m [$xmlobj element -as object $path] |
---|
| 122 | GetValue $m "xaxis.min" _xMin |
---|
| 123 | GetValue $m "xaxis.max" _xMax |
---|
| 124 | GetSize $m "xaxis.numpoints" _xNum |
---|
| 125 | GetValue $m "yaxis.min" _yMin |
---|
| 126 | GetValue $m "yaxis.max" _yMax |
---|
| 127 | GetSize $m "yaxis.numpoints" _yNum |
---|
[1373] | 128 | foreach {key path} { |
---|
[1929] | 129 | group about.group |
---|
| 130 | label about.label |
---|
| 131 | color about.color |
---|
| 132 | style about.style |
---|
| 133 | type about.type |
---|
| 134 | xlabel xaxis.label |
---|
| 135 | xdesc xaxis.description |
---|
| 136 | xunits xaxis.units |
---|
| 137 | xscale xaxis.scale |
---|
| 138 | xmin xaxis.min |
---|
| 139 | xmax xaxis.max |
---|
| 140 | ylabel yaxis.label |
---|
| 141 | ydesc yaxis.description |
---|
| 142 | yunits yaxis.units |
---|
| 143 | yscale yaxis.scale |
---|
| 144 | ymin yaxis.min |
---|
| 145 | ymax yaxis.max |
---|
[2744] | 146 | type about.type |
---|
[1373] | 147 | } { |
---|
[1929] | 148 | set str [$m get $path] |
---|
| 149 | if {"" != $str} { |
---|
| 150 | set _hints($key) $str |
---|
| 151 | } |
---|
[1373] | 152 | } |
---|
[1463] | 153 | itcl::delete object $m |
---|
[3330] | 154 | set _numPoints [expr $_xNum * $_yNum] |
---|
| 155 | if { $_numPoints == 0 } { |
---|
| 156 | set _vtkdata "" |
---|
[3571] | 157 | return |
---|
[1373] | 158 | } |
---|
[3330] | 159 | append out "DATASET STRUCTURED_POINTS\n" |
---|
| 160 | append out "DIMENSIONS $_xNum $_yNum 1" |
---|
| 161 | set xSpace [expr ($_xMax - $_xMin) / double($_xNum - 1)] |
---|
| 162 | set ySpace [expr ($_yMax - $_yMin) / double($_yNum - 1)] |
---|
| 163 | append out "SPACING $xSpace $ySpace 0\n" |
---|
| 164 | append out "ORIGIN 0 0 0\n" |
---|
| 165 | set _vtkdata $out |
---|
[3571] | 166 | set _isValid 1 |
---|
[839] | 167 | } |
---|
| 168 | |
---|
| 169 | # ---------------------------------------------------------------------- |
---|
| 170 | # Destructor |
---|
| 171 | # ---------------------------------------------------------------------- |
---|
[1373] | 172 | itcl::body Rappture::Unirect2d::destructor {} { |
---|
[3330] | 173 | # empty |
---|
[839] | 174 | } |
---|
| 175 | |
---|
| 176 | # ---------------------------------------------------------------------- |
---|
| 177 | # method blob |
---|
[2744] | 178 | # Returns a base64 encoded, gzipped Tcl list that represents the |
---|
| 179 | # Tcl command and data to recreate the uniform rectangular grid |
---|
| 180 | # on the nanovis server. |
---|
[839] | 181 | # ---------------------------------------------------------------------- |
---|
[1373] | 182 | itcl::body Rappture::Unirect2d::blob {} { |
---|
[839] | 183 | set data "unirect2d" |
---|
[1463] | 184 | lappend data "xmin" $_xMin "xmax" $_xMax "xnum" $_xNum |
---|
| 185 | lappend data "ymin" $_yMin "ymax" $_yMax "ynum" $_yNum |
---|
[839] | 186 | return $data |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | # ---------------------------------------------------------------------- |
---|
[2128] | 190 | # method mesh |
---|
[2744] | 191 | # Returns a base64 encoded, gzipped Tcl list that represents the |
---|
| 192 | # Tcl command and data to recreate the uniform rectangular grid |
---|
| 193 | # on the nanovis server. |
---|
[2128] | 194 | # ---------------------------------------------------------------------- |
---|
| 195 | itcl::body Rappture::Unirect2d::mesh {} { |
---|
| 196 | lappend out $_xMin $_xMax $_xNum $_yMin $_yMax $_yNum |
---|
| 197 | return $out |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | # ---------------------------------------------------------------------- |
---|
[839] | 201 | # method limits <axis> |
---|
[2744] | 202 | # Returns a list {min max} representing the limits for the |
---|
| 203 | # specified axis. |
---|
[839] | 204 | # ---------------------------------------------------------------------- |
---|
[1373] | 205 | itcl::body Rappture::Unirect2d::limits {which} { |
---|
[839] | 206 | set min "" |
---|
| 207 | set max "" |
---|
| 208 | switch -- $which { |
---|
[1929] | 209 | x - xlin - xlog { |
---|
| 210 | set min $_xMin |
---|
| 211 | set max $_xMax |
---|
| 212 | } |
---|
| 213 | y - ylin - ylog { |
---|
| 214 | set min $_yMin |
---|
| 215 | set max $_yMax |
---|
| 216 | } |
---|
[4474] | 217 | z - zlin - zlog { |
---|
| 218 | set min 0 |
---|
| 219 | set max 0 |
---|
| 220 | } |
---|
[1929] | 221 | default { |
---|
| 222 | error "unknown axis description \"$which\"" |
---|
| 223 | } |
---|
[839] | 224 | } |
---|
| 225 | return [list $min $max] |
---|
| 226 | } |
---|
| 227 | |
---|
[4474] | 228 | # |
---|
| 229 | # units -- |
---|
| 230 | # |
---|
| 231 | # Returns the units of the given axis. |
---|
| 232 | # |
---|
| 233 | itcl::body Rappture::Unirect2d::units { axis } { |
---|
| 234 | if { [info exists _hints(${axis}units)] } { |
---|
| 235 | return $_hints(${axis}units) |
---|
| 236 | } |
---|
| 237 | return "" |
---|
| 238 | } |
---|
[1111] | 239 | |
---|
[4474] | 240 | # |
---|
| 241 | # label -- |
---|
| 242 | # |
---|
| 243 | # Returns the label of the given axis. |
---|
| 244 | # |
---|
| 245 | itcl::body Rappture::Unirect2d::label { axis } { |
---|
| 246 | if { [info exists _hints(${axis}label)] } { |
---|
| 247 | return $_hints(${axis}label) |
---|
| 248 | } |
---|
| 249 | return "" |
---|
| 250 | } |
---|
| 251 | |
---|
[1111] | 252 | # ---------------------------------------------------------------------- |
---|
| 253 | # USAGE: hints ?<keyword>? |
---|
| 254 | # |
---|
| 255 | # Returns a list of key/value pairs for various hints about plotting |
---|
| 256 | # this curve. If a particular <keyword> is specified, then it returns |
---|
| 257 | # the hint for that <keyword>, if it exists. |
---|
| 258 | # ---------------------------------------------------------------------- |
---|
[1373] | 259 | itcl::body Rappture::Unirect2d::hints { {keyword ""} } { |
---|
| 260 | if {[info exists _hints(xlabel)] && "" != $_hints(xlabel) |
---|
[1929] | 261 | && [info exists _hints(xunits)] && "" != $_hints(xunits)} { |
---|
| 262 | set _hints(xlabel) "$_hints(xlabel) ($_hints(xunits))" |
---|
[1111] | 263 | } |
---|
[1373] | 264 | if {[info exists _hints(ylabel)] && "" != $_hints(ylabel) |
---|
[1929] | 265 | && [info exists _hints(yunits)] && "" != $_hints(yunits)} { |
---|
| 266 | set _hints(ylabel) "$_hints(ylabel) ($_hints(yunits))" |
---|
[1373] | 267 | } |
---|
| 268 | if {[info exists _hints(group)] && [info exists _hints(label)]} { |
---|
[1929] | 269 | # pop-up help for each curve |
---|
| 270 | set _hints(tooltip) $_hints(label) |
---|
[1373] | 271 | } |
---|
[1111] | 272 | if {$keyword != ""} { |
---|
[1929] | 273 | if {[info exists _hints($keyword)]} { |
---|
| 274 | return $_hints($keyword) |
---|
| 275 | } |
---|
| 276 | return "" |
---|
[1111] | 277 | } |
---|
| 278 | return [array get _hints] |
---|
| 279 | } |
---|
[1463] | 280 | |
---|
| 281 | itcl::body Rappture::Unirect2d::GetSize { obj path varName } { |
---|
| 282 | set string [$obj get $path] |
---|
| 283 | if { [scan $string "%d" value] != 1 || $value < 0 } { |
---|
[1929] | 284 | puts stderr "can't get size \"$string\" of \"$path\"" |
---|
| 285 | return |
---|
[1463] | 286 | } |
---|
| 287 | upvar $varName size |
---|
| 288 | set size $value |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | itcl::body Rappture::Unirect2d::GetValue { obj path varName } { |
---|
| 292 | set string [$obj get $path] |
---|
| 293 | if { [scan $string "%g" value] != 1 } { |
---|
[1929] | 294 | return |
---|
[1463] | 295 | } |
---|
| 296 | upvar $varName number |
---|
| 297 | set number $value |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | itcl::body Rappture::Unirect2d::GetString { obj path varName } { |
---|
| 301 | set string [$obj get $path] |
---|
| 302 | if { $string == "" } { |
---|
[1929] | 303 | puts stderr "can't get string \"$string\" of \"$path\"" |
---|
| 304 | return |
---|
[1463] | 305 | } |
---|
| 306 | upvar $varName str |
---|
| 307 | set str $string |
---|
| 308 | } |
---|
[3330] | 309 | |
---|
| 310 | |
---|