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

Last change on this file since 6480 was 5659, checked in by ldelgass, 9 years ago

whitespace

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