-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCoordinateFrames.py
executable file
·302 lines (223 loc) · 9 KB
/
CoordinateFrames.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
302
#!/usr/bin/env python3
import brewer2mpl as b2m
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
class WorldFramePlotter:
def __init__(self, title, origin_name, xlim, ylim, text_offset_scale):
self.text_offset_scale = text_offset_scale
self.fig = plt.figure(figsize=(12, 9))
self.ax = self.fig.add_subplot()
self.colormap = b2m.get_map("Dark2", "qualitative", 8).mpl_colors
self.ax.set_title(title, fontsize=25)
# Hide top and right border lines
self.ax.spines["top"].set_visible(False)
self.ax.spines["right"].set_visible(False)
# Set up x-axis
self.ax.set_xlim(xlim)
self.ax.set_xticks([])
self.ax.set_xlabel(r"$x_{}$ ".format(origin_name), loc="right", fontsize=22)
xcolor = self.colormap[1]
self.ax.spines["bottom"].set_color(xcolor)
self.ax.spines["bottom"].set_linewidth(3)
# Add arrowhead to x-axis
self.ax.plot(xlim[1], ylim[0], ">", ms=16, color=xcolor, clip_on=False)
# Set up y-axis
self.ax.set_ylim(ylim)
self.ax.set_yticks([])
self.ax.set_ylabel(
r"$y_{}$ ".format(origin_name), loc="top", rotation="horizontal", fontsize=22
)
ycolor = self.colormap[0]
self.ax.spines["left"].set_color(ycolor)
self.ax.spines["left"].set_linewidth(3)
# Add arrowhead to y-axis
self.ax.plot(xlim[0], ylim[1], "^", ms=16, color=ycolor, clip_on=False)
# Plot origin
self.ax.plot(0, 0, "ko", zorder=1000, clip_on=False)
self.ax.text(
-1 * 0.04 * xlim[1], -1 * 0.04 * ylim[1], r"$O_{}$".format(origin_name), fontsize=22
)
def print_offset(self, limits):
(lower, upper) = limits
return self.text_offset_scale * (upper - lower)
def add_labeled_point(self, x, y, label):
self.ax.plot(x, y, "ko")
self.ax.text(
x + self.print_offset(self.ax.get_xlim()),
y + self.print_offset(self.ax.get_ylim()),
f"${label}: ({x}, {y})$",
fontsize=18,
)
def add_grid_lines_for_point(self, x, y):
self.ax.plot([0, x], [y, y], "--", lw=1.0, color="black", alpha=0.3)
self.ax.plot([x, x], [0, y], "--", lw=1.0, color="black", alpha=0.3)
def add_coordinate_frame(self, label, rotation, translation, scale, colors, label_origin=True):
(x, y) = translation
rot_rad = rotation * np.pi / 180
R = np.array([[np.cos(rot_rad), -np.sin(rot_rad)], [np.sin(rot_rad), np.cos(rot_rad)]])
(xx, xy) = scale * R @ np.array([1, 0])
(yx, yy) = scale * R @ np.array([0, 1])
self.ax.arrow(x, y, xx, xy, width=0.3, color=colors[0])
self.ax.arrow(x, y, yx, yy, width=0.3, color=colors[1])
self.ax.plot(x, y, "ko", zorder=1000, clip_on=False)
if label_origin:
self.ax.text(
x - self.print_offset(self.ax.get_xlim()),
y - self.print_offset(self.ax.get_ylim()),
r"$O_{}$".format(label),
fontsize=22,
)
self.ax.text(
x + xx + self.print_offset(self.ax.get_xlim()) / 2,
y + xy,
r"$x_{}$".format(label),
fontsize=16,
)
self.ax.text(
x + yx,
y + yy + self.print_offset(self.ax.get_ylim()) / 2,
r"$y_{}$".format(label),
fontsize=16,
)
def show(self):
self.fig.show()
def cartesian_grid():
plot = WorldFramePlotter("Points on a Cartesian grid", " ", (0, 5), (0, 5), 0.04)
plot.add_labeled_point(2, 2, "p")
plot.add_grid_lines_for_point(2, 2)
plot.add_labeled_point(3, 4, "q")
plot.add_grid_lines_for_point(3, 4)
plot.add_labeled_point(4, 1, "r")
plot.add_grid_lines_for_point(4, 1)
plot.show()
def world_frame_1():
plot = WorldFramePlotter(
"World frame with constituent local frames", "W", (0, 100), (0, 100), 0.04
)
frame1_colors = (plot.colormap[3], plot.colormap[2])
plot.add_coordinate_frame("A", 40, (25, 65), 20, frame1_colors)
frame2_colors = (plot.colormap[5], plot.colormap[4])
plot.add_coordinate_frame("B", -30, (73, 32), 5, frame2_colors)
plot.add_labeled_point(60, 70, "p_W")
plot.show()
def world_frame_translation():
plot = WorldFramePlotter(
"Translation between coordinate frames A and B", "W", (0, 100), (0, 100), 0.04
)
frame1_colors = (plot.colormap[3], plot.colormap[2])
origin_A = np.array((25, 65))
plot.add_coordinate_frame("A", 0, origin_A, 10, frame1_colors)
frame2_colors = (plot.colormap[5], plot.colormap[4])
origin_B = np.array((73, 32))
plot.add_coordinate_frame("B", 0, origin_B, 10, frame2_colors)
coords = np.array([origin_A, origin_B])
(dx, dy) = origin_B - origin_A
plot.ax.arrow(
origin_A[0],
origin_A[1],
dx - 2,
dy + 1,
ls=(0, (10, 10)),
lw=0.5,
head_width=2,
head_length=2,
color="black",
alpha=0.4,
)
midpoint = coords.mean(axis=0)
offset = plot.print_offset(plot.ax.get_ylim())
plot.ax.text(midpoint[0], midpoint[1] + offset, r"$T^B_A$", alpha=0.4, fontsize=22)
plot.show()
def world_frame_rotation():
plot = WorldFramePlotter(
"Rotation between coordinate frames A and B", "W", (0, 100), (0, 100), 0.04
)
frame1_colors = (plot.colormap[3], plot.colormap[2])
origin = np.array((50, 50))
plot.add_coordinate_frame("A", 0, origin, 20, frame1_colors)
frame2_colors = (plot.colormap[5], plot.colormap[4])
plot.add_coordinate_frame("B", 33, origin, 20, frame2_colors, False)
arcx = mpatches.Arc(origin, 25, 25, theta2=33, ls=(0, (5, 5)), alpha=0.4)
plot.ax.add_patch(arcx)
arcy = mpatches.Arc(origin, 25, 25, theta1=90, theta2=123, ls=(0, (5, 5)), alpha=0.4)
plot.ax.add_patch(arcy)
plot.ax.text(63, 53, r"$\theta$", fontsize=18, alpha=0.4)
plot.ax.text(45, 63, r"$\theta$", fontsize=18, alpha=0.4)
print_offset = plot.print_offset(plot.ax.get_xlim())
plot.ax.text(origin[0] - print_offset, origin[1] - 2.5 * print_offset, r"$O_B$", fontsize=22)
plot.show()
def world_frame_scale():
plot = WorldFramePlotter(
"Scale between coordinate frames A and B", "W", (0, 100), (0, 100), 0.04
)
frame1_colors = (plot.colormap[3], plot.colormap[2])
origin1 = np.array((30, 50))
plot.add_coordinate_frame("A", 0, origin1, 20, frame1_colors)
frame2_colors = (plot.colormap[5], plot.colormap[4])
origin2 = np.array((70, 50))
plot.add_coordinate_frame("B", 0, origin2, 4, frame2_colors)
plot.show()
def transform_as_separate_operations(sx, sy, theta, tx, ty, x_A, y_A):
"""
Transforms a point (x, y) by the translation (tx, ty), rotation (theta) and
scale (sx, sy) factors, by formulating the transformation according to:
S = [[sx 0]
[ 0 sy]]
R = [[cos(theta) -sin(theta)]
[sin(theta) cos(theta)]]
T = [tx ty]
Inputs:
sx, sy - Scale factors for the x and y axes, respectively
theta - Rotation (counter-clockwise positive) in degrees between the two
coordinate frames.
tx, ty - Translation along x and y axes, respectively
x, y - Point coordinates to be transformed
Returns:
A tuple (x_B, y_B) representing the transformed point
"""
scale = np.array([[sx, 0], [0, sy]])
theta_in_radians = theta / 180 * np.pi
rotation = np.array(
[
[np.cos(theta_in_radians), -np.sin(theta_in_radians)],
[np.sin(theta_in_radians), np.cos(theta_in_radians)],
]
)
translation = np.array([tx, ty])
p_A = np.array([x_A, y_A])
[x_B, y_B] = scale @ rotation @ p_A + translation
return (x_B, y_B)
def transform_as_gamma_matrix(sx, sy, theta, tx, ty, x_A, y_A):
"""
Transforms a point (x, y) by the translation (tx, ty), rotation (theta) and
scale (sx, sy) factors, by formulating the transformation according to:
Gamma = [[sx*cos(theta) -sx*sin(theta) tx],
[sy*sin(theta) sy*sin(theta) ty],
[ 0 0 1]]
Inputs:
sx, sy - Scale factors for the x and y axes, respectively
theta - Rotation (counter-clockwise positive) in degrees between the two
coordinate frames.
tx, ty - Translation along x and y axes, respectively
x, y - Point coordinates to be transformed
Returns:
A tuple (x_B, y_B) representing the transformed point
"""
theta_in_radians = theta / 180 * np.pi
Gamma = np.array(
[
[sx * np.cos(theta_in_radians), -sx * np.sin(theta_in_radians), tx],
[sy * np.sin(theta_in_radians), sy * np.cos(theta_in_radians), ty],
[0, 0, 1],
]
)
p_A = np.array([x_A, y_A, 1])
[x_B, y_B, _] = Gamma @ p_A
return (x_B, y_B)
if __name__ == "__main__":
cartesian_grid()
world_frame_1()
world_frame_translation()
world_frame_rotation()
world_frame_scale()