Repository navigation
Expand file tree
/
Copy pathresolve_self_intersection.py
More file actions
executable file
·57 lines (48 loc) · 1.97 KB
/
Copy pathresolve_self_intersection.py
File metadata and controls
executable file
·57 lines (48 loc) · 1.97 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
#!/usr/bin/env python
""" Resolve self-intersections in mesh.
"""
import argparse
import pymesh
import logging
import numpy as np
import tempfile
import os.path
def parse_args():
parser = argparse.ArgumentParser(description=__doc__);
parser.add_argument("--max-iterations", type=int, default=1,
help="Max number of times of calling resolve function.");
parser.add_argument("--with-rounding", action="store_true",
help="Agressive rounding to avoid self-intersection");
parser.add_argument("--precision", type=int, default=12,
help="Number of decimal digits to round to");
parser.add_argument("in_mesh", help="input mesh");
parser.add_argument("out_mesh", help="output mesh");
return parser.parse_args();
def main():
args = parse_args();
mesh = pymesh.load_mesh(args.in_mesh);
if (args.with_rounding):
mesh = pymesh.form_mesh(
np.round(mesh.vertices, args.precision),
mesh.faces);
intersecting_faces = pymesh.detect_self_intersection(mesh);
counter = 0;
while len(intersecting_faces) > 0 and counter < args.max_iterations:
if (args.with_rounding):
involved_vertices = np.unique(mesh.faces[intersecting_faces].ravel());
mesh.vertices_ref[involved_vertices, :] =\
np.round(mesh.vertices[involved_vertices, :],
args.precision//2);
mesh = pymesh.resolve_self_intersection(mesh, "igl");
mesh, __ = pymesh.remove_duplicated_faces(mesh, fins_only=True);
if (args.with_rounding):
mesh = pymesh.form_mesh(
np.round(mesh.vertices, args.precision),
mesh.faces);
intersecting_faces = pymesh.detect_self_intersection(mesh);
counter += 1;
if len(intersecting_faces) > 0:
logging.warn("Resolving failed: max iteration reached!");
pymesh.save_mesh(args.out_mesh, mesh);
if __name__ == "__main__":
main();