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