如何使用transformations.py旋转点

时间:2016-04-20 17:36:24

标签: python numpy matrix rotation

如何使用受欢迎的 transformations.py library围绕轴旋转点?

我试图围绕z轴旋转一个90度的点,但我没有得到预期的结果,虽然文件的文档有几个创建转换矩阵的例子,它实际上并没有显示如何使用这些矩阵来应用转换。我的代码是:

from math import pi
import transformations as tf
import numpy as np

alpha, beta, gamma = 0, 0, pi/2.
origin, xaxis, yaxis, zaxis = (0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)
Rx = tf.rotation_matrix(alpha, xaxis)
Ry = tf.rotation_matrix(beta, yaxis)
Rz = tf.rotation_matrix(gamma, zaxis)
R = tf.concatenate_matrices(Rx, Ry, Rz)

point0 = np.array([[0, 1, 0, 0]])

point1 = R * point0
#point1 = R * point0.T # same result
#point1 = np.multiply(R, point0) # same result
#point1 = np.multiply(R, point0.T) # same result
point1_expected = np.array([[1, 0, 0, 0]])

assert point1.tolist() == point1_expected.tolist() # this fails

这将point1计算为:

[[  0.00000000e+00  -1.00000000e+00   0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   6.12323400e-17   0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]]

对我来说毫无意义。将4x4矩阵乘以4x1矩阵应得到4x1,而不是4x4。我做错了什么?

1 个答案:

答案 0 :(得分:2)

transformations.py适用于np.array个对象,而不是np.matrix,即使在whatever_matrix函数中也是如此。 (这是一件好事,因为np.matrix太可怕了。)你需要使用dot进行矩阵乘法:

point1 = R.dot(point0)

此外,由于某种原因,您已将point0设为行向量。它应该是列向量或普通的1维向量:

point0 = np.array([0, 1, 0, 0])
相关问题