-
Notifications
You must be signed in to change notification settings - Fork 2
/
mesh_module.py
388 lines (324 loc) · 11.5 KB
/
mesh_module.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import numpy as np
class Mesh:
def __init__(self):
pass
def write_obj(self, file_name):
if self.vertices is None:
raise Exception('set vertices')
return
if self.uv_map is None:
raise Exception('set uv_map')
return
if self.faces is None:
raise Exception('set faces')
return
with open('%s.obj'%file_name, 'w') as file:
file.write('# vertex info\n')
for v in self.vertices:
str = "v %f %f %f\n" % v
file.write(str)
file.write('# texture UV info\n')
for uv in self.uv_map:
str = "vt %f %f\n" % uv
file.write(str)
file.write('# face info\n')
for f in self.faces:
str = "f {0}/{0} {1}/{1} {2}/{2}\n".format(f[0]+1, f[1]+1, f[2]+1)
file.write(str)
class SquareMesh(Mesh):
def __init__(self, side, resolution):
self.res = resolution
self.square_size = 1
self.range = (-0.5, 0.5)
self.half_range = (0, 0.5)
self.make_mesh(side)
def make_mesh(self, side):
if(side == 'front'):
self.make_front()
elif side == 'right':
self.make_right()
elif side == 'left':
self.make_left()
elif side == 'top':
self.make_top()
elif side == 'bottom':
self.make_bottom()
def divide_range(self, range_to_div, num_div):
dist = range_to_div[1] - range_to_div[0]
interval = dist/num_div
divided = [range_to_div[0] + interval * i for i in range(num_div+1)]
return divided
def make_front(self):
# set vertices
self.vertices = []
x_range = self.divide_range(self.range, self.res)
y_range = self.divide_range(self.range, self.res)
z_value = self.square_size / 2
for y in y_range:
for x in x_range:
self.vertices.append((x,y,z_value))
u_range = self.divide_range((0,1), self.res)
v_range = self.divide_range((0,1), self.res)
# set uv map
self.uv_map = []
for v in v_range:
for u in u_range:
self.uv_map.append((u,v))
# set faces
res = self.res
i_range = range(res)
j_range = range(res)
bottom_left = []
for i in i_range:
for j in j_range:
bottom_left.append(i * (res+1) + j)
self.faces = []
for k in bottom_left:
self.faces.append((k, k+1, k+res+1))
self.faces.append((k+1, k+res+2, k+res+1))
def make_right(self):
# set vertices
self.vertices = []
x_value = self.square_size / 2
# You have to consider origin of camera texture(uv coordinate) here. That is why z_range is flipped
y_range = self.divide_range(self.range, self.res)
z_range = reversed(self.divide_range(self.half_range, int(self.res/2)))
for z in z_range:
for y in y_range:
self.vertices.append((x_value,y,z))
u_range = self.divide_range((0, 0.5), int(self.res/2))
v_range = self.divide_range((0,1), self.res)
# set uv map
self.uv_map = []
for u in u_range:
for v in v_range:
self.uv_map.append((u,v))
# set faces
res = self.res
i_range = range(int(res/2))
j_range = range(res)
bottom_left = []
for i in i_range:
for j in j_range:
bottom_left.append(i * (res+1) + j)
self.faces = []
for k in bottom_left:
#rather than rotating, flip faces
self.faces.append((k, k+res+1, k+1))
self.faces.append((k+1, k+res+1, k+res+2))
def make_left(self):
# set vertices
self.vertices = []
x_value = -self.square_size / 2
y_range = self.divide_range(self.range, self.res)
z_range = self.divide_range(self.half_range, int(self.res/2))
for z in z_range:
for y in y_range:
self.vertices.append((x_value,y,z))
u_range = self.divide_range((0.5, 1), int(self.res/2))
v_range = self.divide_range((0,1), self.res)
# set uv map
self.uv_map = []
for u in u_range:
for v in v_range:
self.uv_map.append((u,v))
# set faces
res = self.res
i_range = range(int(res/2))
j_range = range(res)
bottom_left = []
for i in i_range:
for j in j_range:
bottom_left.append(i * (res+1) + j)
self.faces = []
for k in bottom_left:
#rather than rotating, flip faces
self.faces.append((k, k+res+1, k+1))
self.faces.append((k+1, k+res+1, k+res+2))
def make_top(self):
# set vertices
self.vertices = []
x_range = self.divide_range(self.range, self.res)
y_value = self.square_size / 2
z_range = reversed(self.divide_range(self.half_range, int(self.res/2)))
for z in z_range:
for x in x_range:
self.vertices.append((x,y_value,z))
u_range = self.divide_range((0, 1), self.res)
v_range = self.divide_range((0,0.5), int(self.res/2))
# set uv map
self.uv_map = []
for v in v_range:
for u in u_range:
self.uv_map.append((u,v))
# set faces
res = self.res
i_range = range(int(res/2))
j_range = range(res)
bottom_left = []
for i in i_range:
for j in j_range:
bottom_left.append(i * (res+1) + j)
self.faces = []
for k in bottom_left:
#rather than rotating, flip faces
self.faces.append((k, k+1, k+res+1))
self.faces.append((k+1, k+res+2, k+res+1))
def make_bottom(self):
# set vertices
self.vertices = []
x_range = self.divide_range(self.range, self.res)
y_value = -self.square_size / 2
z_range = self.divide_range(self.half_range, int(self.res/2))
for z in z_range:
for x in x_range:
self.vertices.append((x,y_value,z))
u_range = self.divide_range((0, 1), self.res)
v_range = self.divide_range((0.5,1), int(self.res/2))
# set uv map
self.uv_map = []
for v in v_range:
for u in u_range:
self.uv_map.append((u,v))
# set faces
res = self.res
i_range = range(int(res/2))
j_range = range(res)
bottom_left = []
for i in i_range:
for j in j_range:
bottom_left.append(i * (res+1) + j)
self.faces = []
for k in bottom_left:
#rather than rotating, flip faces
self.faces.append((k, k+1, k+res+1))
self.faces.append((k+1, k+res+2, k+res+1))
def make_right_vertices(self):
vertices = []
x_value = self.square_size / 2
y_range = self.divide_range(self.range, self.res)
z_range = self.divide_range(self.half_range, int(self.res/2))
for z in z_range:
for y in y_range:
vertices.append((x_value,y,z))
return vertices
def arrange_vertices(self, side):
v_mat = np.array(self.vertices)
v_mat = v_mat.T
v_mat = np.pad(v_mat, ((0,1),(0,0)), 'constant', constant_values = 1)
td = (self.range[1] - self.range[0]) / 2
if side == 'front':
M1 = self.get_translate_matrix(0, 0, 0)
M2 = self.get_rotation_matrix('x', 0)
M3 = self.get_translate_matrix(0, 0, td)
elif side == 'right':
M1 = self.get_rotation_matrix('x', 90)
M2 = self.get_rotation_matrix('z', -90)
M3 = self.get_translate_matrix(-td, 0, 0)
elif side == 'left':
M1 = self.get_rotation_matrix('x', 90)
M2 = self.get_rotation_matrix('z', 90)
M3 = self.get_translate_matrix(td, 0, 0)
elif side == 'top':
M1 = self.get_rotation_matrix('z', 180)
M2 = self.get_rotation_matrix('x', -90)
M3 = self.get_translate_matrix(0, td, 0)
elif side == 'bottom':
M1 = self.get_rotation_matrix('x', 90)
M2 = self.get_rotation_matrix('z', 0)
M3 = self.get_translate_matrix(0, -td, 0)
else:
raise Exception("side should be one of 'front', 'right', 'left', 'up', 'down'")
M = np.matmul(M3, M2)
M = np.matmul(M, M1)
v_mat = np.matmul(M, v_mat)
v_mat = v_mat.T
self.vertices = []
for v in v_mat:
self.vertices.append((v[0], v[1], v[2]))
def get_translate_matrix(self, tx, ty, tz):
mat = np.eye(4)
mat[0,3] = tx
mat[1,3] = ty
mat[2,3] = tz
return mat
def get_rotation_matrix(self, axis, deg):
# rotation by axis
c = np.cos(np.radians(deg))
s = np.sin(np.radians(deg))
mat = np.eye(4)
if axis == 'x':
mat[1,1] = c
mat[2,2] = c
mat[1,2] = -s
mat[2,1] = s
elif axis == 'y':
mat[0,0] = c
mat[2,2] = c
mat[0,2] = s
mat[2,0] = -s
elif axis == 'z':
mat[0,0] = c
mat[0,1] = -s
mat[1,1] = c
mat[1,0] = s
return mat
def make_UV_map(self, side):
self.uv_map = []
if side == 'front':
u_range = self.divide_range((0,1), self.res)
v_range = self.divide_range((0,1), self.res)
else:
u_range = self.divide_range((0,1), self.res)
v_range = self.divide_range((0.5,1), int(self.res/2))
for v in v_range:
for u in u_range:
self.uv_map.append((u,v))
def make_surfaces(self, side):
res = self.res
if side == 'front':
i_range = range(res)
j_range = range(res)
else:
i_range = range(res)
j_range = range(int(res/2))
bottom_left = []
for i in i_range:
for j in j_range:
bottom_left.append(j * (res+1) + i)
self.faces = []
for k in bottom_left:
self.faces.append((k, k+1, k+res+1))
self.faces.append((k+1, k+res+2, k+res+1))
class FishMesh(Mesh):
def __init__(self, model_type, square_mesh):
x,y = self.map_to_fisheye(model_type, square_mesh.vertices)
self.vertices = []
for i in range(len(x)):
self.vertices.append((x[i], y[i], 0))
self.uv_map = square_mesh.uv_map.copy()
self.faces = square_mesh.faces.copy()
def map_to_fisheye(self, model_type, vertices):
v_mat = np.array(vertices)
x = v_mat[:,0]
y = v_mat[:,1]
z = v_mat[:,2]
theta = np.arctan2(np.sqrt(x*x + y*y), z)
phi = np.arctan2(y, x)
if model_type == 'equidistance':
f = 1 / (np.pi/2)
r = theta * f
elif model_type == 'orthogonal':
f = 1
r = f * np.sin(theta)
elif model_type == 'stereographic':
f = 1/2
r = 2 * f * np.tan(theta/2)
elif model_type == 'equisolid':
f = np.sqrt(2)/2
r = 2 * f * np.sin(theta/2)
else:
raise Exception("model_type should be 'equidistance' or 'orthogonal' or 'stereographic' or 'equisolid'")
_x = r * np.cos(phi)
_y = r * np.sin(phi)
return _x, _y