source: branches/1.3/gui/scripts/unirect2d.tcl @ 4790

Last change on this file since 4790 was 4790, checked in by ldelgass, 6 years ago

unirect2d doesn't have a values method (it only represents the mesh), the
values are in the _values vector in the field object. Also some comment fixes.

File size: 9.2 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2
3# ----------------------------------------------------------------------
4#  COMPONENT: unirect2d - 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::Unirect2d {
22    private variable _axisOrder "x y"
23    private variable _xMax      0
24    private variable _xMin      0
25    private variable _xNum      0
26    private variable _yMax      0
27    private variable _yMin      0
28    private variable _yNum      0
29    private variable _hints
30    private variable _vtkdata ""
31    private variable _numPoints 0
32
33    private common _xp2obj       ;      # used for fetch/release ref counting
34    private common _obj2ref      ;      # used for fetch/release ref counting
35    private variable _isValid 0;        # Indicates if the data is valid.
36
37    private method GetString { obj path varName }
38    private method GetValue  { obj path varName }
39    private method GetSize   { obj path varName }
40
41    constructor {xmlobj path} {
42        # defined below
43    }
44    destructor {
45        # defined below
46    }
47    public proc fetch {xmlobj path}
48    public proc release {obj}
49    public method limits {axis}
50    public method units { axis }
51    public method label { axis }
52    public method blob {}
53    public method hints {{keyword ""}}
54    public method mesh {}
55    public method dimensions {} {
56        return 2
57    }
58    public method isvalid {} {
59        return $_isValid
60    }
61    public method numpoints {} {
62        return $_numPoints
63    }
64    public method vtkdata {} {
65        return $_vtkdata
66    }
67}
68
69#
70# fetch <xmlobj> <path>
71#
72#    Clients use this instead of a constructor to fetch the Mesh for a
73#    particular <path> in the <xmlobj>.  When the client is done with the mesh,
74#    he calls "release" to decrement the reference count.  When the mesh is no
75#    longer needed, it is cleaned up automatically.
76#
77itcl::body Rappture::Unirect2d::fetch {xmlobj path} {
78    set handle "$xmlobj|$path"
79    if {[info exists _xp2obj($handle)]} {
80        set obj $_xp2obj($handle)
81        incr _obj2ref($obj)
82        return $obj
83    }
84    set obj [Rappture::Unirect2d ::#auto $xmlobj $path]
85    set _xp2obj($handle) $obj
86    set _obj2ref($obj) 1
87    return $obj
88}
89
90# ----------------------------------------------------------------------
91# USAGE: Rappture::Unirect2d::release <obj>
92#
93# Clients call this when they're no longer using a Mesh fetched
94# previously by the "fetch" proc.  This decrements the reference
95# count for the mesh and destroys the object when it is no longer
96# in use.
97# ----------------------------------------------------------------------
98itcl::body Rappture::Unirect2d::release { obj } {
99    if { ![info exists _obj2ref($obj)] } {
100        error "can't find reference count for $obj"
101    }
102    incr _obj2ref($obj) -1
103    if {$_obj2ref($obj) <= 0} {
104        unset _obj2ref($obj)
105        foreach handle [array names _xp2obj] {
106            if {$_xp2obj($handle) == $obj} {
107                unset _xp2obj($handle)
108            }
109        }
110        itcl::delete object $obj
111    }
112}
113
114# ----------------------------------------------------------------------
115# Constructor
116# ----------------------------------------------------------------------
117itcl::body Rappture::Unirect2d::constructor {xmlobj path} {
118    if {![Rappture::library isvalid $xmlobj]} {
119        error "bad value \"$xmlobj\": should be Rappture::library"
120    }
121    set m [$xmlobj element -as object $path]
122    GetValue $m "xaxis.min" _xMin
123    GetValue $m "xaxis.max" _xMax
124    GetSize $m "xaxis.numpoints" _xNum
125    GetValue $m "yaxis.min" _yMin
126    GetValue $m "yaxis.max" _yMax
127    GetSize $m "yaxis.numpoints" _yNum
128    foreach {key path} {
129        group   about.group
130        label   about.label
131        color   about.color
132        style   about.style
133        type    about.type
134        xlabel  xaxis.label
135        xdesc   xaxis.description
136        xunits  xaxis.units
137        xscale  xaxis.scale
138        xmin    xaxis.min
139        xmax    xaxis.max
140        ylabel  yaxis.label
141        ydesc   yaxis.description
142        yunits  yaxis.units
143        yscale  yaxis.scale
144        ymin    yaxis.min
145        ymax    yaxis.max
146        type    about.type
147    } {
148        set str [$m get $path]
149        if {"" != $str} {
150            set _hints($key) $str
151        }
152    }
153    itcl::delete object $m
154    set _numPoints [expr $_xNum * $_yNum]
155    if { $_numPoints == 0 } {
156        set _vtkdata ""
157        return
158    }
159    append out "DATASET STRUCTURED_POINTS\n"
160    append out "DIMENSIONS $_xNum $_yNum 1"
161    set xSpace [expr ($_xMax - $_xMin) / double($_xNum - 1)]
162    set ySpace [expr ($_yMax - $_yMin) / double($_yNum - 1)]
163    append out "SPACING $xSpace $ySpace 0\n"
164    append out "ORIGIN 0 0 0\n"
165    set _vtkdata $out
166    set _isValid 1
167}
168
169# ----------------------------------------------------------------------
170# Destructor
171# ----------------------------------------------------------------------
172itcl::body Rappture::Unirect2d::destructor {} {
173    # empty
174}
175
176# ----------------------------------------------------------------------
177# method blob
178#       Returns a Tcl list that represents the Tcl command and data to
179#       recreate the uniform rectangular grid on the nanovis server.
180# ----------------------------------------------------------------------
181itcl::body Rappture::Unirect2d::blob {} {
182    set data "unirect2d"
183    lappend data "xmin" $_xMin "xmax" $_xMax "xnum" $_xNum
184    lappend data "ymin" $_yMin "ymax" $_yMax "ynum" $_yNum
185    return $data
186}
187
188# ----------------------------------------------------------------------
189# method mesh
190#       Returns a Tcl list that represents the mesh limits and dims.
191# ----------------------------------------------------------------------
192itcl::body Rappture::Unirect2d::mesh {} {
193    lappend out $_xMin $_xMax $_xNum $_yMin $_yMax $_yNum
194    return $out
195}
196
197# ----------------------------------------------------------------------
198# method limits <axis>
199#       Returns a list {min max} representing the limits for the
200#       specified axis.
201# ----------------------------------------------------------------------
202itcl::body Rappture::Unirect2d::limits {which} {
203    set min ""
204    set max ""
205    switch -- $which {
206        x - xlin - xlog {
207            set min $_xMin
208            set max $_xMax
209        }
210        y - ylin - ylog {
211            set min $_yMin
212            set max $_yMax
213        }
214        z - zlin - zlog {
215            set min 0
216            set max 0
217        }
218        default {
219            error "unknown axis description \"$which\""
220        }
221    }
222    return [list $min $max]
223}
224
225#
226# units --
227#
228#       Returns the units of the given axis.
229#
230itcl::body Rappture::Unirect2d::units { axis } {
231    if { [info exists _hints(${axis}units)] } {
232        return $_hints(${axis}units)
233    }
234    return ""
235}
236
237#
238# label --
239#
240#       Returns the label of the given axis.
241#
242itcl::body Rappture::Unirect2d::label { axis } {
243    if { [info exists _hints(${axis}label)] } {
244        return $_hints(${axis}label)
245    }
246    return ""
247}
248
249# ----------------------------------------------------------------------
250# USAGE: hints ?<keyword>?
251#
252# Returns a list of key/value pairs for various hints about plotting
253# this curve.  If a particular <keyword> is specified, then it returns
254# the hint for that <keyword>, if it exists.
255# ----------------------------------------------------------------------
256itcl::body Rappture::Unirect2d::hints { {keyword ""} } {
257    if {[info exists _hints(xlabel)] && "" != $_hints(xlabel)
258        && [info exists _hints(xunits)] && "" != $_hints(xunits)} {
259        set _hints(xlabel) "$_hints(xlabel) ($_hints(xunits))"
260    }
261    if {[info exists _hints(ylabel)] && "" != $_hints(ylabel)
262        && [info exists _hints(yunits)] && "" != $_hints(yunits)} {
263        set _hints(ylabel) "$_hints(ylabel) ($_hints(yunits))"
264    }
265    if {[info exists _hints(group)] && [info exists _hints(label)]} {
266        # pop-up help for each curve
267        set _hints(tooltip) $_hints(label)
268    }
269    if {$keyword != ""} {
270        if {[info exists _hints($keyword)]} {
271            return $_hints($keyword)
272        }
273        return ""
274    }
275    return [array get _hints]
276}
277
278itcl::body Rappture::Unirect2d::GetSize { obj path varName } {
279    set string [$obj get $path]
280    if { [scan $string "%d" value] != 1 || $value < 0 } {
281        puts stderr "can't get size \"$string\" of \"$path\""
282        return
283    }
284    upvar $varName size
285    set size $value
286}
287
288itcl::body Rappture::Unirect2d::GetValue { obj path varName } {
289    set string [$obj get $path]
290    if { [scan $string "%g" value] != 1 } {
291        return
292    }
293    upvar $varName number
294    set number $value
295}
296
297itcl::body Rappture::Unirect2d::GetString { obj path varName } {
298    set string [$obj get $path]
299    if { $string == "" } {
300        puts stderr "can't get string \"$string\" of \"$path\""
301        return
302    }
303    upvar $varName str
304    set str $string
305}
306
307
Note: See TracBrowser for help on using the repository browser.