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

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

whitespace

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