source: trunk/gui/scripts/unirect3d.tcl @ 2035

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