-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmake_reconstruction_data.py
64 lines (54 loc) · 2.06 KB
/
make_reconstruction_data.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
import os
import random
import networkx as nx
import numpy as np
import trimesh
import point_cloud_utils as pcu
def neighborhood(G, node, n):
path_lengths = nx.single_source_dijkstra_path_length(G, node)
return [node for node, length in zip(path_lengths.keys(), path_lengths.values()) if length <= n]
def make_sparse(file, out_path, samples=[100, 1000]):
"""
Sample sparse point clouds from mesh.
"""
print(file)
for sample in samples:
file_name = os.path.join(out_path, os.path.basename(
file.replace(".ply", f"_{sample}_sparse.ply")))
if not os.path.exists(file_name):
v, f, _ = pcu.load_mesh_vfn(file)
m = 0
tolerance = 0.95
i = 0
while abs(m/sample) < tolerance:
f_i, bc = pcu.sample_mesh_poisson_disk(
v, f, sample)
grid = pcu.interpolate_barycentric_coords(
f, f_i, bc, v)
m = grid.shape[0]
i += 1
print(abs(m/sample))
trimesh.points.PointCloud(grid).export(file_name)
def make_partial(file, out_path, hops=20):
"""
Get partial meshes by removing vertex and its neighborhood.
"""
mesh = trimesh.load(file, process=False)
n_verts = mesh.vertices.shape[0]
file_name = os.path.basename(file)
mesh = trimesh.load(file, process=False)
mesh.export(os.path.join(out_path, file_name))
for i in random.sample(range(0, n_verts), 2):
mesh = trimesh.load(file, process=False)
G = nx.Graph()
G.add_edges_from(mesh.edges)
idxs = neighborhood(G, i, hops)
mask_vertices = np.zeros(len(mesh.vertices), dtype=np.bool)
mask_vertices[idxs] = True
mask_vertices = np.invert(mask_vertices)
mask = mask_vertices[mesh.faces].any(axis=1)
mesh.update_faces(mask)
mesh.remove_unreferenced_vertices()
file_name = os.path.basename(file.replace(".ply", f"_{i}_partial.ply"))
print(file_name)
mesh.export(os.path.join(out_path, file_name))