This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot.py
103 lines (82 loc) · 2.82 KB
/
plot.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
#!/usr/bin/python
"""
Plot track data
usage:
./plot.py <track XML> [<log file> [<log file> ...]]
"""
import sys
from math import sin, cos, pi
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
from track import Track
from packets import sensors_gen
# Notes:
#
# The arc segments are interpolated via Bezier curves. See
# http://www.whizkidtech.redprince.net/bezier/circle/
# or
# http://www.tinaja.com/glib/ellipse4.pdf
# for explanation of recommended scale factors
# Bezier curve - length of control points fro PI/2 circle split
KAPPA = 0.5522847498
def track_border(track, left_offset):
verts, codes = [], []
x, y, heading = (0, 0, 0)
verts.append((x, y, heading))
codes.append(Path.MOVETO)
for segment in track.segments:
if segment.arc is not None:
if segment.arc > 0:
radius = segment.radius - left_offset
else:
radius = segment.radius + left_offset
scale = abs(segment.arc) * radius * KAPPA * 2.0 / pi
verts.append((x + scale * cos(heading),
y + scale * sin(heading),
heading))
codes.append(Path.CURVE4)
x, y, heading = segment.step((x, y, heading))
if segment.arc is None:
codes.append(Path.LINETO)
else:
codes.append(Path.CURVE4)
verts.append((x - scale * cos(heading),
y - scale * sin(heading),
heading))
codes.append(Path.CURVE4)
verts.append((x, y, heading))
vertsL = [(x + left_offset*cos(a + pi/2.0), y + left_offset*sin(a + pi/2.0))
for (x, y, a) in verts]
return Path(vertsL, codes)
def draw(track):
pathL = track_border(track, track.width/2.0)
pathR = track_border(track, -track.width/2.0)
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
patch = patches.PathPatch(pathL, facecolor='white')
ax.add_patch(patch)
patch = patches.PathPatch(pathR, facecolor='none')
ax.add_patch(patch)
margin = 100 # in meters
bbox = pathL.get_extents()
bbox.update_from_data_xy(pathR.get_extents().get_points(), ignore=False)
(xmin, ymin), (xmax, ymax) = bbox.get_points()
ax.set_xlim(xmin - margin, xmax + margin)
ax.set_ylim(ymin - margin, ymax + margin)
return fig
if __name__ == "__main__":
if len(sys.argv) < 2:
print(__doc__)
sys.exit(2)
filename = sys.argv[1]
track = Track.from_xml_file(filename)
fig = draw(track)
for filename in sys.argv[2:]:
x, y = [], []
for sensors in sensors_gen(filename):
x.append(sensors.pos3d[0])
y.append(sensors.pos3d[1])
plt.plot(x, y, '--')
plt.show()
# vim: expandtab sw=4 ts=4