The following image shows two coordinate systems A and B, positioned in the global frame G:
I need to find quaternion which rotates A to B, but expressed in the frame local to A.
The first part is easy, B.conjugate()*A
(subtracting the rotation of B
from the rotation of A
), which gives π/2 rotation around (-1,0,0)
. That is obviously correct, since rotating A
around the global -x
axis by 90° gives B
.
Now I need to express that result in the A
; the desired result is π/2 around (0,0,-1)
(that's what global -x
is in A), but I am not able to get there just with rotation composition.
I can convert the quaternion to angle-axis representation (π/2 around (-1,0,0)
), rotate the axis as A*(-1,0,0)=(0,0,-1)
, and convert back to quaternion, but I would like to avoid that conversion if possible.
How to get the rotation using quaternion multiplication only?
The code itself is c++, but I check that in python with minieigen:
from minieigen import *
from math import *
A=Quaternion((0,-1,0),pi/2)
B=Quaternion((sqrt(3)/3,-sqrt(3)/3,sqrt(3)/3),(2/3.)*pi)
# rotation in global frame:
rg=B.conjugate()*A # is Quaternion((-1,0,0),pi/2)
# rotation in local frame:
# ?? what now?! hack around with angle-axis
aa=rg.toAngleAxis()
rl=Quaternion(A*aa[1],aa[0]) # is Quaternion((0,0,-1),pi/2)
for q in A,B,rg,rl: print q
which gives:
Quaternion((0,-1,0),1.5707963267948966)
Quaternion((0.5773502691896257,-0.5773502691896257,0.5773502691896257),2.0943951023931953)
Quaternion((-1,-7.850462293418876e-17,-7.850462293418876e-17),1.5707963267948968)
Quaternion((-1.1102230246251568e-16,-7.850462293418877e-17,-1),1.5707963267948968)
That was an easy one, but I leave it here for the record:
A*B.conjugate()