1 | |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: unirect2d - represents a uniform rectangular 2-D mesh. |
---|
4 | # |
---|
5 | # This object represents one field in an XML description of a device. |
---|
6 | # It simplifies the process of extracting data vectors that represent |
---|
7 | # the field. |
---|
8 | # ====================================================================== |
---|
9 | # AUTHOR: Michael McLennan, Purdue University |
---|
10 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
11 | # |
---|
12 | # See the file "license.terms" for information on usage and |
---|
13 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | # ====================================================================== |
---|
15 | package require Itcl |
---|
16 | package require BLT |
---|
17 | |
---|
18 | namespace eval Rappture { # forward declaration } |
---|
19 | |
---|
20 | itcl::class Rappture::Unirect2d { |
---|
21 | constructor {xmlobj field cname {extents 1}} { # defined below } |
---|
22 | destructor { # defined below } |
---|
23 | |
---|
24 | public method limits {axis} |
---|
25 | public method blob {} |
---|
26 | public method mesh {} |
---|
27 | public method values {} |
---|
28 | public method hints {{keyword ""}} |
---|
29 | private method GetString { obj path varName } |
---|
30 | private method GetValue { obj path varName } |
---|
31 | private method GetSize { obj path varName } |
---|
32 | |
---|
33 | private variable _axisOrder "x y" |
---|
34 | private variable _xMax 0 |
---|
35 | private variable _xMin 0 |
---|
36 | private variable _xNum 0 |
---|
37 | private variable _yMax 0 |
---|
38 | private variable _yMin 0 |
---|
39 | private variable _yNum 0 |
---|
40 | private variable _compNum 1 |
---|
41 | private variable _values ""; # BLT vector containing the z-values |
---|
42 | private variable _hints |
---|
43 | } |
---|
44 | |
---|
45 | # ---------------------------------------------------------------------- |
---|
46 | # Constructor |
---|
47 | # ---------------------------------------------------------------------- |
---|
48 | itcl::body Rappture::Unirect2d::constructor {xmlobj field cname {extents 1}} { |
---|
49 | if {![Rappture::library isvalid $xmlobj]} { |
---|
50 | error "bad value \"$xmlobj\": should be Rappture::library" |
---|
51 | } |
---|
52 | set path [$field get $cname.mesh] |
---|
53 | |
---|
54 | set m [$xmlobj element -as object $path] |
---|
55 | GetValue $m "xaxis.min" _xMin |
---|
56 | GetValue $m "xaxis.max" _xMax |
---|
57 | GetSize $m "xaxis.numpoints" _xNum |
---|
58 | GetValue $m "yaxis.min" _yMin |
---|
59 | GetValue $m "yaxis.max" _yMax |
---|
60 | GetSize $m "yaxis.numpoints" _yNum |
---|
61 | set _compNum $extents |
---|
62 | foreach {key path} { |
---|
63 | group about.group |
---|
64 | label about.label |
---|
65 | color about.color |
---|
66 | style about.style |
---|
67 | type about.type |
---|
68 | xlabel xaxis.label |
---|
69 | xdesc xaxis.description |
---|
70 | xunits xaxis.units |
---|
71 | xscale xaxis.scale |
---|
72 | xmin xaxis.min |
---|
73 | xmax xaxis.max |
---|
74 | ylabel yaxis.label |
---|
75 | ydesc yaxis.description |
---|
76 | yunits yaxis.units |
---|
77 | yscale yaxis.scale |
---|
78 | ymin yaxis.min |
---|
79 | ymax yaxis.max |
---|
80 | } { |
---|
81 | set str [$m get $path] |
---|
82 | if {"" != $str} { |
---|
83 | set _hints($key) $str |
---|
84 | } |
---|
85 | } |
---|
86 | foreach {key} { axisorder } { |
---|
87 | set str [$field get $cname.$key] |
---|
88 | if {"" != $str} { |
---|
89 | set _hints($key) $str |
---|
90 | } |
---|
91 | } |
---|
92 | itcl::delete object $m |
---|
93 | |
---|
94 | set _values [blt::vector create \#auto] |
---|
95 | set values [$field get "$cname.values"] |
---|
96 | if { $values == "" } { |
---|
97 | set values [$field get "$cname.zvalues"] |
---|
98 | } |
---|
99 | $_values set $values |
---|
100 | set n [expr $_xNum * $_yNum * $_compNum] |
---|
101 | if { [$_values length] != $n } { |
---|
102 | error "wrong \# of values in \"$cname.values\": expected $n values" |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | # ---------------------------------------------------------------------- |
---|
107 | # Destructor |
---|
108 | # ---------------------------------------------------------------------- |
---|
109 | itcl::body Rappture::Unirect2d::destructor {} { |
---|
110 | if { $_values != "" } { |
---|
111 | blt::vector destroy $_values |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | # ---------------------------------------------------------------------- |
---|
116 | # method blob |
---|
117 | # Returns a base64 encoded, gzipped Tcl list that represents the |
---|
118 | # Tcl command and data to recreate the uniform rectangular grid |
---|
119 | # on the nanovis server. |
---|
120 | # ---------------------------------------------------------------------- |
---|
121 | itcl::body Rappture::Unirect2d::blob {} { |
---|
122 | set data "unirect2d" |
---|
123 | lappend data "xmin" $_xMin "xmax" $_xMax "xnum" $_xNum |
---|
124 | lappend data "ymin" $_yMin "ymax" $_yMax "ynum" $_yNum |
---|
125 | lappend data "xmin" $_xMin "ymin" $_yMin "xmax" $_xMax "ymax" $_yMax |
---|
126 | foreach key { axisorder xunits yunits units } { |
---|
127 | set hint [hints $key] |
---|
128 | if { $hint != "" } { |
---|
129 | lappend data $key $hint |
---|
130 | } |
---|
131 | } |
---|
132 | if { [$_values length] > 0 } { |
---|
133 | lappend data "values" [$_values range 0 end] |
---|
134 | } |
---|
135 | return [Rappture::encoding::encode -as zb64 "$data"] |
---|
136 | } |
---|
137 | |
---|
138 | # ---------------------------------------------------------------------- |
---|
139 | # method mesh |
---|
140 | # Returns a base64 encoded, gzipped Tcl list that represents the |
---|
141 | # Tcl command and data to recreate the uniform rectangular grid |
---|
142 | # on the nanovis server. |
---|
143 | # ---------------------------------------------------------------------- |
---|
144 | itcl::body Rappture::Unirect2d::mesh {} { |
---|
145 | set dx [expr {($_xMax - $_xMin) / double($_xNum)}] |
---|
146 | set dy [expr {($_yMax - $_yMin) / double($_yNum)}] |
---|
147 | for { set i 0 } { $i < $_xNum } { incr i } { |
---|
148 | set x [expr {$_xMin + (double($i) * $dx)}] |
---|
149 | for { set j 0 } { $j < $_yNum } { incr j } { |
---|
150 | set y [expr {$_yMin + (double($i) * $dy)}] |
---|
151 | lappend data $x $y |
---|
152 | } |
---|
153 | } |
---|
154 | return $data |
---|
155 | } |
---|
156 | |
---|
157 | # ---------------------------------------------------------------------- |
---|
158 | # method values |
---|
159 | # Returns a base64 encoded, gzipped Tcl list that represents the |
---|
160 | # Tcl command and data to recreate the uniform rectangular grid |
---|
161 | # on the nanovis server. |
---|
162 | # ---------------------------------------------------------------------- |
---|
163 | itcl::body Rappture::Unirect2d::values {} { |
---|
164 | if { [$_values length] > 0 } { |
---|
165 | return [$_values range 0 end] |
---|
166 | } |
---|
167 | return "" |
---|
168 | } |
---|
169 | |
---|
170 | # ---------------------------------------------------------------------- |
---|
171 | # method limits <axis> |
---|
172 | # Returns a list {min max} representing the limits for the |
---|
173 | # specified axis. |
---|
174 | # ---------------------------------------------------------------------- |
---|
175 | itcl::body Rappture::Unirect2d::limits {which} { |
---|
176 | set min "" |
---|
177 | set max "" |
---|
178 | |
---|
179 | switch -- $which { |
---|
180 | x - xlin - xlog { |
---|
181 | set min $_xMin |
---|
182 | set max $_xMax |
---|
183 | set axis "xaxis" |
---|
184 | } |
---|
185 | y - ylin - ylog { |
---|
186 | set min $_yMin |
---|
187 | set max $_yMax |
---|
188 | set axis "yaxis" |
---|
189 | } |
---|
190 | v - vlin - vlog - z - zlin - zlog { |
---|
191 | if { [$_values length] > 0 } { |
---|
192 | set min [blt::vector expr min($_values)] |
---|
193 | set max [blt::vector expr max($_values)] |
---|
194 | } else { |
---|
195 | set min 0.0 |
---|
196 | set max 1.0 |
---|
197 | } |
---|
198 | set axis "zaxis" |
---|
199 | } |
---|
200 | default { |
---|
201 | error "unknown axis description \"$which\"" |
---|
202 | } |
---|
203 | } |
---|
204 | # set val [$_field get $axis.min] |
---|
205 | # if {"" != $val && "" != $min} { |
---|
206 | # if {$val > $min} { |
---|
207 | # # tool specified this min -- don't go any lower |
---|
208 | # set min $val |
---|
209 | # } |
---|
210 | # } |
---|
211 | # set val [$_field get $axis.max] |
---|
212 | # if {"" != $val && "" != $max} { |
---|
213 | # if {$val < $max} { |
---|
214 | # # tool specified this max -- don't go any higher |
---|
215 | # set max $val |
---|
216 | # } |
---|
217 | # } |
---|
218 | |
---|
219 | return [list $min $max] |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | # ---------------------------------------------------------------------- |
---|
224 | # USAGE: hints ?<keyword>? |
---|
225 | # |
---|
226 | # Returns a list of key/value pairs for various hints about plotting |
---|
227 | # this curve. If a particular <keyword> is specified, then it returns |
---|
228 | # the hint for that <keyword>, if it exists. |
---|
229 | # ---------------------------------------------------------------------- |
---|
230 | itcl::body Rappture::Unirect2d::hints { {keyword ""} } { |
---|
231 | if {[info exists _hints(xlabel)] && "" != $_hints(xlabel) |
---|
232 | && [info exists _hints(xunits)] && "" != $_hints(xunits)} { |
---|
233 | set _hints(xlabel) "$_hints(xlabel) ($_hints(xunits))" |
---|
234 | } |
---|
235 | if {[info exists _hints(ylabel)] && "" != $_hints(ylabel) |
---|
236 | && [info exists _hints(yunits)] && "" != $_hints(yunits)} { |
---|
237 | set _hints(ylabel) "$_hints(ylabel) ($_hints(yunits))" |
---|
238 | } |
---|
239 | |
---|
240 | if {[info exists _hints(group)] && [info exists _hints(label)]} { |
---|
241 | # pop-up help for each curve |
---|
242 | set _hints(tooltip) $_hints(label) |
---|
243 | } |
---|
244 | if {$keyword != ""} { |
---|
245 | if {[info exists _hints($keyword)]} { |
---|
246 | return $_hints($keyword) |
---|
247 | } |
---|
248 | return "" |
---|
249 | } |
---|
250 | return [array get _hints] |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | itcl::body Rappture::Unirect2d::GetSize { obj path varName } { |
---|
255 | set string [$obj get $path] |
---|
256 | if { [scan $string "%d" value] != 1 || $value < 0 } { |
---|
257 | puts stderr "can't get size \"$string\" of \"$path\"" |
---|
258 | return |
---|
259 | } |
---|
260 | upvar $varName size |
---|
261 | set size $value |
---|
262 | } |
---|
263 | |
---|
264 | itcl::body Rappture::Unirect2d::GetValue { obj path varName } { |
---|
265 | set string [$obj get $path] |
---|
266 | if { [scan $string "%g" value] != 1 } { |
---|
267 | return |
---|
268 | } |
---|
269 | upvar $varName number |
---|
270 | set number $value |
---|
271 | } |
---|
272 | |
---|
273 | itcl::body Rappture::Unirect2d::GetString { obj path varName } { |
---|
274 | set string [$obj get $path] |
---|
275 | if { $string == "" } { |
---|
276 | puts stderr "can't get string \"$string\" of \"$path\"" |
---|
277 | return |
---|
278 | } |
---|
279 | upvar $varName str |
---|
280 | set str $string |
---|
281 | } |
---|