def rotation_matrix(a, b):
"""
Create rotation matrix to rotate vector a into b.
After http://math.stackexchange.com/a/476311
Parameters
----------
a,b
xyz-vectors
"""
v = np.cross(a, b)
sin = np.linalg.norm(v)
if sin == 0:
return np.identity(3)
cos = np.vdot(a, b)
vx = np.mat([[0, -v[2], v[1]], [v[2], 0., -v[0]], [-v[1], v[0], 0.]])
R = np.identity(3) + vx + vx * vx * (1 - cos) / (sin ** 2)
return R
评论列表
文章目录