ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to rotate vector by quaternion in python

asked 2014-10-28 17:58:31 -0500

robzz gravatar image

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-10-28 19:31:00 -0500

robzz gravatar image

updated 2021-11-11 10:06:40 -0500

lucasw gravatar image

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):
    # comment this out if v1 doesn't need to be a unit vector
    v1 = tf.transformations.unit_vector(v1)
    q2 = list(v1)
    return tf.transformations.quaternion_multiply(
        tf.transformations.quaternion_multiply(q1, q2), 
edit flag offensive delete link more



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.

vik748 gravatar image vik748  ( 2018-07-12 10:02:50 -0500 )edit

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

JadTawil gravatar image JadTawil  ( 2018-10-21 15:06:01 -0500 )edit

and there is no more complicated way?

mojovski gravatar image mojovski  ( 2018-11-02 09:53:07 -0500 )edit

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

Enmar gravatar image Enmar  ( 2019-09-30 02:37:22 -0500 )edit

This answer is concise, matches the code above:

Pre-multiply the vector with q and post-multiply it with the conjugate q*

lucasw gravatar image lucasw  ( 2021-11-11 10:11:23 -0500 )edit

We're assuming this is a 3-long vector, right?

AndyZe gravatar image AndyZe  ( 2021-11-11 13:17:20 -0500 )edit

answered 2014-10-28 18:29:46 -0500

tfoote gravatar image

There's a copy of embedded inside tf for linear math operations.

edit flag offensive delete link more



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

robzz gravatar image robzz  ( 2014-10-28 18:43:41 -0500 )edit

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.

robzz gravatar image robzz  ( 2014-10-28 18:48:59 -0500 )edit

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

Rick Armstrong gravatar image Rick Armstrong  ( 2014-10-28 19:30:22 -0500 )edit

@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?

robzz gravatar image robzz  ( 2014-10-28 19:38:24 -0500 )edit

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

robzz gravatar image robzz  ( 2014-10-28 19:58:25 -0500 )edit

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

Rick Armstrong gravatar image Rick Armstrong  ( 2014-10-29 17:33:39 -0500 )edit

Question Tools



Asked: 2014-10-28 17:58:31 -0500

Seen: 11,123 times

Last updated: Nov 11 '21