NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.quaternions.quat2angle_axis

Next topic

nibabel.quaternions.rotate_vector

Reggie -- the one

nibabel.quaternions.quat2mat

nibabel.quaternions.quat2mat(q)

Calculate rotation matrix corresponding to quaternion

Parameters:

q : 4 element array-like

Returns:

M : (3,3) array

Rotation matrix corresponding to input quaternion q

Notes

Rotation matrix applies to column vectors, and is applied to the left of coordinate vectors. The algorithm here allows non-unit quaternions.

References

Algorithm from http://en.wikipedia.org/wiki/Rotation_matrix#Quaternion

Examples

>>> import numpy as np
>>> M = quat2mat([1, 0, 0, 0]) # Identity quaternion
>>> np.allclose(M, np.eye(3))
True
>>> M = quat2mat([0, 1, 0, 0]) # 180 degree rotn around axis 0
>>> np.allclose(M, np.diag([1, -1, -1]))
True