source: branches/1.4/gui/scripts/unirect3d.tcl @ 5312

Last change on this file since 5312 was 5010, checked in by ldelgass, 9 years ago

merge r4997 from trunk

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