source: trunk/gui/scripts/unirect2d.tcl @ 3917

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

fix axis labels for contour/heightmap

File size: 9.4 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 base64 encoded, gzipped Tcl list that represents the
179#       Tcl command and data to recreate the uniform rectangular grid
180#       on the nanovis server.
181# ----------------------------------------------------------------------
182itcl::body Rappture::Unirect2d::blob {} {
183    set data "unirect2d"
184    lappend data "xmin" $_xMin "xmax" $_xMax "xnum" $_xNum
185    lappend data "ymin" $_yMin "ymax" $_yMax "ynum" $_yNum
186    lappend data "xmin" $_xMin "ymin" $_yMin "xmax" $_xMax "ymax" $_yMax
187    return $data
188}
189
190# ----------------------------------------------------------------------
191# method mesh
192#       Returns a base64 encoded, gzipped Tcl list that represents the
193#       Tcl command and data to recreate the uniform rectangular grid
194#       on the nanovis server.
195# ----------------------------------------------------------------------
196itcl::body Rappture::Unirect2d::mesh {} {
197    lappend out $_xMin $_xMax $_xNum $_yMin $_yMax $_yNum
198    return $out
199}
200
201# ----------------------------------------------------------------------
202# method limits <axis>
203#       Returns a list {min max} representing the limits for the
204#       specified axis.
205# ----------------------------------------------------------------------
206itcl::body Rappture::Unirect2d::limits {which} {
207    set min ""
208    set max ""
209    switch -- $which {
210        x - xlin - xlog {
211            set min $_xMin
212            set max $_xMax
213            set axis "xaxis"
214        }
215        y - ylin - ylog {
216            set min $_yMin
217            set max $_yMax
218            set axis "yaxis"
219        }
220        default {
221            error "unknown axis description \"$which\""
222        }
223    }
224    return [list $min $max]
225}
226
227#
228# units --
229#
230#       Returns the units of the given axis.
231#
232itcl::body Rappture::Unirect2d::units { axis } {
233    if { [info exists _hints(${axis}units)] } {
234        return $_hints(${axis}units)
235    }
236    return ""
237}
238
239#
240# label --
241#
242#       Returns the label of the given axis.
243#
244itcl::body Rappture::Unirect2d::label { axis } {
245    if { [info exists _hints(${axis}label)] } {
246        return $_hints(${axis}label)
247    }
248    return ""
249}
250
251# ----------------------------------------------------------------------
252# USAGE: hints ?<keyword>?
253#
254# Returns a list of key/value pairs for various hints about plotting
255# this curve.  If a particular <keyword> is specified, then it returns
256# the hint for that <keyword>, if it exists.
257# ----------------------------------------------------------------------
258itcl::body Rappture::Unirect2d::hints { {keyword ""} } {
259    if {[info exists _hints(xlabel)] && "" != $_hints(xlabel)
260        && [info exists _hints(xunits)] && "" != $_hints(xunits)} {
261        set _hints(xlabel) "$_hints(xlabel) ($_hints(xunits))"
262    }
263    if {[info exists _hints(ylabel)] && "" != $_hints(ylabel)
264        && [info exists _hints(yunits)] && "" != $_hints(yunits)} {
265        set _hints(ylabel) "$_hints(ylabel) ($_hints(yunits))"
266    }
267    if {[info exists _hints(group)] && [info exists _hints(label)]} {
268        # pop-up help for each curve
269        set _hints(tooltip) $_hints(label)
270    }
271    if {$keyword != ""} {
272        if {[info exists _hints($keyword)]} {
273            return $_hints($keyword)
274        }
275        return ""
276    }
277    return [array get _hints]
278}
279
280itcl::body Rappture::Unirect2d::GetSize { obj path varName } {
281    set string [$obj get $path]
282    if { [scan $string "%d" value] != 1 || $value < 0 } {
283        puts stderr "can't get size \"$string\" of \"$path\""
284        return
285    }
286    upvar $varName size
287    set size $value
288}
289
290itcl::body Rappture::Unirect2d::GetValue { obj path varName } {
291    set string [$obj get $path]
292    if { [scan $string "%g" value] != 1 } {
293        return
294    }
295    upvar $varName number
296    set number $value
297}
298
299itcl::body Rappture::Unirect2d::GetString { obj path varName } {
300    set string [$obj get $path]
301    if { $string == "" } {
302        puts stderr "can't get string \"$string\" of \"$path\""
303        return
304    }
305    upvar $varName str
306    set str $string
307}
308
309
Note: See TracBrowser for help on using the repository browser.