source: branches/1.3/gui/scripts/unirect3d.tcl @ 4531

Last change on this file since 4531 was 4494, checked in by ldelgass, 10 years ago

merge flow fixes from trunk

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