Reputation: 64870
How do you use the popular transformations.py library to rotate a point around an axis?
I'm trying to rotate a point 90 degrees about the z-axis, but I'm not getting the expected results, and although the file's docs have several examples of creating transformation matrices, it doesn't actually show how to use these matrices to apply the transformation. My code is:
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
This calculates point1 as:
[[ 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]]
which makes no sense to me. Multiplying a 4x4 matrix by a 4x1 matrix should result in a 4x1, not another 4x4. What am I doing wrong?
Upvotes: 3
Views: 1463
Reputation: 281843
transformations.py
works with np.array
objects, not np.matrix
, even in the whatever_matrix
functions. (This is a good thing, because np.matrix
is horrible.) You need to use dot
for matrix multiplication:
point1 = R.dot(point0)
Also, you've made point0
a row vector for some reason. It should either be a column vector or a plain 1-dimensional vector:
point0 = np.array([0, 1, 0, 0])
Upvotes: 2