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

Last change on this file since 2219 was 2144, checked in by gah, 13 years ago

vtk-contour-viewer additions: still experimental

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        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    for { set i 0 } { $i < $_xNum } { incr i } {
150        set x [expr {$_xMin + (double($i) * $dx)}]
151        for { set j 0 } { $j < $_yNum } { 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.