Rotate 3-vectors using unit quaternions.
vecp = quatvecmult(q,vec)
Rotates the 3-vector(s) vec
using unit quaternion(s) q
. The input 3-vector(s), which must be normalized to one, are given as a 3xN array. The unit quaternion(s), which must be normalized to one, are given as a 4xN array. The rotated vector(s) vecp
are returned as a 3xN array.
Note that quatvecmult
is vectorized and will accept arrays of unit quaternions of size 4xNxMx... and 3-vectors of size 3xNxMx... with an arbitrary number of dimensions, in which case the rotated vectors will be returned as an array of size 3xNxMx... .
Taking an arbitrary 3-vector and quaternion and normalizing the results, we compute the rotated vector
q = [0.1; 0.438; -0.879; 0.699]; q = q./sqrt(sum(q.^2)) vec = [0.3; 0.9; -0.4]; vec = vec./sqrt(sum(vec.^2))
q = 0.0827 0.3621 -0.7267 0.5779 vec = 0.2914 0.8742 -0.3885
vecp = quatvecmult(q,vec)
vecp = -0.8705 0.2851 -0.4012
We can convince ourselves that using q
to rotate vec
is just as valid as using the equivalent rotation matrix to rotate vec
.
R = quat2rotmat(q); R*vec
ans = -0.8705 0.2851 -0.4012
rotmat2quat, quat2euler, euler2quat, quatinv