Repository navigation
Expand file tree
/
Copy pathplot_multi.py
More file actions
87 lines (81 loc) · 2.87 KB
/
Copy pathplot_multi.py
File metadata and controls
87 lines (81 loc) · 2.87 KB
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
import os, time, datetime, threading, random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from cortix.src.module import Module
from cortix.src.port import Port
from cortix.src.cortix_main import Cortix
import shapely.geometry as geo
import shapely.ops
class Plot(Module):
def __init__(self,shape = None, modules=5):
super().__init__()
self.filetime = str(datetime.datetime.now())[:10]
self.dir = '/tmp/bb'
self.length = 0
self.fps = 60
if not os.path.exists(self.dir):
os.makedirs(self.dir)
self.filename = os.path.join(self.dir,'bb_data'+self.filetime+'.csv')
self.length = modules
self.shape = shape
self.timestamp=str(datetime.datetime.now())
self.bndry = []
coords = list(self.shape.exterior.coords)
#Parse the box(LineRing) to create a list of line obstacles
for c,f in enumerate(coords):
try:
cr = geo.LineString([coords[c],coords[c+1]])
except IndexError:
cr = geo.LineString([coords[c],coords[-1]])
break
self.bndry.append(cr)
def run(self):
print('start plot')
self.dic = {}
c = 0
while True:
for i in self.ports:
if not 'plot' in str(i):
continue
lis = self.recv(i)
if isinstance(lis,str):
c+=1
if c >=self.length:
self.plot()
return
continue
for line in lis:
if line.name not in self.dic:
self.dic[line.name]=[]
self.dic[line.name].append(line.circle)
def update(self,i):
linelist = []
for line in self.dic:
x,y = self.dic[line][i].exterior.xy
self.linedic[line].set_data(x,y)
linelist.append(self.linedic[line])
return linelist
def init(self,ax):
for f in self.bndry:
x,y = f.xy
ax.plot(x,y,'r')
ax.autoscale()
ax.set_aspect( 'equal', adjustable='datalim')
ax.relim()
return ax
def plot(self):
print('starting plot')
self.linedic = dict()
fig = plt.figure()
ax = fig.add_subplot(111)
for line in self.dic:
if line not in self.linedic:
self.linedic[line], = ax.plot([],[],'b')
print('creating animation')
ani = animation.FuncAnimation(fig, self.update, frames=[f for f in range(len(self.dic[line]))],
init_func=lambda:self.init(ax), blit=False)
plt.savefig('output_ball.png')
ani.save('bb_animation.mp4', fps=self.fps)
print('goodbye')
return