# psychopy.tools.mathtools.applyQuat¶

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

Rotate points/coordinates using a quaternion.

This is similar to using applyMatrix with a rotation matrix. However, it is computationally less intensive to use applyQuat if one only wishes to rotate points.

Parameters
• q (array_like) – Quaternion to invert in form [x, y, z, w] where w is real and x, y, z are imaginary components.

• points (array_like) – 2D array of vectors or points to transform, where each row is a single point. Only the x, y, and z components (the first three columns) are rotated. Additional columns are copied.

• 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

Transformed points.

Return type

ndarray

Examples

Rotate points using a quaternion:

points = [[1., 0., 0.], [0., -1., 0.]]
quat = quatFromAxisAngle(-90.0, [0., 0., -1.], degrees=True)
pointsRotated = applyQuat(quat, points)
# [[0. 1. 0.]
#  [1. 0. 0.]]


Show that you get the same result as a rotation matrix:

axis = [0., 0., -1.]
angle = -90.0
rotMat = rotationMatrix(axis, angle)[:3, :3]  # rotation sub-matrix only
rotQuat = quatFromAxisAngle(angle, axis, degrees=True)
points = [[1., 0., 0.], [0., -1., 0.]]
isClose = np.allclose(applyMatrix(rotMat, points),  # True
applyQuat(rotQuat, points))


Specifying an array to q where each row is a quaternion transforms points in corresponding rows of points:

points = [[1., 0., 0.], [0., -1., 0.]]
quats = [quatFromAxisAngle(-90.0, [0., 0., -1.], degrees=True),
quatFromAxisAngle(45.0, [0., 0., -1.], degrees=True)]
applyQuat(quats, points)