-
Notifications
You must be signed in to change notification settings - Fork 5
/
basic.py
470 lines (358 loc) · 12.7 KB
/
basic.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from .utils import ypr_to_R
class Sphere:
'''
Draws a sphere at a given position.
'''
def __init__(self, ax, r, c='b', x0=np.array([0, 0, 0]).T, resolution=20):
'''
Initialize the sphere.
Params:
ax: (matplotlib axis) the axis where the sphere should be drawn
r: (float) radius of the sphere
c: (string) color of the sphere, default 'b'
x0: (3x1 numpy.ndarray) initial position of the sphere, default
is [0, 0, 0]
resolution: (int) resolution of the plot, default 20
Returns:
None
'''
self.ax = ax
self.r = r
self.color = c
self.x0 = x0
self.reso = resolution
def draw(self):
'''
Draw the sphere with the initially defined position when the class was
instantiated.
Args:
None
Returns:
None
'''
vertices = np.linspace(0, 2*np.pi, self.reso+1)
u, v = np.meshgrid(vertices, vertices)
x = self.r * np.cos(u) * np.sin(v) + self.x0[0]
y = self.r * np.sin(u) * np.sin(v) + self.x0[1]
z = self.r * np.cos(v) + self.x0[2]
self.ax.plot_surface(x, y, z, color=self.color)
def draw_at(self, position=np.array([0.0, 0.0, 0.0]).T):
'''
Draw the sphere at a given position.
Args:
position: (3x1 numpy.ndarray) position of the sphere,
default = [0.0, 0.0, 0.0]
Returns:
None
'''
vertices = np.linspace(0, 2*np.pi, self.reso+1)
u, v = np.meshgrid(vertices, vertices)
x = self.r * np.cos(u) * np.sin(v) + position[0]
y = self.r * np.sin(u) * np.sin(v) + position[1]
z = self.r * np.cos(v) + position[2]
self.ax.plot_surface(x, y, z, color=self.color)
class Arrow:
'''
Draws an arrow at a given position, with a given attitude.
'''
def __init__(self, ax, direction, c='b', x0=np.array([0.0, 0.0, 0.0]).T, \
length=1.0):
'''
Initialize the arrow.
Params:
ax: (matplotlib axis) the axis where the arrow should be drawn
direction: (3x1 numpy.ndarray) direction of the arrow
c: (string) color of the arrow, default = 'b'
x0: (3x1 numpy.ndarray) origin of the arrow,
default = [0.0, 0.0, 0.0]
length: (float) length of the arrow, default = 1.0
Returns:
None
'''
self.ax = ax
self.u0 = direction
self.color = c
self.x0 = x0
self.arrow_length = length
def draw(self):
'''
Draw the arrow with the initially defined parameter when the class was
instantiated.
Args:
None
Returns:
None
'''
x = self.x0
u = self.u0
self.ax.quiver(x[0], x[1], x[1], \
u[0], u[1], u[2], \
color=self.color,
length=self.arrow_length, \
normalize=False)
def draw_from_to(self, x=np.array([0.0, 0.0, 0.0]).T, \
u=np.array([1.0, 0.0, 0.0]).T):
'''
Draw the arrow at a given position, with a given direction
Args:
x: (3x1 numpy.ndarray) origin of the arrow,
default = [0.0, 0.0, 0.0]
u: (3x1 numpy.ndarray) direction of the arrow,
default = [1.0, 0.0, 0.0]
Returns:
None
'''
self.ax.quiver(x[0], x[1], x[2], \
u[0], u[1], u[2], \
color=self.color,
length=self.arrow_length, \
normalize=False)
class Line:
'''
Draws a line at a given position, with a given attitude.
'''
def __init__(self, ax, c='b', x0=np.array([0.0, 0.0, 0.0]).T, \
x1=np.array([1.0, 0.0, 0.0]).T):
'''
Initialize the line.
Params:
ax: (matplotlib axis) the axis where the line should be drawn
c: (string) color of the arrow, default = 'b'
x0: (3x1 numpy.ndarray) start of the line,
default = [0.0, 0.0, 0.0]
x1: (3x1 numpy.ndarray) end of the line,
default = [1.0, 0.0, 0.0]
Returns:
None
'''
self.ax = ax
self.color = c
self.x0 = x0
self.x1 = x1
def draw(self):
'''
Draw the line with the initially defined parameter when the class was
instantiated.
Args:
None
Returns:
None
'''
self.ax.plot([self.x0[0], self.x1[0]], \
[self.x0[1], self.x1[1]], \
[self.x0[2], self.x1[2]], \
color=self.color)
def draw_from_to(self, x0=np.array([0.0, 0.0, 0.0]).T, \
x1=np.array([1.0, 0.0, 0.0]).T):
'''
Draw the line between two points.
Args:
x0: (3x1 numpy.ndarray) start of the line,
default = [0.0, 0.0, 0.0]
x1: (3x1 numpy.ndarray) end of the line,
default = [1.0, 0.0, 0.0]
Returns:
None
'''
self.ax.plot([x0[0], x1[0]], \
[x0[1], x1[1]], \
[x0[2], x1[2]], \
color=self.color)
class Plane:
'''
Draws a plane at a given position.
'''
def __init__(self, ax, h, w, c='b', x=np.array([0, 0, 0]).T, \
R=np.eye(3), resolution=1):
'''
Initialize the sphere.
Params:
ax: (matplotlib axis) the axis where the plane should be drawn
h = (float): height of the plane (x axis)
w = (float): width of the plane (y axis)
c: (string) color of the plane, default 'b'
x: (3x1 numpy.ndarray) initial position of the plane, default
is [0, 0, 0]
R: (3x1 numpy.ndarray) attitude of the plane,
default = eye(3)
resolution: (int) resolution of the plot, default 1
Returns:
None
'''
self.ax = ax
self.h = h
self.w = w
self.color = c
self.x = x
self.R = R
self.reso = resolution
self.uvw = np.array([])
self.mesh_shape = (1, 1)
def draw(self):
'''
Draw the plane with the initially defined position when the class was
instantiated.
Args:
None
Returns:
None
'''
if self.uvw.size == 0:
reso = self.reso
h = self.h/2.0
w = self.w/2.0
vertices_h = np.linspace(-h, h, reso+1)
vertices_w = np.linspace(-w, w, reso+1)
u, v = np.meshgrid(vertices_h, vertices_w)
w = u*0.0
self.mesh_shape = np.shape(u)
self.uvw = np.array([u.ravel(), v.ravel(), w.ravel()])
# NOTE: for higher resolutions, raveling and reshpaing can be
# expensive. Replace this with np.einsum.
uvw = self.R @ self.uvw
self.ax.plot_surface(
uvw[0, :].reshape(self.mesh_shape) + float(self.x[0]),
uvw[1, :].reshape(self.mesh_shape) + float(self.x[1]),
uvw[2, :].reshape(self.mesh_shape) + float(self.x[2]),
color=self.color)
def draw_at(self, x=np.array([0.0, 0.0, 0.0]).T, R=np.eye(3)):
'''
Draw the plane at a given position and attitude.
Args:
x: (3x1 numpy.ndarray) position of plane,
default = [0.0, 0.0, 0.0]
R: (3x1 numpy.ndarray) attitude of the plane,
default = eye(3)
Returns:
None
'''
if self.uvw.size == 0:
reso = self.reso
h = self.h/2.0
w = self.w/2.0
vertices_h = np.linspace(-h, h, reso+1)
vertices_w = np.linspace(-w, w, reso+1)
u, v = np.meshgrid(vertices_h, vertices_w)
w = u*0.0
self.mesh_shape = np.shape(u)
self.uvw = np.array([u.ravel(), v.ravel(), w.ravel()])
# NOTE: for higher resolutions, raveling and reshpaing can be
# expensive. Replace this with np.einsum.
uvw = R @ self.uvw
self.ax.plot_surface(
uvw[0, :].reshape(self.mesh_shape) + float(x[0]),
uvw[1, :].reshape(self.mesh_shape) + float(x[1]),
uvw[2, :].reshape(self.mesh_shape) + float(x[2]),
color=self.color)
class Cube:
'''
Draws a cube at a given position.
'''
def __init__(self, ax, dimensions, c='b', x=np.array([0, 0, 0]).T, \
R=np.eye(3), resolution=10):
'''
Initialize the cube.
Params:
ax: (matplotlib axis) the axis where the cube should be drawn
dimensions = (3x1 numpy.ndarray): dimensions along each axis
c: (string) color of the cube, default 'b'
x: (3x1 numpy.ndarray) initial position of the cube, default
is [0, 0, 0]
R: (3x1 numpy.ndarray) attitude of the cube,
default = eye(3)
resolution: (int) resolution of the plot, default 10
Returns:
None
'''
self.ax = ax
self.d1 = dimensions[0]
self.d2 = dimensions[1]
self.d3 = dimensions[2]
self.color = c
self.x = x
self.R = R
self.reso = resolution
theta = np.pi / 2.0
self.pt1 = np.array([self.d1/2.0, 0.0, 0.0])
self.R1 = ypr_to_R([0.0, theta, 0.0])
self.p1 = Plane(self.ax, self.d3, self.d2, 'r', \
self.pt1, self.R1)
self.pt2 = np.array([-self.d1/2.0, 0.0, 0.0])
self.R2 = ypr_to_R([0.0, -theta, 0.0])
self.p2 = Plane(self.ax, self.d3, self.d2, 'r', \
self.pt2, self.R2)
self.pt3 = np.array([0.0, self.d2/2.0, 0.0])
self.R3 = ypr_to_R([0.0, 0.0, -theta])
self.p3 = Plane(self.ax, self.d1, self.d3, 'r', \
self.pt3, self.R3)
self.pt4 = np.array([0.0, -self.d2/2.0, 0.0])
self.R4 = ypr_to_R([0.0, 0.0, theta])
self.p4 = Plane(self.ax, self.d1, self.d3, 'r', \
self.pt4, self.R4)
self.pt5 = np.array([0.0, 0.0, self.d3/2.0])
self.R5 = np.eye(3)
self.p5 = Plane(self.ax, self.d1, self.d2, 'r', \
self.pt5, self.R5)
self.pt6 = np.array([0.0, 0.0, -self.d3/2.0])
self.R6 = np.eye(3)
self.p6 = Plane(self.ax, self.d1, self.d2, 'r', \
self.pt6, self.R6)
def draw(self):
'''
Draw the cube with the initially defined position when the class was
instantiated.
Args:
None
Returns:
None
'''
self.p1.draw()
self.p2.draw()
self.p3.draw()
self.p4.draw()
self.p5.draw()
self.p6.draw()
def draw_at(self, x=np.array([0.0, 0.0, 0.0]).T, R=np.eye(3)):
'''
Draw the camera at a given point and attitude.
Args:
x: (3x1 numpy.ndarray) position of camera,
default = [0.0, 0.0, 0.0]
R: (3x1 numpy.ndarray) attitude of the camera,
default = eye(3)
Returns:
None
'''
raise NotImplementedError('This has not been implemented correctly')
# self.p1.draw_at(x + R@self.R1@self.pt1, R@self.R1)
# self.p2.draw_at(x + R@self.R2@self.pt2, R@self.R2)
# self.p3.draw_at(x + R@self.R3@self.pt3, R@self.R3)
# self.p4.draw_at(x + R@self.R4@self.pt4, R@self.R4)
# self.p5.draw_at(x + R@self.R5@self.pt5, R@self.R5)
# self.p6.draw_at(x + R@self.R6@self.pt6, R@self.R6)
self.p1.draw_at(x + self.pt1, R@self.R1)
self.p2.draw_at(x + self.pt2, R@self.R2)
self.p3.draw_at(x + self.pt3, R@self.R3)
self.p4.draw_at(x + self.pt4, R@self.R4)
self.p5.draw_at(x + self.pt5, R@self.R5)
self.p6.draw_at(x + self.pt6, R@self.R6)
if __name__ == '__main__':
# Initiate the plot
plt.style.use('seaborn')
fig = plt.figure()
ax = fig.gca(projection='3d')
# s1 = Sphere(ax, 1)
# s1.draw()
# R = ypr_to_R([0, 0, np.pi/2.0])
# p1 = Plane(ax, 3, 2, 'r', [0, 0, 1], R)
# p1.draw()
c1 = Cube(ax, [3, 4, 5])
# c1.draw_at([1,0,0], ypr_to_R([0,0,0]))
c1.draw_at([0,0,0], ypr_to_R([np.pi/4,0,0]))
ax.set_xlim([-5, 5])
ax.set_ylim([-5, 5])
ax.set_zlim([-5, 5])
plt.show()