Haptic Controller
Loading...
Searching...
No Matches
rendering.py
Go to the documentation of this file.
1"""
2Real-time rendering using MatPlotLib.
3"""
4
5import time
6
7import matplotlib
8import matplotlib.pyplot as plt
9import numpy as np
10
11
12# https://matplotlib.org/stable/users/explain/animations/blitting.html
15 self,
16 title=None,
17 xlabel=None,
18 ylabel=None,
19 xlim=[-1.5, 1.5],
20 ylim=None,
21 hide_axis=False,
22 tight=False,
23 enable_grid=False,
24 enable_legend=False,
25 ):
26 matplotlib.rcParams["toolbar"] = "None"
27 self.fig = plt.figure()
28 self.ax = self.fig.add_subplot()
29
30 if ylim is None:
31 ylim = xlim
32
33 self.ax.set_xlim(xlim)
34 self.ax.set_ylim(ylim)
35 self.ax.set_aspect("equal", adjustable="box")
36
37 self.ax.set_title(title)
38 self.ax.set_xlabel(xlabel)
39 self.ax.set_ylabel(ylabel)
40
41 if hide_axis:
42 self.ax.set_axis_off()
43
44 # From https://stackoverflow.com/a/47893499
45 # This also lowers performance for some reason
46 if tight:
47 self.fig.tight_layout(pad=0)
48 w, h = self.fig.get_size_inches()
49 x1, x2 = self.ax.get_xlim()
50 y1, y2 = self.ax.get_ylim()
51 self.fig.set_size_inches(
52 2 * 1.08 * w, 2 * self.ax.get_aspect() * (y2 - y1) / (x2 - x1) * w
53 )
54
55 if enable_grid:
56 self.ax.grid()
57
58 self.enable_legend = enable_legend
59
60 self.plot_dict = {}
61
62 plt.show(block=False)
63 plt.pause(0.1)
64 self.bg = self.fig.canvas.copy_from_bbox(self.fig.bbox)
65
66 def register_plot(self, label, m="o", alpha=1):
67 (points,) = self.ax.plot([], [], m, alpha=alpha, animated=True, label=label)
68 self.plot_dict[label] = points
69
70 def start_drawing(self):
71 self.fig.canvas.restore_region(self.bg)
72
73 def draw_points(self, label, x, y):
74 if label not in self.plot_dict:
75 raise ValueError(f"Plot {label} not registered")
76 self.plot_dict[label].set_data(x, y)
77 self.ax.draw_artist(self.plot_dict[label])
78
79 def end_drawing(self, delay=0):
80 if self.enable_legend:
81 self.ax.draw_artist(self.ax.legend())
82 self.fig.canvas.blit(self.fig.bbox)
83 self.fig.canvas.flush_events()
84
85 if delay > 0:
86 plt.pause(delay)
87
88
89if __name__ == "__main__":
90 pp = PointsInSpace("Dots circling", enable_legend=True)
91 pp.register_plot("dots")
92 pp.register_plot("lines", m="-")
93 frame_count = 50000
94 num_dots = 15
95 speed = 0.005
96
97 tic = time.time()
98 for i in range(frame_count):
99 t = (2 * np.pi / num_dots) * np.arange(num_dots)
100 t += i * speed
101
102 x = np.cos(t) * np.cos(4 * t)
103 y = np.sin(t) * np.cos(4 * t)
104
105 pp.start_drawing()
106 pp.draw_points("dots", x, y)
107 pp.draw_points("lines", x, y)
108 pp.end_drawing()
109
110 print(f"Average FPS: {frame_count / (time.time() - tic)}")
end_drawing(self, delay=0)
Definition rendering.py:79
__init__(self, title=None, xlabel=None, ylabel=None, xlim=[-1.5, 1.5], ylim=None, hide_axis=False, tight=False, enable_grid=False, enable_legend=False)
Definition rendering.py:25
register_plot(self, label, m="o", alpha=1)
Definition rendering.py:66
draw_points(self, label, x, y)
Definition rendering.py:73