-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest.py
More file actions
67 lines (57 loc) · 1.51 KB
/
test.py
File metadata and controls
67 lines (57 loc) · 1.51 KB
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
import sys
import time
import torch
import gridhopping
if len(sys.argv)!=2:
print("* args: <grid resolution>")
sys.exit()
#
#
#
def sdf_sphere(xyz):
return torch.sqrt(torch.pow(xyz, 2.0).sum(1)) - 0.4
def make_batcher(sdb_scene, k):
def f(xyz):
#
sdv = []
n = len(xyz)//k
for i in range(0, n):
_xyz = xyz[ (i*k):((i+1)*k) ]
sdv.append(sdb_scene(_xyz))
#
if n*k != len(xyz):
_xyz = xyz[ (n*k): ]
sdv.append(sdb_scene(_xyz))
#
return torch.cat(sdv)
return f
#
#
#
N = int(sys.argv[1])
print("* grid resolution set to: %d" % N)
sdb_scene = make_batcher(sdf_sphere, 10**5)
t = time.time()
cells, vals = gridhopping.run(sdb_scene, N)
gridhopping.polygonize_cells("out.stl", cells, vals)
print('* elapsed time (gh): %d [ms]' % int(1000.0*(time.time() - t)))
from skimage.measure import marching_cubes
import numpy
t = time.time()
def get_raster_points(n):
points = numpy.meshgrid(
numpy.linspace(-0.5, 0.5, n),
numpy.linspace(-0.5, 0.5, n),
numpy.linspace(-0.5, 0.5, n)
)
points = numpy.stack(points)
points = numpy.swapaxes(points, 1, 2)
points = points.reshape(3, -1).transpose().astype(numpy.float32)
return points
G = torch.from_numpy(get_raster_points(N))
voxels = sdb_scene(G.reshape(-1, 3)).reshape((N, N, N)).numpy()
vertices, faces, normals, _ = marching_cubes(voxels, level=0)
print('* elapsed time (mc): %d [ms]' % int(1000.0*(time.time() - t)))
#import trimesh
#mesh = trimesh.Trimesh(vertices=vertices, faces=faces, vertex_normals=normals)
#mesh.show()