psychopy.tools.mathtools.quatToMatrix

psychopy.tools.mathtools.quatToMatrix(q, out=None, dtype=None)[source]

Create a 4x4 rotation matrix from a quaternion.

Parameters
  • q (tuple, list or ndarray of float) – Quaternion to convert in form [x, y, z, w] where w is real and x, y, z are imaginary components.

  • out (ndarray or None) – Optional output array. Must be same shape and dtype as the expected output if out was not specified.

  • dtype (dtype or str, optional) – Data type for computations can either be ‘float32’ or ‘float64’. If out is specified, the data type of out is used and this argument is ignored. If out is not provided, ‘float64’ is used by default.

Returns

4x4 rotation matrix in row-major order.

Return type

ndarray or None

Examples

Convert a quaternion to a rotation matrix:

point = [0., 1., 0., 1.]  # 4-vector form [x, y, z, 1.0]
ori = [0., 0., 0., 1.]
rotMat = quatToMatrix(ori)
# rotate 'point' using matrix multiplication
newPoint = np.matmul(rotMat.T, point)  # returns [-1., 0., 0., 1.]

Rotate all points in an array (each row is a coordinate):

points = np.asarray([[0., 0., 0., 1.],
                     [0., 1., 0., 1.],
                     [1., 1., 0., 1.]])
newPoints = points.dot(rotMat)

Notes

  • Quaternions are normalized prior to conversion.


Back to top