在Python中用Euler角旋转三维对象

2024-09-28 05:16:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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中的euler角旋转下面定义的长方体吗?在

中心=(2.1,-0.1,0.757761) 长度=0.3,宽度=0.4,高度=0.1,均以米为单位。 按照附图中的代码。 例如,沿x、y、z方向,Euler角度为0,0120。在

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

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

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

提前谢谢!在


Tags: thetoalphasizeplotnponesax

热门问题