tensorbay.geometry.transform#

The implementation of 3D transformations in the 3D coordinate system.

class tensorbay.geometry.transform.Transform3D(translation=(0, 0, 0), rotation=(1, 0, 0, 0), *, matrix=None)[source]#

Bases: tensorbay.utility.repr.ReprMixin

This class defines the concept of Transform3D.

Transform3D contains rotation and translation of the 3D transform.

Parameters
  • translation (Iterable[float]) – Translation in a sequence of [x, y, z].

  • rotation (Union[Iterable[float], quaternion.quaternion]) – Rotation in a sequence of [w, x, y, z] or numpy quaternion.

  • matrix (Optional[Union[Sequence[Sequence[float]], numpy.ndarray]]) – A 4x4 or 3x4 transform matrix.

Raises

ValueError – If the shape of the input matrix is not correct.

Return type

None

Examples

Initialization Method 1: Init from translation and rotation.

>>> Transform3D([1, 1, 1], [1, 0, 0, 0])
Transform3D(
  (translation): Vector3D(1, 1, 1),
  (rotation): quaternion(1, 0, 0, 0)
)

Initialization Method 2: Init from transform matrix in sequence.

>>> Transform3D(matrix=[[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]])
Transform3D(
  (translation): Vector3D(1, 1, 1),
  (rotation): quaternion(1, -0, -0, -0)
)

Initialization Method 3: Init from transform matrix in numpy array.

>>> import numpy as np
>>> Transform3D(matrix=np.array([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]))
Transform3D(
  (translation): Vector3D(1, 1, 1),
  (rotation): quaternion(1, -0, -0, -0)
)
classmethod loads(contents)[source]#

Load a Transform3D from a dict containing rotation and translation.

Parameters

contents (Mapping[str, Mapping[str, float]]) – A dict containing rotation and translation of a 3D transform.

Returns

The loaded Transform3D object.

Return type

tensorbay.geometry.transform._T

Example

>>> contents = {
...     "translation": {"x": 1.0, "y": 2.0, "z": 3.0},
...     "rotation": {"w": 1.0, "x": 0.0, "y": 0.0, "z": 0.0},
... }
>>> Transform3D.loads(contents)
Transform3D(
  (translation): Vector3D(1.0, 2.0, 3.0),
  (rotation): quaternion(1, 0, 0, 0)
)
property translation: tensorbay.geometry.vector.Vector3D#

Return the translation of the 3D transform.

Returns

Translation in Vector3D.

Examples

>>> transform = Transform3D(matrix=[[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]])
>>> transform.translation
Vector3D(1, 1, 1)
property rotation: quaternion.quaternion#

Return the rotation of the 3D transform.

Returns

Rotation in numpy quaternion.

Examples

>>> transform = Transform3D(matrix=[[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]])
>>> transform.rotation
quaternion(1, -0, -0, -0)
dumps()[source]#

Dumps the Transform3D into a dict.

Returns

A dict containing rotation and translation information of the Transform3D.

Return type

Dict[str, Dict[str, float]]

Examples

>>> transform = Transform3D(matrix=[[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]])
>>> transform.dumps()
{
    'translation': {'x': 1, 'y': 1, 'z': 1},
    'rotation': {'w': 1.0, 'x': -0.0, 'y': -0.0, 'z': -0.0},
}
set_translation(x, y, z)[source]#

Set the translation of the transform.

Parameters
  • x (float) – The x coordinate of the translation.

  • y (float) – The y coordinate of the translation.

  • z (float) – The z coordinate of the translation.

Return type

None

Examples

>>> transform = Transform3D([1, 1, 1], [1, 0, 0, 0])
>>> transform.set_translation(3, 4, 5)
>>> transform
Transform3D(
  (translation): Vector3D(3, 4, 5),
  (rotation): quaternion(1, 0, 0, 0)
)
set_rotation(w=None, x=None, y=None, z=None, *, quaternion=None)[source]#

Set the rotation of the transform.

Parameters
  • w (Optional[float]) – The w componet of the roation quaternion.

  • x (Optional[float]) – The x componet of the roation quaternion.

  • y (Optional[float]) – The y componet of the roation quaternion.

  • z (Optional[float]) – The z componet of the roation quaternion.

  • quaternion (Optional[quaternion.quaternion]) – Numpy quaternion representing the rotation.

Return type

None

Examples

>>> transform = Transform3D([1, 1, 1], [1, 0, 0, 0])
>>> transform.set_rotation(0, 1, 0, 0)
>>> transform
Transform3D(
  (translation): Vector3D(1, 1, 1),
  (rotation): quaternion(0, 1, 0, 0)
)
as_matrix()[source]#

Return the transform as a 4x4 transform matrix.

Returns

A 4x4 numpy array represents the transform matrix.

Return type

numpy.ndarray

Examples

>>> transform = Transform3D([1, 2, 3], [0, 1, 0, 0])
>>> transform.as_matrix()
array([[ 1.,  0.,  0.,  1.],
       [ 0., -1.,  0.,  2.],
       [ 0.,  0., -1.,  3.],
       [ 0.,  0.,  0.,  1.]])
inverse()[source]#

Return the inverse of the transform.

Returns

A Transform3D object representing the inverse of this Transform3D.

Parameters

self (tensorbay.geometry.transform._T) –

Return type

tensorbay.geometry.transform._T

Examples

>>> transform = Transform3D([1, 2, 3], [0, 1, 0, 0])
>>> transform.inverse()
Transform3D(
  (translation): Vector3D(-1.0, 2.0, 3.0),
  (rotation): quaternion(0, -1, -0, -0)
)