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

Last change on this file since 3647 was 3330, checked in by gah, 11 years ago

merge (by hand) with Rappture1.2 branch

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