prevent infinite history track

This commit is contained in:
Ruben van de Ven 2024-11-05 14:29:51 +01:00
parent 9871848407
commit 9aff9c7c06

View file

@ -78,19 +78,31 @@ class DrawnTrack:
self.track_id = track_id self.track_id = track_id
self.renderer = renderer self.renderer = renderer
self.drawn_positions = [] self.drawn_positions = []
self.drawn_predictions = [] # self.drawn_predictions = []
self.predictions: deque[DrawnPrediction] = deque(maxlen=20) # TODO; make configurable self.predictions: deque[DrawnPrediction] = deque(maxlen=2) # TODO; make configurable
self.shapes: list[pyglet.shapes.Line] = [] self.shapes: list[pyglet.shapes.Line] = []
self.set_track(track, H) self.set_track(track, H)
self.set_prediction(track) self.set_prediction(track)
def __del__(self):
self.destroy()
def destroy(self):
# TODO)) not working yet
logger.warning(f'del track {self}')
for s in self.shapes:
s.delete()
for p in self.predictions:
p.destroy()
def set_track(self, track: Track, H): def set_track(self, track: Track, H):
self.update_at = time.time() self.update_at = time.time()
self.track = track self.track = track
self.H = H self.H = H
self.coords = [d.get_foot_coords() for d in track.history] self.coords = [d.get_foot_coords() for d in track.history[-30:]] # maximum of 30 past positions (prevent collapse of FPS on loitering object)
# perhaps only do in constructor: # perhaps only do in constructor:
self.inv_H = np.linalg.pinv(self.H) self.inv_H = np.linalg.pinv(self.H)
@ -98,6 +110,7 @@ class DrawnTrack:
def set_prediction(self, track: Track): def set_prediction(self, track: Track):
# TODO: turn into add_prediction # TODO: turn into add_prediction
pred_coords = [] pred_coords = []
if not track.predictions: if not track.predictions:
return return
@ -248,6 +261,11 @@ class DrawnPrediction:
line.opacity = 5 line.opacity = 5
prediction_shapes.append(line) prediction_shapes.append(line)
self.pred_shapes.append(prediction_shapes) self.pred_shapes.append(prediction_shapes)
def destroy(self):
for pred in self.pred_shapes:
for shape in pred:
shape.delete()
def update_opacities(self, dt: float): def update_opacities(self, dt: float):
@ -547,6 +565,7 @@ class Renderer:
# TODO make delay configurable # TODO make delay configurable
if self.drawn_tracks[track_id].update_at < time.time() - 5: if self.drawn_tracks[track_id].update_at < time.time() - 5:
# TODO fade out # TODO fade out
self.drawn_tracks[track_id].destroy()
del self.drawn_tracks[track_id] del self.drawn_tracks[track_id]
def update_predictions(self): def update_predictions(self):
@ -592,6 +611,7 @@ class Renderer:
self.window.clear() self.window.clear()
self.batch_bg.draw() self.batch_bg.draw()
for track in self.drawn_tracks.values(): for track in self.drawn_tracks.values():
for shape in track.shapes: for shape in track.shapes: