RigidTransformation#

class biotite.structure.RigidTransformation(rotation: ndarray[tuple[XYZ, XYZ], dtype[floating]] | ndarray[tuple[M, XYZ, XYZ], dtype[floating]], translation: ndarray[tuple[XYZ], dtype[floating]] | ndarray[tuple[M, XYZ], dtype[floating]] | None = None)[source]#

Bases: Generic[M]

A rigid transformation, consisting of a rotation followed by a translation.

Applying the transformation to coordinates \(x\) yields

\[x' = R x + t,\]

where \(R\) is the rotation matrix and \(t\) is the translation vector.

Parameters:
rotationndarray, shape=(3,3) or shape=(m,3,3), dtype=float

The rotation matrix.

translationndarray, shape=(3,) or shape=(m,3), dtype=float, optional

The translation vector. By default, no translation is applied.

Attributes:
rotation, translationndarray

Same as the parameters. The dimensions are always expanded to (m,3,3) or (m,3), respectively.

Notes

Two transformations can be combined via the * operator: (transform1 * transform2).apply(atoms) is equivalent to transform1.apply(transform2.apply(atoms)). The inverse of a transformation is obtained via the unary - operator.

Examples

>>> coord = np.arange(15).reshape(5,3)
>>> print(coord)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
>>> # Rotates 90 degrees around the z-axis and moves along the x-axis
>>> transform = RigidTransformation(
...     rotation=np.array([
...         [0, -1,  0],
...         [1,  0,  0],
...         [0,  0,  1]
...     ]),
...     translation=np.array([10, 0, 0])
... )
>>> print(transform.apply(coord))
[[ 9.  0.  2.]
 [ 6.  3.  5.]
 [ 3.  6.  8.]
 [ 0.  9. 11.]
 [-3. 12. 14.]]
>>> # The inverse transformation restores the original coordinates
>>> print((-transform).apply(transform.apply(coord)))
[[ 0.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]
 [ 9. 10. 11.]
 [12. 13. 14.]]
>>> # Combining a transformation with its inverse gives the identity
>>> print((-transform * transform).as_matrix()[0])
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
apply(atoms: Atom | AtomArray[N] | AtomArrayStack[_M, N] | ndarray[tuple[XYZ], dtype[floating]] | ndarray[tuple[N, XYZ], dtype[floating]] | ndarray[tuple[_M, N, XYZ], dtype[floating]]) → Atom | AtomArray[N] | AtomArrayStack[_M, N] | ndarray[tuple[XYZ], dtype[floating]] | ndarray[tuple[N, XYZ], dtype[floating]] | ndarray[tuple[_M, N, XYZ], dtype[floating]]#

Apply this transformation on the given structure.

Parameters:
atomsAtom or AtomArray or AtomArrayStack or ndarray, shape=(3,) or shape=(n,3) or shape=(m,n,3), dtype=float

The structure to apply the transformation on.

Returns:
transformedAtom or AtomArray or AtomArrayStack or ndarray, shape=(3,) or shape=(n,3) or shape=(m,n,3), dtype=float

A copy of the atoms structure, with transformations applied. Only coordinates are returned, if coordinates were given in atoms.

Examples

>>> coord = np.arange(15).reshape(5,3)
>>> print(coord)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
>>> # Rotates 90 degrees around the z-axis
>>> transform = RigidTransformation(
...     rotation=np.array([
...         [0, -1,  0],
...         [1,  0,  0],
...         [0,  0,  1]
...     ]),
... )
>>> print(transform.apply(coord))
[[ -1.   0.   2.]
 [ -4.   3.   5.]
 [ -7.   6.   8.]
 [-10.   9.  11.]
 [-13.  12.  14.]]
as_matrix() → ndarray[tuple[M, int, int], dtype[floating]]#

Get the rotation and translation as a combined 4x4 transformation matrix.

Multiplying this matrix with coordinates in the form (x, y, z, 1) will apply the same transformation as apply() to coordinates in the form (x, y, z).

Returns:
transformation_matrixndarray, shape=(m,4,4), dtype=float

The transformation matrix. m is the number of models in the transformation.

Examples

>>> coord = np.arange(15).reshape(5,3)
>>> print(coord)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
>>> # Rotates 90 degrees around the z-axis
>>> transform = RigidTransformation(
...     rotation=np.array([
...         [0, -1,  0],
...         [1,  0,  0],
...         [0,  0,  1]
...     ]),
... )
>>> print(transform.apply(coord))
[[ -1.   0.   2.]
 [ -4.   3.   5.]
 [ -7.   6.   8.]
 [-10.   9.  11.]
 [-13.  12.  14.]]
>>> # Use a 4x4 matrix for transformation as alternative
>>> coord_4 = np.concatenate([coord, np.ones((len(coord), 1))], axis=-1)
>>> print(coord_4)
[[ 0.  1.  2.  1.]
 [ 3.  4.  5.  1.]
 [ 6.  7.  8.  1.]
 [ 9. 10. 11.  1.]
 [12. 13. 14.  1.]]
>>> print((transform.as_matrix()[0] @ coord_4.T).T)
[[ -1.   0.   2.   1.]
 [ -4.   3.   5.   1.]
 [ -7.   6.   8.   1.]
 [-10.   9.  11.   1.]
 [-13.  12.  14.   1.]]