forked from jkotur/Puma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.py
331 lines (241 loc) · 8.55 KB
/
mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import sys
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from drawable import Drawable
import shaders as sh
from copy import copy
def index(seq, key = None , func = None ):
if key != None :
return next((i for i in xrange(len(seq)) if key == seq[i]), -1)
elif func != None :
return next((i for i in xrange(len(seq)) if f(seq[i])), -1)
else :
raise ValueError('You have to specify key or func argument')
class Mesh( Drawable ) :
def __init__( self , f = None ) :
Drawable.__init__( self )
self.verts , self.v , self.n , self.t , self.ev , self.et , self.tn = [[]] * 7
self.volume_size = 0
self.volume = np.zeros( 0 , np.float32 )
self.normal = np.zeros( 0 , np.float32 )
self.pts_len = 0
self.lpos = [0]*3
self.prog = None
if file : self.fromFile( f )
def draw( self ) :
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glVertexPointer( 3 , GL_FLOAT , 0 , self.v )
glNormalPointer( GL_FLOAT , 0 , self.n )
glDrawElements( GL_TRIANGLES , len(self.t) , GL_UNSIGNED_INT , self.t )
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
def gfx_init( self ) :
self.init_volumes_shader()
def create_volume( self , p ) :
self.lpos = np.resize( p , 4 );
self.lpos[3] = 1;
return
j = 0
for i in range(0,len(self.et),2) :
vid1 = self.ev[i ]*3
vid2 = self.ev[i+1]*3
v1 = np.array( self.verts[vid1:vid1+3] , np.float32 )
v2 = np.array( self.verts[vid2:vid2+3] , np.float32 )
dp = p - (v1+v2)/2.0
tid1 = self.et[i ]
tid2 = self.et[i+1]
d1 = np.dot( self.tn[tid1] , dp )
d2 = np.dot( self.tn[tid2] , dp )
if d1 * d2 < 0 :
w1 = v1 + (v1-p)*2
w2 = v2 + (v2-p)*2
if d1 < 0 :
self.volume[j :j+ 3] = v1
self.volume[j+ 3:j+ 6] = v2
self.volume[j+ 6:j+ 9] = w2
self.volume[j+ 9:j+12] = w2
self.volume[j+12:j+15] = w1
self.volume[j+15:j+18] = v1
else :
self.volume[j :j+ 3] = w2
self.volume[j+ 3:j+ 6] = v2
self.volume[j+ 6:j+ 9] = v1
self.volume[j+ 9:j+12] = v1
self.volume[j+12:j+15] = w1
self.volume[j+15:j+18] = w2
# for i in range(0,t,3) :
# self.
# if d1 > 0 :
# normal = self.tn[tid1]
# else :
# normal = self.tn[tid2]
# self.normal[j :j+ 3] = normal
# self.normal[j+ 3:j+ 6] = normal
# self.normal[j+ 6:j+ 9] = normal
# self.volume[j+ 9:j+12] = normal
# self.volume[j+12:j+15] = normal
# self.volume[j+15:j+18] = normal
j += 18
self.volume_size = j
#
def draw_volume( self , light_matrix , culling = GL_NONE , visible = False ) :
# self.draw_volume_static( visible )
if culling == GL_NONE :
culling = 0.0
elif culling == GL_BACK :
culling = 1.0
elif culling == GL_FRONT :
culling =-1.0
else :
raise ValueError('Uknown culling value')
self.draw_volume_dynamic( np.dot(np.transpose(light_matrix),self.lpos) , culling , visible )
def init_volumes_shader( self ) :
print 'Loading shaders'
try :
self.prog = sh.compile_program("volume")
self.loc_mmv = sh.get_loc(self.prog,'modelview' )
self.loc_mp = sh.get_loc(self.prog,'projection')
self.loc_lpos= sh.get_loc(self.prog,'lpos' )
self.loc_cull= sh.get_loc(self.prog,'culling' )
except ValueError as ve :
print "Shader compilation failed: " + str(ve)
sys.exit(0)
print 'Create adjacency data'
self.adj = self.create_adjacency( self.et , self.t , self.vidx , self.pts_len )
self.adj = np.array( self.adj , np.uint32 )
def draw_volume_dynamic( self , lpos , culling = 0.0 , visible = False ) :
assert(self.prog)
glUseProgram( self.prog )
mmv = glGetFloatv(GL_MODELVIEW_MATRIX)
mp = glGetFloatv(GL_PROJECTION_MATRIX)
glUniformMatrix4fv(self.loc_mmv,1,GL_FALSE,mmv)
glUniformMatrix4fv(self.loc_mp ,1,GL_FALSE,mp )
glUniform3f( self.loc_lpos , lpos[0] , lpos[1] , lpos[2] )
glUniform1f( self.loc_cull , culling )
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glVertexPointer( 3 , GL_FLOAT , 0 , self.v )
glNormalPointer( GL_FLOAT , 0 , self.n )
glDrawElements( GL_TRIANGLES_ADJACENCY , len(self.adj) , GL_UNSIGNED_INT , self.adj )
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glUseProgram( 0 )
def draw_volume_static( self , visible = False ) :
if len(self.volume) == 0 : return
if visible :
# glDisable( GL_CULL_FACE )
glEnable( GL_BLEND )
glBlendFunc( GL_ONE_MINUS_SRC_ALPHA , GL_ONE )
glEnableClientState(GL_VERTEX_ARRAY)
# glEnableClientState(GL_NORMAL_ARRAY)
glVertexPointer( 3 , GL_FLOAT , 0 , self.volume )
# glNormalPointer( GL_FLOAT , 0 , self.normal )
glDrawArrays( GL_TRIANGLES , 0 , self.volume_size/3 )
glDisableClientState(GL_VERTEX_ARRAY)
# glDisableClientState(GL_NORMAL_ARRAY)
if visible :
glDisable( GL_BLEND )
# glEnable( GL_CULL_FACE )
def fromFile( self , path ) :
f = open(path,'r')
try :
self.verts = self._readVertices ( f )
self.v , self.n , self.vidx = self._readPoints ( f , self.verts )
self.t = self._readTriangels( f )
self.ev,self.et = self._readEdges ( f )
except IOError as (errno, strerror):
print path , " I/O error({0}): {1}".format(errno, strerror)
except ValueError as e :
print "Could not convert data: " , e
self.pts_len = len(self.v)/3
assert( self.pts_len == len(self.n)/3 )
self.n += self.calculate_triangles_normals( self.n , self.t )
self.v += self.calculate_triangles_normals( self.v , self.t )
self.verts = np.array( self.verts , np.float32 )
self.v = np.array( self.v , np.float32 )
self.n = np.array( self.n , np.float32 )
self.t = np.array( self.t , np.uint32 )
self.ev = np.array( self.ev , np.uint32 )
self.et = np.array( self.et , np.uint32 )
# self.an = self.normals_to_arrays( self.n )
self.volume.resize( len(self.t)*3*2 )
self.normal.resize( len(self.t)*3*2 )
def create_adjacency( self , et , t , v , nlen ) :
adj = []
nebscount = 3
for ti in range(0,len(t),3) :
if nebscount == 3 :
nebscount = 0
else : raise ValueError('more than 3 naighbours found')
cur = [ v[t[ti]] , -1 , v[t[ti+1]] , -1 , v[t[ti+2]] , -1 ]
curv= [ t[ti] , -1 , t[ti+1] , -1 , t[ti+2] , -1 ]
curt= [ t[ti] , -1 , t[ti+1] , -1 , t[ti+2] , -1 ]
ti /= 3
for i in range(len(et)) :
# print ti , et[max(0,i-8):min(len(et),i+8)]
if et[i] == ti :
j = i+1 if i%2==0 else i-1
neb = [ v[vi] for vi in t[et[j]*3:et[j]*3+3] ]
nebv= t[et[j]*3:et[j]*3+3]
# print ti , et[j] , i , j , cur , neb , [ v[vi] for vi in t[et[(i-1)]*3:et[(i-1)]*3+3] ] , [ v[vi] for vi in t[et[i+1]*3:et[i+1]*3+3] ]
idx = [ index( cur , key = neb[i] ) for i in range(3) ]
l = 0
if 0 in idx and 2 in idx :
l = 1
elif 2 in idx and 4 in idx :
l = 3
elif 4 in idx and 0 in idx :
l = 5
else :
raise ValueError('Neighbour triangle is invalid: ' + str(cur) + str(neb) + str(idx) )
# print cur , neb , idx , l , index( idx , key = -1 )
cur [l] = neb [ index( idx , key = -1 ) ]
curv[l] = nebv[ index( idx , key = -1 ) ]
curt[l] = et[j]+nlen
nebscount += 1
# if nebscount == 3 :
# break
adj += curt
return adj
def calculate_triangles_normals( self , ns , ts ) :
tn = []
for i in range(0,len(ts),3) :
n = np.array((0,0,0),np.float32)
for k in ts[i:i+3] :
n += np.array( ns[k*3:k*3+3] , np.float32 )
tn += list( n/3.0 )
return tn
def normals_to_arrays( self , n ) :
an = []
for i in range(0,len(n),3) :
an.append( np.array( n[i:i+3] , np.float32 ) )
return an
def _readVertices( self , f ) :
v = []
for i in xrange(int(f.readline())) :
v += map( float , f.readline().split(' ') )
return v
def _readPoints( self , f , verts ) :
v , n = [] , []
vi = []
for i in xrange(int(f.readline())) :
l = f.readline().split(' ')
vi.append( int(l[0]) )
ind = int(l[0])*3
v += verts[ind:ind+3]
n += l[1:]
return v , n , vi
def _readTriangels( self , f ) :
t = []
for i in xrange(int(f.readline())) :
t += map( int , f.readline().split(' ') )
return t
def _readEdges( self , f ) :
ev , et = [] , []
for i in xrange(int(f.readline())) :
l = map( int , f.readline().split(' ') )
ev += l[:2]
et += l[2:]
return ev , et