-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoxel.py
59 lines (45 loc) · 2 KB
/
voxel.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
import numpy as np
def evaluate_voxel_prediction(preds, gt, thresh):
preds_occupy = preds[:, 1, :, :] >= thresh
diff = np.sum(np.logical_xor(preds_occupy, gt[:, 1, :, :]))
intersection = np.sum(np.logical_and(preds_occupy, gt[:, 1, :, :]))
union = np.sum(np.logical_or(preds_occupy, gt[:, 1, :, :]))
num_fp = np.sum(np.logical_and(preds_occupy, gt[:, 0, :, :])) # false positive
num_fn = np.sum(np.logical_and(np.logical_not(preds_occupy), gt[:, 1, :, :])) # false negative
return np.array([diff, intersection, union, num_fp, num_fn])
def voxel2mesh(voxels):
cube_verts = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0],
[1, 1, 1]] # 8 points
cube_faces = [[0, 1, 2], [1, 3, 2], [2, 3, 6], [3, 7, 6], [0, 2, 6], [0, 6, 4], [0, 5, 1],
[0, 4, 5], [6, 7, 5], [6, 5, 4], [1, 7, 3], [1, 5, 7]] # 12 face
cube_verts = np.array(cube_verts)
cube_faces = np.array(cube_faces) + 1
l, m, n = voxels.shape
scale = 0.01
cube_dist_scale = 1.1
verts = []
faces = []
curr_vert = 0
for i in range(l):
for j in range(m):
for k in range(n):
# If there is a non-empty voxel
if voxels[i, j, k] > 0:
verts.extend(scale * (cube_verts + cube_dist_scale * np.array([[i, j, k]])))
faces.extend(cube_faces + curr_vert)
curr_vert += len(cube_verts)
return np.array(verts), np.array(faces)
def write_obj(filename, verts, faces):
""" write the verts and faces on file."""
with open(filename, 'w') as f:
# write vertices
f.write('g\n# %d vertex\n' % len(verts))
for vert in verts:
f.write('v %f %f %f\n' % tuple(vert))
# write faces
f.write('# %d faces\n' % len(faces))
for face in faces:
f.write('f %d %d %d\n' % tuple(face))
def voxel2obj(filename, pred):
verts, faces = voxel2mesh(pred)
write_obj(filename, verts, faces)