1 """Plot JSON formatted scenario."""
3 from math import cos, pi, sin
5 from matplotlib import pyplot as plt
6 from sys import argv, exit
19 def get_scenario(fname):
20 """Load scenario from file."""
22 raise ValueError("File name as argument needed")
23 with open(fname, "r") as f:
24 scenario = loads(f.read())
27 def plot_nodes(nodes=[]):
28 """Return ``xcoords``, ``ycoords`` arrays of nodes to plot.
31 nodes -- The list of nodes to plot.
36 xcoords.append(n[0] - MINX)
37 ycoords.append(n[1] - MINY)
38 return (xcoords, ycoords)
41 """Return ``xcoords``, ``ycoords`` arrays of car frame to plot.
44 pose -- The pose of a car.
47 lfx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
48 lfx += BCAR_DF * cos(pose[2])
49 lfx += BCAR_SD * cos(pose[2])
52 lf3x += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
53 lf3x += 2/3 * BCAR_DF * cos(pose[2])
54 lf3x += BCAR_SD * cos(pose[2])
57 lrx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
58 lrx += -BCAR_DR * cos(pose[2])
59 lrx += -BCAR_SD * cos(pose[2])
62 rrx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
63 rrx += -BCAR_DR * cos(pose[2])
64 rrx += -BCAR_SD * cos(pose[2])
67 rfx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
68 rfx += BCAR_DF * cos(pose[2])
69 rfx += BCAR_SD * cos(pose[2])
72 rf3x += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
73 rf3x += 2/3 * BCAR_DF * cos(pose[2])
74 rf3x += BCAR_SD * cos(pose[2])
77 lfy += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
78 lfy += BCAR_DF * sin(pose[2])
79 lfy += BCAR_SD * sin(pose[2])
82 lf3y += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
83 lf3y += 2/3 * BCAR_DF * sin(pose[2])
84 lf3y += BCAR_SD * sin(pose[2])
87 lry += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
88 lry += -BCAR_DR * sin(pose[2])
89 lry += -BCAR_SD * sin(pose[2])
92 rry += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
93 rry += -BCAR_DR * sin(pose[2])
94 rry += -BCAR_SD * sin(pose[2])
97 rfy += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
98 rfy += BCAR_DF * sin(pose[2])
99 rfy += BCAR_SD * sin(pose[2])
102 rf3y += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
103 rf3y += 2/3 * BCAR_DF * sin(pose[2])
104 rf3y += BCAR_SD * sin(pose[2])
107 cfx += BCAR_DF * cos(pose[2])
108 cfx += BCAR_SD * cos(pose[2])
111 cfy += BCAR_DF * sin(pose[2])
112 cfy += BCAR_SD * sin(pose[2])
114 xcoords = (lfx, lrx, rrx, rfx, cfx, rf3x, lf3x, cfx, lfx)
115 ycoords = (lfy, lry, rry, rfy, cfy, rf3y, lf3y, cfy, lfy)
116 return ([x - MINX for x in xcoords], [y - MINY for y in ycoords])
118 def plot_car_corners(pose):
119 """Return ``xcoords``, ``ycoords`` arrays of car frame corners.
122 pose -- The pose of a car.
125 lfx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
126 lfx += BCAR_DF * cos(pose[2])
127 lfx += BCAR_SD * cos(pose[2])
130 lrx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
131 lrx += -BCAR_DR * cos(pose[2])
132 lrx += -BCAR_SD * cos(pose[2])
135 rrx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
136 rrx += -BCAR_DR * cos(pose[2])
137 rrx += -BCAR_SD * cos(pose[2])
140 rfx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
141 rfx += BCAR_DF * cos(pose[2])
142 rfx += BCAR_SD * cos(pose[2])
145 lfy += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
146 lfy += BCAR_DF * sin(pose[2])
147 lfy += BCAR_SD * sin(pose[2])
150 lry += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
151 lry += -BCAR_DR * sin(pose[2])
152 lry += -BCAR_SD * sin(pose[2])
155 rry += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
156 rry += -BCAR_DR * sin(pose[2])
157 rry += -BCAR_SD * sin(pose[2])
160 rfy += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
161 rfy += BCAR_DF * sin(pose[2])
162 rfy += BCAR_SD * sin(pose[2])
164 xcoords = (lfx, lrx, rrx, rfx)
165 ycoords = (lfy, lry, rry, rfy)
166 return ([x - MINX for x in xcoords], [y - MINY for y in ycoords])
168 if __name__ == "__main__":
172 elif (len(argv) == 3):
175 sc2 = get_scenario(SCEN_FILE2)
177 SCEN_FILE = "sc.json"
179 scenario = get_scenario(SCEN_FILE)
181 # Font size to be approximately the same in the paper:
185 plt.rcParams["font.family"] = "cmr10"
186 plt.rcParams["font.size"] = 12
187 plt.rcParams['hatch.linewidth'] = 0.5
188 plt.rcParams['lines.linewidth'] = 1.0
191 # here subplot starts
192 ax = fig.add_subplot(111)
193 ax.set_aspect("equal")
194 ax.set_title("Real-world parking scenario")
195 ax.set_xlabel("x [m]")
196 ax.set_ylabel("y [m]")
198 # For Possible Entry Points (Possible Entry Configurations) use:
199 #ax.set_xlim([37.6, 45.6])
200 #ax.set_ylim([2.4, 8.0])
201 #ax.set_title("Possible Entry Configurations")
203 # For Last Maneuver use:
204 #ax.set_xlim([38, 44])
205 #ax.set_ylim([3.1, 6.9])
207 # For Scenario 3-2 detail use:
208 #ax.set_xlim([0, 15])
209 #ax.set_ylim([35, 50])
211 # For Real-world parking scenario in Introduction section use:
212 #ax.set_xlim([33, 48.5])
213 #ax.set_ylim([2.4, 10.9])
214 #ax.text(34, 9.2, "Initial configuration", color="red")
215 #ax.text(43.5, 8.5, "Final path", color="blue")
216 #ax.text(48.25, 5.5, "Entry\nconfigurations", color="orange", ha="right")
217 #ax.text(38, 3.8, "Parking\nslot", color="blue", ha="right", backgroundcolor="white")
218 #ax.text(35.2, 5.5, "Goal configuration", color="green")
220 # Set min and max to center the plot.
221 MINX = scenario["init"][0]
222 MINY = scenario["init"][1]
223 if "obst" in scenario and len(scenario["obst"]) > 0:
224 for o in scenario["obst"]:
233 # Plot all the nodes (if exists.)
234 if "nodes_x" in scenario and "nodes_y" in scenario:
236 [x - MINX for x in scenario["nodes_x"]],
237 [y - MINY for y in scenario["nodes_y"]],
243 # Plot all the steered2 nodes (if exists.)
244 if "steered2_x" in scenario and "steered2_y" in scenario:
246 [x - MINX for x in scenario["steered2_x"]],
247 [y - MINY for y in scenario["steered2_y"]],
253 # Plot all the steered1 nodes (if exists.)
254 if "steered1_x" in scenario and "steered1_y" in scenario:
256 [x - MINX for x in scenario["steered1_x"]],
257 [y - MINY for y in scenario["steered1_y"]],
263 # Plot obstacles, slot.
264 if "obst" in scenario and len(scenario["obst"]) > 0:
265 for o in scenario["obst"]:
268 ax.fill(*plot_nodes(o), color="black", fill=False, hatch="//")
269 if "slot" in scenario and len(scenario["slot"]) > 0:
270 plt.plot(*plot_nodes(scenario["slot"][0]), color="blue", linewidth=2)
271 #for s in scenario["slot"]:
272 # plt.plot(*plot_nodes(s), color="black")
274 # For the Possible Entry Configurations from the paper, use:
275 if False and "inits" in scenario:
276 max_i = len(scenario["inits"]) - 1
278 i = scenario["inits"][ii]
279 plt.plot(*plot_car(i), color="gray")
280 plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
282 i = scenario["inits"][ii]
283 plt.plot(*plot_car(i), color="gray")
284 plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
286 i = scenario["inits"][ii]
287 plt.plot(*plot_car(i), color="gray")
288 plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
289 ii = int(max_i * 3/4)
290 i = scenario["inits"][ii]
291 plt.plot(*plot_car(i), color="gray")
292 plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
294 i = scenario["inits"][ii]
295 plt.plot(*plot_car(i), color="gray")
296 plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
298 # Plot `init`, `entry`, and `goal` configurations.
299 if "init" in scenario and len(scenario["init"]) == 3:
300 plt.plot(*plot_car(scenario["init"]), color="red")
302 scenario["init"][0] - MINX,
303 scenario["init"][1] - MINY,
308 if "entries" in scenario:
309 for e in scenario["entries"]:
310 plt.plot(*plot_car(e), color="orange")
318 if "entry" in scenario and len(scenario["entry"]) == 3:
319 plt.plot(*plot_car(scenario["entry"]), color="magenta")
321 scenario["entry"][0] - MINX,
322 scenario["entry"][1] - MINY,
327 if "entry" in scenario and len(scenario["entry"]) == 4:
328 esc = scenario["entry"]
329 plt.plot(*plot_car([esc[0], esc[1], esc[2]]), color="magenta")
330 plt.plot(*plot_car([esc[0], esc[1], esc[3]]), color="magenta")
332 scenario["entry"][0] - MINX,
333 scenario["entry"][1] - MINY,
338 if "goal" in scenario and len(scenario["goal"]) == 3:
339 plt.plot(*plot_car(scenario["goal"]), color="green")
341 scenario["goal"][0] - MINX,
342 scenario["goal"][1] - MINY,
348 # Plot `path` and `max_path`.
349 if sc2 and "path" in sc2 and len(sc2["path"]) > 0:
350 plt.plot(*plot_nodes(sc2["path"]), color="orange")
351 if "orig_path" in scenario and len(scenario["orig_path"]) > 0:
353 *plot_nodes(scenario["orig_path"]),
358 if "path" in scenario and len(scenario["path"]) > 0:
360 *plot_nodes(scenario["path"]),
364 for p in scenario["path"]:
365 #plt.plot(*plot_car(p), color="green")
367 #cc = plot_car_corners(p)
368 #plt.plot(cc[0][0], cc[1][0], color="red", marker=".", ms=1)
369 #plt.plot(cc[0][1], cc[1][1], color="red", marker=".", ms=1)
370 #plt.plot(cc[0][2], cc[1][2], color="red", marker=".", ms=1)
371 #plt.plot(cc[0][3], cc[1][3], color="red", marker=".", ms=1)
373 # If there are possible starts specified, you may print and plot them.
374 #if "starts" in scenario and len(scenario["starts"]) > 0:
375 # print("possible starts:")
376 # for p in scenario["starts"]:
377 # plt.plot(*p, color="red", marker="+", ms=12)
378 # print(" {}".format(p))
380 # For the Last Maneuver figure from the paper, use:
381 # - `init2` -- orange
382 #plt.plot(*plot_car(scenario["init2"]), color="orange")
384 # scenario["init2"][0] - MINX,
385 # scenario["init2"][1] - MINY,
390 # - `goal2` -- orange
391 #plt.plot(*plot_car(scenario["goal2"]), color="orange")
393 # scenario["goal2"][0] - MINX,
394 # scenario["goal2"][1] - MINY,
399 # - `goal2` -- middle (orange)
400 #plt.plot(*plot_car(scenario["goals"][0]), color="orange")
402 # scenario["goal2"][0] - MINX,
403 # scenario["goal2"][1] - MINY,
409 #plt.plot(*plot_car(scenario["init1"]), color="green")
411 # scenario["init1"][0] - MINX,
412 # scenario["init1"][1] - MINY,
418 #plt.plot(*plot_car(scenario["goal1"]), color="green")
420 # scenario["goal1"][0] - MINX,
421 # scenario["goal1"][1] - MINY,
427 # The `scenario` may also include:
428 # - `last` -- not sure what this is, see the source code. Maybe overlaps
430 # - `last1` -- used to demonstrate In-Slot Planner (was Parking Slot
432 # - `last2` -- used to demonstrate In-Slot Planner (was Parking Slot
434 # - `max_orig_path` -- maximum original path. I used this when comparing
435 # original paths but I had to copy the `max_orig_path` by hand from
436 # different scenario result.
437 # - `orig_path` -- the path before the optimization.
438 # - `max_path` -- the maximum path after optimization. Must be copied by
440 # - `path` -- optimized path of the scenario.
442 handles, labels = ax.get_legend_handles_labels()
444 # Uncommnent the following line and comment the plt.show() to store to the
446 plt.savefig("out.pdf", bbox_inches="tight")