Finding quaternion representing the rotation from one vector to another given the normals of the two vectors

1.2k Views Asked by At

Assuming I have 2 vectors in 3D space, with a known normal of the 2 vectors, how to find the quaternion angle between the 2 vectors with the correct roll.

2

There are 2 best solutions below

0
On

The angle $\alpha$ between two vectors $u$ and $v$ in $\mathbb R^3$ can be calculated via $\cos \alpha = \dfrac{\langle u, v\rangle}{\Vert u\Vert\Vert v\Vert}$. Depending on a choice of order of the two vectors, you can find the corresponding axis of rotation by letting $n = u \times v$. Finally you look up the formula for how to rotate a vector by an angle $\alpha$ around the axis $n$ using quaternions. The formula is something along the lines of $q = (\cos(\frac{\alpha}{2}), \sin(\frac{\alpha}{2})(u+v-u\times v)$, but I highly doubt that what I wrote is correct.

0
On

Here's python code to find the angle:

# theta = angular difference between the quaternions
def qangle(x, y):
    return math.acos(qdot(x, y)/(qnorm(x) * qnorm(y)))

# dot product gives a real
def qdot(x, y):
    return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] +x[3]*y[3]

# norm of quaternion is absolute value = |q| : real ; also called modulus of q
# q * qstar(q) = square(|q|)
def qnorm(x):
    return math.sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3])

# Pure Quaternion is a quaternion that has a zero scalar term
# => pq = [0, a, b, c] = [0, v_]

The normals to the vectors are not needed.