psychopy.tools.mathtools.alignTo

psychopy.tools.mathtools.alignTo(v, t, out=None, dtype=None)[source]

Compute a quaternion which rotates one vector to align with another.

Parameters
  • v (array_like) – Vector [x, y, z] to rotate. Can be Nx3, but must have the same shape as t.

  • t (array_like) – Target [x, y, z] vector to align to. Can be Nx3, but must have the same shape as v.

  • 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

Quaternion which rotates v to t.

Return type

ndarray

Examples

Rotate some vectors to align with other vectors, inputs should be normalized:

vec = [[1, 0, 0], [0, 1, 0], [1, 0, 0]]
targets = [[0, 1, 0], [0, -1, 0], [-1, 0, 0]]

qr = alignTo(vec, targets)
vecRotated = applyQuat(qr, vec)

numpy.allclose(vecRotated, targets)  # True

Get matrix which orients vertices towards a point:

point = [5, 6, 7]
vec = [0, 0, -1]  # initial facing is -Z (forward in GL)

targetVec = normalize(point - vec)
qr = alignTo(vec, targetVec)  # get rotation to align

M = quatToMatrix(qr)  # 4x4 transformation matrix

Back to top