-
Notifications
You must be signed in to change notification settings - Fork 1
/
chaikin3d.py
executable file
·95 lines (82 loc) · 2.92 KB
/
chaikin3d.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import sys
sys.path.insert(0, "src/")
from polyhedron import Polyhedron
from wavefront_reader import WaveFrontReader
from arg_utils import gen_arg_parser, read_args
import plotting
def save_poly(poly, figure, output):
# writing to file
if not output:
return
print(f"Saving file to {output!r}")
if output.endswith(".obj"):
with open(output, "w") as f:
poly.save(f)
elif output.endswith(".html"):
assert figure is not None, "Must plot the mesh when saving to html"
figure.write_html(output)
else:
raise ValueError(f'Invalid output: "{output}"')
# Main function
def main():
"""Main function"""
arg_parser = gen_arg_parser()
# a : command-line arguments
a = read_args(arg_parser)
vprint = print if a.verbose else lambda *args, **kwargs: None
# create a renderer
Renderer = a.renderer_class
renderer = Renderer(verbose=a.verbose)
# input file
reader = WaveFrontReader(a.input, True, a.rotate_mesh, a.verbosity)
poly = reader.to_polyhedron()
# do chaikin generations before any graphics ?
if a.plot != "evolution" and a.plot != "animation":
assert (
a["chaikin generations"] >= 0
), f"Number of generations must be positive ({a.chaikin_generations} >= 0)"
for _ in range(a.chaikin_generations):
vprint(" - 3D Chaikin -")
poly = poly.Chaikin3D(a)
vprint("Chaikin done")
# switch the plot type
if a.plot == "simple" or a.plot == "none":
poly_dd = renderer.get_polyhedron_draw_data(
poly, type_="any", alpha=a.alpha, color=a.polygon_color
)
if a.show_main_edges:
main_conn_dd = renderer.get_edges_draw_data(
poly,
type_="main",
line_color=a.main_edge_color,
node_color=a.node_color,
)
else:
main_conn_dd = list()
if a.show_graphical_edges:
graphical_conn_dd = renderer.get_edges_draw_data(
poly,
type_="graphical",
line_color=a.graphical_edge_color,
node_color=a.node_color,
)
else:
graphical_conn_dd = list()
fig = renderer.figure(poly_dd + graphical_conn_dd + main_conn_dd)
save_poly(poly, fig, a.output)
if a.plot == "simple":
fig.show()
elif a.plot == "full":
fig = plotting.draw_full(renderer, poly, a)
save_poly(poly, fig, a.output)
elif a.plot == "evolution":
fig = plotting.draw_chaikin_evolution(renderer, poly, a)
save_poly(poly, fig, a.output)
elif a.plot == "animation":
raise NotImplementedError("Animation plot not implemetned yet")
plotting.chaikin_animation(renderer, poly, a)
else:
raise ValueError(f'Unrecognized plot type "{a.plot}"')
if __name__ == "__main__":
main()