-
Notifications
You must be signed in to change notification settings - Fork 12
/
voxel.py
122 lines (91 loc) · 3.8 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
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
import numpy as np
import os
def call(command):
os.system('%s > /dev/null 2>&1' % command)
def voxel2obj(filename, pred, show=True, threshold=0.4):
verts, faces = voxel2mesh(pred, threshold )
write_obj(filename, verts, faces)
if show:
call('meshlab ' + filename)
def voxel2mesh(voxels, threshold=0):
# these faces and verticies define the various sides of a cube
top_verts = [[0,0,1], [1,0,1], [1,1,1], [0,1,1]]
top_faces = [[1,2,4], [2,3,4]]
bottom_verts = [[0,0,0], [1,0,0], [1,1,0], [0,1,0]]
bottom_faces = [[2,1,4], [3,2,4]]
left_verts = [[0,0,0], [0,0,1], [0,1,0], [0,1,1]]
left_faces = [[1,2,4], [3,1,4]]
right_verts = [[1,0,0], [1,0,1], [1,1,0], [1,1,1]]
right_faces = [[2,1,4], [1,3,4]]
front_verts = [[0,1,0], [1,1,0], [0,1,1], [1,1,1]]
front_faces = [[2,1,4], [1,3,4]]
back_verts = [[0,0,0], [1,0,0], [0,0,1], [1,0,1]]
back_faces = [[1,2,4], [3,1,4]]
top_verts = np.array(top_verts)
top_faces = np.array(top_faces)
bottom_verts = np.array(bottom_verts)
bottom_faces = np.array(bottom_faces)
left_verts = np.array(left_verts)
left_faces = np.array(left_faces)
right_verts = np.array(right_verts)
right_faces = np.array(right_faces)
front_verts = np.array(front_verts)
front_faces = np.array(front_faces)
back_verts = np.array(back_verts)
back_faces = np.array(back_faces)
new_voxels = np.zeros((voxels.shape[0]+2, voxels.shape[1]+2, voxels.shape[2]+2))
new_voxels[1:voxels.shape[0]+1,1:voxels.shape[1]+1,1:voxels.shape[2]+1 ] = voxels
voxels= new_voxels
scale = 0.01
cube_dist_scale = 1.
verts = []
faces = []
curr_vert = 0
a,b,c= np.where(voxels>threshold)
for i,j,k in zip(a,b,c):
#top
if voxels[i,j,k+1]<threshold:
verts.extend(scale * (top_verts + cube_dist_scale * np.array([[i-1, j-1, k-1]])))
faces.extend(top_faces + curr_vert)
curr_vert += len(top_verts)
#bottom
if voxels[i,j,k-1]<threshold:
verts.extend(scale * (bottom_verts + cube_dist_scale * np.array([[i-1, j-1, k-1]])))
faces.extend(bottom_faces + curr_vert)
curr_vert += len(bottom_verts)
#left
if voxels[i-1,j,k]<threshold:
verts.extend(scale * (left_verts+ cube_dist_scale * np.array([[i-1, j-1, k-1]])))
faces.extend(left_faces + curr_vert)
curr_vert += len(left_verts)
#right
if voxels[i+1,j,k]<threshold:
verts.extend(scale * (right_verts + cube_dist_scale * np.array([[i-1, j-1, k-1]])))
faces.extend(right_faces + curr_vert)
curr_vert += len(right_verts)
#front
if voxels[i,j+1,k]<threshold:
verts.extend(scale * (front_verts + cube_dist_scale * np.array([[i-1, j-1, k-1]])))
faces.extend(front_faces + curr_vert)
curr_vert += len(front_verts)
#back
if voxels[i,j-1,k]<threshold:
verts.extend(scale * (back_verts + cube_dist_scale * np.array([[i-1, j-1, k-1]])))
faces.extend(back_faces + curr_vert)
curr_vert += len(back_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 mesh2obj( verts, faces):
filename = 'temp.obj'
write_obj(filename, verts, faces)
call('meshlab ' + filename)