Ask Your Question
8

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
3

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

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 imagerobzz ( 2014-10-28 18:43:41 -0500 )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 imagerobzz ( 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 imageRick 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 imagerobzz ( 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 imagerobzz ( 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 imageRick Armstrong ( 2014-10-29 17:33:39 -0500 )edit
8

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

robzz gravatar image

updated 2014-10-28 19:40:27 -0500

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]
edit flag offensive delete link more

Comments

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.

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

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

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

and there is no more complicated way?

mojovski gravatar imagemojovski ( 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 imageEnmar ( 2019-09-30 02:37:22 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

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

Seen: 6,630 times

Last updated: Oct 28 '14