source: trunk/gui/scripts/drawing.tcl @ 3868

Last change on this file since 3868 was 3862, checked in by ldelgass, 11 years ago

Add polydata as synonym for polygon in drawing output. After any users have
been converted, polygon should be used for an n-sided regular polygon.

File size: 11.3 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2
3# ----------------------------------------------------------------------
4#  COMPONENT: drawing - 2D drawing of data
5# ======================================================================
6#  AUTHOR:  Michael McLennan, Purdue University
7#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
8#
9#  See the file "license.terms" for information on usage and
10#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11# ======================================================================
12package require Itcl
13package require BLT
14
15set count 0
16
17namespace eval Rappture {
18    # forward declaration
19}
20
21itcl::class Rappture::Drawing {
22    private variable _drawing
23    private variable _xmlobj
24    private variable _actors
25    private variable _styles
26    private variable _shapes
27    private variable _labels
28    private variable _types
29    private variable _data
30    private variable _hints
31    private variable _units
32    private variable _limits
33
34    constructor {xmlobj path} {
35        # defined below
36    }
37    destructor {
38        # defined below
39    }
40    public method limits {axis}
41    public method label { elem }
42    public method type { elem }
43    public method style { elem }
44    public method shape { elem }
45    public method values { elem }
46    public method data { elem }
47    public method hints {{keyword ""}}
48    public method components { args }
49    private method PdbToVtk { cname contents }
50}
51
52# ----------------------------------------------------------------------
53# Constructor
54# ----------------------------------------------------------------------
55itcl::body Rappture::Drawing::constructor {xmlobj path} {
56    package require vtk
57    if {![Rappture::library isvalid $xmlobj]} {
58        error "bad value \"$xmlobj\": should be Rappture::library"
59    }
60    set _xmlobj $xmlobj
61    set _drawing [$xmlobj element -as object $path]
62    set _units [$_drawing get units]
63
64    set xunits [$xmlobj get units]
65    if {"" == $xunits || "arbitrary" == $xunits} {
66        set xunits "um"
67    }
68    array set _limits {
69        xMin 0
70        xMax 0
71        yMin 0
72        yMax 0
73        zMin 0
74        zMax 0
75    }
76    # determine the overall size of the device
77    foreach elem [$_xmlobj children $path] {
78        switch -glob -- $elem {
79            polygon* {
80                set _data($elem) [$_xmlobj get $path.$elem.vtk]
81                set _data($elem) [string trim $_data($elem)]
82                set _styles($elem) [$_xmlobj get $path.$elem.about.style]
83                set _labels($elem) [$_xmlobj get $path.$elem.about.label]
84                set _types($elem) polydata
85            }
86            polydata* {
87                set _data($elem) [$_xmlobj get $path.$elem.vtk]
88                set _data($elem) [string trim $_data($elem)]
89                set _styles($elem) [$_xmlobj get $path.$elem.about.style]
90                set _labels($elem) [$_xmlobj get $path.$elem.about.label]
91                set _types($elem) polydata
92            }
93            streamlines* {
94                set _data($elem) [$_xmlobj get $path.$elem.vtk]
95                set _data($elem) [string trim $_data($elem)]
96                set _styles($elem) [$_xmlobj get $path.$elem.about.style]
97                set _labels($elem) [$_xmlobj get $path.$elem.about.label]
98                set _types($elem) streamlines
99            }
100            glyphs* {
101                set _data($elem) [$_xmlobj get $path.$elem.vtk]
102                set _data($elem) [string trim $_data($elem)]
103                set _styles($elem) [$_xmlobj get $path.$elem.about.style]
104                set _labels($elem) [$_xmlobj get $path.$elem.about.label]
105                set _shapes($elem) [$_xmlobj get $path.$elem.about.shape]
106                set _types($elem) glyphs
107            }
108            molecule* {
109                set pdbdata [$_xmlobj get $path.$elem.pdb]
110                if { $pdbdata != "" } {
111                    if 1 {
112                        global count
113                        set f [open /tmp/file$count.pdb "w"]
114                        incr count
115                        puts $f $pdbdata
116                        close $f
117                    }
118                    set contents [Rappture::PdbToVtk $pdbdata]
119                    if 1 {
120                        set f [open /tmp/convertedpdb.vtk "w"]
121                        puts $f $contents
122                        close $f
123                    }
124                } else {
125                    set contents [$_xmlobj get $path.$elem.vtk]
126                }
127                set _data($elem) [string trim $contents]
128                set _styles($elem) [$_xmlobj get $path.$elem.about.style]
129                set _labels($elem) [$_xmlobj get $path.$elem.about.label]
130                set _types($elem) molecule
131            }
132        }
133    }
134    foreach {key path} {
135        group   about.group
136        label   about.label
137        color   about.color
138        camera  about.camera
139        type    about.type
140        xlabel  xaxis.label
141        xdesc   xaxis.description
142        xunits  xaxis.units
143        xscale  xaxis.scale
144        xmin    xaxis.min
145        xmax    xaxis.max
146        ylabel  yaxis.label
147        ydesc   yaxis.description
148        yunits  yaxis.units
149        yscale  yaxis.scale
150        ymin    yaxis.min
151        ymax    yaxis.max
152        zlabel  zaxis.label
153        zdesc   zaxis.description
154        zunits  zaxis.units
155        zscale  zaxis.scale
156        zmin    zaxis.min
157        zmax    zaxis.max
158    } {
159        set str [$_drawing get $path]
160        if {"" != $str} {
161            set _hints($key) $str
162        }
163    }
164    foreach {key} { axisorder } {
165        set str [$_drawing get $key]
166        if {"" != $str} {
167            set _hints($key) $str
168        }
169    }
170}
171
172# ----------------------------------------------------------------------
173# Destructor
174# ----------------------------------------------------------------------
175itcl::body Rappture::Drawing::destructor {} {
176    # empty
177}
178
179#
180# label --
181#
182#       Returns the label of the named drawing element.
183#
184itcl::body Rappture::Drawing::label { elem } {
185    if { [info exists _labels($elem)] } {
186        return $_labels($elem)
187    }
188    return ""
189}
190
191#
192# type --
193#
194#       Returns the type of the named drawing element.
195#
196itcl::body Rappture::Drawing::type { elem } {
197    if { [info exists _types($elem)] } {
198        return $_types($elem)
199    }
200    return ""
201}
202
203#
204# style --
205#
206#       Returns the style string of the named drawing element.
207#
208itcl::body Rappture::Drawing::style { elem } {
209    if { [info exists _styles($elem)] } {
210        return $_styles($elem)
211    }
212    return ""
213}
214
215#
216# shape --
217#
218#       Returns the shape of the glyphs in the drawing element.
219#
220itcl::body Rappture::Drawing::shape { elem } {
221    set shape ""
222    if { [info exists _shapes($elem)] } {
223        return $_shapes($elem)
224    }
225    switch -- $shape {
226        arrow - cone - cube - cylinder - dodecahedron -
227        icosahedron - line - octahedron - sphere - tetrahedron  {
228            return $shape
229        }
230        default {
231            puts stderr "unknown glyph shape \"$shape\""
232        }
233    }
234    return ""
235}
236
237#
238# data --
239#
240#       Returns the data of the named drawing element.
241#
242itcl::body Rappture::Drawing::data { elem } {
243    if { [info exists _data($elem)] } {
244        return $_data($elem)
245    }
246    return ""
247}
248
249# ----------------------------------------------------------------------
250# method values
251#       Returns a base64 encoded, gzipped Tcl list that represents the
252#       Tcl command and data to recreate the uniform rectangular grid
253#       on the nanovis server.
254# ----------------------------------------------------------------------
255itcl::body Rappture::Drawing::values { elem } {
256    if { [info exists _data($elem)] } {
257        return $_data($elem)
258    }
259    return ""
260}
261
262itcl::body Rappture::Drawing::components { args } {
263    return [array names _data]
264}
265
266# ----------------------------------------------------------------------
267# method limits <axis>
268#       Returns a list {min max} representing the limits for the
269#       specified axis.
270# ----------------------------------------------------------------------
271itcl::body Rappture::Drawing::limits {which} {
272    set min ""
273    set max ""
274    foreach key [array names _data] {
275        set actor $_actors($key)
276        foreach key { xMin xMax yMin yMax zMin zMax} value [$actor GetBounds] {
277            set _limits($key) $value
278        }
279        break
280    }   
281   
282    foreach key [array names _actors] {
283        set actor $_actors($key)
284        foreach { xMin xMax yMin yMax zMin zMax} [$actor GetBounds] break
285        if { $xMin < $_limits(xMin) } {
286            set _limits(xMin) $xMin
287        }
288        if { $xMax > $_limits(xMax) } {
289            set _limits(xMax) $xMax
290        }
291        if { $yMin < $_limits(yMin) } {
292            set _limits(yMin) $yMin
293        }
294        if { $yMax > $_limits(yMax) } {
295            set _limits(yMax) $yMax
296        }
297        if { $zMin < $_limits(zMin) } {
298            set _limits(zMin) $zMin
299        }
300        if { $zMax > $_limits(zMax) } {
301            set _limits(zMax) $zMax
302        }
303    }
304    switch -- $which {
305        x {
306            set min $_limits(xMin)
307            set max $_limits(xMax)
308            set axis "xaxis"
309        }
310        y {
311            set min $_limits(yMin)
312            set max $_limits(yMax)
313            set axis "yaxis"
314        }
315        v - z {
316            set min $_limits(zMin)
317            set max $_limits(zMax)
318            set axis "zaxis"
319        }
320        default {
321            error "unknown axis description \"$which\""
322        }
323    }
324    return [list $min $max]
325}
326
327# ----------------------------------------------------------------------
328# USAGE: hints ?<keyword>?
329#
330# Returns a list of key/value pairs for various hints about plotting
331# this curve.  If a particular <keyword> is specified, then it returns
332# the hint for that <keyword>, if it exists.
333# ----------------------------------------------------------------------
334itcl::body Rappture::Drawing::hints { {keyword ""} } {
335    if 0 {
336    if {[info exists _hints(xlabel)] && "" != $_hints(xlabel)
337        && [info exists _hints(xunits)] && "" != $_hints(xunits)} {
338        set _hints(xlabel) "$_hints(xlabel) ($_hints(xunits))"
339    }
340    if {[info exists _hints(ylabel)] && "" != $_hints(ylabel)
341        && [info exists _hints(yunits)] && "" != $_hints(yunits)} {
342        set _hints(ylabel) "$_hints(ylabel) ($_hints(yunits))"
343    }
344    }
345    if {[info exists _hints(group)] && [info exists _hints(label)]} {
346        # pop-up help for each curve
347        set _hints(tooltip) $_hints(label)
348    }
349    if {$keyword != ""} {
350        if {[info exists _hints($keyword)]} {
351            return $_hints($keyword)
352        }
353        return ""
354    }
355    return [array get _hints]
356}
357
358itcl::body Rappture::Drawing::PdbToVtk { cname contents } {
359    package require vtk
360
361    set reader $this-datasetreader
362    vtkPDBReader $reader
363
364    # Write the contents to a file just in case it's binary.
365    set tmpfile $cname[pid].pdb
366    set f [open "$tmpfile" "w"]
367    fconfigure $f -translation binary -encoding binary
368    puts $f $contents
369    close $f
370    $reader SetFileName $tmpfile
371    $reader Update
372    file delete $tmpfile
373
374    set tmpfile $cname[pid].vtk
375    set writer $this-datasetwriter
376    vtkDataSetWriter $writer
377    $writer SetInputConnection [$reader GetOutputPort]
378    $writer SetFileName $tmpfile
379    $writer Write
380    rename $reader ""
381    rename $writer ""
382
383    set f [open "$tmpfile" "r"]
384    fconfigure $f -translation binary -encoding binary
385    set vtkdata [read $f]
386    close $f
387    file delete $tmpfile
388    return $vtkdata
389}
Note: See TracBrowser for help on using the repository browser.