| 1 |
from ._io import pyplot_to_numpy |
| 2 |
from .external import transformations as tf |
| 3 |
|
| 4 |
|
| 5 |
def plot_trajectory( |
| 6 |
transforms, |
| 7 |
is_relative=False, |
| 8 |
mode="xz", |
| 9 |
style="b.", |
| 10 |
axis=True, |
| 11 |
): |
| 12 |
"""Plot the trajectory using transform matrices |
| 13 |
|
| 14 |
Parameters |
| 15 |
---------- |
| 16 |
transforms: numpy.ndarray |
| 17 |
transform matrices with the shape of [N, 4, 4] |
| 18 |
where N is the # of poses. |
| 19 |
is_relative: bool |
| 20 |
True for relative poses. default: False. |
| 21 |
mode: str |
| 22 |
x and y axis of trajectory. default: 'xz' following kitti format. |
| 23 |
style: str |
| 24 |
style of ploting, default: 'b.' |
| 25 |
axis: bool |
| 26 |
False to disable axis. |
| 27 |
|
| 28 |
Returns |
| 29 |
------- |
| 30 |
dst: numpy.ndarray |
| 31 |
trajectory |
| 32 |
|
| 33 |
""" |
| 34 |
import matplotlib.pyplot as plt |
| 35 |
|
| 36 |
if is_relative: |
| 37 |
for i in range(1, len(transforms)): |
| 38 |
transforms[i] = transforms[i - 1].dot(transforms[i]) |
| 39 |
|
| 40 |
if len(mode) != 2 and all(x in "xyz" for x in mode): |
| 41 |
raise ValueError("Unsupported mode: {}".format(mode)) |
| 42 |
|
| 43 |
x = [] |
| 44 |
y = [] |
| 45 |
index_x = "xyz".index(mode[0]) |
| 46 |
index_y = "xyz".index(mode[1]) |
| 47 |
for T in transforms: |
| 48 |
translate = tf.translation_from_matrix(T) |
| 49 |
x.append(translate[index_x]) |
| 50 |
y.append(translate[index_y]) |
| 51 |
|
| 52 |
|
| 53 |
backend = plt.get_backend() |
| 54 |
plt.switch_backend("agg") |
| 55 |
|
| 56 |
plt.plot(x, y, style) |
| 57 |
|
| 58 |
if not axis: |
| 59 |
plt.axis("off") |
| 60 |
|
| 61 |
dst = pyplot_to_numpy() |
| 62 |
plt.close() |
| 63 |
|
| 64 |
|
| 65 |
plt.switch_backend(backend) |
| 66 |
|
| 67 |
return dst |