位置+欧拉角到变换矩阵
Position + Euler Angles to Transformation Matrix
我有一个来自 Unity 场景的位置和三个欧拉角:
# position
cam_pos = [-0.1219461, -0.04402884, -1.995793]
# rotation (in degrees)
cam_rot = [-1.261, 176.506, 0.038]
在Unity中坐标系是LHS,欧拉角约定是Z,X,Y。我想知道如何将这个旋转和平移信息变成一个 4x4 的变换矩阵。现在我得到的是:
import numpy as np
import pyquaternion as pyq
cam_pos = [-0.1219461, -0.04402884, -1.995793]
cam_rot = [-1.261, 176.506, 0.038]
qx = pyq.Quaternion(axis=[1, 0, 0], angle=np.radians(cam_rot[0]))
qy = pyq.Quaternion(axis=[0, 1, 0], angle=np.radians(cam_rot[1]))
qz = pyq.Quaternion(axis=[0, 0, 1], angle=np.radians(cam_rot[2]))
qr = qz * qx * qy
trans = np.zeros((4,4))
trans[0:3, 0:3] = rotmat
trans[0:3, 3] = cam_pos
trans[3, 3] = 1
这给了我
[[-9.98140077e-01 -6.63064448e-04 6.09585697e-02 -1.21946100e-01]
[-2.00317624e-03 9.99757601e-01 -2.19254941e-02 -4.40288400e-02]
[-6.09292554e-02 -2.20068252e-02 -9.97899457e-01 -1.99579300e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
但是在将结果与其他一些数据一起绘制后,我很确定我从根本上误解了这里的数学。非常感谢任何帮助!
在 Unity 中,eulerAngles 按以下顺序表示旋转:绕 Z 轴、绕 X 轴、绕 Y 轴。
我们可以通过以矩阵形式应用这些旋转中的每一个来在 4x4 转换矩阵中表示它:
(摘自:https://en.wikibooks.org/wiki/Cg_Programming/Unity/Rotations)
这将为您提供 4x4 矩阵的左上角 3x3 值。
以下是 python 中的一些片段:
import math
# ...
cam_rot_rad = [math.radian(rot) for rot_deg in cam_rot]
x_rad = cam_rot_rad[0]
y_rad = cam_rot_rad[1]
z_rad = cam_rot_rad[2]
rot_z = np.identity(4)
rot_z[0,0] = math.cos(z_rad)
rot_z[0,1] = -math.sin(z_rad)
rot_z[1,0] = math.sin(z_rad)
rot_z[1,1] = math.cos(z_rad)
rot_x = np.identity(4)
rot_x[1,1] = math.cos(x_rad)
rot_x[1,2] = -math.sin(x_rad)
rot_x[2,1] = math.sin(x_rad)
rot_x[2,2] = math.cos(x_rad)
rot_y = np.identity(4)
rot_y[0,0] = math.cos(x_rad)
rot_y[0,2] = math.sin(x_rad)
rot_y[2,0] = -math.sin(x_rad)
rot_y[2,2] = math.cos(x_rad)
# xform = rot_y*rot_x*rot_z
xform = np.dot(rot_y, np.dot(rot_x, rot_z))
# this could also be achieved by multiplying a 4x4 translation matrix
xform[0:3, 3] = cam_pos
我有一个来自 Unity 场景的位置和三个欧拉角:
# position
cam_pos = [-0.1219461, -0.04402884, -1.995793]
# rotation (in degrees)
cam_rot = [-1.261, 176.506, 0.038]
在Unity中坐标系是LHS,欧拉角约定是Z,X,Y。我想知道如何将这个旋转和平移信息变成一个 4x4 的变换矩阵。现在我得到的是:
import numpy as np
import pyquaternion as pyq
cam_pos = [-0.1219461, -0.04402884, -1.995793]
cam_rot = [-1.261, 176.506, 0.038]
qx = pyq.Quaternion(axis=[1, 0, 0], angle=np.radians(cam_rot[0]))
qy = pyq.Quaternion(axis=[0, 1, 0], angle=np.radians(cam_rot[1]))
qz = pyq.Quaternion(axis=[0, 0, 1], angle=np.radians(cam_rot[2]))
qr = qz * qx * qy
trans = np.zeros((4,4))
trans[0:3, 0:3] = rotmat
trans[0:3, 3] = cam_pos
trans[3, 3] = 1
这给了我
[[-9.98140077e-01 -6.63064448e-04 6.09585697e-02 -1.21946100e-01]
[-2.00317624e-03 9.99757601e-01 -2.19254941e-02 -4.40288400e-02]
[-6.09292554e-02 -2.20068252e-02 -9.97899457e-01 -1.99579300e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
但是在将结果与其他一些数据一起绘制后,我很确定我从根本上误解了这里的数学。非常感谢任何帮助!
在 Unity 中,eulerAngles 按以下顺序表示旋转:绕 Z 轴、绕 X 轴、绕 Y 轴。
我们可以通过以矩阵形式应用这些旋转中的每一个来在 4x4 转换矩阵中表示它:
这将为您提供 4x4 矩阵的左上角 3x3 值。
以下是 python 中的一些片段:
import math
# ...
cam_rot_rad = [math.radian(rot) for rot_deg in cam_rot]
x_rad = cam_rot_rad[0]
y_rad = cam_rot_rad[1]
z_rad = cam_rot_rad[2]
rot_z = np.identity(4)
rot_z[0,0] = math.cos(z_rad)
rot_z[0,1] = -math.sin(z_rad)
rot_z[1,0] = math.sin(z_rad)
rot_z[1,1] = math.cos(z_rad)
rot_x = np.identity(4)
rot_x[1,1] = math.cos(x_rad)
rot_x[1,2] = -math.sin(x_rad)
rot_x[2,1] = math.sin(x_rad)
rot_x[2,2] = math.cos(x_rad)
rot_y = np.identity(4)
rot_y[0,0] = math.cos(x_rad)
rot_y[0,2] = math.sin(x_rad)
rot_y[2,0] = -math.sin(x_rad)
rot_y[2,2] = math.cos(x_rad)
# xform = rot_y*rot_x*rot_z
xform = np.dot(rot_y, np.dot(rot_x, rot_z))
# this could also be achieved by multiplying a 4x4 translation matrix
xform[0:3, 3] = cam_pos