Statistics
| Revision:

root / PyOpenGL-Demo / dek / OglSurface / triangle.py @ 1

History | View | Annotate | Download (5.1 kB)

1
#!/usr/bin/env python
2

    
3
# This is statement is required by the build system to query build info
4
if __name__ == '__build__':
5
        raise Exception
6

    
7

    
8
import sys
9
import string
10
try:
11
        import numpy as Numeric
12
except ImportError, err:
13
        try: 
14
                import Numeric
15
        except ImportError, err:
16
                print "This demo requires the numpy or Numeric extension, sorry"
17
                import sys
18
                sys.exit()
19
import NumericPDB
20
import string
21
from OpenGL.GL import *
22
from OpenGL.Tk import *
23
import Image
24
import OglFrame
25

    
26
MAP="test.ppm"
27

    
28
class Surface:
29
        def SetupWindow(self):
30
                self.OglFrame = OglFrame.OglFrame(None, 
31
                                                                                  redraw=self.Display, 
32
                                                                                  depth=1,
33
                                                                                  double=1)
34
                self.OglFrame.ogl.set_background(0,0,0)
35

    
36
        def Display(self, event=None):
37
                glEnable(GL_DEPTH_TEST)
38
                glDepthMask(GL_TRUE)
39
                glEnable(GL_CULL_FACE)
40
                glCullFace(GL_BACK)
41

    
42
                if self.surface:
43
                        glCallList(self.surfacelist)
44
                if self.bond:
45
                        glCallList(self.bondlist)
46

    
47
        def SetupSurface(self):
48
                self.surfacelist = glGenLists(1)
49
                glNewList(self.surfacelist, GL_COMPILE);
50
                
51
                glEnable(GL_LIGHTING)
52
                glEnable(GL_BLEND)
53

    
54
                color1 = color2 = color3 = (1,1,1,1)
55
                glColorMaterial(GL_FRONT, GL_DIFFUSE)
56
                glEnable(GL_COLOR_MATERIAL)
57
                glBegin(GL_TRIANGLES)
58
                for i in range(len(self.faces)):
59

    
60
                        tri = self.faces[i]
61
                        vert1 = tuple(self.vert[tri[0]-1])
62
                        vert2 = tuple(self.vert[tri[1]-1])
63
                        vert3 = tuple(self.vert[tri[2]-1])
64
                        norm1 = tuple(self.norm[tri[0]-1])
65
                        norm2 = tuple(self.norm[tri[1]-1])
66
                        norm3 = tuple(self.norm[tri[2]-1])
67

    
68
                        color = self.colorlist[int(self.nearest[tri[0]-1])-1]
69
                        glColor3f(color[0], color[1], color[2])
70
                        glNormal3fv(norm1)
71
                        glVertex3fv(vert1)
72

    
73
                        color = self.colorlist[int(self.nearest[tri[1]-1])-1]
74
                        glColor3f(color[0], color[1], color[2])
75
                        glNormal3fv(norm2)
76
                        glVertex3fv(vert2)
77

    
78
                        color = self.colorlist[int(self.nearest[tri[2]-1])-1]
79
                        glColor3f(color[0], color[1], color[2])
80
                        glNormal3fv(norm3)
81
                        glVertex3fv(vert3)
82

    
83
                glEnd()
84
                glDisable(GL_LIGHTING)
85
                glDisable(GL_BLEND)
86
                glEndList()
87

    
88
                self.surface=1
89

    
90
        def SetupBonds(self):
91
                self.bond=1
92
                self.bondlist = glGenLists(1)
93
                glNewList(self.bondlist, GL_COMPILE);
94
                glDisable(GL_LIGHTING)
95
                glDisable(GL_BLEND)
96
                glBegin(GL_LINES)
97
                for i in self.topol:
98
                        at1, at2 = i[0], i[1]
99
                        color = self.colorlist2[at1]
100
                        apply(glColor3f, tuple(color))
101
                        glVertex3f(self.crd[at1][0], \
102
                                                self.crd[at1][1], \
103
                                                self.crd[at1][2])
104
                        color = self.colorlist2[at2]
105
                        apply(glColor3f, tuple(color))
106
                        glVertex3f(self.crd[at2][0], \
107
                                                self.crd[at2][1], \
108
                                                self.crd[at2][2])
