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

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

add pdb reader to drawing

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