From 2e92185afd5c27528d07d9ef3a957df9b9bb6447 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Thu, 21 Apr 2022 16:47:25 +0200 Subject: [PATCH] First version of usable Displayer class done --- display/dislpay_2d.py | 45 +++++++++++++++++++++++++++++++----------- examples/gps_fusion.py | 9 +++++++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/display/dislpay_2d.py b/display/dislpay_2d.py index 1da68b8..963cfbe 100644 --- a/display/dislpay_2d.py +++ b/display/dislpay_2d.py @@ -4,32 +4,53 @@ import numpy as np import matplotlib.gridspec as gridspec from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes import functools +from time import sleep +import itertools +# +# Displayer class +# + class Displayer: def __init__(self, width, height): self._fig, self._ax = plt.subplots() self._fig.set_figwidth(width) self._fig.set_figheight(height) - self._xdata, self._ydata = [], [] - self._obj, = plt.plot([], [], 'ro') - self._circle = plt.Circle((5, 5), 1, color='r', fill=False) - self._objects = [self._obj, self._circle] + self._object_map = {} + self._circle_map = {} + self._objects = [] + + + def register_object(self, obj_name, obj_color='r'): + self._object_map[obj_name], = plt.plot([], [], color=obj_color, marker='o') + self._circle_map[obj_name] = plt.Circle((0, 0), 1, color=obj_color, fill=False) + + self._objects.append(self._object_map[obj_name]) + self._objects.append(self._circle_map[obj_name]) + + + def move_object(self, obj_name, x, y): + self._object_map[obj_name].set_data([x, y]) + self._circle_map[obj_name].center = (x, y) + def __init_animation(self): self._ax.set_xlim(0, 20) self._ax.set_ylim(0, 20) - self._ax.add_patch(self._circle) + + for key in self._circle_map: + self._ax.add_patch(self._circle_map[key]) + return self._objects - def __update_animation(self, t): - self._obj.set_data([t, t]) - self._circle.center = (t, t) - self._circle.radius = t*0.1 - return self._objects - def animate(self, n_steps): - anim = FuncAnimation(self._fig, self.__update_animation, frames=np.linspace(0, n_steps-1, n_steps), + def animate(self, n_steps, anim_callback): + def anim_callback_wrapper(t): + anim_callback(self, t) + return self._objects + + anim = FuncAnimation(self._fig, anim_callback_wrapper, frames=np.linspace(0, n_steps-1, n_steps), init_func=self.__init_animation, blit=True) plt.show() \ No newline at end of file diff --git a/examples/gps_fusion.py b/examples/gps_fusion.py index 56a34d7..a86adfc 100644 --- a/examples/gps_fusion.py +++ b/examples/gps_fusion.py @@ -6,8 +6,13 @@ from matplotlib.animation import FuncAnimation from display.dislpay_2d import Displayer +def update(disp, t): + disp.move_object("test", t, t) + disp.move_object("test2", 20-t, 20-t) def run(): disp = Displayer(width=6, height=6) - # disp.show_object([1, 1], 4, "green") - disp.animate(n_steps = 100) \ No newline at end of file + #disp.show_object([1, 1], 4, "green") + disp.register_object("test") + disp.register_object("test2", 'g') + disp.animate(n_steps = 100, anim_callback=update) \ No newline at end of file