如何找到2个3D矢量之间的3个欧拉角?
当我有一个Vector并且我想要获得它的旋转时,通常可以使用此链接:Calculate rotations to look at a 3D point?
但是如何根据彼此计算它们呢?
答案 0 :(得分:10)
正如其他人已经指出的那样,你的问题应该修改。让我们调用你的向量a
和b
。我认为length(a)==length(b) > 0
否则我无法回答这个问题。
计算向量v = a x b
的{{3}}; v
给出了旋转的轴。通过计算cross product,您可以获得应该使用cos(angle)=dot(a,b)/(length(a)length(b))
旋转的角度的余弦,并使用acos
来唯一地确定角度(@Archie谢谢你指出我早先的错误)。此时,您拥有旋转的轴角度表示。
剩下的工作是将此表示转换为您要查找的表示:欧拉角。正如您所发现的那样dot product是一种方法。您必须在v = [ 0, 0, 0]
时处理退化情况,即角度为0或180度时。
我个人不喜欢Euler角度,它们搞砸了你应用的稳定性,它们不适合插值,另见
答案 1 :(得分:1)
首先,您必须从向量2中减去向量1,以便相对于向量1获得向量2。使用这些值,您可以计算欧拉角。
为了直观地理解从矢量到欧拉的计算,我们设想一个半径为1且原点位于其中心的球体。矢量表示3D坐标中其表面上的点。此点也可以由球面2D坐标定义:纬度和经度,俯仰和偏航。
按顺序“滚动< - 俯仰< - 偏航”计算可以按如下方式进行:
要计算偏航,请考虑象限计算两个平面轴(x和z)的切线。
yaw = atan2(x, z) *180.0/PI;
俯仰是完全相同的,但是当它的平面与偏航一起旋转时,“相邻”在两个轴上。为了找到它的长度,我们将不得不使用毕达哥拉斯定理。
float padj = sqrt(pow(x, 2) + pow(z, 2));
pitch = atan2(padj, y) *180.0/PI;
注意:
答案 2 :(得分:0)
我花了很多时间找到这个答案,所以我现在想与您分享。
首先,您需要找到旋转矩阵,然后使用scipy
可以轻松找到所需的角度。
没有捷径可做。 所以我们先声明一些函数...
import numpy as np
from scipy.spatial.transform import Rotation
def normalize(v):
return v / np.linalg.norm(v)
def find_additional_vertical_vector(vector):
ez = np.array([0, 0, 1])
look_at_vector = normalize(vector)
up_vector = normalize(ez - np.dot(look_at_vector, ez) * look_at_vector)
return up_vector
def calc_rotation_matrix(v1_start, v2_start, v1_target, v2_target):
"""
calculating M the rotation matrix from base U to base V
M @ U = V
M = V @ U^-1
"""
def get_base_matrices():
u1_start = normalize(v1_start)
u2_start = normalize(v2_start)
u3_start = normalize(np.cross(u1_start, u2_start))
u1_target = normalize(v1_target)
u2_target = normalize(v2_target)
u3_target = normalize(np.cross(u1_target, u2_target))
U = np.hstack([u1_start.reshape(3, 1), u2_start.reshape(3, 1), u3_start.reshape(3, 1)])
V = np.hstack([u1_target.reshape(3, 1), u2_target.reshape(3, 1), u3_target.reshape(3, 1)])
return U, V
def calc_base_transition_matrix():
return np.dot(V, np.linalg.inv(U))
if not np.isclose(np.dot(v1_target, v2_target), 0, atol=1e-03):
raise ValueError("v1_target and v2_target must be vertical")
U, V = get_base_matrices()
return calc_base_transition_matrix()
def get_euler_rotation_angles(start_look_at_vector, target_look_at_vector, start_up_vector=None, target_up_vector=None):
if start_up_vector is None:
start_up_vector = find_additional_vertical_vector(start_look_at_vector)
if target_up_vector is None:
target_up_vector = find_additional_vertical_vector(target_look_at_vector)
rot_mat = calc_rotation_matrix(start_look_at_vector, start_up_vector, target_look_at_vector, target_up_vector)
is_equal = np.allclose(rot_mat @ start_look_at_vector, target_look_at_vector, atol=1e-03)
print(f"rot_mat @ start_look_at_vector1 == target_look_at_vector1 is {is_equal}")
rotation = Rotation.from_matrix(rot_mat)
return rotation.as_euler(seq="xyz", degrees=True)
将XYZ欧拉旋转角从1个向量找出到另一个向量可能会给您带来多个答案。
假设您正在旋转的是某种形状的look_at_vector
,并且您希望此形状保持不倒置并仍然看着target_look_at_vector
if __name__ == "__main__":
# Example 1
start_look_at_vector = normalize(np.random.random(3))
target_look_at_vector = normalize(np.array([-0.70710688829422, 0.4156269133090973, -0.5720613598823547]))
phi, theta, psi = get_euler_rotation_angles(start_look_at_vector, target_look_at_vector)
print(f"phi_x_rotation={phi}, theta_y_rotation={theta}, psi_z_rotation={psi}")
现在,如果您想对形状进行特定的角色旋转,我的代码也支持该功能!
您只需要将target_up_vector
作为参数即可。
只要确保它与您给出的target_look_at_vector垂直即可。
if __name__ == "__main__":
# Example 2
# look and up must be vertical
start_look_at_vector = normalize(np.array([1, 2, 3]))
start_up_vector = normalize(np.array([1, -3, 2]))
target_look_at_vector = np.array([0.19283590755300162, 0.6597510192626469, -0.7263217228739983])
target_up_vector = np.array([-0.13225754322703182, 0.7509361508721898, 0.6469955018014842])
phi, theta, psi = get_euler_rotation_angles(
start_look_at_vector, target_look_at_vector, start_up_vector, target_up_vector
)
print(f"phi_x_rotation={phi}, theta_y_rotation={theta}, psi_z_rotation={psi}")
答案 3 :(得分:-2)
在MATLAB中获取旋转矩阵非常简单 e.g。
A = [1.353553385, 0.200000003, 0.35]
B = [1 2 3]
[q] = vrrotvec(A,B)
Rot_mat = vrrotvec2mat(q)