[3330] | 1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
[2504] | 2 | |
---|
[1] | 3 | # ---------------------------------------------------------------------- |
---|
| 4 | # COMPONENT: field - extracts data from an XML description of a field |
---|
| 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 |
---|
[115] | 12 | # |
---|
| 13 | # See the file "license.terms" for information on usage and |
---|
| 14 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[1] | 15 | # ====================================================================== |
---|
| 16 | |
---|
[3406] | 17 | # TODO: |
---|
| 18 | # |
---|
| 19 | # o How to describe vector values in a field? |
---|
| 20 | # <components>3</components> |
---|
| 21 | # <values></values> |
---|
| 22 | # |
---|
| 23 | # Does anything need to know the limits for each component of the vector? |
---|
| 24 | # |
---|
[1] | 25 | |
---|
[3330] | 26 | # |
---|
| 27 | # Possible field dataset types: |
---|
| 28 | # |
---|
| 29 | # 2D Datasets |
---|
| 30 | # vtk (range of z-axis is zero). |
---|
| 31 | # unirect2d (deprecated except where extents > 1) |
---|
| 32 | # cloud (x,y point coordinates) (deprecated) |
---|
| 33 | # mesh |
---|
| 34 | # 3D Datasets |
---|
| 35 | # vtk |
---|
| 36 | # unirect3d |
---|
| 37 | # cloud (x,y,z coordinates) (deprecated) |
---|
| 38 | # mesh |
---|
| 39 | # dx (FIXME: make dx-to-vtk converter work) |
---|
| 40 | # ucd avs |
---|
| 41 | # |
---|
| 42 | # Viewers: |
---|
| 43 | # Format Dim Description Viewer Server |
---|
| 44 | # vtk 2 vtk file data. contour vtkvis |
---|
| 45 | # vtk 3 vtk file data. isosurface vtkvis |
---|
| 46 | # mesh 2 points-on-mesh heightmap vtkvis |
---|
| 47 | # mesh 3 points-on-mesh isosurface vtkvis |
---|
| 48 | # dx 3 DX volume nanovis |
---|
| 49 | # unirect2d 2 unirect3d + extents > 1 flow flow nanovis |
---|
| 50 | # unirect3d 3 unirect2d + extents > 1 flow flow nanovis |
---|
| 51 | # |
---|
[3406] | 52 | # With <views>, can specify which viewer for a specific datasets. So it's OK |
---|
| 53 | # to if the same dataset can be viewed in more than one way. |
---|
| 54 | # o Any 2D dataset can be viewed as a contour/heightmap. |
---|
[3701] | 55 | # o Any 3D dataset can be viewed as a isosurface. |
---|
[3406] | 56 | # o Any 2D dataset with vector data can be streamlines. |
---|
| 57 | # o Any 3D uniform rectilinear dataset can be viewed as a volume. |
---|
| 58 | # o Any 3D dataset with vector data can be streamlines or flow. |
---|
[3700] | 59 | # |
---|
| 60 | # Vector data 2/3 streamlines |
---|
| 61 | # Scalar data 1/ |
---|
[3330] | 62 | # |
---|
[3406] | 63 | # Need <views> to properly do things like qdot: volume with a polydata |
---|
| 64 | # transparent shell. The view will combine the two objects <field> and |
---|
| 65 | # <drawing> (??) into a single viewer. |
---|
[3330] | 66 | # |
---|
[3406] | 67 | package require Itcl |
---|
| 68 | package require BLT |
---|
| 69 | |
---|
| 70 | namespace eval Rappture { |
---|
| 71 | # forward declaration |
---|
| 72 | } |
---|
| 73 | |
---|
[1] | 74 | itcl::class Rappture::Field { |
---|
[3754] | 75 | protected variable _dim 0; # Dimension of the mesh |
---|
[3571] | 76 | private variable _xmlobj ""; # ref to XML obj with field data |
---|
| 77 | private variable _limits; # maps axis name => {z0 z1} limits |
---|
| 78 | private variable _field "" |
---|
| 79 | private variable _comp2fldName ; # cname => field names. |
---|
[3776] | 80 | private variable _comp2type ; # cname => type (e.g. "vectors") |
---|
[3684] | 81 | private variable _comp2size ; # cname => # of components in element |
---|
[3707] | 82 | private variable _comp2assoc; # cname => association (e.g. pointdata) |
---|
[3571] | 83 | private variable _fld2Components; # field name => number of components |
---|
| 84 | private variable _fld2Label; # field name => label |
---|
| 85 | private variable _fld2Units; # field name => units |
---|
[3330] | 86 | private variable _hints |
---|
[3405] | 87 | private variable _viewer ""; # Hints which viewer to use |
---|
[3546] | 88 | private variable _xv ""; # For 1D meshes only. Holds the points |
---|
[3571] | 89 | private variable _isValid 0; # Indicates if the field contains |
---|
| 90 | # valid data. |
---|
[3594] | 91 | private variable _isValidComponent; # Array of valid components found |
---|
[3330] | 92 | constructor {xmlobj path} { |
---|
| 93 | # defined below |
---|
| 94 | } |
---|
| 95 | destructor { |
---|
| 96 | # defined below |
---|
| 97 | } |
---|
[3571] | 98 | public method blob { cname } |
---|
[11] | 99 | public method components {args} |
---|
[3571] | 100 | public method controls {option args} |
---|
| 101 | public method extents {{cname -overall}} |
---|
[3684] | 102 | public method numComponents {cname} |
---|
[3429] | 103 | public method fieldlimits {} |
---|
[3571] | 104 | public method flowhints { cname } |
---|
[1] | 105 | public method hints {{key ""}} |
---|
[1189] | 106 | public method isunirect2d {} |
---|
[1373] | 107 | public method isunirect3d {} |
---|
[3571] | 108 | public method limits {axis} |
---|
| 109 | public method mesh {{cname -overall}} |
---|
| 110 | public method style { cname } |
---|
[2504] | 111 | public method type {} |
---|
[3571] | 112 | public method values { cname } |
---|
[3330] | 113 | public method vtkdata {cname} |
---|
[3571] | 114 | |
---|
[3405] | 115 | public method fieldnames { cname } { |
---|
[3571] | 116 | if { ![info exists _comp2fldName($cname)] } { |
---|
[3405] | 117 | return "" |
---|
| 118 | } |
---|
[3571] | 119 | return $_comp2fldName($cname) |
---|
[3405] | 120 | } |
---|
| 121 | public method fieldinfo { fname } { |
---|
[3571] | 122 | lappend out $_fld2Label($fname) |
---|
| 123 | lappend out $_fld2Units($fname) |
---|
| 124 | lappend out $_fld2Components($fname) |
---|
[3405] | 125 | return $out |
---|
| 126 | } |
---|
[3571] | 127 | public method isvalid {} { |
---|
| 128 | return $_isValid |
---|
| 129 | } |
---|
| 130 | public method viewer {} { |
---|
| 131 | return $_viewer |
---|
| 132 | } |
---|
[3330] | 133 | protected method Build {} |
---|
[11] | 134 | protected method _getValue {expr} |
---|
[1] | 135 | |
---|
[2744] | 136 | private variable _path ""; # Path of this object in the XML |
---|
| 137 | private variable _units "" ; # system of units for this field |
---|
[1] | 138 | private variable _zmax 0 ;# length of the device |
---|
| 139 | |
---|
[11] | 140 | private variable _comp2dims ;# maps component name => dimensionality |
---|
| 141 | private variable _comp2xy ;# maps component name => x,y vectors |
---|
[3330] | 142 | private variable _comp2vtk ;# maps component name => vtk file data |
---|
[447] | 143 | private variable _comp2dx ;# maps component name => OpenDX data |
---|
[839] | 144 | private variable _comp2unirect2d ;# maps component name => unirect2d obj |
---|
[1463] | 145 | private variable _comp2unirect3d ;# maps component name => unirect3d obj |
---|
[136] | 146 | private variable _comp2style ;# maps component name => style settings |
---|
[1] | 147 | private variable _comp2cntls ;# maps component name => x,y control points |
---|
[1373] | 148 | private variable _comp2extents |
---|
[3330] | 149 | private variable _comp2limits; # Array of limits per component |
---|
[2504] | 150 | private variable _type "" |
---|
[1441] | 151 | private variable _comp2flowhints |
---|
[3330] | 152 | private variable _comp2mesh |
---|
[1] | 153 | private common _counter 0 ;# counter for unique vector names |
---|
[3027] | 154 | |
---|
[3684] | 155 | private method AvsToVtk { cname contents } |
---|
[3330] | 156 | private method BuildPointsOnMesh { cname } |
---|
[3027] | 157 | private method ConvertToVtkData { cname } |
---|
[3701] | 158 | protected method GetAssociation { cname } |
---|
| 159 | protected method GetTypeAndSize { cname } |
---|
[3699] | 160 | protected method ReadVtkDataSet { cname contents } |
---|
| 161 | |
---|
[3585] | 162 | private method VerifyVtkDataSet { contents } |
---|
[3330] | 163 | private variable _values "" |
---|
[1] | 164 | } |
---|
| 165 | |
---|
| 166 | # ---------------------------------------------------------------------- |
---|
| 167 | # CONSTRUCTOR |
---|
| 168 | # ---------------------------------------------------------------------- |
---|
[11] | 169 | itcl::body Rappture::Field::constructor {xmlobj path} { |
---|
[3491] | 170 | package require vtk |
---|
[11] | 171 | if {![Rappture::library isvalid $xmlobj]} { |
---|
[1570] | 172 | error "bad value \"$xmlobj\": should be Rappture::library" |
---|
[1] | 173 | } |
---|
[11] | 174 | set _xmlobj $xmlobj |
---|
[2616] | 175 | set _path $path |
---|
[3571] | 176 | set _field [$xmlobj element -as object $path] |
---|
| 177 | set _units [$_field get units] |
---|
[1] | 178 | |
---|
[87] | 179 | set xunits [$xmlobj get units] |
---|
| 180 | if {"" == $xunits || "arbitrary" == $xunits} { |
---|
[1570] | 181 | set xunits "um" |
---|
[87] | 182 | } |
---|
| 183 | |
---|
[1] | 184 | # determine the overall size of the device |
---|
| 185 | set z0 [set z1 0] |
---|
[11] | 186 | foreach elem [$_xmlobj children components] { |
---|
[1570] | 187 | switch -glob -- $elem { |
---|
| 188 | box* { |
---|
| 189 | if {![regexp {[0-9]$} $elem]} { |
---|
| 190 | set elem "${elem}0" |
---|
| 191 | } |
---|
| 192 | set z0 [$_xmlobj get components.$elem.corner0] |
---|
| 193 | set z0 [Rappture::Units::convert $z0 \ |
---|
| 194 | -context $xunits -to $xunits -units off] |
---|
[11] | 195 | |
---|
[1570] | 196 | set z1 [$_xmlobj get components.$elem.corner1] |
---|
| 197 | set z1 [Rappture::Units::convert $z1 \ |
---|
| 198 | -context $xunits -to $xunits -units off] |
---|
[11] | 199 | |
---|
[1570] | 200 | set _limits($elem) [list $z0 $z1] |
---|
| 201 | } |
---|
| 202 | } |
---|
[1] | 203 | } |
---|
| 204 | set _zmax $z1 |
---|
| 205 | |
---|
| 206 | # build up vectors for various components of the field |
---|
[3330] | 207 | Build |
---|
[1] | 208 | } |
---|
| 209 | |
---|
| 210 | # ---------------------------------------------------------------------- |
---|
| 211 | # DESTRUCTOR |
---|
| 212 | # ---------------------------------------------------------------------- |
---|
| 213 | itcl::body Rappture::Field::destructor {} { |
---|
[3571] | 214 | itcl::delete object $_field |
---|
[11] | 215 | # don't destroy the _xmlobj! we don't own it! |
---|
[1] | 216 | |
---|
[11] | 217 | foreach name [array names _comp2xy] { |
---|
[1570] | 218 | eval blt::vector destroy $_comp2xy($name) |
---|
[1] | 219 | } |
---|
[839] | 220 | foreach name [array names _comp2unirect2d] { |
---|
[1570] | 221 | itcl::delete object $_comp2unirect2d($name) |
---|
[839] | 222 | } |
---|
[1373] | 223 | foreach name [array names _comp2unirect3d] { |
---|
[1570] | 224 | itcl::delete object $_comp2unirect3d($name) |
---|
[1373] | 225 | } |
---|
[1441] | 226 | foreach name [array names _comp2flowhints] { |
---|
[1570] | 227 | itcl::delete object $_comp2flowhints($name) |
---|
[1441] | 228 | } |
---|
[3330] | 229 | foreach name [array names _comp2mesh] { |
---|
| 230 | # Data is in the form of a mesh and a vector. |
---|
| 231 | foreach { mesh vector } $_comp2mesh($name) break |
---|
| 232 | # Release the mesh (may be shared) |
---|
| 233 | set class [$mesh info class] |
---|
| 234 | ${class}::release $mesh |
---|
| 235 | # Destroy the vector |
---|
| 236 | blt::vector destroy $vector |
---|
| 237 | } |
---|
[1] | 238 | } |
---|
| 239 | |
---|
| 240 | # ---------------------------------------------------------------------- |
---|
[136] | 241 | # USAGE: components ?-name|-dimensions|-style? ?<pattern>? |
---|
[1] | 242 | # |
---|
[11] | 243 | # Returns a list of names or types for the various components of |
---|
| 244 | # this field. If the optional glob-style <pattern> is specified, |
---|
| 245 | # then it returns only the components with names matching the pattern. |
---|
[1] | 246 | # ---------------------------------------------------------------------- |
---|
[11] | 247 | itcl::body Rappture::Field::components {args} { |
---|
| 248 | Rappture::getopts args params { |
---|
[1570] | 249 | flag what -name default |
---|
| 250 | flag what -dimensions |
---|
| 251 | flag what -style |
---|
| 252 | flag what -particles |
---|
| 253 | flag what -flow |
---|
| 254 | flag what -box |
---|
[11] | 255 | } |
---|
| 256 | |
---|
| 257 | set pattern * |
---|
| 258 | if {[llength $args] > 0} { |
---|
[1570] | 259 | set pattern [lindex $args 0] |
---|
| 260 | set args [lrange $args 1 end] |
---|
[11] | 261 | } |
---|
| 262 | if {[llength $args] > 0} { |
---|
[1570] | 263 | error "wrong # args: should be \"components ?switches? ?pattern?\"" |
---|
[11] | 264 | } |
---|
| 265 | |
---|
[3571] | 266 | # There's only one dimension of the field. Components can't have |
---|
| 267 | # different dimensions in the same field. They would by definition be |
---|
| 268 | # using different meshes and viewers. |
---|
| 269 | if { $params(what) == "-dimensions" } { |
---|
| 270 | return "${_dim}D" |
---|
| 271 | } |
---|
| 272 | # BE CAREFUL: return component names in proper order |
---|
[1] | 273 | set rlist "" |
---|
[3581] | 274 | set components {} |
---|
| 275 | # First compile a list of valid components that match the pattern |
---|
| 276 | foreach cname [$_field children -type component] { |
---|
| 277 | if { ![info exists _isValidComponent($cname)] } { |
---|
| 278 | continue |
---|
| 279 | } |
---|
| 280 | if { [string match $pattern $cname] } { |
---|
| 281 | lappend components $cname |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | # Now handle the tests. |
---|
[3571] | 285 | switch -- $params(what) { |
---|
| 286 | -name { |
---|
[3581] | 287 | set rlist $components |
---|
[1570] | 288 | } |
---|
[3571] | 289 | -style { |
---|
| 290 | foreach cname $components { |
---|
| 291 | if { [info exists _comp2style($cname)] } { |
---|
| 292 | lappend rlist $_comp2style($cname) |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|
[1] | 296 | } |
---|
| 297 | return $rlist |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | # ---------------------------------------------------------------------- |
---|
[11] | 301 | # USAGE: mesh ?<name>? |
---|
[1] | 302 | # |
---|
| 303 | # Returns a list {xvec yvec} for the specified field component <name>. |
---|
| 304 | # If the name is not specified, then it returns the vectors for the |
---|
| 305 | # overall field (sum of all components). |
---|
| 306 | # ---------------------------------------------------------------------- |
---|
[3330] | 307 | itcl::body Rappture::Field::mesh {{cname -overall}} { |
---|
| 308 | if {$cname == "-overall" || $cname == "component0"} { |
---|
| 309 | set cname [lindex [components -name] 0] |
---|
[9] | 310 | } |
---|
[3330] | 311 | if {[info exists _comp2xy($cname)]} { |
---|
| 312 | return [lindex $_comp2xy($cname) 0] ;# return xv |
---|
[1] | 313 | } |
---|
[3330] | 314 | if { [info exists _comp2vtk($cname)] } { |
---|
| 315 | # FIXME: extract mesh from VTK file data. |
---|
[3546] | 316 | if { $_comp2dims($cname) == "1D" } { |
---|
| 317 | return $_xv |
---|
| 318 | } |
---|
[3330] | 319 | error "method \"mesh\" is not implemented for VTK file data" |
---|
[2504] | 320 | } |
---|
[3330] | 321 | if {[info exists _comp2dx($cname)]} { |
---|
[1570] | 322 | return "" ;# no mesh -- it's embedded in the value data |
---|
[447] | 323 | } |
---|
[3330] | 324 | if {[info exists _comp2mesh($cname)]} { |
---|
[2727] | 325 | return "" ;# no mesh -- it's embedded in the value data |
---|
| 326 | } |
---|
[3330] | 327 | if {[info exists _comp2unirect2d($cname)]} { |
---|
| 328 | set mobj [lindex $_comp2unirect2d($cname) 0] |
---|
[1570] | 329 | return [$mobj mesh] |
---|
[839] | 330 | } |
---|
[3330] | 331 | if {[info exists _comp2unirect3d($cname)]} { |
---|
| 332 | set mobj [lindex $_comp2unirect3d($cname) 0] |
---|
[1570] | 333 | return [$mobj mesh] |
---|
[1373] | 334 | } |
---|
[3571] | 335 | error "can't get field mesh: Unknown component \"$cname\": should be one of [join [lsort [array names _comp2dims]] {, }]" |
---|
[1] | 336 | } |
---|
| 337 | |
---|
| 338 | # ---------------------------------------------------------------------- |
---|
[11] | 339 | # USAGE: values ?<name>? |
---|
| 340 | # |
---|
| 341 | # Returns a list {xvec yvec} for the specified field component <name>. |
---|
| 342 | # If the name is not specified, then it returns the vectors for the |
---|
| 343 | # overall field (sum of all components). |
---|
| 344 | # ---------------------------------------------------------------------- |
---|
[3571] | 345 | itcl::body Rappture::Field::values {cname} { |
---|
[3330] | 346 | if {$cname == "component0"} { |
---|
| 347 | set cname "component" |
---|
[11] | 348 | } |
---|
[3330] | 349 | if {[info exists _comp2xy($cname)]} { |
---|
| 350 | return [lindex $_comp2xy($cname) 1] ;# return yv |
---|
[11] | 351 | } |
---|
[3330] | 352 | # VTK file data |
---|
| 353 | if { [info exists _comp2vtk($cname)] } { |
---|
| 354 | # FIXME: extract the values from the VTK file data |
---|
[3546] | 355 | if { $_comp2dims($cname) == "1D" } { |
---|
| 356 | return $_values |
---|
| 357 | } |
---|
[3330] | 358 | error "method \"values\" is not implemented for vtk file data" |
---|
[3027] | 359 | } |
---|
[3330] | 360 | # Points-on-mesh |
---|
| 361 | if { [info exists _comp2mesh($cname)] } { |
---|
| 362 | set vector [lindex $_comp2mesh($cname) 1] |
---|
| 363 | return [$vector range 0 end] |
---|
[2504] | 364 | } |
---|
[3330] | 365 | if {[info exists _comp2dx($cname)]} { |
---|
| 366 | return $_comp2dx($cname) ;# return gzipped, base64-encoded DX data |
---|
[11] | 367 | } |
---|
[3330] | 368 | if {[info exists _comp2unirect2d($cname)]} { |
---|
| 369 | return [$_comp2unirect2d($cname) values] |
---|
[447] | 370 | } |
---|
[3330] | 371 | if {[info exists _comp2unirect3d($cname)]} { |
---|
| 372 | return [$_comp2unirect3d($cname) blob] |
---|
[839] | 373 | } |
---|
[3571] | 374 | error "can't get field values. Unknown component \"$cname\": should be [join [lsort [array names _comp2dims]] {, }]" |
---|
[11] | 375 | } |
---|
| 376 | |
---|
| 377 | # ---------------------------------------------------------------------- |
---|
[839] | 378 | # USAGE: blob ?<name>? |
---|
| 379 | # |
---|
| 380 | # Returns a string representing the blob of data for the mesh and values. |
---|
| 381 | # ---------------------------------------------------------------------- |
---|
[3571] | 382 | itcl::body Rappture::Field::blob {cname} { |
---|
[3330] | 383 | if {$cname == "component0"} { |
---|
| 384 | set cname "component" |
---|
[839] | 385 | } |
---|
[3330] | 386 | if {[info exists _comp2xy($cname)]} { |
---|
[1570] | 387 | return "" |
---|
[839] | 388 | } |
---|
[3330] | 389 | if { [info exists _comp2vtk($cname)] } { |
---|
| 390 | error "blob not implemented for VTK file data" |
---|
[839] | 391 | } |
---|
[3330] | 392 | if {[info exists _comp2dx($cname)]} { |
---|
| 393 | return $_comp2dx($cname) ;# return gzipped, base64-encoded DX data |
---|
[2727] | 394 | } |
---|
[3330] | 395 | if {[info exists _comp2unirect2d($cname)]} { |
---|
[3365] | 396 | set blob [$_comp2unirect2d($cname) blob] |
---|
| 397 | lappend blob "values" $_values |
---|
| 398 | return $blob |
---|
[3027] | 399 | } |
---|
[3330] | 400 | if {[info exists _comp2unirect3d($cname)]} { |
---|
| 401 | return [$_comp2unirect3d($cname) blob] |
---|
[2504] | 402 | } |
---|
[3571] | 403 | error "can't get field blob: Unknown component \"$cname\": should be one of [join [lsort [array names _comp2dims]] {, }]" |
---|
[839] | 404 | } |
---|
| 405 | |
---|
| 406 | # ---------------------------------------------------------------------- |
---|
[11] | 407 | # USAGE: limits <axis> |
---|
| 408 | # |
---|
| 409 | # Returns a list {min max} representing the limits for the specified |
---|
| 410 | # axis. |
---|
| 411 | # ---------------------------------------------------------------------- |
---|
[64] | 412 | itcl::body Rappture::Field::limits {which} { |
---|
| 413 | set min "" |
---|
| 414 | set max "" |
---|
[3330] | 415 | blt::vector tmp zero |
---|
[64] | 416 | |
---|
[3330] | 417 | foreach cname [array names _comp2dims] { |
---|
| 418 | switch -- $_comp2dims($cname) { |
---|
[1570] | 419 | 1D { |
---|
| 420 | switch -- $which { |
---|
[3330] | 421 | x - xlin { |
---|
| 422 | set pos 0; set log 0; set axis x |
---|
| 423 | } |
---|
| 424 | xlog { |
---|
| 425 | set pos 0; set log 1; set axis x |
---|
| 426 | } |
---|
| 427 | y - ylin - v - vlin { |
---|
| 428 | set pos 1; set log 0; set axis y |
---|
| 429 | } |
---|
| 430 | ylog - vlog { |
---|
| 431 | set pos 1; set log 1; set axis y |
---|
| 432 | } |
---|
[1570] | 433 | default { |
---|
[3571] | 434 | error "bad axis \"$which\": should be x, xlin, xlog, y, ylin, ylog, v, vlin, vlog" |
---|
[1570] | 435 | } |
---|
| 436 | } |
---|
[11] | 437 | |
---|
[3330] | 438 | set vname [lindex $_comp2xy($cname) $pos] |
---|
[1570] | 439 | $vname variable vec |
---|
[11] | 440 | |
---|
[1570] | 441 | if {$log} { |
---|
| 442 | # on a log scale, use abs value and ignore 0's |
---|
| 443 | $vname dup tmp |
---|
| 444 | $vname dup zero |
---|
| 445 | zero expr {tmp == 0} ;# find the 0's |
---|
| 446 | tmp expr {abs(tmp)} ;# get the abs value |
---|
| 447 | tmp expr {tmp + zero*max(tmp)} ;# replace 0's with abs max |
---|
[3330] | 448 | set axisMin [blt::vector expr min(tmp)] |
---|
| 449 | set axisMax [blt::vector expr max(tmp)] |
---|
[1570] | 450 | } else { |
---|
[3330] | 451 | set axisMin $vec(min) |
---|
| 452 | set axisMax $vec(max) |
---|
[1570] | 453 | } |
---|
[16] | 454 | |
---|
[1570] | 455 | if {"" == $min} { |
---|
[3330] | 456 | set min $axisMin |
---|
| 457 | } elseif {$axisMin < $min} { |
---|
| 458 | set min $axisMin |
---|
[1570] | 459 | } |
---|
| 460 | if {"" == $max} { |
---|
[3330] | 461 | set max $axisMax |
---|
| 462 | } elseif {$axisMax > $max} { |
---|
| 463 | set max $axisMax |
---|
[1570] | 464 | } |
---|
| 465 | } |
---|
| 466 | 2D - 3D { |
---|
[3365] | 467 | if {[info exists _comp2unirect3d($cname)]} { |
---|
[3330] | 468 | set limits [$_comp2unirect3d($cname) limits $which] |
---|
| 469 | foreach {axisMin axisMax} $limits break |
---|
| 470 | set axis v |
---|
| 471 | } elseif {[info exists _comp2limits($cname)]} { |
---|
| 472 | array set limits $_comp2limits($cname) |
---|
| 473 | switch -- $which { |
---|
[1570] | 474 | x - xlin - xlog { |
---|
[3330] | 475 | set axis x |
---|
| 476 | foreach {axisMin axisMax} $limits(x) break |
---|
[1570] | 477 | } |
---|
| 478 | y - ylin - ylog { |
---|
[3330] | 479 | set axis y |
---|
| 480 | foreach {axisMin axisMax} $limits(y) break |
---|
[1570] | 481 | } |
---|
| 482 | z - zlin - zlog { |
---|
[3481] | 483 | set axis z |
---|
[3330] | 484 | foreach {axisMin axisMax} $limits(z) break |
---|
[1570] | 485 | } |
---|
| 486 | v - vlin - vlog { |
---|
[3330] | 487 | set axis v |
---|
| 488 | foreach {axisMin axisMax} $limits(v) break |
---|
| 489 | } |
---|
| 490 | default { |
---|
| 491 | if { ![info exists limits($which)] } { |
---|
| 492 | error "limits: unknown axis \"$which\"" |
---|
| 493 | } |
---|
| 494 | set axis v |
---|
| 495 | foreach {axisMin axisMax} $limits($which) break |
---|
| 496 | } |
---|
| 497 | } |
---|
[1570] | 498 | } else { |
---|
[3330] | 499 | set axisMin 0 ;# HACK ALERT! must be OpenDX data |
---|
| 500 | set axisMax 1 |
---|
| 501 | set axis v |
---|
[1570] | 502 | } |
---|
| 503 | } |
---|
| 504 | } |
---|
[3330] | 505 | if { "" == $min || $axisMin < $min } { |
---|
| 506 | set min $axisMin |
---|
[1570] | 507 | } |
---|
[3330] | 508 | if { "" == $max || $axisMax > $max } { |
---|
| 509 | set max $axisMax |
---|
[1570] | 510 | } |
---|
[11] | 511 | } |
---|
[64] | 512 | blt::vector destroy tmp zero |
---|
[3571] | 513 | set val [$_field get "${axis}axis.min"] |
---|
[64] | 514 | if {"" != $val && "" != $min} { |
---|
[1570] | 515 | if {$val > $min} { |
---|
| 516 | # tool specified this min -- don't go any lower |
---|
| 517 | set min $val |
---|
| 518 | } |
---|
[64] | 519 | } |
---|
[3571] | 520 | set val [$_field get "${axis}axis.max"] |
---|
[64] | 521 | if {"" != $val && "" != $max} { |
---|
[1570] | 522 | if {$val < $max} { |
---|
| 523 | # tool specified this max -- don't go any higher |
---|
| 524 | set max $val |
---|
| 525 | } |
---|
[64] | 526 | } |
---|
| 527 | return [list $min $max] |
---|
[11] | 528 | } |
---|
| 529 | |
---|
[3428] | 530 | |
---|
[11] | 531 | # ---------------------------------------------------------------------- |
---|
[3429] | 532 | # USAGE: fieldlimits |
---|
[3428] | 533 | # |
---|
| 534 | # Returns a list {min max} representing the limits for the specified |
---|
| 535 | # axis. |
---|
| 536 | # ---------------------------------------------------------------------- |
---|
[3429] | 537 | itcl::body Rappture::Field::fieldlimits {} { |
---|
[3428] | 538 | foreach cname [array names _comp2limits] { |
---|
| 539 | array set limits $_comp2limits($cname) |
---|
[3571] | 540 | foreach fname $_comp2fldName($cname) { |
---|
[3428] | 541 | if { ![info exists limits($fname)] } { |
---|
[3573] | 542 | puts stderr "ERROR: field \"$fname\" unknown in \"$cname\"" |
---|
[3428] | 543 | continue |
---|
| 544 | } |
---|
| 545 | foreach {min max} $limits($fname) break |
---|
| 546 | if { ![info exists overall($fname)] } { |
---|
| 547 | set overall($fname) $limits($fname) |
---|
| 548 | continue |
---|
| 549 | } |
---|
| 550 | foreach {omin omax} $overall($fname) break |
---|
| 551 | if { $min < $omin } { |
---|
| 552 | set omin $min |
---|
| 553 | } |
---|
| 554 | if { $max > $omax } { |
---|
| 555 | set omax $max |
---|
| 556 | } |
---|
| 557 | set overall($fname) [list $min $max] |
---|
| 558 | } |
---|
| 559 | } |
---|
| 560 | if { [info exists overall] } { |
---|
| 561 | return [array get overall] |
---|
| 562 | } |
---|
| 563 | return "" |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | # ---------------------------------------------------------------------- |
---|
[1] | 567 | # USAGE: controls get ?<name>? |
---|
[729] | 568 | # USAGE: controls validate <path> <value> |
---|
[1] | 569 | # USAGE: controls put <path> <value> |
---|
| 570 | # |
---|
| 571 | # Returns a list {path1 x1 y1 val1 path2 x2 y2 val2 ...} representing |
---|
| 572 | # control points for the specified field component <name>. |
---|
| 573 | # ---------------------------------------------------------------------- |
---|
| 574 | itcl::body Rappture::Field::controls {option args} { |
---|
| 575 | switch -- $option { |
---|
[1570] | 576 | get { |
---|
[3330] | 577 | set cname [lindex $args 0] |
---|
| 578 | if {[info exists _comp2cntls($cname)]} { |
---|
| 579 | return $_comp2cntls($cname) |
---|
[1570] | 580 | } |
---|
| 581 | return "" |
---|
| 582 | } |
---|
| 583 | validate { |
---|
| 584 | set path [lindex $args 0] |
---|
| 585 | set value [lindex $args 1] |
---|
| 586 | set units [$_xmlobj get $path.units] |
---|
[729] | 587 | |
---|
[1570] | 588 | if {"" != $units} { |
---|
| 589 | set nv [Rappture::Units::convert \ |
---|
| 590 | $value -context $units -to $units -units off] |
---|
| 591 | } else { |
---|
| 592 | set nv $value |
---|
| 593 | } |
---|
| 594 | if {![string is double $nv] |
---|
| 595 | || [regexp -nocase {^(inf|nan)$} $nv]} { |
---|
| 596 | error "Value out of range" |
---|
| 597 | } |
---|
[729] | 598 | |
---|
[1570] | 599 | set rawmin [$_xmlobj get $path.min] |
---|
| 600 | if {"" != $rawmin} { |
---|
| 601 | set minv $rawmin |
---|
| 602 | if {"" != $units} { |
---|
| 603 | set minv [Rappture::Units::convert \ |
---|
| 604 | $minv -context $units -to $units -units off] |
---|
| 605 | set nv [Rappture::Units::convert \ |
---|
| 606 | $value -context $units -to $units -units off] |
---|
| 607 | } |
---|
| 608 | # fix for the case when the user tries to |
---|
| 609 | # compare values like minv=-500 nv=-0600 |
---|
| 610 | set nv [format "%g" $nv] |
---|
| 611 | set minv [format "%g" $minv] |
---|
[729] | 612 | |
---|
[1570] | 613 | if {$nv < $minv} { |
---|
| 614 | error "Minimum value allowed here is $rawmin" |
---|
| 615 | } |
---|
| 616 | } |
---|
[729] | 617 | |
---|
[1570] | 618 | set rawmax [$_xmlobj get $path.max] |
---|
| 619 | if {"" != $rawmax} { |
---|
| 620 | set maxv $rawmax |
---|
| 621 | if {"" != $units} { |
---|
| 622 | set maxv [Rappture::Units::convert \ |
---|
| 623 | $maxv -context $units -to $units -units off] |
---|
| 624 | set nv [Rappture::Units::convert \ |
---|
| 625 | $value -context $units -to $units -units off] |
---|
| 626 | } |
---|
| 627 | # fix for the case when the user tries to |
---|
| 628 | # compare values like maxv=-500 nv=-0600 |
---|
| 629 | set nv [format "%g" $nv] |
---|
| 630 | set maxv [format "%g" $maxv] |
---|
[729] | 631 | |
---|
[1570] | 632 | if {$nv > $maxv} { |
---|
| 633 | error "Maximum value allowed here is $rawmax" |
---|
| 634 | } |
---|
| 635 | } |
---|
[729] | 636 | |
---|
[1570] | 637 | return "ok" |
---|
| 638 | } |
---|
| 639 | put { |
---|
| 640 | set path [lindex $args 0] |
---|
| 641 | set value [lindex $args 1] |
---|
| 642 | $_xmlobj put $path.current $value |
---|
[3330] | 643 | Build |
---|
[1570] | 644 | } |
---|
| 645 | default { |
---|
[3571] | 646 | error "bad field controls option \"$option\": should be get or put" |
---|
[1570] | 647 | } |
---|
[1] | 648 | } |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | # ---------------------------------------------------------------------- |
---|
| 652 | # USAGE: hints ?<keyword>? |
---|
| 653 | # |
---|
| 654 | # Returns a list of key/value pairs for various hints about plotting |
---|
| 655 | # this field. If a particular <keyword> is specified, then it returns |
---|
| 656 | # the hint for that <keyword>, if it exists. |
---|
| 657 | # ---------------------------------------------------------------------- |
---|
| 658 | itcl::body Rappture::Field::hints {{keyword ""}} { |
---|
[3330] | 659 | if { ![info exists _hints] } { |
---|
| 660 | foreach {key path} { |
---|
| 661 | camera camera.position |
---|
| 662 | color about.color |
---|
| 663 | default about.default |
---|
| 664 | group about.group |
---|
| 665 | label about.label |
---|
| 666 | scale about.scale |
---|
| 667 | seeds about.seeds |
---|
| 668 | style about.style |
---|
| 669 | type about.type |
---|
| 670 | xlabel about.xaxis.label |
---|
| 671 | ylabel about.yaxis.label |
---|
| 672 | zlabel about.zaxis.label |
---|
| 673 | xunits about.xaxis.units |
---|
| 674 | yunits about.yaxis.units |
---|
| 675 | zunits about.zaxis.units |
---|
| 676 | units units |
---|
| 677 | updir updir |
---|
| 678 | vectors about.vectors |
---|
| 679 | } { |
---|
[3571] | 680 | set str [$_field get $path] |
---|
[3330] | 681 | if { "" != $str } { |
---|
| 682 | set _hints($key) $str |
---|
| 683 | } |
---|
| 684 | } |
---|
| 685 | foreach {key path} { |
---|
| 686 | toolid tool.id |
---|
| 687 | toolname tool.name |
---|
| 688 | toolcommand tool.execute |
---|
| 689 | tooltitle tool.title |
---|
| 690 | toolrevision tool.version.application.revision |
---|
| 691 | } { |
---|
| 692 | set str [$_xmlobj get $path] |
---|
| 693 | if { "" != $str } { |
---|
| 694 | set _hints($key) $str |
---|
| 695 | } |
---|
| 696 | } |
---|
| 697 | # Set toolip and path hints |
---|
| 698 | set _hints(path) $_path |
---|
| 699 | if { [info exists _hints(group)] && [info exists _hints(label)] } { |
---|
| 700 | # pop-up help for each curve |
---|
| 701 | set _hints(tooltip) $_hints(label) |
---|
| 702 | } |
---|
[1] | 703 | } |
---|
[3330] | 704 | if { $keyword != "" } { |
---|
| 705 | if {[info exists _hints($keyword)]} { |
---|
| 706 | return $_hints($keyword) |
---|
[1570] | 707 | } |
---|
| 708 | return "" |
---|
[1] | 709 | } |
---|
[3330] | 710 | return [array get _hints] |
---|
[1] | 711 | } |
---|
| 712 | |
---|
| 713 | # ---------------------------------------------------------------------- |
---|
[3330] | 714 | # USAGE: Build |
---|
[1] | 715 | # |
---|
| 716 | # Used internally to build up the vector representation for the |
---|
| 717 | # field when the object is first constructed, or whenever the field |
---|
| 718 | # data changes. Discards any existing vectors and builds everything |
---|
| 719 | # from scratch. |
---|
| 720 | # ---------------------------------------------------------------------- |
---|
[3330] | 721 | itcl::body Rappture::Field::Build {} { |
---|
| 722 | |
---|
| 723 | # Discard any existing data |
---|
[11] | 724 | foreach name [array names _comp2xy] { |
---|
[1570] | 725 | eval blt::vector destroy $_comp2xy($name) |
---|
[1] | 726 | } |
---|
[3330] | 727 | array unset _comp2vtk |
---|
[839] | 728 | foreach name [array names _comp2unirect2d] { |
---|
[1570] | 729 | eval itcl::delete object $_comp2unirect2d($name) |
---|
[839] | 730 | } |
---|
[1463] | 731 | foreach name [array names _comp2unirect3d] { |
---|
[1570] | 732 | eval itcl::delete object $_comp2unirect3d($name) |
---|
[1463] | 733 | } |
---|
[11] | 734 | catch {unset _comp2xy} |
---|
[447] | 735 | catch {unset _comp2dx} |
---|
[11] | 736 | catch {unset _comp2dims} |
---|
[136] | 737 | catch {unset _comp2style} |
---|
[839] | 738 | array unset _comp2unirect2d |
---|
[1373] | 739 | array unset _comp2unirect3d |
---|
| 740 | array unset _comp2extents |
---|
[2504] | 741 | array unset _dataobj2type |
---|
[1] | 742 | # |
---|
| 743 | # Scan through the components of the field and create |
---|
| 744 | # vectors for each part. |
---|
| 745 | # |
---|
[3571] | 746 | array unset _isValidComponent |
---|
| 747 | foreach cname [$_field children -type component] { |
---|
[1570] | 748 | set type "" |
---|
[3571] | 749 | if { ([$_field element $cname.constant] != "" && |
---|
| 750 | [$_field element $cname.domain] != "") || |
---|
| 751 | [$_field element $cname.xy] != "" } { |
---|
[1570] | 752 | set type "1D" |
---|
[3571] | 753 | } elseif { [$_field element $cname.mesh] != "" && |
---|
| 754 | [$_field element $cname.values] != ""} { |
---|
[1570] | 755 | set type "points-on-mesh" |
---|
[3571] | 756 | } elseif { [$_field element $cname.vtk] != ""} { |
---|
[3585] | 757 | set type "vtk" |
---|
[3571] | 758 | set viewer [$_field get "about.view"] |
---|
[3330] | 759 | if { $viewer != "" } { |
---|
| 760 | set _viewer $viewer |
---|
| 761 | } |
---|
[3571] | 762 | } elseif {[$_field element $cname.opendx] != ""} { |
---|
[2744] | 763 | global env |
---|
| 764 | if { [info exists env(VTKVOLUME)] } { |
---|
| 765 | set type "vtkvolume" |
---|
| 766 | } else { |
---|
[3571] | 767 | set type "opendx" |
---|
[2744] | 768 | } |
---|
[3571] | 769 | } elseif {[$_field element $cname.dx] != ""} { |
---|
[2744] | 770 | global env |
---|
| 771 | if { [info exists env(VTKVOLUME)] } { |
---|
| 772 | set type "vtkvolume" |
---|
| 773 | } else { |
---|
| 774 | set type "dx" |
---|
| 775 | } |
---|
[3571] | 776 | } elseif {[$_field element $cname.ucd] != ""} { |
---|
[3546] | 777 | set type "ucd" |
---|
[3330] | 778 | } |
---|
[1570] | 779 | set _comp2style($cname) "" |
---|
[3571] | 780 | if { $type == "" } { |
---|
[3573] | 781 | puts stderr "WARNING: ignoring field component \"$_path.$cname\": no data found." |
---|
[3571] | 782 | continue |
---|
| 783 | } |
---|
[1570] | 784 | # Save the extents of the component |
---|
[3571] | 785 | if { [$_field element $cname.extents] != "" } { |
---|
| 786 | set extents [$_field get $cname.extents] |
---|
[1570] | 787 | } else { |
---|
| 788 | set extents 1 |
---|
| 789 | } |
---|
| 790 | set _comp2extents($cname) $extents |
---|
[2744] | 791 | set _type $type |
---|
[3684] | 792 | |
---|
| 793 | GetTypeAndSize $cname |
---|
| 794 | GetAssociation $cname |
---|
| 795 | if { $_comp2size($cname) > 1 } { |
---|
| 796 | set viewer [$_field get "about.view"] |
---|
| 797 | if { $viewer == "" } { |
---|
| 798 | set _viewer "glyphs" |
---|
| 799 | } |
---|
| 800 | } |
---|
[1570] | 801 | if {$type == "1D"} { |
---|
| 802 | # |
---|
| 803 | # 1D data can be represented as 2 BLT vectors, |
---|
| 804 | # one for x and the other for y. |
---|
| 805 | # |
---|
| 806 | set xv "" |
---|
| 807 | set yv "" |
---|
[3594] | 808 | set _dim 1 |
---|
[3571] | 809 | set val [$_field get $cname.constant] |
---|
[1570] | 810 | if {$val != ""} { |
---|
[3571] | 811 | set domain [$_field get $cname.domain] |
---|
[1570] | 812 | if {$domain == "" || ![info exists _limits($domain)]} { |
---|
| 813 | set z0 0 |
---|
| 814 | set z1 $_zmax |
---|
| 815 | } else { |
---|
| 816 | foreach {z0 z1} $_limits($domain) { break } |
---|
| 817 | } |
---|
| 818 | set xv [blt::vector create x$_counter] |
---|
| 819 | $xv append $z0 $z1 |
---|
[894] | 820 | |
---|
[1570] | 821 | foreach {val pcomp} [_getValue $val] break |
---|
| 822 | set yv [blt::vector create y$_counter] |
---|
| 823 | $yv append $val $val |
---|
[894] | 824 | |
---|
[1570] | 825 | if {$pcomp != ""} { |
---|
| 826 | set zm [expr {0.5*($z0+$z1)}] |
---|
| 827 | set _comp2cntls($cname) \ |
---|
| 828 | [list $pcomp $zm $val "$val$_units"] |
---|
| 829 | } |
---|
| 830 | } else { |
---|
[3571] | 831 | set xydata [$_field get $cname.xy] |
---|
[1570] | 832 | if {"" != $xydata} { |
---|
| 833 | set xv [blt::vector create x$_counter] |
---|
| 834 | set yv [blt::vector create y$_counter] |
---|
| 835 | set tmp [blt::vector create \#auto] |
---|
| 836 | $tmp set $xydata |
---|
| 837 | $tmp split $xv $yv |
---|
| 838 | blt::vector destroy $tmp |
---|
| 839 | } |
---|
| 840 | } |
---|
[894] | 841 | |
---|
[1570] | 842 | if {$xv != "" && $yv != ""} { |
---|
| 843 | # sort x-coords in increasing order |
---|
| 844 | $xv sort $yv |
---|
[3594] | 845 | set _dim 1 |
---|
[1570] | 846 | set _comp2dims($cname) "1D" |
---|
| 847 | set _comp2xy($cname) [list $xv $yv] |
---|
| 848 | incr _counter |
---|
| 849 | } |
---|
| 850 | } elseif {$type == "points-on-mesh"} { |
---|
[3571] | 851 | if { ![BuildPointsOnMesh $cname] } { |
---|
| 852 | continue; # Ignore this component |
---|
| 853 | } |
---|
[1570] | 854 | } elseif {$type == "vtk"} { |
---|
[3571] | 855 | set contents [$_field get $cname.vtk] |
---|
| 856 | if { $contents == "" } { |
---|
[3573] | 857 | puts stderr "WARNING: no data fo \"$_path.$cname.vtk\"" |
---|
[3571] | 858 | continue; # Ignore this component |
---|
| 859 | } |
---|
| 860 | ReadVtkDataSet $cname $contents |
---|
| 861 | set _comp2vtk($cname) $contents |
---|
| 862 | set _comp2style($cname) [$_field get $cname.style] |
---|
[2592] | 863 | incr _counter |
---|
[3571] | 864 | } elseif {$type == "dx" || $type == "opendx" } { |
---|
[1570] | 865 | # |
---|
| 866 | # HACK ALERT! Extract gzipped, base64-encoded OpenDX |
---|
| 867 | # data. Assume that it's 3D. Pass it straight |
---|
| 868 | # off to the NanoVis visualizer. |
---|
| 869 | # |
---|
[3330] | 870 | set _viewer "nanovis" |
---|
[3571] | 871 | set _dim 3 |
---|
| 872 | set _type "dx" |
---|
[1570] | 873 | set _comp2dims($cname) "3D" |
---|
[3571] | 874 | set contents [$_field get -decode no $cname.$type] |
---|
| 875 | if { $contents == "" } { |
---|
[3573] | 876 | puts stderr "WARNING: no data for \"$_path.$cname.$type\"" |
---|
[3571] | 877 | continue; # Ignore this component |
---|
| 878 | } |
---|
| 879 | set _comp2dx($cname) $contents |
---|
[3508] | 880 | if 0 { |
---|
[3557] | 881 | set hdr "@@RP-ENC:zb64\n" |
---|
[3571] | 882 | set data [$_field get -decode no $cname.$type] |
---|
[3557] | 883 | set data "$hdr$data" |
---|
| 884 | set data [Rappture::encoding::decode $data] |
---|
| 885 | set data [Rappture::DxToVtk $data] |
---|
| 886 | set f [open /tmp/$_path.$cname.vtk "w"] |
---|
[3421] | 887 | puts $f $data |
---|
| 888 | close $f |
---|
[2744] | 889 | } |
---|
[3571] | 890 | set _comp2style($cname) [$_field get $cname.style] |
---|
| 891 | if {[$_field element $cname.flow] != ""} { |
---|
[1570] | 892 | set _comp2flowhints($cname) \ |
---|
[3571] | 893 | [Rappture::FlowHints ::\#auto $_field $cname $_units] |
---|
[1570] | 894 | } |
---|
| 895 | incr _counter |
---|
[3571] | 896 | } elseif { $type == "ucd"} { |
---|
| 897 | set contents [$_field get $cname.ucd] |
---|
| 898 | if { $contents == "" } { |
---|
| 899 | continue; # Ignore this compoennt |
---|
[1570] | 900 | } |
---|
[3330] | 901 | set vtkdata [AvsToVtk $cname $contents] |
---|
[3571] | 902 | ReadVtkDataSet $cname $vtkdata |
---|
| 903 | set _comp2vtk($cname) $vtkdata |
---|
| 904 | set _comp2style($cname) [$_field get $cname.style] |
---|
[3330] | 905 | incr _counter |
---|
[1570] | 906 | } |
---|
[3571] | 907 | set _isValidComponent($cname) 1 |
---|
[11] | 908 | } |
---|
[3571] | 909 | if { [array size _isValidComponent] == 0 } { |
---|
[3573] | 910 | puts stderr "WARNING: no valid components for field \"$_path\"" |
---|
[3571] | 911 | return 0 |
---|
| 912 | } |
---|
[3330] | 913 | # Sanity check. Verify that all components of the field have the same |
---|
| 914 | # dimension. |
---|
| 915 | set dim "" |
---|
| 916 | foreach cname [array names _comp2dims] { |
---|
| 917 | if { $dim == "" } { |
---|
| 918 | set dim $_comp2dims($cname) |
---|
| 919 | continue |
---|
| 920 | } |
---|
| 921 | if { $dim != $_comp2dims($cname) } { |
---|
[3573] | 922 | puts stderr "WARNING: field can't have components of different dimensions: [join [array get _comp2dims] ,]" |
---|
[3571] | 923 | return 0 |
---|
[3330] | 924 | } |
---|
| 925 | } |
---|
[3405] | 926 | # FIXME: about.scalars and about.vectors are temporary. With views |
---|
[3406] | 927 | # the label and units for each field will be specified there. |
---|
[3405] | 928 | # |
---|
[3406] | 929 | # FIXME: Test that every <field><component> has the same field names, |
---|
| 930 | # units, components. |
---|
| 931 | # |
---|
[3405] | 932 | # Override what we found in the VTK file with names that the user |
---|
| 933 | # selected. We override the field label and units. |
---|
[3571] | 934 | foreach { fname label units } [$_field get about.scalars] { |
---|
| 935 | if { ![info exists _fld2Name($fname)] } { |
---|
| 936 | set _fld2Name($fname) $fname |
---|
| 937 | set _fld2Components($fname) 1 |
---|
[3405] | 938 | } |
---|
[3571] | 939 | set _fld2Label($fname) $label |
---|
| 940 | set _fld2Units($fname) $units |
---|
[3405] | 941 | } |
---|
[3571] | 942 | foreach { fname label units } [$_field get about.vectors] { |
---|
| 943 | if { ![info exists _fld2Name($fname)] } { |
---|
| 944 | set _fld2Name($fname) $fname |
---|
[3406] | 945 | # We're just marking the field as vector (> 1) for now. |
---|
[3571] | 946 | set _fld2Components($fname) 3 |
---|
[3405] | 947 | } |
---|
[3571] | 948 | set _fld2Label($fname) $label |
---|
| 949 | set _fld2Units($fname) $units |
---|
[3405] | 950 | } |
---|
[3571] | 951 | set _isValid 1 |
---|
| 952 | return 1 |
---|
[11] | 953 | } |
---|
[1] | 954 | |
---|
[11] | 955 | # ---------------------------------------------------------------------- |
---|
| 956 | # USAGE: _getValue <expr> |
---|
| 957 | # |
---|
| 958 | # Used internally to get the value for an expression <expr>. Returns |
---|
| 959 | # a list of the form {val parameterPath}, where val is the numeric |
---|
| 960 | # value of the expression, and parameterPath is the XML path to the |
---|
| 961 | # parameter representing the value, or "" if the <expr> does not |
---|
| 962 | # depend on any parameters. |
---|
| 963 | # ---------------------------------------------------------------------- |
---|
| 964 | itcl::body Rappture::Field::_getValue {expr} { |
---|
| 965 | # |
---|
| 966 | # First, look for the expression among the <parameter>'s |
---|
| 967 | # associated with the device. |
---|
| 968 | # |
---|
| 969 | set found 0 |
---|
| 970 | foreach pcomp [$_xmlobj children parameters] { |
---|
[1570] | 971 | set id [$_xmlobj element -as id parameters.$pcomp] |
---|
| 972 | if {[string equal $id $expr]} { |
---|
| 973 | set val [$_xmlobj get parameters.$pcomp.current] |
---|
| 974 | if {"" == $val} { |
---|
| 975 | set val [$_xmlobj get parameters.$pcomp.default] |
---|
| 976 | } |
---|
| 977 | if {"" != $val} { |
---|
| 978 | set expr $val |
---|
| 979 | set found 1 |
---|
| 980 | break |
---|
| 981 | } |
---|
| 982 | } |
---|
[1] | 983 | } |
---|
[11] | 984 | if {$found} { |
---|
[1570] | 985 | set pcomp "parameters.$pcomp" |
---|
[11] | 986 | } else { |
---|
[1570] | 987 | set pcomp "" |
---|
[11] | 988 | } |
---|
| 989 | |
---|
| 990 | if {$_units != ""} { |
---|
[1570] | 991 | set expr [Rappture::Units::convert $expr \ |
---|
| 992 | -context $_units -to $_units -units off] |
---|
[11] | 993 | } |
---|
| 994 | |
---|
| 995 | return [list $expr $pcomp] |
---|
[1] | 996 | } |
---|
[1189] | 997 | |
---|
| 998 | # |
---|
[1373] | 999 | # isunirect2d -- |
---|
| 1000 | # |
---|
| 1001 | # Returns if the field is a unirect2d object. |
---|
| 1002 | # |
---|
[1189] | 1003 | itcl::body Rappture::Field::isunirect2d { } { |
---|
| 1004 | return [expr [array size _comp2unirect2d] > 0] |
---|
| 1005 | } |
---|
[1373] | 1006 | |
---|
| 1007 | # |
---|
| 1008 | # isunirect3d -- |
---|
| 1009 | # |
---|
| 1010 | # Returns if the field is a unirect3d object. |
---|
| 1011 | # |
---|
| 1012 | itcl::body Rappture::Field::isunirect3d { } { |
---|
| 1013 | return [expr [array size _comp2unirect3d] > 0] |
---|
| 1014 | } |
---|
| 1015 | |
---|
| 1016 | # |
---|
[1441] | 1017 | # flowhints -- |
---|
| 1018 | # |
---|
| 1019 | # Returns the hints associated with a flow vector field. |
---|
| 1020 | # |
---|
| 1021 | itcl::body Rappture::Field::flowhints { cname } { |
---|
| 1022 | if { [info exists _comp2flowhints($cname)] } { |
---|
[1570] | 1023 | return $_comp2flowhints($cname) |
---|
[1441] | 1024 | } |
---|
| 1025 | return "" |
---|
| 1026 | } |
---|
| 1027 | |
---|
| 1028 | # |
---|
[2504] | 1029 | # style -- |
---|
| 1030 | # |
---|
| 1031 | # Returns the style associated with a component of the field. |
---|
| 1032 | # |
---|
| 1033 | itcl::body Rappture::Field::style { cname } { |
---|
| 1034 | if { [info exists _comp2style($cname)] } { |
---|
| 1035 | return $_comp2style($cname) |
---|
| 1036 | } |
---|
| 1037 | return "" |
---|
| 1038 | } |
---|
| 1039 | |
---|
| 1040 | # |
---|
| 1041 | # type -- |
---|
| 1042 | # |
---|
| 1043 | # Returns the style associated with a component of the field. |
---|
| 1044 | # |
---|
| 1045 | itcl::body Rappture::Field::type {} { |
---|
| 1046 | return $_type |
---|
| 1047 | } |
---|
| 1048 | |
---|
| 1049 | # |
---|
[3684] | 1050 | # numComponents -- |
---|
| 1051 | # |
---|
| 1052 | # Returns if the number of components in the field component. |
---|
| 1053 | # |
---|
| 1054 | itcl::body Rappture::Field::numComponents {cname} { |
---|
| 1055 | set name $cname |
---|
| 1056 | regsub -all { } $name {_} name |
---|
| 1057 | if {[info exists _fld2Components($name)] } { |
---|
| 1058 | return $_fld2Components($name) |
---|
| 1059 | } |
---|
| 1060 | return 1; # Default to scalar. |
---|
| 1061 | } |
---|
| 1062 | |
---|
| 1063 | # |
---|
[1373] | 1064 | # extents -- |
---|
| 1065 | # |
---|
| 1066 | # Returns if the field is a unirect2d object. |
---|
| 1067 | # |
---|
[3330] | 1068 | itcl::body Rappture::Field::extents {{cname -overall}} { |
---|
| 1069 | if {$cname == "-overall" } { |
---|
[1570] | 1070 | set max 0 |
---|
[3571] | 1071 | foreach cname [$_field children -type component] { |
---|
[1570] | 1072 | if { ![info exists _comp2unirect3d($cname)] && |
---|
| 1073 | ![info exists _comp2extents($cname)] } { |
---|
| 1074 | continue |
---|
| 1075 | } |
---|
| 1076 | set value $_comp2extents($cname) |
---|
| 1077 | if { $max < $value } { |
---|
| 1078 | set max $value |
---|
| 1079 | } |
---|
| 1080 | } |
---|
| 1081 | return $max |
---|
[1373] | 1082 | } |
---|
[3330] | 1083 | if { $cname == "component0"} { |
---|
| 1084 | set cname [lindex [components -name] 0] |
---|
[1373] | 1085 | } |
---|
[3330] | 1086 | return $_comp2extents($cname) |
---|
[1373] | 1087 | } |
---|
[2718] | 1088 | |
---|
[3330] | 1089 | itcl::body Rappture::Field::ConvertToVtkData { cname } { |
---|
[3027] | 1090 | set ds "" |
---|
[3330] | 1091 | switch -- [typeof $cname] { |
---|
[3027] | 1092 | "unirect2d" { |
---|
[3330] | 1093 | foreach { x1 x2 xN y1 y2 yN } [$dataobj mesh $cname] break |
---|
[3027] | 1094 | set spacingX [expr {double($x2 - $x1)/double($xN - 1)}] |
---|
| 1095 | set spacingY [expr {double($y2 - $y1)/double($yN - 1)}] |
---|
| 1096 | |
---|
| 1097 | set ds [vtkImageData $this-grdataTemp] |
---|
| 1098 | $ds SetDimensions $xN $yN 1 |
---|
| 1099 | $ds SetOrigin $x1 $y1 0 |
---|
| 1100 | $ds SetSpacing $spacingX $spacingY 0 |
---|
| 1101 | set arr [vtkDoubleArray $this-arrTemp] |
---|
[3330] | 1102 | foreach {val} [$dataobj values $cname] { |
---|
[3027] | 1103 | $arr InsertNextValue $val |
---|
| 1104 | } |
---|
| 1105 | [$ds GetPointData] SetScalars $arr |
---|
| 1106 | } |
---|
| 1107 | "unirect3d" { |
---|
[3330] | 1108 | foreach { x1 x2 xN y1 y2 yN z1 z2 zN } [$dataobj mesh $cname] break |
---|
[3027] | 1109 | set spacingX [expr {double($x2 - $x1)/double($xN - 1)}] |
---|
| 1110 | set spacingY [expr {double($y2 - $y1)/double($yN - 1)}] |
---|
| 1111 | set spacingZ [expr {double($z2 - $z1)/double($zN - 1)}] |
---|
| 1112 | |
---|
| 1113 | set ds [vtkImageData $this-grdataTemp] |
---|
| 1114 | $ds SetDimensions $xN $yN $zN |
---|
| 1115 | $ds SetOrigin $x1 $y1 $z1 |
---|
| 1116 | $ds SetSpacing $spacingX $spacingY $spacingZ |
---|
| 1117 | set arr [vtkDoubleArray $this-arrTemp] |
---|
[3330] | 1118 | foreach {val} [$dataobj values $cname] { |
---|
[3027] | 1119 | $arr InsertNextValue $val |
---|
| 1120 | } |
---|
| 1121 | [$ds GetPointData] SetScalars $val |
---|
| 1122 | } |
---|
[3330] | 1123 | "contour" { |
---|
| 1124 | return [$dataobj blob $cname] |
---|
[3027] | 1125 | } |
---|
| 1126 | "dx" { |
---|
[3557] | 1127 | return [Rappture::DxToVtk $_comp2dx($cname)] |
---|
[3027] | 1128 | } |
---|
| 1129 | default { |
---|
[3330] | 1130 | set mesh [$dataobj mesh $cname] |
---|
[3027] | 1131 | switch -- [$mesh GetClassName] { |
---|
| 1132 | vtkPoints { |
---|
| 1133 | # handle cloud of points |
---|
| 1134 | set ds [vtkPolyData $this-polydataTemp] |
---|
| 1135 | $ds SetPoints $mesh |
---|
[3330] | 1136 | [$ds GetPointData] SetScalars [$dataobj values $cname] |
---|
[3027] | 1137 | } |
---|
| 1138 | vtkPolyData { |
---|
| 1139 | set ds [vtkPolyData $this-polydataTemp] |
---|
| 1140 | $ds ShallowCopy $mesh |
---|
[3330] | 1141 | [$ds GetPointData] SetScalars [$dataobj values $cname] |
---|
[3027] | 1142 | } |
---|
| 1143 | vtkUnstructuredGrid { |
---|
| 1144 | # handle 3D grid with connectivity |
---|
| 1145 | set ds [vtkUnstructuredGrid $this-grdataTemp] |
---|
| 1146 | $ds ShallowCopy $mesh |
---|
[3330] | 1147 | [$ds GetPointData] SetScalars [$dataobj values $cname] |
---|
[3027] | 1148 | } |
---|
| 1149 | vtkRectilinearGrid { |
---|
| 1150 | # handle 3D grid with connectivity |
---|
| 1151 | set ds [vtkRectilinearGrid $this-grdataTemp] |
---|
| 1152 | $ds ShallowCopy $mesh |
---|
[3330] | 1153 | [$ds GetPointData] SetScalars [$dataobj values $cname] |
---|
[3027] | 1154 | } |
---|
| 1155 | default { |
---|
| 1156 | error "don't know how to handle [$mesh GetClassName] data" |
---|
| 1157 | } |
---|
| 1158 | } |
---|
| 1159 | } |
---|
| 1160 | } |
---|
| 1161 | |
---|
| 1162 | if {"" != $ds} { |
---|
| 1163 | set writer [vtkDataSetWriter $this-dsWriterTmp] |
---|
| 1164 | $writer SetInput $ds |
---|
| 1165 | $writer SetFileTypeToASCII |
---|
| 1166 | $writer WriteToOutputStringOn |
---|
| 1167 | $writer Write |
---|
| 1168 | set out [$writer GetOutputString] |
---|
| 1169 | $ds Delete |
---|
| 1170 | $writer Delete |
---|
| 1171 | } else { |
---|
| 1172 | set out "" |
---|
| 1173 | error "No DataSet to write" |
---|
| 1174 | } |
---|
| 1175 | |
---|
| 1176 | append out "\n" |
---|
| 1177 | return $out |
---|
| 1178 | } |
---|
| 1179 | |
---|
[3585] | 1180 | itcl::body Rappture::Field::VerifyVtkDataSet { contents } { |
---|
| 1181 | package require vtk |
---|
| 1182 | |
---|
| 1183 | set reader $this-datasetreader |
---|
| 1184 | vtkDataSetReader $reader |
---|
| 1185 | |
---|
| 1186 | # Write the contents to a file just in case it's binary. |
---|
| 1187 | set tmpfile file[pid].vtk |
---|
| 1188 | set f [open "$tmpfile" "w"] |
---|
| 1189 | fconfigure $f -translation binary -encoding binary |
---|
| 1190 | puts $f $contents |
---|
| 1191 | close $f |
---|
| 1192 | |
---|
| 1193 | $reader SetFileName $tmpfile |
---|
[3703] | 1194 | $reader ReadAllNormalsOn |
---|
| 1195 | $reader ReadAllTCoordsOn |
---|
[3585] | 1196 | $reader ReadAllScalarsOn |
---|
[3703] | 1197 | $reader ReadAllColorScalarsOn |
---|
[3585] | 1198 | $reader ReadAllVectorsOn |
---|
[3703] | 1199 | $reader ReadAllTensorsOn |
---|
[3585] | 1200 | $reader ReadAllFieldsOn |
---|
| 1201 | $reader Update |
---|
[3707] | 1202 | file delete $tmpfile |
---|
| 1203 | |
---|
[3585] | 1204 | set dataset [$reader GetOutput] |
---|
| 1205 | set dataAttrs [$dataset GetPointData] |
---|
| 1206 | if { $dataAttrs == ""} { |
---|
| 1207 | puts stderr "WARNING: no point data found in \"$_path\"" |
---|
[3707] | 1208 | rename $reader "" |
---|
[3585] | 1209 | return 0 |
---|
| 1210 | } |
---|
| 1211 | rename $reader "" |
---|
| 1212 | } |
---|
| 1213 | |
---|
[3330] | 1214 | itcl::body Rappture::Field::ReadVtkDataSet { cname contents } { |
---|
[3027] | 1215 | package require vtk |
---|
| 1216 | |
---|
| 1217 | set reader $this-datasetreader |
---|
| 1218 | vtkDataSetReader $reader |
---|
| 1219 | |
---|
| 1220 | # Write the contents to a file just in case it's binary. |
---|
| 1221 | set tmpfile file[pid].vtk |
---|
| 1222 | set f [open "$tmpfile" "w"] |
---|
| 1223 | fconfigure $f -translation binary -encoding binary |
---|
| 1224 | puts $f $contents |
---|
| 1225 | close $f |
---|
[3416] | 1226 | |
---|
[3027] | 1227 | $reader SetFileName $tmpfile |
---|
[3703] | 1228 | $reader ReadAllNormalsOn |
---|
| 1229 | $reader ReadAllTCoordsOn |
---|
[3027] | 1230 | $reader ReadAllScalarsOn |
---|
[3703] | 1231 | $reader ReadAllColorScalarsOn |
---|
[3027] | 1232 | $reader ReadAllVectorsOn |
---|
[3703] | 1233 | $reader ReadAllTensorsOn |
---|
[3027] | 1234 | $reader ReadAllFieldsOn |
---|
| 1235 | $reader Update |
---|
[3707] | 1236 | file delete $tmpfile |
---|
| 1237 | |
---|
[3027] | 1238 | set dataset [$reader GetOutput] |
---|
| 1239 | set limits {} |
---|
| 1240 | foreach {xmin xmax ymin ymax zmin zmax} [$dataset GetBounds] break |
---|
[3330] | 1241 | # Figure out the dimension of the mesh from the bounds. |
---|
| 1242 | set _dim 0 |
---|
| 1243 | if { $xmax > $xmin } { |
---|
| 1244 | incr _dim |
---|
| 1245 | } |
---|
| 1246 | if { $ymax > $ymin } { |
---|
| 1247 | incr _dim |
---|
| 1248 | } |
---|
| 1249 | if { $zmax > $zmin } { |
---|
| 1250 | incr _dim |
---|
| 1251 | } |
---|
| 1252 | if { $_viewer == "" } { |
---|
| 1253 | if { $_dim == 2 } { |
---|
| 1254 | set _viewer contour |
---|
| 1255 | } else { |
---|
| 1256 | set _viewer isosurface |
---|
| 1257 | } |
---|
| 1258 | } |
---|
| 1259 | set _comp2dims($cname) ${_dim}D |
---|
[3546] | 1260 | if { $_dim < 2 } { |
---|
[3707] | 1261 | set numPoints [$dataset GetNumberOfPoints] |
---|
[3546] | 1262 | set xv [blt::vector create \#auto] |
---|
| 1263 | for { set i 0 } { $i < $numPoints } { incr i } { |
---|
[3707] | 1264 | set point [$dataset GetPoint $i] |
---|
[3546] | 1265 | $xv append [lindex $point 0] |
---|
| 1266 | } |
---|
| 1267 | set yv [blt::vector create \#auto] |
---|
[3707] | 1268 | set dataAttrs [$dataset GetPointData] |
---|
| 1269 | if { $dataAttrs == ""} { |
---|
| 1270 | puts stderr "WARNING: no point data found in \"$_path\"" |
---|
| 1271 | rename $reader "" |
---|
| 1272 | return 0 |
---|
| 1273 | } |
---|
| 1274 | set array [$dataAttrs GetScalars] |
---|
| 1275 | if { $array == ""} { |
---|
| 1276 | puts stderr "WARNING: no scalar point data found in \"$_path\"" |
---|
| 1277 | rename $reader "" |
---|
| 1278 | return 0 |
---|
| 1279 | } |
---|
| 1280 | set numTuples [$array GetNumberOfTuples] |
---|
| 1281 | for { set i 0 } { $i < $numTuples } { incr i } { |
---|
| 1282 | $yv append [$array GetComponent $i 0] |
---|
| 1283 | } |
---|
| 1284 | $xv sort $yv |
---|
[3546] | 1285 | set _comp2xy($cname) [list $xv $yv] |
---|
| 1286 | } |
---|
[3330] | 1287 | lappend limits x [list $xmin $xmax] |
---|
| 1288 | lappend limits y [list $ymin $ymax] |
---|
| 1289 | lappend limits z [list $zmin $zmax] |
---|
[3027] | 1290 | set dataAttrs [$dataset GetPointData] |
---|
| 1291 | if { $dataAttrs == ""} { |
---|
[3573] | 1292 | puts stderr "WARNING: no point data found in \"$_path\"" |
---|
[3707] | 1293 | rename $reader "" |
---|
[3571] | 1294 | return 0 |
---|
[3027] | 1295 | } |
---|
[3330] | 1296 | set vmin 0 |
---|
| 1297 | set vmax 1 |
---|
| 1298 | set numArrays [$dataAttrs GetNumberOfArrays] |
---|
| 1299 | if { $numArrays > 0 } { |
---|
| 1300 | set array [$dataAttrs GetArray 0] |
---|
[3707] | 1301 | # Calling GetRange with component set to -1 will return |
---|
| 1302 | # either the scalar range or vector magnitude range |
---|
| 1303 | foreach {vmin vmax} [$array GetRange -1] break |
---|
[3330] | 1304 | |
---|
| 1305 | for {set i 0} {$i < [$dataAttrs GetNumberOfArrays] } {incr i} { |
---|
| 1306 | set array [$dataAttrs GetArray $i] |
---|
[3405] | 1307 | set fname [$dataAttrs GetArrayName $i] |
---|
[3707] | 1308 | foreach {min max} [$array GetRange -1] break |
---|
[3405] | 1309 | lappend limits $fname [list $min $max] |
---|
[3571] | 1310 | set _fld2Units($fname) "" |
---|
| 1311 | set _fld2Label($fname) $fname |
---|
[3684] | 1312 | # Let the VTK file override the <type> designated. |
---|
[3571] | 1313 | set _fld2Components($fname) [$array GetNumberOfComponents] |
---|
| 1314 | lappend _comp2fldName($cname) $fname |
---|
[3330] | 1315 | } |
---|
[3027] | 1316 | } |
---|
[3700] | 1317 | |
---|
[3330] | 1318 | lappend limits v [list $vmin $vmax] |
---|
| 1319 | set _comp2limits($cname) $limits |
---|
[3707] | 1320 | |
---|
[3416] | 1321 | rename $reader "" |
---|
[3027] | 1322 | } |
---|
| 1323 | |
---|
[3330] | 1324 | # |
---|
| 1325 | # vtkdata -- |
---|
| 1326 | # |
---|
| 1327 | # Returns a string representing the mesh and field data for a specific |
---|
| 1328 | # component in the legacy VTK file format. |
---|
| 1329 | # |
---|
| 1330 | itcl::body Rappture::Field::vtkdata {cname} { |
---|
| 1331 | if {$cname == "component0"} { |
---|
| 1332 | set cname "component" |
---|
| 1333 | } |
---|
| 1334 | # DX: Convert DX to VTK |
---|
| 1335 | if {[info exists _comp2dx($cname)]} { |
---|
[3757] | 1336 | set data $_comp2dx($cname) |
---|
| 1337 | set data [Rappture::encoding::decode $data] |
---|
| 1338 | return [Rappture::DxToVtk $data] |
---|
[3330] | 1339 | } |
---|
| 1340 | # Unirect3d: isosurface |
---|
| 1341 | if {[info exists _comp2unirect3d($cname)]} { |
---|
| 1342 | return [$_comp2unirect3d($cname) vtkdata] |
---|
| 1343 | } |
---|
| 1344 | # VTK file data: |
---|
| 1345 | if { [info exists _comp2vtk($cname)] } { |
---|
| 1346 | return $_comp2vtk($cname) |
---|
| 1347 | } |
---|
| 1348 | # Points on mesh: Construct VTK file output. |
---|
| 1349 | if { [info exists _comp2mesh($cname)] } { |
---|
| 1350 | # Data is in the form mesh and vector |
---|
| 1351 | foreach {mesh vector} $_comp2mesh($cname) break |
---|
[3460] | 1352 | set label $cname |
---|
[3421] | 1353 | regsub -all { } $label {_} label |
---|
[3330] | 1354 | append out "# vtk DataFile Version 3.0\n" |
---|
| 1355 | append out "[hints label]\n" |
---|
| 1356 | append out "ASCII\n" |
---|
| 1357 | append out [$mesh vtkdata] |
---|
[3684] | 1358 | |
---|
| 1359 | if { $_comp2assoc($cname) == "pointdata" } { |
---|
| 1360 | set vtkassoc "POINT_DATA" |
---|
| 1361 | } elseif { $_comp2assoc($cname) == "celldata" } { |
---|
| 1362 | set vtkassoc "CELL_DATA" |
---|
| 1363 | } elseif { $_comp2assoc($cname) == "fielddata" } { |
---|
[3700] | 1364 | set vtkassoc "FIELD" |
---|
[3684] | 1365 | } else { |
---|
| 1366 | error "unknown association \"$_comp2assoc($cname)\"" |
---|
| 1367 | } |
---|
| 1368 | set elemSize [numComponents $cname] |
---|
| 1369 | set numValues [expr [$vector length] / $elemSize] |
---|
[3701] | 1370 | if { $_comp2assoc($cname) == "fielddata" } { |
---|
| 1371 | append out "$vtkassoc FieldData 1\n" |
---|
| 1372 | append out "$label $elemSize $numValues double\n" |
---|
[3684] | 1373 | } else { |
---|
[3701] | 1374 | append out "$vtkassoc $numValues\n" |
---|
| 1375 | if { $_comp2type($cname) == "colorscalars" } { |
---|
| 1376 | # Must be float for ASCII, unsigned char for BINARY |
---|
| 1377 | append out "COLOR_SCALARS $label $elemSize\n" |
---|
| 1378 | } elseif { $_comp2type($cname) == "normals" } { |
---|
| 1379 | # elemSize must equal 3 |
---|
| 1380 | append out "NORMALS $label double\n" |
---|
| 1381 | } elseif { $_comp2type($cname) == "scalars" } { |
---|
| 1382 | # elemSize can be 1, 2, 3 or 4 |
---|
| 1383 | append out "SCALARS $label double $elemSize\n" |
---|
| 1384 | append out "LOOKUP_TABLE default\n" |
---|
| 1385 | } elseif { $_comp2type($cname) == "tcoords" } { |
---|
| 1386 | # elemSize must be 1, 2, or 3 |
---|
| 1387 | append out "TEXTURE_COORDINATES $label $elemSize double\n" |
---|
| 1388 | } elseif { $_comp2type($cname) == "tensors" } { |
---|
| 1389 | # elemSize must equal 9 |
---|
| 1390 | append out "TENSORS $label double\n" |
---|
| 1391 | } elseif { $_comp2type($cname) == "vectors" } { |
---|
| 1392 | # elemSize must equal 3 |
---|
| 1393 | append out "VECTORS $label double\n" |
---|
| 1394 | } else { |
---|
| 1395 | error "unknown element type \"$_comp2type($cname)\"" |
---|
| 1396 | } |
---|
[3684] | 1397 | } |
---|
[3700] | 1398 | append out [$vector range 0 end] |
---|
| 1399 | append out "\n" |
---|
[3585] | 1400 | if 0 { |
---|
| 1401 | VerifyVtkDataSet $out |
---|
| 1402 | } |
---|
[3330] | 1403 | return $out |
---|
| 1404 | } |
---|
| 1405 | error "can't find vtkdata for $cname. This method should only be called by the vtkheightmap widget" |
---|
| 1406 | } |
---|
| 1407 | |
---|
| 1408 | # |
---|
| 1409 | # BuildPointsOnMesh -- |
---|
| 1410 | # |
---|
| 1411 | # Parses the field XML description to build a mesh and values vector |
---|
| 1412 | # representing the field. Right now we handle the deprecated types |
---|
| 1413 | # of "cloud", "unirect2d", and "unirect3d" (mostly for flows). |
---|
| 1414 | # |
---|
| 1415 | itcl::body Rappture::Field::BuildPointsOnMesh {cname} { |
---|
| 1416 | # |
---|
| 1417 | # More complex 2D/3D data is represented by a mesh |
---|
| 1418 | # object and an associated vector for field values. |
---|
| 1419 | # |
---|
[3571] | 1420 | set path [$_field get $cname.mesh] |
---|
[3330] | 1421 | if {[$_xmlobj element $path] == ""} { |
---|
| 1422 | # Unknown mesh designated. |
---|
[3571] | 1423 | return 0 |
---|
[3330] | 1424 | } |
---|
[3684] | 1425 | set viewer [$_field get "about.view"] |
---|
| 1426 | if { $viewer != "" } { |
---|
| 1427 | set _viewer $viewer |
---|
| 1428 | } |
---|
[3330] | 1429 | set element [$_xmlobj element -as type $path] |
---|
[3405] | 1430 | set name $cname |
---|
| 1431 | regsub -all { } $name {_} name |
---|
[3571] | 1432 | set _fld2Label($name) $name |
---|
[3405] | 1433 | set label [hints zlabel] |
---|
| 1434 | if { $label != "" } { |
---|
[3571] | 1435 | set _fld2Label($name) $label |
---|
[3405] | 1436 | } |
---|
[3571] | 1437 | set _fld2Units($name) [hints zunits] |
---|
[3684] | 1438 | set _fld2Components($name) $_comp2size($cname) |
---|
[3571] | 1439 | lappend _comp2fldName($cname) $name |
---|
[3330] | 1440 | |
---|
| 1441 | # Handle bizarre cases that hopefully will be deprecated. |
---|
| 1442 | if { $element == "unirect3d" } { |
---|
| 1443 | # Special case: unirect3d (should be deprecated) + flow. |
---|
[3571] | 1444 | if { [$_field element $cname.extents] != "" } { |
---|
[3684] | 1445 | set vectorsize [$_field get $cname.extents] |
---|
[3330] | 1446 | } else { |
---|
[3684] | 1447 | set vectorsize 1 |
---|
[3330] | 1448 | } |
---|
| 1449 | set _dim 3 |
---|
[3557] | 1450 | if { $_viewer == "" } { |
---|
| 1451 | set _viewer flowvis |
---|
| 1452 | } |
---|
[3330] | 1453 | set _comp2dims($cname) "3D" |
---|
| 1454 | set _comp2unirect3d($cname) \ |
---|
[3684] | 1455 | [Rappture::Unirect3d \#auto $_xmlobj $_field $cname $vectorsize] |
---|
[3571] | 1456 | set _comp2style($cname) [$_field get $cname.style] |
---|
| 1457 | if {[$_field element $cname.flow] != ""} { |
---|
[3330] | 1458 | set _comp2flowhints($cname) \ |
---|
[3571] | 1459 | [Rappture::FlowHints ::\#auto $_field $cname $_units] |
---|
[3330] | 1460 | } |
---|
| 1461 | incr _counter |
---|
[3571] | 1462 | return 1 |
---|
[3330] | 1463 | } |
---|
[3571] | 1464 | if { $element == "unirect2d" && [$_field element $cname.flow] != "" } { |
---|
[3330] | 1465 | # Special case: unirect2d (normally deprecated) + flow. |
---|
[3571] | 1466 | if { [$_field element $cname.extents] != "" } { |
---|
[3684] | 1467 | set vectorsize [$_field get $cname.extents] |
---|
[3330] | 1468 | } else { |
---|
[3684] | 1469 | set vectorsize 1 |
---|
[3330] | 1470 | } |
---|
| 1471 | set _dim 2 |
---|
[3557] | 1472 | if { $_viewer == "" } { |
---|
| 1473 | set _viewer "flowvis" |
---|
| 1474 | } |
---|
[3330] | 1475 | set _comp2dims($cname) "2D" |
---|
| 1476 | set _comp2unirect2d($cname) \ |
---|
| 1477 | [Rappture::Unirect2d \#auto $_xmlobj $path] |
---|
[3571] | 1478 | set _comp2style($cname) [$_field get $cname.style] |
---|
[3330] | 1479 | set _comp2flowhints($cname) \ |
---|
[3571] | 1480 | [Rappture::FlowHints ::\#auto $_field $cname $_units] |
---|
| 1481 | set _values [$_field get $cname.values] |
---|
[3365] | 1482 | set limits {} |
---|
| 1483 | foreach axis { x y } { |
---|
| 1484 | lappend limits $axis [$_comp2unirect2d($cname) limits $axis] |
---|
| 1485 | } |
---|
| 1486 | set xv [blt::vector create \#auto] |
---|
| 1487 | $xv set $_values |
---|
[3428] | 1488 | lappend limits $cname [$xv limits] |
---|
[3481] | 1489 | lappend limits v [$xv limits] |
---|
[3365] | 1490 | blt::vector destroy $xv |
---|
| 1491 | set _comp2limits($cname) $limits |
---|
[3330] | 1492 | incr _counter |
---|
[3571] | 1493 | return 1 |
---|
[3330] | 1494 | } |
---|
| 1495 | switch -- $element { |
---|
| 1496 | "cloud" { |
---|
| 1497 | set mesh [Rappture::Cloud::fetch $_xmlobj $path] |
---|
| 1498 | } |
---|
| 1499 | "mesh" { |
---|
| 1500 | set mesh [Rappture::Mesh::fetch $_xmlobj $path] |
---|
| 1501 | } |
---|
| 1502 | "unirect2d" { |
---|
[3557] | 1503 | if { $_viewer == "" } { |
---|
| 1504 | set _viewer "heightmap" |
---|
| 1505 | } |
---|
[3330] | 1506 | set mesh [Rappture::Unirect2d::fetch $_xmlobj $path] |
---|
| 1507 | } |
---|
| 1508 | } |
---|
[3571] | 1509 | if { ![$mesh isvalid] } { |
---|
| 1510 | return 0 |
---|
| 1511 | } |
---|
[3330] | 1512 | set _dim [$mesh dimensions] |
---|
| 1513 | if {$_dim == 1} { |
---|
[3585] | 1514 | # 1D data: Create vectors for graph widget. |
---|
[3330] | 1515 | # Is this used anywhere? |
---|
| 1516 | # |
---|
| 1517 | # OOPS! This is 1D data |
---|
| 1518 | # Forget the cloud/field -- store BLT vectors |
---|
| 1519 | # |
---|
| 1520 | # Is there a natural growth path in generating output from 1D to |
---|
| 1521 | # higher dimensions? If there isn't, let's kill this in favor |
---|
| 1522 | # or explicitly using a <curve> instead. Otherwise, the features |
---|
| 1523 | # (methods such as xmarkers) or the <curve> need to be added |
---|
| 1524 | # to the <field>. |
---|
| 1525 | # |
---|
| 1526 | set xv [blt::vector create x$_counter] |
---|
| 1527 | set yv [blt::vector create y$_counter] |
---|
| 1528 | |
---|
| 1529 | $yv set [$mesh points] |
---|
| 1530 | $xv seq 0 1 [$yv length] |
---|
| 1531 | # sort x-coords in increasing order |
---|
| 1532 | $xv sort $yv |
---|
| 1533 | |
---|
| 1534 | set _comp2dims($cname) "1D" |
---|
| 1535 | set _comp2xy($cname) [list $xv $yv] |
---|
| 1536 | incr _counter |
---|
[3571] | 1537 | return 1 |
---|
| 1538 | } |
---|
| 1539 | if {$_dim == 2} { |
---|
[3585] | 1540 | # 2D data: By default surface or contour plot using heightmap widget. |
---|
[3330] | 1541 | set _type "heightmap" |
---|
[3571] | 1542 | set v [blt::vector create \#auto] |
---|
| 1543 | $v set [$_field get $cname.values] |
---|
| 1544 | if { [$v length] == 0 } { |
---|
| 1545 | return 0 |
---|
| 1546 | } |
---|
[3585] | 1547 | if { $_viewer == "" } { |
---|
| 1548 | set _viewer "contour" |
---|
| 1549 | } |
---|
[3330] | 1550 | set _comp2dims($cname) "[$mesh dimensions]D" |
---|
[3571] | 1551 | set _comp2mesh($cname) [list $mesh $v] |
---|
| 1552 | set _comp2style($cname) [$_field get $cname.style] |
---|
[3330] | 1553 | incr _counter |
---|
| 1554 | array unset _comp2limits $cname |
---|
| 1555 | lappend _comp2limits($cname) x [$mesh limits x] |
---|
| 1556 | lappend _comp2limits($cname) y [$mesh limits y] |
---|
[3571] | 1557 | lappend _comp2limits($cname) $cname [$v limits] |
---|
| 1558 | lappend _comp2limits($cname) v [$v limits] |
---|
| 1559 | return 1 |
---|
| 1560 | } |
---|
| 1561 | if {$_dim == 3} { |
---|
[3585] | 1562 | # 3D data: By default isosurfaces plot using isosurface widget. |
---|
[3557] | 1563 | if { $_viewer == "" } { |
---|
| 1564 | set _viewer "isosurface" |
---|
| 1565 | } |
---|
[3330] | 1566 | set _type "isosurface" |
---|
[3571] | 1567 | set v [blt::vector create \#auto] |
---|
| 1568 | $v set [$_field get $cname.values] |
---|
| 1569 | if { [$v length] == 0 } { |
---|
| 1570 | return 0 |
---|
| 1571 | } |
---|
| 1572 | set _comp2dims($cname) "[$mesh dimensions]D" |
---|
| 1573 | set _comp2mesh($cname) [list $mesh $v] |
---|
| 1574 | set _comp2style($cname) [$_field get $cname.style] |
---|
| 1575 | incr _counter |
---|
| 1576 | lappend _comp2limits($cname) x [$mesh limits x] |
---|
| 1577 | lappend _comp2limits($cname) y [$mesh limits y] |
---|
| 1578 | lappend _comp2limits($cname) z [$mesh limits z] |
---|
| 1579 | lappend _comp2limits($cname) $cname [$v limits] |
---|
| 1580 | lappend _comp2limits($cname) v [$v limits] |
---|
| 1581 | return 1 |
---|
[3330] | 1582 | } |
---|
[3405] | 1583 | error "unhandled case in field dim=$_dim element=$element" |
---|
[3330] | 1584 | } |
---|
| 1585 | |
---|
[3546] | 1586 | itcl::body Rappture::Field::AvsToVtk { cname contents } { |
---|
[3330] | 1587 | package require vtk |
---|
| 1588 | |
---|
| 1589 | set reader $this-datasetreader |
---|
| 1590 | vtkAVSucdReader $reader |
---|
| 1591 | |
---|
| 1592 | # Write the contents to a file just in case it's binary. |
---|
[3546] | 1593 | set tmpfile $cname[pid].ucd |
---|
[3330] | 1594 | set f [open "$tmpfile" "w"] |
---|
| 1595 | fconfigure $f -translation binary -encoding binary |
---|
| 1596 | puts $f $contents |
---|
| 1597 | close $f |
---|
| 1598 | $reader SetFileName $tmpfile |
---|
| 1599 | $reader Update |
---|
| 1600 | file delete $tmpfile |
---|
| 1601 | |
---|
[3546] | 1602 | set tmpfile $cname[pid].vtk |
---|
[3330] | 1603 | set writer $this-datasetwriter |
---|
| 1604 | vtkDataSetWriter $writer |
---|
| 1605 | $writer SetInputConnection [$reader GetOutputPort] |
---|
| 1606 | $writer SetFileName $tmpfile |
---|
| 1607 | $writer Write |
---|
| 1608 | rename $reader "" |
---|
| 1609 | rename $writer "" |
---|
[3546] | 1610 | |
---|
[3330] | 1611 | set f [open "$tmpfile" "r"] |
---|
| 1612 | fconfigure $f -translation binary -encoding binary |
---|
| 1613 | set vtkdata [read $f] |
---|
| 1614 | close $f |
---|
[3548] | 1615 | file delete $tmpfile |
---|
[3330] | 1616 | return $vtkdata |
---|
| 1617 | } |
---|
[3684] | 1618 | |
---|
| 1619 | itcl::body Rappture::Field::GetTypeAndSize { cname } { |
---|
| 1620 | array set type2components { |
---|
[3697] | 1621 | "colorscalars" 4 |
---|
| 1622 | "normals" 3 |
---|
| 1623 | "scalars" 1 |
---|
| 1624 | "tcoords" 2 |
---|
| 1625 | "tensors" 9 |
---|
| 1626 | "vectors" 3 |
---|
[3684] | 1627 | } |
---|
| 1628 | set type [$_field get $cname.elemtype] |
---|
| 1629 | if { $type == "" } { |
---|
[3699] | 1630 | set type "scalars" |
---|
[3684] | 1631 | } |
---|
| 1632 | if { ![info exists type2components($type)] } { |
---|
| 1633 | error "unknown <elemtype> \"$type\" in field" |
---|
| 1634 | } |
---|
| 1635 | set size [$_field get $cname.elemsize] |
---|
| 1636 | if { $size == "" } { |
---|
| 1637 | set size $type2components($type) |
---|
| 1638 | } |
---|
| 1639 | set _comp2type($cname) $type |
---|
| 1640 | set _comp2size($cname) $size |
---|
| 1641 | } |
---|
| 1642 | |
---|
| 1643 | itcl::body Rappture::Field::GetAssociation { cname } { |
---|
| 1644 | set assoc [$_field get $cname.association] |
---|
| 1645 | if { $assoc == "" } { |
---|
| 1646 | set _comp2assoc($cname) "pointdata" |
---|
| 1647 | return |
---|
| 1648 | } |
---|
| 1649 | switch -- $assoc { |
---|
| 1650 | "pointdata" - "celldata" - "fielddata" { |
---|
| 1651 | set _comp2assoc($cname) $assoc |
---|
| 1652 | return |
---|
| 1653 | } |
---|
| 1654 | default { |
---|
| 1655 | error "unknown field association \"$assoc\"" |
---|
| 1656 | } |
---|
| 1657 | } |
---|
| 1658 | } |
---|