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