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

How to rotate vector by quaternion in python

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

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
11

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

robzz gravatar image

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

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)
    q2.append(0.0)
    return tf.transformations.quaternion_multiply(
        tf.transformations.quaternion_multiply(q1, q2), 
        tf.transformations.quaternion_conjugate(q1)
    )[:3]
edit flag offensive delete link more

Comments

2

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 -0600 )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 -0600 )edit

and there is no more complicated way?

mojovski gravatar image mojovski  ( 2018-11-02 09:53:07 -0600 )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 -0600 )edit

This answer is concise, matches the code above: https://gamedev.stackexchange.com/que...

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

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

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

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

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

tfoote gravatar image

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

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

edit flag offensive delete link more

Comments

1

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

robzz gravatar image robzz  ( 2014-10-28 18:43:41 -0600 )edit
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.

robzz gravatar image robzz  ( 2014-10-28 18:48:59 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

Question Tools

3 followers

Stats

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

Seen: 11,369 times

Last updated: Nov 11 '21