我在3D空间中有点,我找到了重心(CG),我想沿着通过CG并平行于Z轴的矢量将这些点旋转一定角度(假设为30度)。
我发现CG和定义的轴平行于Z轴穿过CG。我在一些博客上找到的代码段如下所示,但我稍微修改了一下。
def rotation_matrix(angle, direction, point):
"""Return matrix to rotate about axis defined by point and direction.
"""
sina = math.sin(angle)
cosa = math.cos(angle)
direction = unit_vector(direction[:3])
# rotation matrix around unit vector
R = numpy.diag([cosa, cosa, cosa])
R += numpy.outer(direction, direction) * (1.0 - cosa)
direction *= sina
R += numpy.array([[ 0.0, -direction[2], direction[1]],
[ direction[2], 0.0, -direction[0]],
[-direction[1], direction[0], 0.0]])
M = numpy.identity(4)
M[:3, :3] = R
if point is not None:
# rotation not around origin
point = numpy.array(point[:3], dtype=numpy.float64, copy=False)
M[:3, 3] = point - numpy.dot(R, point)
return M
实际结果并没有像我期望的那样轮换积分。我想要旋转的矢量垂直于XY平面并平行于Z轴。这段代码在其他方向上旋转点我无法弄清楚。
数据的CG是::
cg = 0.5631200, 0.6244500, 0.0852599
定义的向量如下:
v_tail = np.array([x_c, y_c, 0.0])
v_head = np.array([x_c, y_c, z_c])
v = v_head - v_tail
vector v = [0. 0. 0.08526]
我正在尝试旋转的点的x,y,z坐标如下::
x y z
0 0.59046 0.62928 0.07307
1 0.59021 0.62943 0.07376
2 0.58970 0.62961 0.07333
3 0.58997 0.62907 0.07220
4 0.59081 0.62902 0.07266
也许你的代码片段试图实现Rodrigues' rotation但有一些错误(我不太熟悉numpy来发现错误)
但对于特定情况,有更简单的方法: 要围绕此轴创建旋转矩阵,您需要执行以下步骤:
-matrix of translation by (-cg.x, -cg.y, 0) T
-matrix of rotation around z-axis by angle R
-matrix of backward translation by (cg.x, cg.y, 0) BT
得到的矩阵是
M = T * R * BT