forked from PyMesh/PyMesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minkowski_sum.py
executable file
·68 lines (58 loc) · 1.87 KB
/
minkowski_sum.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
#!/usr/bin/env python
""" Compute Minkowski sum of a mesh with a path
"""
import argparse
import pymesh
import pymesh.wires
import numpy as np
import os.path
def parse_args():
parser = argparse.ArgumentParser(description=__doc__);
parser.add_argument("input_mesh");
parser.add_argument("path");
parser.add_argument("output_mesh");
return parser.parse_args();
def load_path_file(filename):
path = [];
with open(filename, 'r') as fin:
for line in fin:
fields = line.split();
if len(fields) == 0 or line[0] == "#":
continue;
path.append([float(x) for x in fields]);
return np.array(path, dtype=float);
def chain_wires(wires):
assert(wires.num_vertices > 0);
visited = np.zeros(wires.num_vertices, dtype=bool);
path = [0];
visited[0] = True;
while not np.all(visited):
front = path[0];
front_neighbors = wires.get_vertex_neighbors(int(front));
for v in front_neighbors:
if visited[v]: continue;
path.insert(0, v);
visited[v] = True;
break;
end = path[-1];
end_neighbors = wires.get_vertex_neighbors(int(end));
for v in end_neighbors:
if visited[v]: continue;
visited[v] = True;
path.append(v);
break;
first_neighbors = wires.get_vertex_neighbors(int(path[0])).squeeze();
if len(path) > 2 and path[-1] in first_neighbors:
# Close the loop.
path.append(path[0]);
path = wires.vertices[path];
return path;
def main():
args = parse_args();
mesh = pymesh.load_mesh(args.input_mesh);
wires = pymesh.wires.WireNetwork.create_from_file(args.path);
path = chain_wires(wires);
result = pymesh.minkowski_sum(mesh, path);
pymesh.save_mesh(args.output_mesh, result);
if __name__ == "__main__":
main();