首页 > 解决方案 > 平移坐标,然后在 X 和 Y 中旋转坐标,使 Z' 指向原点

问题描述

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

我试过绕任意轴旋转 2 次,然后独立平移,但是一旦坐标改变一次,我的 X'Y'Z' 坐标系的结果位置就会出现错误

标签: coordinatestransformwolfram-mathematica

解决方案


我通过绕任意轴旋转 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
`

推荐阅读