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

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

add pdb to vtk converter to drawing

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