python中基于Euler矩阵的物体旋转

2024-03-28 10:30:53 发布

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

我试图用欧拉矩阵旋转一个滚动体(或圆柱体)。为此,我使用以下函数。在

def roll( R, zi, zf, Euler):

    # R is the radius of the cylinder
    # t is the angle which is running from 0 to 2*pi
    # zi is the lower z co-ordinate of cylinder
    # zf is the upper z co-ordinate of cylinder
    t = np.arange( 0, 2* np.pi + 0.1, 0.1)
    z = np.array([zi, zf])    
    t, z = np.meshgrid(t, z)
    p, q = t.shape
    r = R* np.ones([p,q], float)
    # polar co-ordinates to Cartesian co-ordinate
    x, y, z = pol2cart(r,t,z)

    # Euler rotation
    rot0 = np.array([x[0,:], y[0,:], z[0,:]])
    rot1 = np.array([x[1,:], y[1,:], z[1,:]])
    # mult is the matrix multiplication
    mat0 = mult( Euler, rot0)
    mat1 = mult( Euler, rot1)
    #
    x[0,:] = mat0[0,:]
    y[0,:] = mat0[1,:]
    z[0,:] = mat0[2,:]
    #
    x[1,:] = mat1[0,:]
    y[1,:] = mat1[1,:]
    z[1,:] = mat1[2,:]
    #
    return x, y, z

当Euler旋转矩阵为Euler = np.array([[1,0,0],[0,1,0],[0,0,1]])且函数的输入为x, y, z = roll(1, -2, 2, np.array([[1,0,0],[0,1,0],[0,0,1]]) )时,该函数工作良好。使用ax.plot_surface(x,y,z)我得到了下面的图。 enter image description here

但是当我试图用欧拉矩阵旋转物体时,我得到了意想不到的结果。在

enter image description here

这里的旋转是45度,这是正确的,但对象的形状不合适。在


Tags: ofthe函数isnp矩阵arrayeuler
1条回答
网友
1楼 · 发布于 2024-03-28 10:30:53

你就快到了。几件事:

实际上,您使用的是cylindrical coordinates而不是球形的。我没有检查numpy是否有一个cyl2cat,但这也不是很难自己写的:

def cyl2cat(r, theta, z):
    return (r*np.cos(theta), r*np.sin(theta), z)

对于旋转,我不太明白你为什么要分两步走。可以使用numpy的^{}来旋转网格

^{pr2}$

并重塑旋转坐标:

x_rot = rot[0,:].reshape(x.shape)
# ...

组合起来

import numpy as np

def cyl2cart(r,theta,z):
    return (r*np.cos(theta), r*np.sin(theta), z)

def roll( R, zi, zf, Euler):               
    t = np.arange( 0, 2* np.pi + 0.1, 0.1)          
    z = np.array([zi, zf])                          
    t, z = np.meshgrid(t, z)                        
    p, q = t.shape                                  
    r = R* np.ones([p,q], float)                    
    # cylindrical coordinates to Cartesian coordinate   
    x, y, z = cyl2cart(r,t,z)                       

    # Euler rotation                                
    rot = np.dot(                                                
        Euler,                                            
        np.array([x.ravel(), y.ravel(), z.ravel()]) 
    )                                               
    x_rot = rot[0,:].reshape(x.shape)               
    y_rot = rot[1,:].reshape(y.shape)               
    z_rot = rot[2,:].reshape(z.shape)               
    return x_rot, y_rot, z_rot  

现在roll执行您想要的操作:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax=fig.add_subplot(111, projection='3d')
x,y,z=roll(1,-2,2,np.array([[1,0,0],[0,1/np.sqrt(2),-1/np.sqrt(2)],[0,1/np.sqrt(2),1/np.sqrt(2)]]))
ax.plot_surface(x,y,z)
plt.show()

等等:

enter image description here

注意,轴的纵横比是不一样的,这就是为什么圆柱体出现椭圆曲率的原因。Axes3D中获得相等的轴不是简单的,但是可以通过绘制一个立方体边界框(几乎是从this复制/粘贴)来实现的

ax.set_aspect('equal')    
max_range = np.array([x.max()-x.min(), y.max()-y.min(), z.max()-z.min()]).max()
Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(x.max()+x.min())
Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(y.max()+y.min())
Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(z.max()+z.min())
# Comment or uncomment following both lines to test the fake bounding box:
for xb, yb, zb in zip(Xb, Yb, Zb):
   ax.plot([xb], [yb], [zb], 'w')

只需在ax.plot_surface(...之后添加此元素,圆柱体将显示为圆形曲率。在

相关问题 更多 >