source: branches/1.3/gui/scripts/cloud.tcl @ 4571

Last change on this file since 4571 was 4504, checked in by gah, 10 years ago

merge transferfunctioneditor into nanovis, fix cloud with units

File size: 9.4 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: cloud - represents the mesh for a cloud of points
4#
5#  This object represents the mesh for a cloud of points in an XML
6#  description of a device.  It simplifies the process of extracting
7#  data that represent the mesh.
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
16
17namespace eval Rappture { # forward declaration }
18
19itcl::class Rappture::Cloud {
20    private variable _xmlobj "";        # ref to XML obj with device data
21    private variable _cloud "";         # lib obj representing this cloud
22    private variable _units "" ;        # system of units for x, y, z
23    private variable _axis2label;       #
24    private variable _axis2units;       #
25    private variable _limits;           # limits x, y, z
26    private common _xp2obj ;            # Used for fetch/release ref counting
27    private common _obj2ref ;           # Used for fetch/release ref counting
28    private variable _numPoints 0
29    private variable _vtkdata ""
30    private variable _points ""
31    private variable _dim 0
32    private variable _isValid 0;        # Indicates if the data is valid.
33
34    constructor {xmlobj path} {
35        # defined below
36    }
37    destructor {
38        # defined below
39    }
40    public method points {}
41    public method mesh {}
42    public method units { axis }
43    public method label { axis }
44    public method vtkdata {}
45    public method size {}
46    public method dimensions {}
47    public method limits {which}
48    public method hints {{key ""}}
49    public method numpoints {} {
50        return $_numPoints
51    }
52    public method isvalid {} {
53        return $_isValid
54    }
55    public proc fetch {xmlobj path}
56    public proc release {obj}
57}
58
59# ----------------------------------------------------------------------
60# USAGE: Rappture::Cloud::fetch <xmlobj> <path>
61#
62# Clients use this instead of a constructor to fetch the Cloud for
63# a particular <path> in the <xmlobj>.  When the client is done with
64# the cloud, he calls "release" to decrement the reference count.
65# When the cloud is no longer needed, it is cleaned up automatically.
66# ----------------------------------------------------------------------
67itcl::body Rappture::Cloud::fetch {xmlobj path} {
68    set handle "$xmlobj|$path"
69    if {[info exists _xp2obj($handle)]} {
70        set obj $_xp2obj($handle)
71        incr _obj2ref($obj)
72        return $obj
73    }
74
75    set obj [Rappture::Cloud ::#auto $xmlobj $path]
76    set _xp2obj($handle) $obj
77    set _obj2ref($obj) 1
78    return $obj
79}
80
81# ----------------------------------------------------------------------
82# USAGE: Rappture::Cloud::release <obj>
83#
84# Clients call this when they're no longer using a Cloud fetched
85# previously by the "fetch" proc.  This decrements the reference
86# count for the cloud and destroys the object when it is no longer
87# in use.
88# ----------------------------------------------------------------------
89itcl::body Rappture::Cloud::release {obj} {
90    if {[info exists _obj2ref($obj)]} {
91        incr _obj2ref($obj) -1
92        if {$_obj2ref($obj) <= 0} {
93            unset _obj2ref($obj)
94            foreach handle [array names _xp2obj] {
95                if {$_xp2obj($handle) == $obj} {
96                    unset _xp2obj($handle)
97                }
98            }
99            itcl::delete object $obj
100        }
101    } else {
102        error "can't find reference count for $obj"
103    }
104}
105
106# ----------------------------------------------------------------------
107# CONSTRUCTOR
108# ----------------------------------------------------------------------
109itcl::body Rappture::Cloud::constructor {xmlobj path} {
110    if {![Rappture::library isvalid $xmlobj]} {
111        error "bad value \"$xmlobj\": should be Rappture::library"
112    }
113    set _xmlobj $xmlobj
114    set _cloud [$xmlobj element -as object $path]
115
116    set _units [$_cloud get units]
117    set first [lindex $_units 0]
118    set list {}
119    foreach u $_units axis { x y z } {
120        if { $u != "" } {
121            set _axis2units($axis) $u
122        } else {
123            set _axis2units($axis) $first
124        }
125        lappend list $_axis2units($axis)
126    }
127    set _units $list
128    foreach label [$_cloud get labels] axis { x y z } {
129        if { $label != "" } {
130            set _axis2label($axis) $label
131        } else {
132            set _axis2label($axis) [string toupper $axis]
133        }
134    }
135
136    set _numPoints 0
137    set _points {}
138    foreach line [split [$xmlobj get $path.points] \n] {
139        if {"" == [string trim $line]} {
140            continue
141        }
142
143        # make sure we have x,y,z
144        while {[llength $line] < 3} {
145            lappend line "0"
146        }
147
148        # Extract each point and add it to the points list
149        foreach {x y z} $line break
150        foreach axis {x y z} units $_units {
151            if { $units == "" } {
152                set value [set $axis]
153            } else {
154                set value [Rappture::Units::convert [set $axis] \
155                        -context $units -to $units -units off]
156            }
157            set $axis $value;           # Set the (x/y/z) coordinate to
158                                        # converted value.
159            if { ![info exists _limits($axis)] } {
160                set _limits($axis) [list $value $value]
161            } else {
162                foreach { min max } $_limits($axis) break
163                if {$value < $min} {
164                    set min $value
165                }
166                if {$value > $max} {
167                    set max $value
168                }
169                set _limits($axis) [list $min $max]
170            }
171        }
172        append _points "$x $y $z\n"
173        incr _numPoints
174    }
175    append out "DATASET POLYDATA\n"
176    append out "POINTS $_numPoints double\n"
177    append out $_points
178    set _vtkdata $out
179    if { $_numPoints == 0 } {
180        return
181    }
182    set _dim 0
183    foreach { xmin xmax } $_limits(x) break
184    if { $xmax > $xmin } {
185        incr _dim
186    }
187    foreach { ymin ymax } $_limits(y) break
188    if { $ymax > $ymin } {
189        incr _dim
190    }
191    foreach { zmin zmax } $_limits(z) break
192    if { $zmax > $zmin } {
193        incr _dim
194    }
195    set _isValid 1
196}
197
198# ----------------------------------------------------------------------
199# DESTRUCTOR
200# ----------------------------------------------------------------------
201itcl::body Rappture::Cloud::destructor {} {
202    # don't destroy the _xmlobj! we don't own it!
203    itcl::delete object $_cloud
204}
205
206# ----------------------------------------------------------------------
207# USAGE: points
208#
209# Returns the vtk object containing the points for this mesh.
210# ----------------------------------------------------------------------
211itcl::body Rappture::Cloud::points {} {
212    return $_points
213}
214
215# ----------------------------------------------------------------------
216# USAGE: mesh
217#
218# Returns the vtk object representing the mesh.
219# ----------------------------------------------------------------------
220itcl::body Rappture::Cloud::mesh {} {
221    return $_points
222}
223
224# ----------------------------------------------------------------------
225# USAGE: size
226#
227# Returns the number of points in this cloud.
228# ----------------------------------------------------------------------
229itcl::body Rappture::Cloud::size {} {
230    return $_numPoints
231}
232
233# ----------------------------------------------------------------------
234# USAGE: dimensions
235#
236# Returns the number of dimensions for this object: 1, 2, or 3.
237# ----------------------------------------------------------------------
238itcl::body Rappture::Cloud::dimensions {} {
239    return $_dim
240}
241
242# ----------------------------------------------------------------------
243# USAGE: limits x|y|z
244#
245# Returns the {min max} values for the limits of the specified axis.
246# ----------------------------------------------------------------------
247itcl::body Rappture::Cloud::limits { axis } {
248    if { ![info exists _limits($axis)] } {
249        error "bad axis \"$axis\": should be x, y, z"
250    }
251    return $_limits($axis)
252}
253
254#
255# units --
256#
257#       Returns the units of the given axis.
258#
259itcl::body Rappture::Cloud::units { axis } {
260    if { ![info exists _axis2units($axis)] } {
261        return ""
262    }
263    return $_axis2units($axis)
264}
265
266#
267# label --
268#
269#       Returns the label of the given axis.
270#
271itcl::body Rappture::Cloud::label { axis } {
272    if { ![info exists _axis2label($axis)] } {
273        return ""
274    }
275    return $_axis2label($axis)
276}
277
278# ----------------------------------------------------------------------
279# USAGE: hints ?<keyword>?
280#
281# Returns a list of key/value pairs for various hints about plotting
282# this field.  If a particular <keyword> is specified, then it returns
283# the hint for that <keyword>, if it exists.
284# ----------------------------------------------------------------------
285itcl::body Rappture::Cloud::hints {{keyword ""}} {
286    foreach key {label color units} {
287        set str [$_cloud get $key]
288        if {"" != $str} {
289            set hints($key) $str
290        }
291    }
292
293    if {$keyword != ""} {
294        if {[info exists hints($keyword)]} {
295            return $hints($keyword)
296        }
297        return ""
298    }
299    return [array get hints]
300}
301
302itcl::body Rappture::Cloud::vtkdata {} {
303    return $_vtkdata
304}
Note: See TracBrowser for help on using the repository browser.