您如何使用流行的transformations.py库来绕轴旋转点?
我试图将点围绕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。我究竟做错了什么?
transformations.py
使用np.array
对象,而不是对象,np.matrix
即使在whatever_matrix
函数中也是如此。(这是一件好事,因为np.matrix
这太可怕了。)您需要使用dot
矩阵乘法:
point1 = R.dot(point0)
另外,point0
由于某种原因,您制作了行向量。它应该是列向量或普通的一维向量:
point0 = np.array([0, 1, 0, 0])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句