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

Last change on this file since 3050 was 3050, checked in by gah, 12 years ago
File size: 9.6 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    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        type    about.type
81    } {
82        set str [$m get $path]
83        if {"" != $str} {
84            set _hints($key) $str
85        }
86    }
87    foreach {key} { axisorder } {
88        set str [$field get $cname.$key]
89        if {"" != $str} {
90            set _hints($key) $str
91        }
92    }
93    itcl::delete object $m
94   
95    set _values [blt::vector create \#auto]
96    set values [$field get "$cname.values"]
97    if { $values == "" } {
98        set values [$field get "$cname.zvalues"]
99    }
100    $_values set $values
101    set n [expr $_xNum * $_yNum * $_compNum]
102    if { [$_values length] != $n } {
103        error "wrong \# of values in \"$cname.values\": expected $n values, got [$_values length]"
104    }
105}
106
107# ----------------------------------------------------------------------
108# Destructor
109# ----------------------------------------------------------------------
110itcl::body Rappture::Unirect2d::destructor {} {
111    if { $_values != "" } {
112        blt::vector destroy $_values
113    }
114}
115
116# ----------------------------------------------------------------------
117# method blob
118#       Returns a base64 encoded, gzipped Tcl list that represents the
119#       Tcl command and data to recreate the uniform rectangular grid
120#       on the nanovis server.
121# ----------------------------------------------------------------------
122itcl::body Rappture::Unirect2d::blob {} {
123    set data "unirect2d"
124    lappend data "xmin" $_xMin "xmax" $_xMax "xnum" $_xNum
125    lappend data "ymin" $_yMin "ymax" $_yMax "ynum" $_yNum
126    lappend data "xmin" $_xMin "ymin" $_yMin "xmax" $_xMax "ymax" $_yMax
127    foreach key { axisorder xunits yunits units } {
128        set hint [hints $key]
129        if { $hint != "" } {
130            lappend data $key $hint
131        }
132    }
133    if { [$_values length] > 0 } {
134        lappend data "values" [$_values range 0 end]
135    }
136    return [Rappture::encoding::encode -as zb64 "$data"]
137}
138
139# ----------------------------------------------------------------------
140# method mesh.old
141#       Returns a base64 encoded, gzipped Tcl list that represents the
142#       Tcl command and data to recreate the uniform rectangular grid
143#       on the nanovis server.
144# ----------------------------------------------------------------------
145itcl::body Rappture::Unirect2d::mesh.old {} {
146    set dx [expr {($_xMax - $_xMin) / double($_xNum)}]
147    set dy [expr {($_yMax - $_yMin) / double($_yNum)}]
148    foreach {a b} $_axisOrder break
149    for { set i 0 } { $i < [set _${a}Num] } { incr i } {
150        set x [expr {$_xMin + (double($i) * $dx)}]
151        for { set j 0 } { $j < [set _${b}Num] } { incr j } {
152            set y [expr {$_yMin + (double($i) * $dy)}]
153            lappend data $x $y
154        }
155    }
156    return $data
157}
158
159# ----------------------------------------------------------------------
160# method mesh
161#       Returns a base64 encoded, gzipped Tcl list that represents the
162#       Tcl command and data to recreate the uniform rectangular grid
163#       on the nanovis server.
164# ----------------------------------------------------------------------
165itcl::body Rappture::Unirect2d::mesh {} {
166    lappend out $_xMin $_xMax $_xNum $_yMin $_yMax $_yNum
167    return $out
168}
169
170# ----------------------------------------------------------------------
171# method values
172#       Returns a base64 encoded, gzipped Tcl list that represents the
173#       Tcl command and data to recreate the uniform rectangular grid
174#       on the nanovis server.
175# ----------------------------------------------------------------------
176itcl::body Rappture::Unirect2d::values {} {
177    if { [$_values length] > 0 } {
178        return [$_values range 0 end]
179    }
180    return ""
181}
182
183# ----------------------------------------------------------------------
184# method limits <axis>
185#       Returns a list {min max} representing the limits for the
186#       specified axis.
187# ----------------------------------------------------------------------
188itcl::body Rappture::Unirect2d::limits {which} {
189    set min ""
190    set max ""
191
192    switch -- $which {
193        x - xlin - xlog {
194            set min $_xMin
195            set max $_xMax
196            set axis "xaxis"
197        }
198        y - ylin - ylog {
199            set min $_yMin
200            set max $_yMax
201            set axis "yaxis"
202        }
203        v - vlin - vlog - z - zlin - zlog {
204            if { [$_values length] > 0 } {
205               set min [blt::vector expr min($_values)]
206               set max [blt::vector expr max($_values)]
207            } else {
208                set min 0.0
209                set max 1.0
210            }
211            set axis "zaxis"
212        }
213        default {
214            error "unknown axis description \"$which\""
215        }
216    }
217#     set val [$_field get $axis.min]
218#     if {"" != $val && "" != $min} {
219#         if {$val > $min} {
220#             # tool specified this min -- don't go any lower
221#             set min $val
222#         }
223#     }
224#     set val [$_field get $axis.max]
225#     if {"" != $val && "" != $max} {
226#         if {$val < $max} {
227#             # tool specified this max -- don't go any higher
228#             set max $val
229#         }
230#     }
231
232    return [list $min $max]
233}
234
235
236# ----------------------------------------------------------------------
237# USAGE: hints ?<keyword>?
238#
239# Returns a list of key/value pairs for various hints about plotting
240# this curve.  If a particular <keyword> is specified, then it returns
241# the hint for that <keyword>, if it exists.
242# ----------------------------------------------------------------------
243itcl::body Rappture::Unirect2d::hints { {keyword ""} } {
244    if {[info exists _hints(xlabel)] && "" != $_hints(xlabel)
245        && [info exists _hints(xunits)] && "" != $_hints(xunits)} {
246        set _hints(xlabel) "$_hints(xlabel) ($_hints(xunits))"
247    }
248    if {[info exists _hints(ylabel)] && "" != $_hints(ylabel)
249        && [info exists _hints(yunits)] && "" != $_hints(yunits)} {
250        set _hints(ylabel) "$_hints(ylabel) ($_hints(yunits))"
251    }
252   
253    if {[info exists _hints(group)] && [info exists _hints(label)]} {
254        # pop-up help for each curve
255        set _hints(tooltip) $_hints(label)
256    }
257    if {$keyword != ""} {
258        if {[info exists _hints($keyword)]} {
259            return $_hints($keyword)
260        }
261        return ""
262    }
263    return [array get _hints]
264}
265
266
267itcl::body Rappture::Unirect2d::GetSize { obj path varName } {
268    set string [$obj get $path]
269    if { [scan $string "%d" value] != 1 || $value < 0 } {
270        puts stderr "can't get size \"$string\" of \"$path\""
271        return
272    }
273    upvar $varName size
274    set size $value
275}
276
277itcl::body Rappture::Unirect2d::GetValue { obj path varName } {
278    set string [$obj get $path]
279    if { [scan $string "%g" value] != 1 } {
280        return
281    }
282    upvar $varName number
283    set number $value
284}
285
286itcl::body Rappture::Unirect2d::GetString { obj path varName } {
287    set string [$obj get $path]
288    if { $string == "" } {
289        puts stderr "can't get string \"$string\" of \"$path\""
290        return
291    }
292    upvar $varName str
293    set str $string
294}
Note: See TracBrowser for help on using the repository browser.