平移坐标,然后旋转X和Y坐标,使Z'指向原点

时间:2019-06-12 15:06:04

标签: coordinates transform wolfram-mathematica

我想建立一个矩阵(将4x4或3x3转换为XYZ-> X'Y'Z'),该矩阵将通过{x,y,z}转换原点,然后旋转矩阵以使Z '轴指向原点{X = 0,Y = 0,Z = 0}

我尝试绕任意轴旋转2圈,然后独立进行平移,但是一旦坐标更改一次,我的X'Y'Z'坐标系的最终位置就会出现错误

1 个答案:

答案 0 :(得分:0)

我通过绕任意轴旋转2次来解决此问题,然后使用@ 0x5453中的LookAt矩阵。随附代码,其中Mv是最终的4x4矩阵:

`def findWorld2Cam([x,y,z]):

#   EXTRINSIC
camtheta=np.arctan(location[0]/location[2])
T=np.eye(4)
T[0:3,3]=-location
u=[0,1,0]
theta=camtheta
Rtheta=np.array([[np.cos(theta) + u[0]**2* (1 - np.cos(theta)), 
   u[0]* u[1]* (1 - np.cos(theta)) - u[2]* np.sin(theta), 
   u[0] *u[2]* (1 - np.cos(theta)) + u[1]* np.sin(theta), 0],
  [u[1] *u[0]* (1 - np.cos(theta)) + u[2]* np.sin(theta), 
   np.cos(theta) + u[1]**2 *(1 - np.cos(theta)), 
   u[1] *u[2]* (1 - np.cos(theta)) - u[0] *np.sin(theta), 0],
  [u[2] *u[0]* (1 - np.cos(theta)) - u[1] *np.sin(theta), 
   u[2]* u[1] *(1 - np.cos(theta)) + u[0]* np.sin(theta), 
   np.cos(theta) + u[2]**2 *(1 - np.cos(theta)), 0], [0, 0, 0, 1]])
#u=np.dot(Rtheta,[1,0,0,1])
u=[1,0,0]
camphi=np.arctan(location[1]/location[2])
phi=-camphi
Rphi=np.array([[np.cos(phi) + u[0]**2* (1 - np.cos(phi)), 
   u[0]* u[1]* (1 - np.cos(phi)) - u[2]* np.sin(phi), 
   u[0] *u[2]* (1 - np.cos(phi)) + u[1]* np.sin(phi), 0],
  [u[1] *u[0]* (1 - np.cos(phi)) + u[2]* np.sin(phi), 
   np.cos(phi) + u[1]**2 *(1 - np.cos(phi)), 
   u[1] *u[2]* (1 - np.cos(phi)) - u[0] *np.sin(phi), 0],
  [u[2] *u[0]* (1 - np.cos(phi)) - u[1] *np.sin(phi), 
   u[2]* u[1] *(1 - np.cos(phi)) + u[0]* np.sin(phi), 
   np.cos(phi) + u[2]**2 *(1 - np.cos(phi)), 0], [0, 0, 0, 1]])
R=np.dot(Rphi,Rtheta)
up1=np.dot(R,[0,1,0,1])

theta=np.arctan(location[0]/location[2])
phi=np.arctan(location[1]/location[2])
l=[0,0,0]
Mt=np.eye(4)
F=l-location
Mt[0:3,3]=-location
forward=F/np.linalg.norm(F)
up=up1[0:3]/np.linalg.norm(up1[0:3])
left=np.cross(up,forward)
Mr=np.eye(4)
Mr[0,0:3]=left
Mr[1,0:3]=up
Mr[2,0:3]=forward
Mv=np.dot(Mr,Mt)
return Mv
`
相关问题