-
Notifications
You must be signed in to change notification settings - Fork 2
/
lin_ship_visualize_animation.py
301 lines (232 loc) · 10.5 KB
/
lin_ship_visualize_animation.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from __future__ import division
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 4 18:18:24 2012
@author: gustavo
"""
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 4 17:41:01 2012
@author: gustavo
"""
from global_help import Rectangle_centered
import matplotlib.patches
import matplotlib.pyplot as plt
import matplotlib
import matplotlib as mpl
import numpy as np
import shapely.geometry as geo
def intersect_paths(paths1,paths2):
for path1 in paths1:
for path2 in paths2:
if path1.intersects_path(path2):
return True
return False
class Ship_Sprite():
def __init__(self):
self.body_length = 3.0 #vertical width
self.body_width = 3.0 #horizontal height
self.horz_flame_patch = Rectangle_centered((0.0,0.0),1.0,1.0,linestyle='solid',color='g')
self.vert_flame_patch = Rectangle_centered((2.0,0.0),1.0,1.0,linestyle='solid',color='r')
self.body_patch = matplotlib.patches.Circle(xy=(0,0),radius=1.5)
self.patches = [self.body_patch,self.horz_flame_patch,self.vert_flame_patch]
self.body_patches = [self.body_patch]
#self.ship_collection = matplotlib.collections.PatchCollection(self.patches,match_original=True)
#self.flame_color_map = matplotlib.cm.get_cmap(name='hot')
self.flame_color_map = matplotlib.cm.get_cmap(name='autumn')
#this is for optimization -- like representing the ship body as a single poly
s = geo.Polygon()
for p in self.body_patches:
v = p.get_patch_transform().transform_path(p.get_path()).vertices
s = s.union(geo.Polygon(v))
self.shapely_body = s.simplify(1e-6*self.body_length)
self.ship_patch = mpl.patches.Polygon(np.array(self.shapely_body.exterior.xy).T)
self.ship_exterior_path = self.ship_patch.get_path()
def update_thrust(self,horz_thrust,vert_thrust):
horz_flame_patch = self.horz_flame_patch
if(horz_thrust == 0):
horz_flame_patch.set_visible(False)
else:
horz_flame_patch.set_visible(True)
horz_flame_width = .5 + (abs(horz_thrust)**4)/2
horz_flame_size = 2*abs(horz_thrust)
horz_flame_patch.set_width(horz_flame_size)
horz_flame_patch.set_height(horz_flame_width)
horz_flame_patch.set_color(self.flame_color_map(abs(horz_thrust)))
#place horz flame in the right place
if(horz_thrust > 0):
horz_flame_patch.set_xy((-self.body_length/2 -horz_flame_size/2,0.0))
elif(horz_thrust < 0):
horz_flame_patch.set_xy((self.body_length/2 + horz_flame_size/2,0.0))
vert_flame_patch = self.vert_flame_patch
if(vert_thrust == 0):
vert_flame_patch.set_visible(False)
else:
vert_flame_patch.set_visible(True)
vert_flame_width = .5 + (abs(vert_thrust)**4)/2
vert_flame_size = 2*abs(vert_thrust)
vert_flame_patch.set_width(vert_flame_width)
vert_flame_patch.set_height(vert_flame_size)
vert_flame_patch.set_color(self.flame_color_map(abs(vert_thrust)))
#put the angular flame in the right place
if(vert_thrust > 0):
vert_flame_patch.set_xy(
(0 ,
-vert_flame_size/2-self.body_width/2))
else:
vert_flame_patch.set_xy(
(0 ,
vert_flame_size/2+self.body_width/2))
def update_pose(self,x,y):
(self.x,self.y) = (x,y)
def update_transform_axes(self,mpl_axes):
trans0 = mpl_axes.transData
trans1 = matplotlib.transforms.Affine2D().translate(self.x,self.y)
trans3 = matplotlib.transforms.Affine2D().scale(1)
for p in self.patches:
p.set_transform(trans3 + trans1+trans0)
def get_ship_path(self):
"""
get transformed path
"""
trans = matplotlib.transforms.Affine2D().translate(self.x,self.y)
return trans.transform_path(self.ship_patch.get_path())
def collision(self,obstacle_paths):
trans1 = matplotlib.transforms.Affine2D().translate(self.x,self.y)
trans = trans1
return intersect_paths(obstacle_paths,
#[trans.transform_path(p) for p in mpl.collections.PatchCollection(self.patches).get_paths()]
[trans.transform_path(p.get_path()) for p in self.body_patches]
)
def collision1(self,obstacle_patches):
trans1 = matplotlib.transforms.Affine2D().translate(self.x,self.y)
trans = trans1
transformed_obstacle_paths = [p.get_patch_transform().transform_path(p.get_path()) for p in obstacle_patches]
return intersect_paths(transformed_obstacle_paths,
#[trans.transform_path(p) for p in mpl.collections.PatchCollection(self.patches).get_paths()]
[trans.transform_path(p.get_path()) for p in self.body_patches]
)
def collision2(self,obstacle_paths):
#use a single ship patch
trans1 = matplotlib.transforms.Affine2D().translate(self.x,self.y)
trans = trans1
return intersect_paths(obstacle_paths,
[trans.transform_path(self.ship_patch.get_path())]
)
def collision3(self,shapely_multipoly):
#broken
trans1 = matplotlib.transforms.Affine2D().translate(self.x,self.y)
trans = trans1
xy = trans.transform_path(self.ship_exterior_path).vertices
return shapely_multipoly.intersects(geo.Polygon(xy))
def set_alpha(self,alpha):
for p in self.patches:
p.set_alpha(alpha)
@staticmethod
def make_trail_plot(ax,thrust,traj):
assert thrust.shape[0] == traj.shape[0]
for i in xrange(thrust.shape[0]):
a = Ship_Sprite()
a.update_thrust(horz_thrust=thrust[i,0],
vert_thrust=thrust[i,1])
a.update_pose(traj[i,0],traj[i,1])
a.update_transform_axes(ax)
a.set_alpha(0.3)
collide = False and a.collision2(obstacle_pc.get_paths())
if collide:
print i,'collide'
for p in a.patches:
#p.set_alpha(0.6)
if(collide):
p.set_color('r')
ax.add_artist(p)
def benchmark_collision3(n):
a = Ship_Sprite()
for i in np.linspace(0,20,n):
theta = np.random.random()*np.pi*2
a.update_pose(i,0,theta)
a.collision3(shapely_obstacles)
def benchmark_collision2(n):
a = Ship_Sprite()
for i in np.linspace(0,20,n):
theta = np.random.random()*np.pi*2
a.update_pose(i,0,theta)
a.collision2(obstacle_pc.get_paths())
def benchmark_collision(n):
a = Ship_Sprite()
for i in np.linspace(0,20,n):
theta = np.random.random()*np.pi*2
a.update_pose(i,0,theta)
a.collision(obstacle_pc.get_paths())
if __name__ == '__main__':
import matplotlib.animation as animation
import shelve
if True:
# import ipdb
#ship_shelve = shelve.open('ship.shelve')
ship_shelve = shelve.open('rrt_2d_di_best_of_all.shelve')
# field_shelve = shelve.open('field1.shelve')
# obstacle_paths = field_shelve['obstacle_paths']
import ship_field
print 'loading local vars',ship_shelve.keys()
#to pacify the linter
traj = None
utraj = None
for k in ship_shelve.keys():
locals()[k]=ship_shelve[k]
trail_figure = plt.figure(None)
trail_plot = trail_figure.gca() #.subplot(111,aspect='equal')
trail_plot.set_axis_bgcolor((.9,.9,.9)) #gray background
#process utraj so that impulses don't totally wash out the thrust visualization.
m = 2*np.mean(np.abs(utraj))
utraj = np.clip(utraj,-m,m)
#only draw these time indices
trail_indices = np.arange(0,traj.shape[0],2)
max_horz_thrust = np.max(np.abs(utraj[:,0]))
max_vert_thrust = np.max(np.abs(utraj[:,1]))
trail_utraj = utraj[trail_indices]
trail_utraj[:,0] = trail_utraj[:,0]/max_horz_thrust
trail_utraj[:,1] = trail_utraj[:,1]/max_vert_thrust
trail_traj = traj[trail_indices,2:4] #use position coordinates
Ship_Sprite.make_trail_plot(trail_plot,trail_utraj,trail_traj)
trail_plot.set_xlim(np.min(traj[:,2])-10,np.max(traj[:,2])+10)
trail_plot.set_ylim(np.min(traj[:,3]-10),np.max(traj[:,3])+10)
trail_plot.set_aspect('equal')
trail_plot.set_xlim(-10,110)
trail_plot.set_ylim(-10,110)
pc = ship_field.get_patch_collection()
pc.set_color('gray')
trail_plot.add_collection(pc)
if False: #plot explored states
s = shelve.open('kin_rrt.shelve')
tree = s['tree']
states = [node['state'] for node in tree.node.values()]
states = np.array(states).T
trail_plot.plot(states[0], states[1], 'r.', zorder=0,alpha=.4)
s.close()
#assert False
ani_fig = plt.figure(None)
ani_ax = ani_fig.gca()
ani_ax.set_xlim(np.min(traj[:,2])-10,np.max(traj[:,2])+10)
ani_ax.set_ylim(np.min(traj[:,3]-10),np.max(traj[:,3])+10)
ani_ax.set_aspect('equal')
ani_ax.set_xlim(-10,110)
ani_ax.set_ylim(-10,110)
pc = ship_field.get_patch_collection()
pc.set_color('gray')
ani_ax.add_collection(pc)
ship_sprite = Ship_Sprite()
for p in ship_sprite.patches:
ani_ax.add_artist(p)
#import copy
def update_frame(i):
j = trail_indices[i]
print i,j
ship_sprite.update_pose(traj[j,2],traj[j,3])
ship_sprite.update_thrust(utraj[j,0]/max_horz_thrust,
utraj[j,1]/max_vert_thrust)
ship_sprite.update_transform_axes(ani_ax)
ani_ax.set_title('time index: %d'%(j))
#ani_ax.add_artist(copy.copy(ship_sprite.ship_collection))
ani = animation.FuncAnimation(fig=ani_fig,func=update_frame,frames=trail_indices.size,interval=50)
#ani.save('test.mp4', fps=20, codec='mpeg4', clear_temp=True)