Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate a point using transformations.py

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?

like image 814
Cerin Avatar asked May 25 '26 16:05

Cerin


1 Answers

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])
like image 122
user2357112 supports Monica Avatar answered May 28 '26 08:05

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!