source: trunk/gui/scripts/unirect2d.tcl @ 2606

Last change on this file since 2606 was 2353, checked in by gah, 13 years ago

new nanoscale

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