[5091] | 1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
[11] | 2 | # ---------------------------------------------------------------------- |
---|
| 3 | # COMPONENT: cloud - represents the mesh for a cloud of points |
---|
| 4 | # |
---|
| 5 | # This object represents the mesh for a cloud of points in an XML |
---|
| 6 | # description of a device. It simplifies the process of extracting |
---|
| 7 | # data that represent the mesh. |
---|
| 8 | # ====================================================================== |
---|
| 9 | # AUTHOR: Michael McLennan, Purdue University |
---|
[3177] | 10 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[115] | 11 | # |
---|
| 12 | # See the file "license.terms" for information on usage and |
---|
| 13 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[11] | 14 | # ====================================================================== |
---|
| 15 | package require Itcl |
---|
| 16 | |
---|
| 17 | namespace eval Rappture { # forward declaration } |
---|
| 18 | |
---|
| 19 | itcl::class Rappture::Cloud { |
---|
[3571] | 20 | private variable _xmlobj ""; # ref to XML obj with device data |
---|
| 21 | private variable _cloud ""; # lib obj representing this cloud |
---|
[3923] | 22 | private variable _units "" ; # system of units for x, y, z |
---|
[5091] | 23 | private variable _axis2label; # |
---|
| 24 | private variable _axis2units; # |
---|
[3571] | 25 | private variable _limits; # limits x, y, z |
---|
| 26 | private common _xp2obj ; # Used for fetch/release ref counting |
---|
| 27 | private common _obj2ref ; # Used for fetch/release ref counting |
---|
| 28 | private variable _numPoints 0 |
---|
| 29 | private variable _vtkdata "" |
---|
| 30 | private variable _points "" |
---|
| 31 | private variable _dim 0 |
---|
| 32 | private variable _isValid 0; # Indicates if the data is valid. |
---|
[11] | 33 | |
---|
[5091] | 34 | constructor {xmlobj path} { |
---|
| 35 | # defined below |
---|
[3571] | 36 | } |
---|
[5091] | 37 | destructor { |
---|
| 38 | # defined below |
---|
[3571] | 39 | } |
---|
[11] | 40 | public method points {} |
---|
[17] | 41 | public method mesh {} |
---|
[3921] | 42 | public method units { axis } |
---|
| 43 | public method label { axis } |
---|
[3330] | 44 | public method vtkdata {} |
---|
[11] | 45 | public method size {} |
---|
| 46 | public method dimensions {} |
---|
| 47 | public method limits {which} |
---|
| 48 | public method hints {{key ""}} |
---|
[3330] | 49 | public method numpoints {} { |
---|
[5090] | 50 | return $_numPoints |
---|
[3330] | 51 | } |
---|
[3571] | 52 | public method isvalid {} { |
---|
| 53 | return $_isValid |
---|
| 54 | } |
---|
[11] | 55 | public proc fetch {xmlobj path} |
---|
| 56 | public proc release {obj} |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | # ---------------------------------------------------------------------- |
---|
| 60 | # USAGE: Rappture::Cloud::fetch <xmlobj> <path> |
---|
| 61 | # |
---|
| 62 | # Clients use this instead of a constructor to fetch the Cloud for |
---|
| 63 | # a particular <path> in the <xmlobj>. When the client is done with |
---|
| 64 | # the cloud, he calls "release" to decrement the reference count. |
---|
| 65 | # When the cloud is no longer needed, it is cleaned up automatically. |
---|
| 66 | # ---------------------------------------------------------------------- |
---|
| 67 | itcl::body Rappture::Cloud::fetch {xmlobj path} { |
---|
| 68 | set handle "$xmlobj|$path" |
---|
| 69 | if {[info exists _xp2obj($handle)]} { |
---|
[1929] | 70 | set obj $_xp2obj($handle) |
---|
| 71 | incr _obj2ref($obj) |
---|
| 72 | return $obj |
---|
[11] | 73 | } |
---|
| 74 | |
---|
| 75 | set obj [Rappture::Cloud ::#auto $xmlobj $path] |
---|
| 76 | set _xp2obj($handle) $obj |
---|
| 77 | set _obj2ref($obj) 1 |
---|
| 78 | return $obj |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | # ---------------------------------------------------------------------- |
---|
| 82 | # USAGE: Rappture::Cloud::release <obj> |
---|
| 83 | # |
---|
| 84 | # Clients call this when they're no longer using a Cloud fetched |
---|
| 85 | # previously by the "fetch" proc. This decrements the reference |
---|
| 86 | # count for the cloud and destroys the object when it is no longer |
---|
| 87 | # in use. |
---|
| 88 | # ---------------------------------------------------------------------- |
---|
| 89 | itcl::body Rappture::Cloud::release {obj} { |
---|
| 90 | if {[info exists _obj2ref($obj)]} { |
---|
[1929] | 91 | incr _obj2ref($obj) -1 |
---|
| 92 | if {$_obj2ref($obj) <= 0} { |
---|
| 93 | unset _obj2ref($obj) |
---|
| 94 | foreach handle [array names _xp2obj] { |
---|
| 95 | if {$_xp2obj($handle) == $obj} { |
---|
| 96 | unset _xp2obj($handle) |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | itcl::delete object $obj |
---|
| 100 | } |
---|
[11] | 101 | } else { |
---|
[1929] | 102 | error "can't find reference count for $obj" |
---|
[11] | 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | # ---------------------------------------------------------------------- |
---|
| 107 | # CONSTRUCTOR |
---|
| 108 | # ---------------------------------------------------------------------- |
---|
| 109 | itcl::body Rappture::Cloud::constructor {xmlobj path} { |
---|
| 110 | if {![Rappture::library isvalid $xmlobj]} { |
---|
[1929] | 111 | error "bad value \"$xmlobj\": should be Rappture::library" |
---|
[11] | 112 | } |
---|
| 113 | set _xmlobj $xmlobj |
---|
| 114 | set _cloud [$xmlobj element -as object $path] |
---|
| 115 | |
---|
[3922] | 116 | set _units [$_cloud get units] |
---|
| 117 | set first [lindex $_units 0] |
---|
[3923] | 118 | set list {} |
---|
[3922] | 119 | foreach u $_units axis { x y z } { |
---|
| 120 | if { $u != "" } { |
---|
[5091] | 121 | set _axis2units($axis) $u |
---|
[3922] | 122 | } else { |
---|
[5091] | 123 | set _axis2units($axis) $first |
---|
[1929] | 124 | } |
---|
[3923] | 125 | lappend list $_axis2units($axis) |
---|
[11] | 126 | } |
---|
[3923] | 127 | set _units $list |
---|
[3922] | 128 | foreach label [$_cloud get labels] axis { x y z } { |
---|
| 129 | if { $label != "" } { |
---|
[4442] | 130 | set _axis2label($axis) $label |
---|
[3922] | 131 | } else { |
---|
[4442] | 132 | set _axis2label($axis) [string toupper $axis] |
---|
[3922] | 133 | } |
---|
| 134 | } |
---|
[11] | 135 | |
---|
[3330] | 136 | set _numPoints 0 |
---|
| 137 | set _points {} |
---|
[11] | 138 | foreach line [split [$xmlobj get $path.points] \n] { |
---|
[1929] | 139 | if {"" == [string trim $line]} { |
---|
| 140 | continue |
---|
| 141 | } |
---|
[11] | 142 | |
---|
[1929] | 143 | # make sure we have x,y,z |
---|
| 144 | while {[llength $line] < 3} { |
---|
| 145 | lappend line "0" |
---|
| 146 | } |
---|
[11] | 147 | |
---|
[3571] | 148 | # Extract each point and add it to the points list |
---|
[1929] | 149 | foreach {x y z} $line break |
---|
[4924] | 150 | foreach axis {x y z} { |
---|
| 151 | # Units on point coordinates are NOT supported |
---|
| 152 | set value [set $axis] |
---|
| 153 | # Update limits |
---|
[3571] | 154 | if { ![info exists _limits($axis)] } { |
---|
| 155 | set _limits($axis) [list $value $value] |
---|
[1929] | 156 | } else { |
---|
[3571] | 157 | foreach { min max } $_limits($axis) break |
---|
[5091] | 158 | if {$value < $min} { |
---|
[3571] | 159 | set min $value |
---|
| 160 | } |
---|
[5091] | 161 | if {$value > $max} { |
---|
[3571] | 162 | set max $value |
---|
| 163 | } |
---|
| 164 | set _limits($axis) [list $min $max] |
---|
[1929] | 165 | } |
---|
| 166 | } |
---|
[3330] | 167 | append _points "$x $y $z\n" |
---|
[5090] | 168 | incr _numPoints |
---|
[11] | 169 | } |
---|
[3330] | 170 | append out "DATASET POLYDATA\n" |
---|
[3571] | 171 | append out "POINTS $_numPoints double\n" |
---|
[3330] | 172 | append out $_points |
---|
| 173 | set _vtkdata $out |
---|
[3571] | 174 | if { $_numPoints == 0 } { |
---|
| 175 | return |
---|
| 176 | } |
---|
[4133] | 177 | set _dim 0 |
---|
[3571] | 178 | foreach { xmin xmax } $_limits(x) break |
---|
[4133] | 179 | if { $xmax > $xmin } { |
---|
| 180 | incr _dim |
---|
[3571] | 181 | } |
---|
[4133] | 182 | foreach { ymin ymax } $_limits(y) break |
---|
| 183 | if { $ymax > $ymin } { |
---|
| 184 | incr _dim |
---|
[3571] | 185 | } |
---|
[4133] | 186 | foreach { zmin zmax } $_limits(z) break |
---|
| 187 | if { $zmax > $zmin } { |
---|
| 188 | incr _dim |
---|
[3571] | 189 | } |
---|
| 190 | set _isValid 1 |
---|
[5150] | 191 | puts stderr "WARNING: The <cloud> element is deprecated. Please use an unstructured <mesh> instead." |
---|
[11] | 192 | } |
---|
| 193 | |
---|
| 194 | # ---------------------------------------------------------------------- |
---|
| 195 | # DESTRUCTOR |
---|
| 196 | # ---------------------------------------------------------------------- |
---|
| 197 | itcl::body Rappture::Cloud::destructor {} { |
---|
| 198 | # don't destroy the _xmlobj! we don't own it! |
---|
| 199 | itcl::delete object $_cloud |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | # ---------------------------------------------------------------------- |
---|
| 203 | # USAGE: points |
---|
| 204 | # |
---|
| 205 | # Returns the vtk object containing the points for this mesh. |
---|
| 206 | # ---------------------------------------------------------------------- |
---|
| 207 | itcl::body Rappture::Cloud::points {} { |
---|
[3330] | 208 | return $_points |
---|
[11] | 209 | } |
---|
| 210 | |
---|
| 211 | # ---------------------------------------------------------------------- |
---|
[17] | 212 | # USAGE: mesh |
---|
| 213 | # |
---|
| 214 | # Returns the vtk object representing the mesh. |
---|
| 215 | # ---------------------------------------------------------------------- |
---|
| 216 | itcl::body Rappture::Cloud::mesh {} { |
---|
[3330] | 217 | return $_points |
---|
[17] | 218 | } |
---|
| 219 | |
---|
| 220 | # ---------------------------------------------------------------------- |
---|
[11] | 221 | # USAGE: size |
---|
| 222 | # |
---|
| 223 | # Returns the number of points in this cloud. |
---|
| 224 | # ---------------------------------------------------------------------- |
---|
| 225 | itcl::body Rappture::Cloud::size {} { |
---|
[3489] | 226 | return $_numPoints |
---|
[11] | 227 | } |
---|
| 228 | |
---|
| 229 | # ---------------------------------------------------------------------- |
---|
| 230 | # USAGE: dimensions |
---|
| 231 | # |
---|
| 232 | # Returns the number of dimensions for this object: 1, 2, or 3. |
---|
| 233 | # ---------------------------------------------------------------------- |
---|
| 234 | itcl::body Rappture::Cloud::dimensions {} { |
---|
[3330] | 235 | return $_dim |
---|
[11] | 236 | } |
---|
| 237 | |
---|
| 238 | # ---------------------------------------------------------------------- |
---|
| 239 | # USAGE: limits x|y|z |
---|
| 240 | # |
---|
| 241 | # Returns the {min max} values for the limits of the specified axis. |
---|
| 242 | # ---------------------------------------------------------------------- |
---|
[3571] | 243 | itcl::body Rappture::Cloud::limits { axis } { |
---|
| 244 | if { ![info exists _limits($axis)] } { |
---|
| 245 | error "bad axis \"$axis\": should be x, y, z" |
---|
[11] | 246 | } |
---|
[3571] | 247 | return $_limits($axis) |
---|
[11] | 248 | } |
---|
| 249 | |
---|
[3921] | 250 | # |
---|
| 251 | # units -- |
---|
| 252 | # |
---|
| 253 | # Returns the units of the given axis. |
---|
| 254 | # |
---|
| 255 | itcl::body Rappture::Cloud::units { axis } { |
---|
[3922] | 256 | if { ![info exists _axis2units($axis)] } { |
---|
| 257 | return "" |
---|
[3921] | 258 | } |
---|
[3922] | 259 | return $_axis2units($axis) |
---|
[3921] | 260 | } |
---|
| 261 | |
---|
| 262 | # |
---|
| 263 | # label -- |
---|
| 264 | # |
---|
| 265 | # Returns the label of the given axis. |
---|
| 266 | # |
---|
| 267 | itcl::body Rappture::Cloud::label { axis } { |
---|
[4442] | 268 | if { ![info exists _axis2label($axis)] } { |
---|
[3922] | 269 | return "" |
---|
[3921] | 270 | } |
---|
[4442] | 271 | return $_axis2label($axis) |
---|
[3921] | 272 | } |
---|
| 273 | |
---|
[11] | 274 | # ---------------------------------------------------------------------- |
---|
| 275 | # USAGE: hints ?<keyword>? |
---|
| 276 | # |
---|
| 277 | # Returns a list of key/value pairs for various hints about plotting |
---|
| 278 | # this field. If a particular <keyword> is specified, then it returns |
---|
| 279 | # the hint for that <keyword>, if it exists. |
---|
| 280 | # ---------------------------------------------------------------------- |
---|
| 281 | itcl::body Rappture::Cloud::hints {{keyword ""}} { |
---|
| 282 | foreach key {label color units} { |
---|
[1929] | 283 | set str [$_cloud get $key] |
---|
| 284 | if {"" != $str} { |
---|
| 285 | set hints($key) $str |
---|
| 286 | } |
---|
[11] | 287 | } |
---|
| 288 | |
---|
| 289 | if {$keyword != ""} { |
---|
[1929] | 290 | if {[info exists hints($keyword)]} { |
---|
| 291 | return $hints($keyword) |
---|
| 292 | } |
---|
| 293 | return "" |
---|
[11] | 294 | } |
---|
| 295 | return [array get hints] |
---|
| 296 | } |
---|
[3330] | 297 | |
---|
| 298 | itcl::body Rappture::Cloud::vtkdata {} { |
---|
| 299 | return $_vtkdata |
---|
| 300 | } |
---|