dfki-ric / pytransform3d

3D transformations for Python.
https://dfki-ric.github.io/pytransform3d/
Other
632 stars 68 forks source link

Bug in plot_cylinder function. #206

Closed chinaheyu closed 1 year ago

chinaheyu commented 1 year ago

I was recently using plot_cylinder to draw joints for a robot. I found that an RuntimeWarning occurs when the cylindrical z-axis is exactly in the negative direction of the x-axis. Also the cylinder does not appear in axis.

image

Here is my code:

import numpy as np
import matplotlib.pyplot as plt
from pytransform3d.transformations import plot_transform
from pytransform3d.plot_utils import plot_cylinder

a2b = np.array([[0, 0, -1., 0],
                [1, 0, -0, 0],
                [0, -1, 0, 0],
                [0, 0, 0, 1]])

ax = plot_cylinder(length=1.0, radius=0.3, thickness=0.1, A2B=a2b, wireframe=False, alpha=0.2)
plot_transform(ax=ax, A2B=a2b, s=0.3, lw=3)
plt.show()

Here is the output:

...pytransform3d\pytransform3d\plot_utils\_plot_functions.py:216: RuntimeWarning: invalid value encountered in divide
  n1 /= np.linalg.norm(n1)

I tried to change the code in line 212 of file pytransform3d\plot_utils\_plot_functions.py and now the program works as I expected.

image

Here are the changes I made:

# original code of pytransform3d\plot_utils\_plot_functions.py at line 212
    not_axis = np.array([1, 0, 0])
    if (axis == not_axis).all():
        not_axis = np.array([0, 1, 0])

# modified code
    not_axis = np.array([1, 0, 0])
    if np.allclose(axis, not_axis) or np.allclose(-axis, not_axis):
        not_axis = np.array([0, 1, 0])
AlexanderFabisch commented 1 year ago

Hi @chinaheyu ,

thanks for letting me know. If you want, you can make a pull request to the develop branch with your changes. Otherwise I will do it in the next days.

AlexanderFabisch commented 1 year ago

Merged to develop branch and will be part of release 2.2.0. Thanks!