【问题标题】:Rotate 3D object with Euler angles in Python在 Python 中使用欧拉角旋转 3D 对象
【发布时间】:2019-10-13 09:23:33
【问题描述】:

See this picture. In this the cuboid has to rotate along the other axes that were marked in the picture but it stays in the same axis x,y,zThe image attached gives the code of the cuboid.

##defining to plot the cuboid
def plot_cuboid(center, size):
    """
   Create a data array for cuboid plotting.
   ============= ================================================
   Argument      Description
   ============= ================================================
   center        center of the cuboid, triple
   size          size of the cuboid, triple, (x_length,y_width,z_height)
   :type size: tuple, numpy.array, list
   :param size: size of the cuboid, triple, (x_length,y_width,z_height)
   :type center: tuple, numpy.array, list
   :param center: center of the cuboid, triple, (x,y,z)
   """

    # suppose axis direction: x: to left; y: to inside; z: to upper
    # get the (left, outside, bottom) point
    ox, oy, oz = center
    l, w, h = size

    ##defining the points
    x = np.linspace(ox-l/2,ox+l/2,num=10)
    y = np.linspace(oy-w/2,oy+w/2,num=10)
    z = np.linspace(oz-h/2,oz+h/2,num=10)

    ## defining surfaces and extrude them
    x1, z1 = np.meshgrid(x, z)
    y11 = np.ones_like(x1)*(oy-w/2)
    y12 = np.ones_like(x1)*(oy+w/2)
    x2, y2 = np.meshgrid(x, y)
    z21 = np.ones_like(x2)*(oz-h/2)
    z22 = np.ones_like(x2)*(oz+h/2)
    y3, z3 = np.meshgrid(y, z)
    x31 = np.ones_like(y3)*(ox-l/2)
    x32 = np.ones_like(y3)*(ox+l/2)

    ax = fig.gca(projection='3d') ##plot the project cuboid

    #plot outside surface
    ax.plot_surface(x1, y11, z1, color='red', rstride=1, cstride=1, alpha=0.6)
    #plot inside surface
    ax.plot_surface(x1, y12, z1, color='white', rstride=1, cstride=1, alpha=0.6)
    #plot bottom surface
    ax.plot_surface(x2, y2, z21, color='blue', rstride=1, cstride=1, alpha=0.6)
    #plot upper surface
    ax.plot_surface(x2, y2, z22, color='black', rstride=1, cstride=1, alpha=0.6)
    #plot left surface
    ax.plot_surface(x31, y3, z3, color='green', rstride=1, cstride=1, alpha=0.6)
    #plot right surface
    ax.plot_surface(x32, y3, z3, color='pink', rstride=1, cstride=1, alpha=0.6)

    ## Add title 
    plt.title('Plot_for_PSM', fontsize=20)

    ##labelling the axes
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')

`

我的问题是,我们可以用python中的欧拉角旋转下面定义的长方体吗?

中心 =(2.1,-0.1,0.757761) 长度=0.3,宽度=0.4,高度=0.1,均以米为单位。 根据附图中的代码。 例如沿 x,y,z 方向的欧拉角为 0,0,120。

我做了一些程序来用欧拉角旋转一个长方体。但是在达到欧拉角之后,如何旋转长方体是我的问题。任何人都可以建议或获得此问题的代码吗?

实际上,我有四元数,它们被转换成欧拉角,然后想根据这些欧拉角沿着它们的轴用右手定则旋转。你可以查看我的代码直到我做的地方,也可以建议我做错了什么。

代码中'y'代表与x轴的夹角,'p'代表与y轴的夹角,'r'代表与z轴的夹角。预期的结果是长方体必须沿着这些欧拉角(y,p,r)相对于 x,y,z 轴旋转。

提前致谢!

【问题讨论】:

  • 如果您有 Quarterions,那么您可以使用 scipy.spatial.transform.Rotation 应用轮换。此方法还允许您使用欧拉角或余弦矩阵。
  • 感谢您的建议。如果您编写一个简单的代码并用长方体进行解释,那将非常有帮助。提前谢谢你
  • 文档中有大量示例代码
  • 我制作了旋转程序,但只有轴在旋转,而不是长方体。你能建议一个代码来随轴旋转长方体吗?
  • 编辑您的原始帖子以包含您的新代码,我会看看

标签: python numpy 3d rotation euler-angles


【解决方案1】:

首先计算角的位置向量(xyz 坐标),然后在每个角上使用scipy.spatial.transform.Rotation

【讨论】:

  • 谢谢。我会整理并确认您的其他建议。一个疑问是我已经从身体的中心挤出了长方体。现在,我能找到这个长方体角的位置向量吗?
  • 是否有任何代码可以附加到我之前在这篇文章中编写的长方体代码中,指定长方体角的顶点。?
  • 您可以从中心点和边长计算它们。我把它留给你解决......
  • 即使我计算了角位置,那有什么用。我的意思是我该如何进一步进行
  • 我已经告诉过你两次了。我不会为你做你的工作,这不是这个地方的运作方式。你有没有尝试过我的建议?我猜不会,否则你不会要求我为你做这件事。
猜你喜欢
  • 2019-07-07
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多