2 """Plot JSON formatted scenario."""
4 from math import cos, pi, sin
6 from matplotlib import pyplot as plt
7 from sys import argv, exit
20 def get_scenario(fname):
21 """Load scenario from file."""
23 raise ValueError("File name as argument needed")
24 with open(fname, "r") as f:
25 scenario = loads(f.read())
28 def plot_nodes(nodes=[]):
29 """Return ``xcoords``, ``ycoords`` arrays of nodes to plot.
32 nodes -- The list of nodes to plot.
37 xcoords.append(n[0] - MINX)
38 ycoords.append(n[1] - MINY)
39 return (xcoords, ycoords)
42 """Return ``xcoords``, ``ycoords`` arrays of car frame to plot.
45 pose -- The pose of a car.
48 lfx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
49 lfx += BCAR_DF * cos(pose[2])
50 lfx += BCAR_SD * cos(pose[2])
53 lf3x += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
54 lf3x += 2/3 * BCAR_DF * cos(pose[2])
55 lf3x += BCAR_SD * cos(pose[2])
58 lrx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
59 lrx += -BCAR_DR * cos(pose[2])
60 lrx += -BCAR_SD * cos(pose[2])
63 rrx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
64 rrx += -BCAR_DR * cos(pose[2])
65 rrx += -BCAR_SD * cos(pose[2])
68 rfx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
69 rfx += BCAR_DF * cos(pose[2])
70 rfx += BCAR_SD * cos(pose[2])
73 rf3x += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
74 rf3x += 2/3 * BCAR_DF * cos(pose[2])
75 rf3x += BCAR_SD * cos(pose[2])
78 lfy += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
79 lfy += BCAR_DF * sin(pose[2])
80 lfy += BCAR_SD * sin(pose[2])
83 lf3y += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
84 lf3y += 2/3 * BCAR_DF * sin(pose[2])
85 lf3y += BCAR_SD * sin(pose[2])
88 lry += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
89 lry += -BCAR_DR * sin(pose[2])
90 lry += -BCAR_SD * sin(pose[2])
93 rry += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
94 rry += -BCAR_DR * sin(pose[2])
95 rry += -BCAR_SD * sin(pose[2])
98 rfy += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
99 rfy += BCAR_DF * sin(pose[2])
100 rfy += BCAR_SD * sin(pose[2])
103 rf3y += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
104 rf3y += 2/3 * BCAR_DF * sin(pose[2])
105 rf3y += BCAR_SD * sin(pose[2])
108 cfx += BCAR_DF * cos(pose[2])
109 cfx += BCAR_SD * cos(pose[2])
112 cfy += BCAR_DF * sin(pose[2])
113 cfy += BCAR_SD * sin(pose[2])
115 xcoords = (lfx, lrx, rrx, rfx, cfx, rf3x, lf3x, cfx, lfx)
116 ycoords = (lfy, lry, rry, rfy, cfy, rf3y, lf3y, cfy, lfy)
117 return ([x - MINX for x in xcoords], [y - MINY for y in ycoords])
119 def plot_car_corners(pose):
120 """Return ``xcoords``, ``ycoords`` arrays of car frame corners.
123 pose -- The pose of a car.
126 lfx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
127 lfx += BCAR_DF * cos(pose[2])
128 lfx += BCAR_SD * cos(pose[2])
131 lrx += (BCAR_W / 2.0) * cos(pose[2] + pi / 2.0)
132 lrx += -BCAR_DR * cos(pose[2])
133 lrx += -BCAR_SD * cos(pose[2])
136 rrx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
137 rrx += -BCAR_DR * cos(pose[2])
138 rrx += -BCAR_SD * cos(pose[2])
141 rfx += (BCAR_W / 2.0) * cos(pose[2] - pi / 2.0)
142 rfx += BCAR_DF * cos(pose[2])
143 rfx += BCAR_SD * cos(pose[2])
146 lfy += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
147 lfy += BCAR_DF * sin(pose[2])
148 lfy += BCAR_SD * sin(pose[2])
151 lry += (BCAR_W / 2.0) * sin(pose[2] + pi / 2.0)
152 lry += -BCAR_DR * sin(pose[2])
153 lry += -BCAR_SD * sin(pose[2])
156 rry += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
157 rry += -BCAR_DR * sin(pose[2])
158 rry += -BCAR_SD * sin(pose[2])
161 rfy += (BCAR_W / 2.0) * sin(pose[2] - pi / 2.0)
162 rfy += BCAR_DF * sin(pose[2])
163 rfy += BCAR_SD * sin(pose[2])
165 xcoords = (lfx, lrx, rrx, rfx)
166 ycoords = (lfy, lry, rry, rfy)
167 return ([x - MINX for x in xcoords], [y - MINY for y in ycoords])
169 if __name__ == "__main__":
173 elif (len(argv) == 3):
176 sc2 = get_scenario(SCEN_FILE2)
178 SCEN_FILE = "sc.json"
180 scenario = get_scenario(SCEN_FILE)
182 # Font size to be approximately the same in the paper:
186 plt.rc('axes', unicode_minus=False)
187 plt.rcParams["figure.figsize"] = [14, 7]
188 plt.rcParams["font.family"] = "cmr10"
189 plt.rcParams["font.size"] = 22
190 plt.rcParams['hatch.linewidth'] = 0.5
191 plt.rcParams['lines.linewidth'] = 1.0
194 # here subplot starts
195 ax = fig.add_subplot(111)
196 ax.set_aspect("equal")
197 ax.set_title("Real-world parking scenario")
198 ax.set_title("Simple parking scenario")
199 ax.set_xlabel("x [m]")
200 ax.set_ylabel("y [m]")
201 # For stage, comment upper, uncomment following:
206 #ax.set_xlim([4, 32]) # 28
207 #ax.set_ylim([7, 29]) # 22
209 #ax.set_xlim([-1, 27]) # 28
210 #ax.set_ylim([-1, 21]) # 22
212 #ax.set_xlim([-9.9, 18.1]) # 28
213 #ax.set_ylim([-4.9, 17.1]) # 22
215 #ax.set_xlim([4, 32]) # 28
216 #ax.set_ylim([-4, 18]) # 22
218 #ax.set_xlim([-7, 21]) # 28
219 #ax.set_ylim([7, 29]) # 22
221 #ax.set_xlim([-7, 21]) # 28
222 #ax.set_ylim([2, 24]) # 22
224 #ax.set_xlim([-1, 27]) # 28
225 #ax.set_ylim([-1, 21]) # 22
227 # For Possible Entry Points (Possible Entry Configurations) use:
228 #plt.rcParams["font.size"] = 26
229 #ax.set_xlim([37.6, 45.6])
230 #ax.set_ylim([2.4, 8.5])
231 #ax.set_title("Possible configurations")
233 # For Last Maneuver use:
234 #ax.set_xlim([38, 44])
235 #ax.set_ylim([3.1, 6.9])
237 # For Scenario 3-2 detail use:
239 #ax.set_title("Scenario 1")
240 #ax.set_xlim([1, 16]) # w=15
241 #ax.set_ylim([35.1, 49.9]) # h=15
243 # For Scenario 4-0 detail use:
245 #ax.set_title("Scenario 2")
246 #ax.set_xlim([32, 53]) # w=19
247 #ax.set_ylim([0.5, 19.5]) # h=18
249 # For Scenario 4-1 detail use:
251 #ax.set_title("Scenario 3")
252 #ax.set_xlim([32.5, 48.5]) # w=16
253 #ax.set_ylim([1, 16]) # h=15
255 # For Scenario 5-1 detail use:
257 #ax.set_title("Scenario 4")
258 #ax.set_xlim([10.1, 27])
259 #ax.set_ylim([10.1, 26])
261 # For Scenario 4-1-{0,14} detail use:
263 #ax.set_title("Scenario 5") # Scenario 6
264 #ax.set_xlim([32.5, 48.5]) # w=16
265 #ax.set_ylim([1, 16.3]) # h=15.3
267 # For Scenario 5-3-34 detail use:
269 #ax.set_title("Scenario 7") # Scenario 8
270 #ax.set_xlim([5.5, 27.5])
271 #ax.set_ylim([15.1, 35])
273 # For Scenario 5-3-29 detail use:
275 #ax.set_title("Scenario 8")
276 #ax.set_xlim([5.5, 27.5])
277 #ax.set_ylim([10.1, 30])
279 # For Real-world parking scenario in Introduction section use:
280 #ax.set_title("Real-world parking scenario with artificial obstacle")
281 #ax.set_xlim([23, 58.5])
282 #ax.set_ylim([-8.4, 20.9])
283 #ax.text(34, 9.2, "Initial configuration", color="red")
284 #ax.text(43.5, 8.5, "Final path", color="blue")
285 #ax.text(48.25, 5.5, "Entry\nconfigurations", color="orange", ha="right")
286 #ax.text(38, 3.8, "Parking\nslot", color="blue", ha="right", backgroundcolor="white")
287 #ax.text(35.2, 5.5, "Goal configuration", color="green")
290 #ax.set_title("Computed goal")
291 #ax.set_xlim([6.8, 16.2])
292 #ax.set_ylim([15, 20])
294 # For simple scenarios 92 and 96 (simple-1k-test49)
295 # - Use MINY=-25, MINX=-10 for 96.
296 #ax.set_title("Simple parking scenario")
297 #ax.set_xlim([-11, 24]) # w=35
298 #ax.set_ylim([-2.5, 37.5]) # h=40
300 # Set min and max to center the plot.
301 MINX = scenario["init"][0]
302 MINY = scenario["init"][1]
303 MAXX = scenario["init"][0]
304 MAXY = scenario["init"][1]
305 if "obst" in scenario and len(scenario["obst"]) > 0:
306 for o in scenario["obst"]:
318 print("w: {}, h: {}".format(abs(MAXX - MINX), abs(MAXY - MINY)))
322 (-744239.7727016528 - MINX, -1044308.987006895 - MINY),
329 (-744239.7727016528 - MINX, -1044308.987006895 - MINY),
336 (-744238.8067612824 - MINX, -1044309.1038891475 - MINY),
343 (-744238.8067612824 - MINX, -1044309.1038891475 - MINY),
350 # Plot all the nodes (if exists.)
351 if "nodes_x" in scenario and "nodes_y" in scenario:
353 [x - MINX for x in scenario["nodes_x"]],
354 [y - MINY for y in scenario["nodes_y"]],
360 # Plot all the steered2 nodes (if exists.)
361 if "steered2_x" in scenario and "steered2_y" in scenario:
363 [x - MINX for x in scenario["steered2_x"]],
364 [y - MINY for y in scenario["steered2_y"]],
370 # Plot all the steered1 nodes (if exists.)
371 if "steered1_x" in scenario and "steered1_y" in scenario:
373 [x - MINX for x in scenario["steered1_x"]],
374 [y - MINY for y in scenario["steered1_y"]],
380 # Plot obstacles, slot.
381 if "obst" in scenario and len(scenario["obst"]) > 0:
382 for o in scenario["obst"]:
385 ax.fill(*plot_nodes(o), color="black", fill=False, hatch="//") #fill=True for stage
386 if "slot" in scenario and len(scenario["slot"]) > 0:
387 plt.plot(*plot_nodes(scenario["slot"]), color="blue", linewidth=1)
388 #for s in scenario["slot"]:
389 # plt.plot(*plot_nodes(s), color="black")
391 # For the Possible Entry Configurations from the paper, use:
392 #ax.set_title("Computed configurations")
395 if False and inits in scenario:
396 max_i = len(scenario[inits]) - 1
398 i = scenario[inits][ii]
399 plt.plot(*plot_car(i), color=inits_c)
400 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
402 i = scenario[inits][ii]
403 plt.plot(*plot_car(i), color=inits_c)
404 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
406 i = scenario[inits][ii]
407 plt.plot(*plot_car(i), color=inits_c)
408 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
409 ii = int(max_i * 3/4)
410 i = scenario[inits][ii]
411 plt.plot(*plot_car(i), color=inits_c)
412 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
414 i = scenario[inits][ii]
415 plt.plot(*plot_car(i), color=inits_c)
416 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
419 if True and inits in scenario:
420 max_i = len(scenario[inits]) - 1
422 i = scenario[inits][ii]
423 plt.plot(*plot_car(i), color=inits_c)
424 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
426 i = scenario[inits][ii]
427 plt.plot(*plot_car(i), color=inits_c)
428 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
430 i = scenario[inits][ii]
431 plt.plot(*plot_car(i), color=inits_c)
432 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
433 ii = int(max_i * 3/4)
434 i = scenario[inits][ii]
435 plt.plot(*plot_car(i), color=inits_c)
436 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
438 i = scenario[inits][ii]
439 plt.plot(*plot_car(i), color=inits_c)
440 plt.plot(i[0] - MINX, i[1] - MINY, color=inits_c, marker="+", ms=12)
441 # Possible/Candidate entries
447 # fontfamily="serif",
448 # fontstyle="italic",
451 # scenario["slot"][-1][0] - MINX,
452 # scenario["slot"][-1][1] - MINY,
460 # scenario["slot"][-1][0] - MINX - 2,
464 # fontfamily="serif",
465 # fontstyle="italic",
468 # Plot `init`, `entry`, and `goal` configurations.
469 if "init" in scenario and len(scenario["init"]) == 3:
470 plt.plot(*plot_car(scenario["init"]), color="red")
472 scenario["init"][0] - MINX,
473 scenario["init"][1] - MINY,
478 #if "init" in scenario and len(scenario["init"]) == 4:
479 # plt.plot(*plot_car(scenario["init"]), color="red")
480 # scenario["init"][2] = scenario["init"][3]
481 # plt.plot(*plot_car(scenario["init"]), color="red")
483 # scenario["init"][0] - MINX,
484 # scenario["init"][1] - MINY,
489 #if "entries" in scenario:
490 # for e in scenario["entries"]:
491 # plt.plot(*plot_car(e), color="orange")
499 #if "entry" in scenario and len(scenario["entry"]) == 3:
500 # plt.plot(*plot_car(scenario["entry"]), color="magenta")
502 # scenario["entry"][0] - MINX,
503 # scenario["entry"][1] - MINY,
508 #if "entry" in scenario and len(scenario["entry"]) == 4:
509 # esc = scenario["entry"]
510 # plt.plot(*plot_car([esc[0], esc[1], esc[2]]), color="magenta")
511 # plt.plot(*plot_car([esc[0], esc[1], esc[3]]), color="magenta")
513 # scenario["entry"][0] - MINX,
514 # scenario["entry"][1] - MINY,
519 if "goal" in scenario:
520 if len(scenario["goal"]) == 3:
521 plt.plot(*plot_car(scenario["goal"]), color="green")
523 scenario["goal"][0] - MINX,
524 scenario["goal"][1] - MINY,
529 # elif len(scenario["goal"]) == 4:
530 # ctp = scenario["goal"]
531 # plt.plot(*plot_car(scenario["goal"]), color="green")
533 # plt.plot(*plot_car(scenario["goal"]), color="green")
535 # scenario["goal"][0] - MINX,
536 # scenario["goal"][1] - MINY,
542 # Plot `path` and `max_path`.
543 if (sc2 and "opath" in sc2 and isinstance(sc2["opath"], list)
544 and len(sc2["opath"]) > 0):
545 plt.plot(*plot_nodes(sc2["opath"]), color="orange", linestyle="dotted")
546 if (sc2 and "path" in sc2 and isinstance(sc2["path"], list)
547 and len(sc2["path"]) > 0):
548 plt.plot(*plot_nodes(sc2["path"]), color="orange")
549 if ("opath" in scenario and isinstance(scenario["opath"], list)
550 and len(scenario["opath"]) > 0):
552 *plot_nodes(scenario["opath"]),
557 if ("path" in scenario and isinstance(scenario["path"], list)
558 and len(scenario["path"]) > 0):
559 plt.plot(*plot_nodes(scenario["path"]), color="blue")
560 for p in scenario["path"]:
561 #plt.plot(*plot_car(p), color="blue")
563 #cc = plot_car_corners(p)
564 #plt.plot(cc[0][0], cc[1][0], color="red", marker=".", ms=1)
565 #plt.plot(cc[0][1], cc[1][1], color="red", marker=".", ms=1)
566 #plt.plot(cc[0][2], cc[1][2], color="red", marker=".", ms=1)
567 #plt.plot(cc[0][3], cc[1][3], color="red", marker=".", ms=1)
568 if "ispath" in scenario and len(scenario["ispath"]) > 0:
569 plt.plot(*plot_nodes(scenario["ispath"]), color="green")
570 for p in scenario["ispath"]:
571 #plt.plot(*plot_car(p), color="green")
573 #cc = plot_car_corners(p)
574 #plt.plot(cc[0][0], cc[1][0], color="red", marker=".", ms=1)
575 #plt.plot(cc[0][1], cc[1][1], color="red", marker=".", ms=1)
576 #plt.plot(cc[0][2], cc[1][2], color="red", marker=".", ms=1)
577 #plt.plot(cc[0][3], cc[1][3], color="red", marker=".", ms=1)
579 # If there are possible starts specified, you may print and plot them.
580 #if "starts" in scenario and len(scenario["starts"]) > 0:
581 # print("possible starts:")
582 # for p in scenario["starts"]:
583 # plt.plot(*p, color="red", marker="+", ms=12)
584 # print(" {}".format(p))
586 # For the Last Maneuver figure from the paper, use:
587 # - `init2` -- orange
588 #plt.plot(*plot_car(scenario["init2"]), color="orange")
590 # scenario["init2"][0] - MINX,
591 # scenario["init2"][1] - MINY,
596 # - `goal2` -- orange
597 #plt.plot(*plot_car(scenario["goal2"]), color="orange")
599 # scenario["goal2"][0] - MINX,
600 # scenario["goal2"][1] - MINY,
605 # - `goal2` -- middle (orange)
606 #plt.plot(*plot_car(scenario["goals"][0]), color="orange")
608 # scenario["goal2"][0] - MINX,
609 # scenario["goal2"][1] - MINY,
615 #plt.plot(*plot_car(scenario["init1"]), color="green")
617 # scenario["init1"][0] - MINX,
618 # scenario["init1"][1] - MINY,
624 #plt.plot(*plot_car(scenario["goal1"]), color="green")
626 # scenario["goal1"][0] - MINX,
627 # scenario["goal1"][1] - MINY,
633 # The `scenario` may also include:
634 # - `last` -- not sure what this is, see the source code. Maybe overlaps
636 # - `last1` -- used to demonstrate In-Slot Planner (was Parking Slot
638 # - `last2` -- used to demonstrate In-Slot Planner (was Parking Slot
640 # - `max_orig_path` -- maximum original path. I used this when comparing
641 # original paths but I had to copy the `max_orig_path` by hand from
642 # different scenario result.
643 # - `orig_path` -- the path before the optimization.
644 # - `max_path` -- the maximum path after optimization. Must be copied by
646 # - `path` -- optimized path of the scenario.
648 handles, labels = ax.get_legend_handles_labels()
650 # Uncommnent the following line and comment the plt.show() to store to the
652 plt.savefig("out.pdf", bbox_inches="tight")