-
Notifications
You must be signed in to change notification settings - Fork 25
/
boundary_sampling.py
56 lines (42 loc) · 1.57 KB
/
boundary_sampling.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
import trimesh
import igl
import numpy as np
import glob
import multiprocessing as mp
from multiprocessing import Pool
import os
import traceback
from functools import partial
import random
import gc
import configs.config_loader as cfg_loader
# number of distance field samples generated per object
sample_num = 100000
def boundary_sampling(path, sigma):
try:
out_path = os.path.dirname(path)
file_name = os.path.splitext(os.path.basename(path))[0]
input_file = os.path.join(out_path,file_name + '_scaled.off')
out_file = out_path + '/boundary_{}_samples.npz'.format( sigma)
if os.path.exists(out_file):
print('Exists: {}'.format(out_file))
return
mesh = trimesh.load(input_file)
points = mesh.sample(sample_num)
if sigma == 0:
boundary_points = points
else:
boundary_points = points + sigma * np.random.randn(sample_num, 3)
grid_coords = boundary_points.copy()
grid_coords[:, 0], grid_coords[:, 2] = boundary_points[:, 2], boundary_points[:, 0]
grid_coords = 2 * grid_coords
if sigma == 0:
df = np.zeros(boundary_points.shape[0])
else:
df = np.abs(igl.signed_distance(boundary_points, mesh.vertices, mesh.faces)[0])
np.savez(out_file, points=boundary_points, df = df, grid_coords= grid_coords)
print('Finished: {}'.format(path))
except:
print('Error with {}: {}'.format(path, traceback.format_exc()))
del mesh, df, boundary_points, grid_coords, points
gc.collect()