psychopy.tools.mathtools.matrixToQuat

psychopy.tools.mathtools.matrixToQuat(m, out=None, dtype=None)[source]

Convert a rotation matrix to a quaternion.

Parameters
  • m (array_like) – 3x3 rotation matrix (row-major). A 4x4 affine transformation matrix may be provided, assuming the top-left 3x3 sub-matrix is orthonormal and is a rotation group.

  • out (ndarray, optional) – 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

Rotation quaternion.

Return type

ndarray

Notes

  • Depending on the input, returned quaternions may not be exactly the same as the one used to construct the rotation matrix (i.e. by calling quatToMatrix), typically when a large rotation angle is used. However, the returned quaternion should result in the same rotation when applied to points.

Examples

Converting a rotation matrix from the OpenGL matrix stack to a quaternion:

glRotatef(45., -1, 0, 0)

m = np.zeros((4, 4), dtype='float32')  # store the matrix
GL.glGetFloatv(
    GL.GL_MODELVIEW_MATRIX,
    m.ctypes.data_as(ctypes.POINTER(ctypes.c_float)))

qr = matrixToQuat(m.T)  # must be transposed

Interpolation between two 4x4 transformation matrices:

interpWeight = 0.5

posStart = mStart[:3, 3]
oriStart = matrixToQuat(mStart)

posEnd = mEnd[:3, 3]
oriEnd = matrixToQuat(mEnd)

oriInterp = slerp(qStart, qEnd, interpWeight)
posInterp = lerp(posStart, posEnd, interpWeight)

mInterp = posOriToMatrix(posInterp, oriInterp)

Back to top