-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
139 lines (128 loc) · 4.54 KB
/
gui.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import pyvista as pv
from superformula import Superformula
class SuperformulaGUI:
def __init__(self):
self.superformula = Superformula()
self.current_data = pv.PolyData()
self.current_actor = None
self.visualization_type = "point_cloud"
self.update_data()
self.plotter = pv.Plotter()
self.event_type = "always"
self.plotter.add_slider_widget(
callback=lambda x: self.update("resolution", x),
rng=[100, 5000],
value=self.superformula.resolution,
title="resolution",
style="modern",
event_type=self.event_type,
pointa=(0.75, 0.89),
pointb=(0.95, 0.89),
)
self.plotter.add_slider_widget(
callback=lambda x: self.update("a", x),
rng=[-20, 20],
value=self.superformula.a,
title="a",
style="modern",
event_type=self.event_type,
pointa=(0.75, 0.76),
pointb=(0.95, 0.76),
)
self.plotter.add_slider_widget(
callback=lambda x: self.update("b", x),
rng=[-20, 20],
value=self.superformula.b,
title="b",
style="modern",
event_type=self.event_type,
pointa=(0.75, 0.63),
pointb=(0.95, 0.63),
)
self.plotter.add_slider_widget(
callback=lambda x: self.update("m", x),
rng=[-20, 20],
value=self.superformula.m,
title="m",
style="modern",
event_type=self.event_type,
pointa=(0.75, 0.50),
pointb=(0.95, 0.50),
)
self.plotter.add_slider_widget(
callback=lambda x: self.update("n1", x),
rng=[-20, 20],
value=self.superformula.n1,
title="n1",
style="modern",
event_type=self.event_type,
pointa=(0.75, 0.37),
pointb=(0.95, 0.37),
)
self.plotter.add_slider_widget(
callback=lambda x: self.update("n2", x),
rng=[-20, 20],
value=self.superformula.n2,
title="n2",
style="modern",
event_type=self.event_type,
pointa=(0.75, 0.24),
pointb=(0.95, 0.24),
)
self.plotter.add_slider_widget(
callback=lambda x: self.update("n3", x),
rng=[-20, 20],
value=self.superformula.n3,
title="n3",
style="modern",
event_type=self.event_type,
pointa=(0.75, 0.11),
pointb=(0.95, 0.11),
)
self.plotter.add_checkbox_button_widget(
callback=lambda x: self.switch_visualization(x),
value=False,
position=(10.0, 10.0),
color_on="green",
color_off="green",
)
self.plotter.add_checkbox_button_widget(
callback=lambda x: self.save_data(x),
value=False,
position=(70.0, 10.0),
color_on="blue",
color_off="blue",
)
self.update_visualization()
self.plotter.show()
def update(self, param, value):
if param == "resolution":
value = int(value)
self.superformula.__dict__[param] = value
self.update_data()
def update_data(self):
pc = self.superformula.point_cloud()
if self.visualization_type == "point_cloud":
new_data = pv.PolyData(pc)
elif self.visualization_type == "mesh":
triangles = self.superformula.triangulate(pc)
arr3 = np.full((len(triangles), 1), 3)
triangles_pv = np.concatenate([arr3, triangles], axis=1).flatten()
new_data = pv.PolyData(pc, triangles_pv)
self.current_data.overwrite(new_data)
def update_visualization(self):
if self.current_actor is not None:
self.plotter.remove_actor(self.current_actor)
if self.visualization_type == "point_cloud":
self.current_actor = self.plotter.add_points(
self.current_data, color="2192FF", render_points_as_spheres=True
)
elif self.visualization_type == "mesh":
self.current_actor = self.plotter.add_mesh(self.current_data)
def switch_visualization(self, value):
self.visualization_type = "mesh" if value else "point_cloud"
self.update_data()
self.update_visualization()
def save_data(self, value):
self.current_data.save("./superformula.ply")