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

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