109
                glEnd()
110
                glEnable(GL_BLEND)
111
                glEndList()
112

    
113
        def ReadSurface(self):
114
                f = open(self.facefile)
115
                l = f.readlines()
116
                data = string.split(l[2])
117
                numfaces = string.atoi(data[0])
118
                spheres = string.atoi(data[1])
119
                probe_r = string.atof(data[2])
120
                density = string.atof(data[3])
121
                print "Numfaces, spheres, probe_r, density"
122
                print numfaces, spheres, probe_r, density
123

    
124
                self.faces = Numeric.zeros((numfaces, 3))
125
                for i in range(numfaces):
126
                        data = string.split(l[i+3])
127
                        self.faces[i] = map(string.atoi, data[:3])
128
                f.close()
129

    
130
                f = open(self.vertfile)
131
                l = f.readlines()
132
                data = string.split(l[2])
133
                vertices = string.atoi(data[0])
134
                spheres = string.atoi(data[1])
135
                probe_r = string.atof(data[2])
136
                density = string.atof(data[3])
137
                print "Vertices, spheres, probe_r, density"
138
                print vertices, spheres, probe_r, density
139
                f.close()
140

    
141
                self.vert = Numeric.zeros((vertices, 3), 'f')
142
                self.norm = Numeric.zeros((vertices, 3), 'f')
143
                self.nearest = Numeric.zeros((vertices))
144
                for i in range(vertices):
145
                        data = string.split(l[i+3])
146
                        self.vert[i] = map(string.atof, data[:3])
147
                        self.norm[i] = map(string.atof, data[3:6])
148
                        self.nearest[i] = string.atoi(data[7])
149

    
150
                vcen = Numeric.add.reduce(self.vert)/len(self.vert)
151
                self.OglFrame.ogl.set_centerpoint(vcen[0], vcen[1], vcen[2])
152
                
153

    
154
        def ReadPDB(self):
155
                p = NumericPDB.PDB(self.pdbfile)
156
                atomlist = map(lambda x: x.atom, p.records)
157
                k = self.colordict.keys()
158
                self.colorlist = []
159
                self.maplist = []
160
                for i in atomlist:
161
                        if i[0] in k:
162
                                self.colorlist.append(self.colordict[i[0]])
163
                                self.maplist.append(self.mapdict[i[0]])
164
                        else:
165
                                print "unfound atom type:", i
166
                                self.colorlist.append(self.colordict['U'])
167
                                self.maplist.append(self.mapdict['U'])
168

    
169

    
170
        def MakeImage(self):
171
                im = Image.open(self.map)
172
                self.imageWidth = im.size[0]
173
                self.imageHeight = im.size[1]
174
                self.image = im.tostring("raw", "RGBX", 0, -1)
175

    
176
        def __init__(self, facefile="1crn.face", vertfile="1crn.vert", pdbfile="1crn.pdb"):
177
                self.facefile = facefile
178
                self.vertfile = vertfile
179
                self.pdbfile = pdbfile
180

    
181
                self.map = MAP
182

    
183
                alpha = 1
184
                self.colordict = {'C':[.5,.5,.5,alpha], 'O':[1,0,0,alpha], 'N':[0,0,1,alpha], 'S':[1,1,0,alpha], 'P':[1,0,1,alpha],'H':[1,1,1,alpha], 'U':[0,0,0,alpha]}
185
                self.mapdict = {'C':0.5, 'O':.9, 'N':0.1, 'S':0.5, 'P':0.5,'H':0.5, 'U':0.5}
186
                self.surface = None
187
                self.bond = None
188
                self.SetupWindow()
189

    
190

    
191

    
192
                self.ReadPDB()
193
                self.MakeImage()
194
                self.ReadSurface()
195
                self.SetupSurface()
196

    
197

    
198
##                self.OglFrame.ogl.tkRedraw()
199
##                out=tkinter.dooneevent(tkinter.DONT_WAIT)
200
##                while (out):
201
##                        out=tkinter.dooneevent(tkinter.DONT_WAIT)
202
##                self.OglFrame.Photo()
203

    
204
                self.OglFrame.mainloop()
205

    
206

    
207
if __name__ == '__main__':
208
        s = Surface()