]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - scripts/plot_json_objects_scenario.py
Do not use unicode minus
[hubacji1/iamcar2.git] / scripts / plot_json_objects_scenario.py
1 """Plot JSON formatted scenario."""
2 from json import loads
3 from math import cos, pi, sin
4 from math import inf
5 from matplotlib import pyplot as plt
6 from sys import argv, exit
7
8 BCAR_MTR = 10.820
9 BCAR_WB = 2.450
10 BCAR_W = 1.625
11 BCAR_L = 3.760
12 BCAR_SD = 0
13 BCAR_DF = 3.105
14 BCAR_DR = 0.655
15
16 MINX = inf
17 MINY = inf
18
19 def get_scenario(fname):
20     """Load scenario from file."""
21     if fname is None:
22         raise ValueError("File name as argument needed")
23     with open(fname, "r") as f:
24         scenario = loads(f.read())
25     return scenario
26
27 def plot_nodes(nodes=[]):
28     """Return ``xcoords``, ``ycoords`` arrays of nodes to plot.
29
30     Keyword arguments:
31     nodes -- The list of nodes to plot.
32     """
33     xcoords = []
34     ycoords = []
35     for n in nodes:
36         xcoords.append(n[0] - MINX)
37         ycoords.append(n[1] - MINY)
38     return (xcoords, ycoords)
39
40 def plot_car(pose):
41     """Return ``xcoords``, ``ycoords`` arrays of car frame to plot.
42
43     Keyword arguments:
44     pose -- The pose of a car.
45     """
46     lfx = pose[0]
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])
50
51     lf3x = pose[0]
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])
55
56     lrx = pose[0]
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])
60
61     rrx = pose[0]
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])
65
66     rfx = pose[0]
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])
70
71     rf3x = pose[0]
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])
75
76     lfy = pose[1]
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])
80
81     lf3y = pose[1]
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])
85
86     lry = pose[1]
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])
90
91     rry = pose[1]
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])
95
96     rfy = pose[1]
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])
100
101     rf3y = pose[1]
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])
105
106     cfx = pose[0]
107     cfx += BCAR_DF * cos(pose[2])
108     cfx += BCAR_SD * cos(pose[2])
109
110     cfy = pose[1]
111     cfy += BCAR_DF * sin(pose[2])
112     cfy += BCAR_SD * sin(pose[2])
113
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])
117
118 def plot_car_corners(pose):
119     """Return ``xcoords``, ``ycoords`` arrays of car frame corners.
120
121     Keyword arguments:
122     pose -- The pose of a car.
123     """
124     lfx = pose[0]
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])
128
129     lrx = pose[0]
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])
133
134     rrx = pose[0]
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])
138
139     rfx = pose[0]
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])
143
144     lfy = pose[1]
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])
148
149     lry = pose[1]
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])
153
154     rry = pose[1]
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])
158
159     rfy = pose[1]
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])
163
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])
167
168 if __name__ == "__main__":
169     sc2 = None
170     if (len(argv) == 2):
171         SCEN_FILE = argv[1]
172     elif (len(argv) == 3):
173         SCEN_FILE = argv[1]
174         SCEN_FILE2 = argv[2]
175         sc2 = get_scenario(SCEN_FILE2)
176     else:
177         SCEN_FILE = "sc.json"
178
179     scenario = get_scenario(SCEN_FILE)
180
181     # Font size to be approximately the same in the paper:
182     #   - sc1-0: 16
183     #   - sc3-2, Intro: 12
184     #   - sc4-0+: 22
185     plt.rc('axes', unicode_minus=False)
186     plt.rcParams["font.family"] = "cmr10"
187     plt.rcParams["font.size"] = 12
188     plt.rcParams['hatch.linewidth'] = 0.5
189     plt.rcParams['lines.linewidth'] = 1.0
190     fig = plt.figure()
191
192     # here subplot starts
193     ax = fig.add_subplot(111)
194     ax.set_aspect("equal")
195     ax.set_title("Real-world parking scenario")
196     ax.set_xlabel("x [m]")
197     ax.set_ylabel("y [m]")
198
199     # For Possible Entry Points (Possible Entry Configurations) use:
200     #ax.set_xlim([37.6, 45.6])
201     #ax.set_ylim([2.4, 8.0])
202     #ax.set_title("Possible Entry Configurations")
203
204     # For Last Maneuver use:
205     #ax.set_xlim([38, 44])
206     #ax.set_ylim([3.1, 6.9])
207
208     # For Scenario 3-2 detail use:
209     #ax.set_xlim([0, 15])
210     #ax.set_ylim([35, 50])
211
212     # For Real-world parking scenario in Introduction section use:
213     #ax.set_xlim([33, 48.5])
214     #ax.set_ylim([2.4, 10.9])
215     #ax.text(34, 9.2, "Initial configuration", color="red")
216     #ax.text(43.5, 8.5, "Final path", color="blue")
217     #ax.text(48.25, 5.5, "Entry\nconfigurations", color="orange", ha="right")
218     #ax.text(38, 3.8, "Parking\nslot", color="blue", ha="right", backgroundcolor="white")
219     #ax.text(35.2, 5.5, "Goal configuration", color="green")
220
221     # Set min and max to center the plot.
222     MINX = scenario["init"][0]
223     MINY = scenario["init"][1]
224     if "obst" in scenario and  len(scenario["obst"]) > 0:
225         for o in scenario["obst"]:
226             if not o:
227                 continue
228             for n in o:
229                 if n[0] < MINX:
230                     MINX = n[0]
231                 if n[1] < MINY:
232                     MINY = n[1]
233
234     # Plot all the nodes (if exists.)
235     if "nodes_x" in scenario and "nodes_y" in scenario:
236         plt.plot(
237             [x - MINX for x in scenario["nodes_x"]],
238             [y - MINY for y in scenario["nodes_y"]],
239             color="lightgray",
240             marker="o",
241             ms=2,
242             lw=0,
243         )
244     # Plot all the steered2 nodes (if exists.)
245     if "steered2_x" in scenario and "steered2_y" in scenario:
246         plt.plot(
247             [x - MINX for x in scenario["steered2_x"]],
248             [y - MINY for y in scenario["steered2_y"]],
249             color="orange",
250             marker="o",
251             ms=2,
252             lw=0,
253         )
254     # Plot all the steered1 nodes (if exists.)
255     if "steered1_x" in scenario and "steered1_y" in scenario:
256         plt.plot(
257             [x - MINX for x in scenario["steered1_x"]],
258             [y - MINY for y in scenario["steered1_y"]],
259             color="blue",
260             marker="o",
261             ms=2,
262             lw=0,
263         )
264     # Plot obstacles, slot.
265     if "obst" in scenario and len(scenario["obst"]) > 0:
266         for o in scenario["obst"]:
267             if not o:
268                 continue
269             ax.fill(*plot_nodes(o), color="black", fill=False, hatch="//")
270     if "slot" in scenario and len(scenario["slot"]) > 0:
271         plt.plot(*plot_nodes(scenario["slot"][0]), color="blue", linewidth=2)
272         #for s in scenario["slot"]:
273         #    plt.plot(*plot_nodes(s), color="black")
274
275     # For the Possible Entry Configurations from the paper, use:
276     if False and "inits" in scenario:
277         max_i = len(scenario["inits"]) - 1
278         ii = 0
279         i = scenario["inits"][ii]
280         plt.plot(*plot_car(i), color="gray")
281         plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
282         ii = int(max_i / 4)
283         i = scenario["inits"][ii]
284         plt.plot(*plot_car(i), color="gray")
285         plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
286         ii = int(max_i / 2)
287         i = scenario["inits"][ii]
288         plt.plot(*plot_car(i), color="gray")
289         plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
290         ii = int(max_i * 3/4)
291         i = scenario["inits"][ii]
292         plt.plot(*plot_car(i), color="gray")
293         plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
294         ii = max_i
295         i = scenario["inits"][ii]
296         plt.plot(*plot_car(i), color="gray")
297         plt.plot(i[0] - MINX, i[1] - MINY, color="gray", marker="+", ms=12)
298
299     # Plot `init`, `entry`, and `goal` configurations.
300     if "init" in scenario and len(scenario["init"]) == 3:
301         plt.plot(*plot_car(scenario["init"]), color="red")
302         plt.plot(
303             scenario["init"][0] - MINX,
304             scenario["init"][1] - MINY,
305             color="red",
306             marker="+",
307             ms=12
308         )
309     if "entries" in scenario:
310         for e in scenario["entries"]:
311             plt.plot(*plot_car(e), color="orange")
312             plt.plot(
313                 e[0] - MINX,
314                 e[1] - MINY,
315                 color="orange",
316                 marker="+",
317                 ms=12
318             )
319     if "entry" in scenario and len(scenario["entry"]) == 3:
320         plt.plot(*plot_car(scenario["entry"]), color="magenta")
321         plt.plot(
322             scenario["entry"][0] - MINX,
323             scenario["entry"][1] - MINY,
324             color="magenta",
325             marker="+",
326             ms=12
327         )
328     if "entry" in scenario and len(scenario["entry"]) == 4:
329         esc = scenario["entry"]
330         plt.plot(*plot_car([esc[0], esc[1], esc[2]]), color="magenta")
331         plt.plot(*plot_car([esc[0], esc[1], esc[3]]), color="magenta")
332         plt.plot(
333             scenario["entry"][0] - MINX,
334             scenario["entry"][1] - MINY,
335             color="magenta",
336             marker="+",
337             ms=12
338         )
339     if "goal" in scenario and len(scenario["goal"]) == 3:
340         plt.plot(*plot_car(scenario["goal"]), color="green")
341         plt.plot(
342             scenario["goal"][0] - MINX,
343             scenario["goal"][1] - MINY,
344             color="green",
345             marker="+",
346             ms=12
347         )
348
349     # Plot `path` and `max_path`.
350     if sc2 and "path" in sc2 and len(sc2["path"]) > 0:
351         plt.plot(*plot_nodes(sc2["path"]), color="orange")
352     if "orig_path" in scenario and len(scenario["orig_path"]) > 0:
353         plt.plot(
354             *plot_nodes(scenario["orig_path"]),
355             color="blue",
356             linewidth=2,
357             linestyle="dotted",
358         )
359     if "path" in scenario and len(scenario["path"]) > 0:
360         plt.plot(
361             *plot_nodes(scenario["path"]),
362             color="blue",
363             linewidth=2,
364         )
365         for p in scenario["path"]:
366             #plt.plot(*plot_car(p), color="green")
367             pass
368             #cc = plot_car_corners(p)
369             #plt.plot(cc[0][0], cc[1][0], color="red", marker=".", ms=1)
370             #plt.plot(cc[0][1], cc[1][1], color="red", marker=".", ms=1)
371             #plt.plot(cc[0][2], cc[1][2], color="red", marker=".", ms=1)
372             #plt.plot(cc[0][3], cc[1][3], color="red", marker=".", ms=1)
373
374     # If there are possible starts specified, you may print and plot them.
375     #if "starts" in scenario and len(scenario["starts"]) > 0:
376     #    print("possible starts:")
377     #    for p in scenario["starts"]:
378     #        plt.plot(*p, color="red", marker="+", ms=12)
379     #        print(" {}".format(p))
380
381     # For the Last Maneuver figure from the paper, use:
382     #   - `init2` -- orange
383     #plt.plot(*plot_car(scenario["init2"]), color="orange")
384     #plt.plot(
385     #    scenario["init2"][0] - MINX,
386     #    scenario["init2"][1] - MINY,
387     #    color="orange",
388     #    #marker="+",
389     #    ms=12
390     #)
391     #   - `goal2` -- orange
392     #plt.plot(*plot_car(scenario["goal2"]), color="orange")
393     #plt.plot(
394     #    scenario["goal2"][0] - MINX,
395     #    scenario["goal2"][1] - MINY,
396     #    color="orange",
397     #    #marker="+",
398     #    ms=12
399     #)
400     #   - `goal2` -- middle (orange)
401     #plt.plot(*plot_car(scenario["goals"][0]), color="orange")
402     #plt.plot(
403     #    scenario["goal2"][0] - MINX,
404     #    scenario["goal2"][1] - MINY,
405     #    color="orange",
406     #    #marker="+",
407     #    ms=12
408     #)
409     #   - `init1` -- green
410     #plt.plot(*plot_car(scenario["init1"]), color="green")
411     #plt.plot(
412     #    scenario["init1"][0] - MINX,
413     #    scenario["init1"][1] - MINY,
414     #    color="green",
415     #    #marker="+",
416     #    ms=12
417     #)
418     #   - `goal1` -- green
419     #plt.plot(*plot_car(scenario["goal1"]), color="green")
420     #plt.plot(
421     #    scenario["goal1"][0] - MINX,
422     #    scenario["goal1"][1] - MINY,
423     #    color="green",
424     #    #marker="+",
425     #    ms=12
426     #)
427
428     # The `scenario` may also include:
429     #   - `last` -- not sure what this is, see the source code. Maybe overlaps
430     #     with the `goal`.
431     #   - `last1` -- used to demonstrate In-Slot Planner (was Parking Slot
432     #     Planner (PSP.))
433     #   - `last2` -- used to demonstrate In-Slot Planner (was Parking Slot
434     #     Planner (PSP.))
435     #   - `max_orig_path` -- maximum original path. I used this when comparing
436     #     original paths but I had to copy the `max_orig_path` by hand from
437     #     different scenario result.
438     #   - `orig_path` -- the path before the optimization.
439     #   - `max_path` -- the maximum path after optimization. Must be copied by
440     #     hand.
441     #   - `path` -- optimized path of the scenario.
442
443     handles, labels = ax.get_legend_handles_labels()
444
445     # Uncommnent the following line and comment the plt.show() to store to the
446     # file.
447     plt.savefig("out.pdf", bbox_inches="tight")
448     plt.close(fig)