tensorbay.geometry.transform

Transform3D.

Transform3D contains the rotation and translation of a 3D transform. Transform3D.translation is stored as Vector3D, and Transform3D.rotation is stored as numpy quaternion.

class tensorbay.geometry.transform.Transform3D(translation: Iterable[float] = (0, 0, 0), rotation: Union[Iterable[float], quaternion.quaternion] = (1, 0, 0, 0), *, matrix: Optional[Union[Sequence[Sequence[float]], numpy.ndarray]] = 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 – Translation in a sequence of [x, y, z].

  • rotation – Rotation in a sequence of [w, x, y, z] or numpy quaternion.

  • matrix – A 4x4 or 3x4 transform matrix.

Raises

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

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: Dict[str, Dict[str, float]]) tensorbay.geometry.transform._T[source]

Load a Transform3D from a dict containing rotation and translation.

Parameters

contents – A dict containing rotation and translation of a 3D transform.

Returns

The loaded Transform3D object.

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() Dict[str, Dict[str, float]][source]

Dumps the Transform3D into a dict.

Returns

A dict containing rotation and translation information of the Transform3D.

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: float, y: float, z: float) None[source]

Set the translation of the transform.

Parameters
  • x – The x coordinate of the translation.

  • y – The y coordinate of the translation.

  • z – The z coordinate of the translation.

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: Optional[float] = None, x: Optional[float] = None, y: Optional[float] = None, z: Optional[float] = None, *, quaternion: Optional[quaternion.quaternion] = None) None[source]

Set the rotation of the transform.

Parameters
  • w – The w componet of the roation quaternion.

  • x – The x componet of the roation quaternion.

  • y – The y componet of the roation quaternion.

  • z – The z componet of the roation quaternion.

  • quaternion – Numpy quaternion representing the rotation.

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() numpy.ndarray[source]

Return the transform as a 4x4 transform matrix.

Returns

A 4x4 numpy array represents the transform matrix.

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() tensorbay.geometry.transform._T[source]

Return the inverse of the transform.

Returns

A Transform3D object representing the inverse of this Transform3D.

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)
)