# How to rotate vector by quaternion in python

What is the python tf API to rotate a vector by a quaternion to get another vector?

edit retag close merge delete

Sort by » oldest newest most voted There's a copy of transformations.py embedded inside tf for linear math operations.

https://github.com/ros/geometry/blob/...

more

1

Right, so which of those transformation rotates a vector by a quaternion, if any?

1

Do I need to do matrix multiplications outside of those functions to accomplish this? If so, which? Better yet: where can I find examples of doing this? I've been search online for such an example using python tf, but with no luck.

Hi robzz, Have a closer look at the link that tfoote posted; the comments give an explanation and examples.

@RickArmstrong Hm I don't see any comments about rotating a vector by a quaternion there. I posted an answer here with some code that seems to work for the few cases that I've tried, using some of those functions. Would you mind taking a look?

Or, point me to the location in that 1700 line file that you're referring to :)

Yep, that's the ticket. Have a look here to get an idea of /why/ this works.

Okay, based on @tfoote's response and this answer to a related question, I think this does the job:

# rotate vector v1 by quaternion q1
def qv_mult(q1, v1):
v1 = tf.transformations.unit_vector(v1)
q2 = list(v1)
q2.append(0.0)
return tf.transformations.quaternion_multiply(
tf.transformations.quaternion_multiply(q1, q2),
tf.transformations.quaternion_conjugate(q1)
)[:3]

more

1

After a lot of searching, this is what I needed. A comment, the unit_vector conversion might not make sense depending on the application. If you are transforming velocities, you might not want to do the conversion.

Can you please explain the math being done here? Also vik748 can you elaborate?

This is what i needed! I just multiplied this resulting vector by my vector length to get the transformation!