-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
314 lines (241 loc) · 8.07 KB
/
build.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
import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import glm
from stl import mesh
from PIL import Image
import numpy as np
from math import *
import random
import os
import csv
def toMat3(q):
# quaternion to 3x3 matrix
# https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
mat = glm.mat3(1.0)
w, x, y, z = q[0], q[1], q[2], q[3]
qq = q * q
sqw, sqx, sqy, sqz = qq[0], qq[1], qq[2], qq[3]
mat[0, 0] = sqx - sqy - sqz + sqw
mat[1, 1] = -sqx + sqy - sqz + sqw
mat[2, 2] = -sqx - sqy + sqz + sqw
tmp1 = x * y
tmp2 = z * w
mat[1, 0] = 2 * (tmp1 + tmp2)
mat[0, 1] = 2 * (tmp1 - tmp2)
tmp1 = x * z
tmp2 = y * w
mat[2, 0] = 2 * (tmp1 - tmp2)
mat[0, 2] = 2 * (tmp1 + tmp2)
tmp1 = y * z
tmp2 = x * w
mat[2, 1] = 2 * (tmp1 + tmp2)
mat[1, 2] = 2 * (tmp1 - tmp2)
return mat
def toMat4(q):
# quaternion to 4x4 matrix
# https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
mat = glm.mat4(1.0)
w, x, y, z = q[0], q[1], q[2], q[3]
qq = q * q
sqw, sqx, sqy, sqz = qq[0], qq[1], qq[2], qq[3]
mat[0, 0] = sqx - sqy - sqz + sqw
mat[1, 1] = -sqx + sqy - sqz + sqw
mat[2, 2] = -sqx - sqy + sqz + sqw
tmp1 = x * y
tmp2 = z * w
mat[1, 0] = 2 * (tmp1 + tmp2)
mat[0, 1] = 2 * (tmp1 - tmp2)
tmp1 = x * z
tmp2 = y * w
mat[2, 0] = 2 * (tmp1 - tmp2)
mat[0, 2] = 2 * (tmp1 + tmp2)
tmp1 = y * z
tmp2 = x * w
mat[2, 1] = 2 * (tmp1 + tmp2)
mat[1, 2] = 2 * (tmp1 - tmp2)
return mat
def bbox_info(points):
bbox_max = np.max(points, axis=0)
bbox_min = np.min(points, axis=0)
bbox_center = (bbox_max + bbox_min) / 2.0
bbox_size = bbox_max - bbox_min
return bbox_max, bbox_min, bbox_center, bbox_size
def random_uniform_rotation():
# random quaternion
# http://planning.cs.uiuc.edu/node198.html
u1, u2, u3 = [random.random() for i in range(3)]
sigma1, sigma2 = sqrt(1 - u1), sqrt(u1)
theta1, theta2 = 2 * pi * u2, 2 * pi * u3
h = np.array(
[
cos(theta2) * sigma2,
sin(theta1) * sigma1,
cos(theta1) * sigma1,
sin(theta2) * sigma2,
]
)
return toMat3(h)
def glfw_initialize():
# initialize glfw
if not glfw.init():
return
win_width, win_height = 100, 100
window = glfw.create_window(win_width, win_height, "My OpenGL window", None, None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
return window
def setup_fbo(fbo_width, fbo_height):
# fbo
fbo = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
# fbo color texture
color_tex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, color_tex)
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, fbo_width, fbo_height, 0, GL_RGBA, GL_FLOAT, 0
)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
# fbo depth texture
depth_tex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, depth_tex)
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_DEPTH_COMPONENT,
fbo_width,
fbo_height,
0,
GL_DEPTH_COMPONENT,
GL_FLOAT,
0,
)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, color_tex, 0)
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_tex, 0)
draw_buffers = [GL_COLOR_ATTACHMENT0]
glDrawBuffers(1, draw_buffers)
glBindFramebuffer(GL_FRAMEBUFFER, 0)
return fbo
def setup_shader():
# shader source
vertex_shader = """
#version 430
layout(location = 0) in vec3 position;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * vec4(position, 1.0f);
}
"""
fragment_shader = """
#version 430
out vec4 outColor;
void main()
{
outColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
"""
shader = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER),
)
mvploc = glGetUniformLocation(shader, "mvp")
return shader, mvploc
def get_mvp(scale):
# camera configuration
camPos = glm.vec3(0.0, 0.0, -5e7)
camTarget = glm.vec3(0.0, 0.0, 0.0)
camDir = glm.normalize(camPos - camTarget)
camUp = glm.vec3(0.0, 1.0, 0.0)
camRight = glm.normalize(glm.cross(camUp, camDir))
camUp = glm.cross(camDir, camRight)
# model view projecction matrix
view = glm.lookAt(camPos, camTarget, camUp)
projection = glm.ortho(-1.0, 1.0, -1.0, 1.0, 0.1, 1e8)
model = glm.scale(glm.mat4(1.0), glm.vec3(scale))
mvp = projection * view * model
return mvp
def setup_glBuffer(m, rotation):
points = m.points.reshape(-1, 3)
# random rotation
points = np.matmul(rotation, points.T).T
# bounding box centering
_, _, bbox_center, bbox_size = bbox_info(points)
points -= bbox_center
# scale to set the bounding box's size withn (-1,1)
bbox_target = 2
scale = (
bbox_target / bbox_size[0]
if bbox_size[0] > bbox_size[1]
else bbox_target / bbox_size[1]
)
points *= scale
# flatten point array
points = points.flatten().astype(np.float32)
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(
GL_ARRAY_BUFFER,
points.size * ctypes.sizeof(ctypes.c_float),
points,
GL_STATIC_DRAW,
)
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
glBindVertexArray(0)
return vao, vbo
def main():
window = glfw_initialize()
fbo_width, fbo_height = 50, 50
fbo = setup_fbo(fbo_width, fbo_height)
shader, mvploc = setup_shader()
num_view = 40
random_rotation = [random_uniform_rotation() for _ in range(num_view)]
os.makedirs('./size50/', exist_ok=True)
f = open("Thingi10K/goodfile_id.csv", 'r', encoding='utf-8')
rdr = csv.reader(f)
filenames = ["Thingi10K/raw_meshes/"+line[0]+".stl" for line in rdr]
f.close()
for idx, filename in enumerate(filenames):
print(idx)
if not os.path.isfile(filename):
continue
# read mesh
m = mesh.Mesh.from_file(filename)
for scale in [0.3, 0.4, 0.5, 0.6, 0.7]:
for i in range(num_view):
vao, vbo = setup_glBuffer(m, random_rotation[i])
mvp = get_mvp(scale)
glUseProgram(shader)
glUniformMatrix4fv(mvploc, 1, GL_FALSE, glm.value_ptr(mvp))
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
glViewport(0, 0, fbo_width, fbo_height)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
glBindVertexArray(vao)
glDrawArrays(GL_TRIANGLES, 0, m.v0.shape[0] * 3)
glfw.swap_buffers(window)
glfw.poll_events()
captured = glReadPixels(0, 0, fbo_width, fbo_height, GL_RGB, GL_FLOAT)
captured = (captured * 255).astype(np.uint8)
img = Image.fromarray(captured)
file_id = os.path.splitext(filename)[0].split('/')[-1]
img.save(f"size50/{file_id}_sc{scale}_view{i}.png")
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glDeleteBuffers(1, [vbo])
glDeleteVertexArrays(1, [vao])
glfw.terminate()
if __name__ == "__main__":
main()