source: trunk/gui/scripts/moleculeViewer.tcl @ 11

Last change on this file since 11 was 11, checked in by mmc, 16 years ago

Major reorganization of the entire package. The config.xml file
is now irrelevant. All the action is in the tool.xml file. The
main program now organizes all input into 1) side-by-side pages,
2) input/result (wizard-style) pages, or 3) a series of wizard-
style pages. The <input> can have <phase> parts representing
the various pages.

Added a new ContourResult? widget based on Swaroop's vtk plotting
code.

Also, added easymesh and showmesh to the "tools" directory.
We need these for Eric Polizzi's code.

File size: 8.5 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: MoleculeViewer - view a molecule in 3D
3#
4#  This widget brings up a 3D representation of a molecule, which you
5#  can rotate.  It extracts atoms and bonds from the Rappture XML
6#  representation for a <molecule>.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2005
10#  Purdue Research Foundation, West Lafayette, IN
11# ======================================================================
12package require Itk
13package require vtk
14package require vtkinteraction
15
16option add *MoleculeViewer.width 4i widgetDefault
17option add *MoleculeViewer.height 4i widgetDefault
18option add *MoleculeViewer.backdrop black widgetDefault
19
20itcl::class Rappture::MoleculeViewer {
21    inherit itk::Widget
22
23    itk_option define -backdrop backdrop Backdrop "black"
24    itk_option define -device device Device ""
25
26    constructor {tool args} { # defined below }
27    destructor { # defined below }
28
29    protected method _render {}
30    protected method _color2rgb {color}
31
32    private variable _tool ""    ;# tool containing this viewer
33    private variable _actors ""  ;# list of actors in renderer
34}
35                                                                               
36itk::usual MoleculeViewer {
37}
38
39# ----------------------------------------------------------------------
40# CONSTRUCTOR
41# ----------------------------------------------------------------------
42itcl::body Rappture::MoleculeViewer::constructor {tool args} {
43    set _tool $tool
44
45    itk_option add hull.width hull.height
46    pack propagate $itk_component(hull) no
47
48    vtkRenderWindow $this-renWin
49    vtkRenderer $this-ren
50    $this-renWin AddRenderer $this-ren
51
52    vtkRenderWindowInteractor $this-int
53    $this-int SetRenderWindow $this-renWin
54
55    vtkSphereSource $this-sphere
56    $this-sphere SetRadius 1.0
57    $this-sphere SetThetaResolution 18
58    $this-sphere SetPhiResolution 18
59
60    vtkPolyDataMapper $this-map
61    $this-map SetInput [$this-sphere GetOutput]
62
63    itk_component add renderer {
64        vtkTkRenderWidget $itk_interior.ren -rw $this-renWin
65    } {
66    }
67    pack $itk_component(renderer) -expand yes -fill both
68
69    eval itk_initialize $args
70}
71
72# ----------------------------------------------------------------------
73# DESTRUCTOR
74# ----------------------------------------------------------------------
75itcl::body Rappture::MoleculeViewer::destructor {} {
76    rename $this-renWin ""
77    rename $this-ren ""
78    rename $this-int ""
79    rename $this-sphere ""
80    rename $this-map ""
81}
82
83# ----------------------------------------------------------------------
84# USAGE: _render
85#
86# Used internally to rebuild the scene whenever options within this
87# widget change.  Destroys all actors and rebuilds them from scratch.
88# ----------------------------------------------------------------------
89itcl::body Rappture::MoleculeViewer::_render {} {
90    foreach a $_actors {
91        $this-ren RemoveActor $a
92        rename $a ""
93    }
94    set _actors ""
95
96    if {$itk_option(-device) != ""} {
97        set dev $itk_option(-device)
98        set lib [Rappture::library standard]
99
100        set counter 0
101        foreach atom [$dev children -type atom components.molecule] {
102            set symbol [$dev get components.molecule.$atom.symbol]
103            set xyz [$dev get components.molecule.$atom.xyz]
104            regsub {,} $xyz {} xyz
105
106            set aname "::actor[incr counter]"
107            vtkActor $aname
108            $aname SetMapper $this-map
109            eval $aname SetPosition $xyz
110            $this-ren AddActor $aname
111
112            set sfac 0.7
113            set scale [$lib get elements.($symbol).scale]
114            if {$scale != ""} {
115                $aname SetScale [expr {$sfac*$scale}]
116            }
117            set color [$lib get elements.($symbol).color]
118            if {$color != ""} {
119                eval [$aname GetProperty] SetColor [_color2rgb $color]
120            }
121
122            lappend _actors $aname
123        }
124    }
125    $this-ren ResetCamera
126    $this-renWin Render
127}
128
129# ----------------------------------------------------------------------
130# USAGE: _color2rgb color
131#
132# Used internally to convert a Tk color name into the r,g,b values
133# used in Vtk (scaled 0-1).
134# ----------------------------------------------------------------------
135itcl::body Rappture::MoleculeViewer::_color2rgb {color} {
136    foreach {r g b} [winfo rgb $itk_component(hull) $color] {}
137    set r [expr {$r/65535.0}]
138    set g [expr {$g/65535.0}]
139    set b [expr {$b/65535.0}]
140    return [list $r $g $b]
141}
142
143# ----------------------------------------------------------------------
144# OPTION: -backdrop
145# ----------------------------------------------------------------------
146itcl::configbody Rappture::MoleculeViewer::backdrop {
147    eval $this-ren SetBackground [_color2rgb $itk_option(-backdrop)]
148    $this-renWin Render
149}
150
151# ----------------------------------------------------------------------
152# OPTION: -device
153# ----------------------------------------------------------------------
154itcl::configbody Rappture::MoleculeViewer::device {
155    if {$itk_option(-device) != ""
156          && ![Rappture::library isvalid $itk_option(-device)]} {
157        error "bad value \"$itk_option(-device)\": should be Rappture::library object"
158    }
159    after idle [itcl::code $this _render]
160}
161
162#package require Rappture
163#Rappture::MoleculeViewer .e
164#pack .e -expand yes -fill both
165#
166#set dev [Rappture::library {<?xml version="1.0"?>
167#<structure>
168#<components>
169#<molecule id="Aspirin">
170#  <formula>???</formula>
171#  <info>Aspirin molecule</info>
172#  <atom id="1">
173#    <symbol>C</symbol>
174#    <xyz>-1.892  -0.992  -1.578</xyz>
175#  </atom>
176#  <atom id="2">
177#    <symbol>C</symbol>
178#    <xyz>-1.370  -2.149  -0.990</xyz>
179#  </atom>
180#  <atom id="3">
181#    <symbol>C</symbol>
182#    <xyz>-0.079  -2.146  -0.464</xyz>
183#  </atom>
184#  <atom id="4">
185#    <symbol>C</symbol>
186#    <xyz>0.708  -0.986  -0.521</xyz>
187#  </atom>
188#  <atom id="5">
189#    <symbol>C</symbol>
190#    <xyz>0.203   0.156  -1.196</xyz>
191#  </atom>
192#  <atom id="6">
193#    <symbol>C</symbol>
194#    <xyz>-1.108   0.161  -1.654</xyz>
195#  </atom>
196#  <atom id="7">
197#    <symbol>C</symbol>
198#    <xyz>2.085  -1.030   0.104</xyz>
199#  </atom>
200#  <atom id="8">
201#    <symbol>O</symbol>
202#    <xyz>2.533  -2.034   0.636</xyz>
203#  </atom>
204#  <atom id="9">
205#    <symbol>O</symbol>
206#    <xyz>2.879   0.025   0.112</xyz>
207#  </atom>
208#  <atom id="10">
209#    <symbol>O</symbol>
210#    <xyz>0.753   1.334  -1.084</xyz>
211#  </atom>
212#  <atom id="11">
213#    <symbol>C</symbol>
214#    <xyz>0.668   2.025   0.034</xyz>
215#  </atom>
216#  <atom id="12">
217#    <symbol>O</symbol>
218#    <xyz>1.300   3.063   0.152</xyz>
219#  </atom>
220#  <atom id="13">
221#    <symbol>C</symbol>
222#    <xyz>-0.243   1.577   1.144</xyz>
223#  </atom>
224#  <atom id="14">
225#    <symbol>H</symbol>
226#    <xyz>-2.879  -0.962  -1.985</xyz>
227#  </atom>
228#  <atom id="15">
229#    <symbol>H</symbol>
230#    <xyz>-1.988  -3.037  -0.955</xyz>
231#  </atom>
232#  <atom id="16">
233#    <symbol>H</symbol>
234#    <xyz>0.300  -3.063  -0.005</xyz>
235#  </atom>
236#  <atom id="17">
237#    <symbol>H</symbol>
238#    <xyz>-1.489   1.084  -2.059</xyz>
239#  </atom>
240#  <atom id="18">
241#    <symbol>H</symbol>
242#    <xyz>2.566   0.782  -0.326</xyz>
243#  </atom>
244#  <atom id="19">
245#    <symbol>H</symbol>
246#    <xyz>-0.761   0.636   0.933</xyz>
247#  </atom>
248#  <atom id="20">
249#    <symbol>H</symbol>
250#    <xyz>-1.009   2.349   1.290</xyz>
251#  </atom>
252#  <atom id="21">
253#    <symbol>H</symbol>
254#    <xyz>0.346   1.435   2.059</xyz>
255#  </atom>
256#</molecule>
257#</components>
258#</structure>}]
259# add connectivity at some point...
260#CONECT    1    2    6   14                   
261#CONECT    2    1    3   15                   
262#CONECT    3    2    4   16                   
263#CONECT    4    3    5    7                   
264#CONECT    5    4    6   10                   
265#CONECT    6    1    5   17                   
266#CONECT    7    4    8    9                   
267#CONECT    8    7                             
268#CONECT    9    7   18                       
269#CONECT   10    5   11                       
270#CONECT   11   10   12   13                   
271#CONECT   12   11                             
272#CONECT   13   11   19   20   21             
273#CONECT   14    1                             
274#CONECT   15    2                             
275#CONECT   16    3                             
276#CONECT   17    6                             
277#CONECT   18    9                             
278#CONECT   19   13                             
279#CONECT   20   13                             
280#CONECT   21   13               
281
282#.e configure -device $dev
Note: See TracBrowser for help on using the repository browser.