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

Last change on this file since 2136 was 2128, checked in by gah, 13 years ago

Fixed mesh to *not* interpolate grid values. Used in vtkcontourviewer.

File size: 9.4 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    } {
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"
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    for { set i 0 } { $i < $_xNum } { incr i } {
149        set x [expr {$_xMin + (double($i) * $dx)}]
150        for { set j 0 } { $j < $_yNum } { incr j } {
151            set y [expr {$_yMin + (double($i) * $dy)}]
152            lappend data $x $y
153        }
154    }
155    return $data
156}
157
158# ----------------------------------------------------------------------
159# method mesh
160#       Returns a base64 encoded, gzipped Tcl list that represents the
161#       Tcl command and data to recreate the uniform rectangular grid
162#       on the nanovis server.
163# ----------------------------------------------------------------------
164itcl::body Rappture::Unirect2d::mesh {} {
165    lappend out $_xMin $_xMax $_xNum $_yMin $_yMax $_yNum
166    return $out
167}
168
169# ----------------------------------------------------------------------
170# method values
171#       Returns a base64 encoded, gzipped Tcl list that represents the
172#       Tcl command and data to recreate the uniform rectangular grid
173#       on the nanovis server.
174# ----------------------------------------------------------------------
175itcl::body Rappture::Unirect2d::values {} {
176    if { [$_values length] > 0 } {
177        return [$_values range 0 end]
178    }
179    return ""
180}
181
182# ----------------------------------------------------------------------
183# method limits <axis>
184#       Returns a list {min max} representing the limits for the
185#       specified axis.
186# ----------------------------------------------------------------------
187itcl::body Rappture::Unirect2d::limits {which} {
188    set min ""
189    set max ""
190
191    switch -- $which {
192        x - xlin - xlog {
193            set min $_xMin
194            set max $_xMax
195            set axis "xaxis"
196        }
197        y - ylin - ylog {
198            set min $_yMin
199            set max $_yMax
200            set axis "yaxis"
201        }
202        v - vlin - vlog - z - zlin - zlog {
203            if { [$_values length] > 0 } {
204               set min [blt::vector expr min($_values)]
205               set max [blt::vector expr max($_values)]
206            } else {
207                set min 0.0
208                set max 1.0
209            }
210            set axis "zaxis"
211        }
212        default {
213            error "unknown axis description \"$which\""
214        }
215    }
216#     set val [$_field get $axis.min]
217#     if {"" != $val && "" != $min} {
218#         if {$val > $min} {
219#             # tool specified this min -- don't go any lower
220#             set min $val
221#         }
222#     }
223#     set val [$_field get $axis.max]
224#     if {"" != $val && "" != $max} {
225#         if {$val < $max} {
226#             # tool specified this max -- don't go any higher
227#             set max $val
228#         }
229#     }
230
231    return [list $min $max]
232}
233
234
235# ----------------------------------------------------------------------
236# USAGE: hints ?<keyword>?
237#
238# Returns a list of key/value pairs for various hints about plotting
239# this curve.  If a particular <keyword> is specified, then it returns
240# the hint for that <keyword>, if it exists.
241# ----------------------------------------------------------------------
242itcl::body Rappture::Unirect2d::hints { {keyword ""} } {
243    if {[info exists _hints(xlabel)] && "" != $_hints(xlabel)
244        && [info exists _hints(xunits)] && "" != $_hints(xunits)} {
245        set _hints(xlabel) "$_hints(xlabel) ($_hints(xunits))"
246    }
247    if {[info exists _hints(ylabel)] && "" != $_hints(ylabel)
248        && [info exists _hints(yunits)] && "" != $_hints(yunits)} {
249        set _hints(ylabel) "$_hints(ylabel) ($_hints(yunits))"
250    }
251   
252    if {[info exists _hints(group)] && [info exists _hints(label)]} {
253        # pop-up help for each curve
254        set _hints(tooltip) $_hints(label)
255    }
256    if {$keyword != ""} {
257        if {[info exists _hints($keyword)]} {
258            return $_hints($keyword)
259        }
260        return ""
261    }
262    return [array get _hints]
263}
264
265
266itcl::body Rappture::Unirect2d::GetSize { obj path varName } {
267    set string [$obj get $path]
268    if { [scan $string "%d" value] != 1 || $value < 0 } {
269        puts stderr "can't get size \"$string\" of \"$path\""
270        return
271    }
272    upvar $varName size
273    set size $value
274}
275
276itcl::body Rappture::Unirect2d::GetValue { obj path varName } {
277    set string [$obj get $path]
278    if { [scan $string "%g" value] != 1 } {
279        return
280    }
281    upvar $varName number
282    set number $value
283}
284
285itcl::body Rappture::Unirect2d::GetString { obj path varName } {
286    set string [$obj get $path]
287    if { $string == "" } {
288        puts stderr "can't get string \"$string\" of \"$path\""
289        return
290    }
291    upvar $varName str
292    set str $string
293}
Note: See TracBrowser for help on using the repository browser.