tomosipo.geometry.transform module

class tomosipo.geometry.transform.Transform(matrix)[source]

Bases: object

Documentation for Transform

property inv
property num_steps
transform_point(points)[source]

Transform one or multiple points

Parameters:

pointsnp.array

The following shapes are allowed: - (n_rows, 3) - (3,)

Returns:

np.array

Return type:

The shape of the array is (n_rows, 3)

transform_vec(vec)[source]

Transform one or multiple vectors

Parameters:

vecnp.array

The following shapes are allowed: - (n_rows, 3) - (3,)

Returns:

np.array

Return type:

The shape of the array is (n_rows, 3)

tomosipo.geometry.transform.from_perspective(*, pos=None, w=None, v=None, u=None, vol=None, ignore_scale=True)[source]

Create a perspective transform

This perspective transform converts the given frame of reference to the “world” coordinate frame with origin (0, 0, 0) and coordinate axes Z, Y, X.

The frame of reference can either be explicitly provided using the pos, w, v, u parameters. Alternatively, the frame of reference can be specified using a volume geometry.

To perform the perspective transformation without scaling any of the coordinate axes (thus only performing rotation and translation), provide ignore_scale=True.

Parameters:
  • pos (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The position of the new frame of reference.

  • w (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The first coordinate of the new frame of reference.

  • v (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The second coordinate of the new frame of reference.

  • u (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The third coordinate of the new frame of reference.

  • vol (Any) – A volume (vector) geometry that specifies the frame of reference. If provided, the pos, w, v, and u parameters are taken from the volume.

  • ignore_scale (bool) – If True, do not perform any scaling of the coordinate axes. Effectively, the transform only performs rotation and translation in most cases.

Returns:

A transform describing the shift in perspective.

Return type:

Transform

Examples

Create a translation from (1, 2, 3) to (0, 0, 0):

>>> ts.from_perspective(pos=(1, 2, 3), w=(1, 0, 0), v=(0, 1, 0), u=(0, 0, 1))
Transform(
    [[[ 1.  0.  0. -1.]
  [ 0.  1.  0. -2.]
  [ 0.  0.  1. -3.]
  [ 0.  0.  0.  1.]]]
)

Create equivalent translation, using the vol parameter:

>>> ts.from_perspective(vol=ts.volume(pos=(1, 2, 3)))
Transform(
    [[[ 1.  0.  0. -1.]
  [ 0.  1.  0. -2.]
  [ 0.  0.  1. -3.]
  [ 0.  0.  0.  1.]]]
)

Ignore scaling of the coordinate axes:

>>> ts.from_perspective(pos=(1, 2, 3), w=(2, 0, 0), v=(0, 2, 0), u=(0, 0, 2), ignore_scale=True)
Transform(
    [[[ 1.  0.  0. -1.]
  [ 0.  1.  0. -2.]
  [ 0.  0.  1. -3.]
  [ 0.  0.  0.  1.]]]
)

Scale the coordinate axes as well:

>>> ts.from_perspective(pos=(1, 2, 3), w=(2, 0, 0), v=(0, 2, 0), u=(0, 0, 2), ignore_scale=False)
Transform(
    [[[ 0.5  0.   0.  -0.5]
  [ 0.   0.5  0.  -1. ]
  [ 0.   0.   0.5 -1.5]
  [ 0.   0.   0.   1. ]]]
)
tomosipo.geometry.transform.identity()[source]

Create identity transform

Returns:

Return type:

tomosipo.geometry.transform.random_transform()[source]

Generate a random transformation

Returns:

A random rigid transformation

Return type:

Transform

tomosipo.geometry.transform.reflect(*, pos, axis)[source]

Create a reflection transform

The transform reflects in the plane through pos with normal vector axis.

The parameters pos and axis are interpreted as homogeneous coordinates. You may pass in both homogeneous or non-homogeneous coordinates. Also, you may pass in multiple rows for multiple timesteps. The following shapes are allowed:

  • (n_rows, 3) [non-homogeneous] or (n_rows, 4) [homogeneous]

  • (3,) [non-homogeneous] or (4,) [homogeneous]

Parameters:
  • posnp.array or scalar A position intersecting the plane of reflection.

  • axis – A normal vector to the plane of reflection. Need not be unit-normal.

Returns:

Return type:

tomosipo.geometry.transform.rotate(*, pos, axis, angles=None, rad=None, deg=None, right_handed=True)[source]

Create a rotation transform

The transform rotates around axis through position pos by some angles.

The parameters pos and axis are interpreted as homogeneous coordinates. You may pass in both homogeneous or non-homogeneous coordinates. Also, you may pass in multiple rows for multiple timesteps. The following shapes are allowed:

  • (n_rows, 3) [non-homogeneous] or (n_rows, 4) [homogeneous]

  • (3,) [non-homogeneous] or (4,) [homogeneous]

Parameters:
  • posnp.array or scalar The position of the axis of rotation.

  • axis – The direction vector of the axis of rotation.

  • anglesfloat or np.array The angle by which must be rotated in radians.

  • radDEPRECATED

  • degDEPRECATED

  • right_handed – By default, the rotation performs a right-handed rotation (in the anti-clockwise direction). A left-handed rotation is performed when right_handed=False.

Returns:

Return type:

Transform

tomosipo.geometry.transform.scale(scale, *, pos=0, alpha=1.0)[source]

Create a scaling transform

The scaling transform scales the coordinate frame of the object by alpha * scale around position pos.

Parameters:
  • scale

    By how much to scale in each direction. The parameter scale is interpreted as a series of homogeneous coordinates. You may pass in both homogeneous or non-homogeneous coordinates. Also, you may pass in multiple rows for multiple timesteps. The following shapes are allowed:

    • scalar

    • (n_rows, 3) [non-homogeneous] or (n_rows, 4) [homogeneous]

    • (3,) [non-homogeneous] or (4,) [homogeneous]

  • pos – Scale around a custom position. The default is to scale around the origin.

  • alpha – Multiply the scaling by alpha. This is useful if you want the scaling to be time-dependent.

Returns:

A transform describing the scaling

Return type:

Transform

Examples

>>> ts.scale(2)
Transform(
    [[[2. 0. 0. 0.]
  [0. 2. 0. 0.]
  [0. 0. 2. 0.]
  [0. 0. 0. 1.]]]
)
>>> ts.scale((2, 2, 2), pos=(3, 4, 5))
Transform(
    [[[ 2.  0.  0. -3.]
  [ 0.  2.  0. -4.]
  [ 0.  0.  2. -5.]
  [ 0.  0.  0.  1.]]]
)
>>> ts.scale(1, alpha=[2, 3])
Transform(
    [[[2. 0. 0. 0.]
  [0. 2. 0. 0.]
  [0. 0. 2. 0.]
  [0. 0. 0. 1.]]

 [[3. 0. 0. 0.]
  [0. 3. 0. 0.]
  [0. 0. 3. 0.]
  [0. 0. 0. 1.]]]
)

Note that all parameters must have broadcastable length. Otherwise an error is raised.

>>> ts.scale(np.ones((2,3)), pos=np.ones((4, 3)), alpha=np.ones(5))
Traceback (most recent call last):
...
ValueError: Expected `scale`, `pos`, and `alpha` to be broadcastable. Got lengths: 2, 4, and 5.
tomosipo.geometry.transform.to_perspective(*, pos=None, w=None, v=None, u=None, vol=None, ignore_scale=True)[source]

Create a perspective transform

This perspective transform converts the “world” coordinate frame to another frame of reference.

The other frame of reference can either be explicitly provided using the pos, w, v, u parameters. Alternatively, the other frame of reference can be specified using a volume geometry.

To perform the perspective transformation without scaling any of the coordinate axes (thus only performing rotation and translation), provide ignore_scale=True.

Parameters:
  • pos (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The position of the new frame of reference.

  • w (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The first coordinate of the new frame of reference.

  • v (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The second coordinate of the new frame of reference.

  • u (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) – The third coordinate of the new frame of reference.

  • vol (Any) – A volume (vector) geometry that specifies the frame of reference. If provided, the pos, w, v, and u parameters are taken from the volume.

  • ignore_scale (bool) – If True, do not perform any scaling of the coordinate axes. Effectively, the transform only performs rotation and translation in most cases.

Returns:

A transform describing the shift in perspective.

Return type:

Transform

Examples

Create a translation from (0, 0, 0) to (1, 2, 3):

>>> ts.to_perspective(pos=(1, 2, 3), w=(1, 0, 0), v=(0, 1, 0), u=(0, 0, 1))
Transform(
    [[[1. 0. 0. 1.]
  [0. 1. 0. 2.]
  [0. 0. 1. 3.]
  [0. 0. 0. 1.]]]
)

Create equivalent translation, using the vol parameter:

>>> ts.to_perspective(vol=ts.volume(pos=(1, 2, 3)))
Transform(
    [[[1. 0. 0. 1.]
  [0. 1. 0. 2.]
  [0. 0. 1. 3.]
  [0. 0. 0. 1.]]]
)

Ignore scaling of the coordinate axes:

>>> ts.to_perspective(pos=(1, 2, 3), w=(2, 0, 0), v=(0, 2, 0), u=(0, 0, 2), ignore_scale=True)
Transform(
    [[[1. 0. 0. 1.]
  [0. 1. 0. 2.]
  [0. 0. 1. 3.]
  [0. 0. 0. 1.]]]
)

Scale the coordinate axes as well:

>>> ts.to_perspective(pos=(1, 2, 3), w=(2, 0, 0), v=(0, 2, 0), u=(0, 0, 2), ignore_scale=False)
Transform(
    [[[2. 0. 0. 1.]
  [0. 2. 0. 2.]
  [0. 0. 2. 3.]
  [0. 0. 0. 1.]]]
)
tomosipo.geometry.transform.translate(axis, *, alpha=1)[source]

Create a translation transform

Parameters:
  • axis (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray | Tuple[float, float, float, float] | Iterable[Tuple[float, float, float, float]]) –

    By how much to translate. The parameter axis is interpreted as a series of homogeneous coordinates. You may pass in both homogeneous or non-homogeneous coordinates. Also, you may pass in multiple rows for multiple timesteps. The following shapes are allowed:

    • (n_rows, 3) [non-homogeneous] or (n_rows, 4) [homogeneous]

    • (3,) [non-homogeneous] or (4,) [homogeneous]

  • alpha (float | Collection[float] | ndarray) – A scalar multiplier of axis. This parameter can be used to conveniently translate along a single axis.

Returns:

A transform describing the translation

Return type:

Transform

Examples

>>> ts.translate((1, 2, 3))
Transform(
    [[[1. 0. 0. 1.]
  [0. 1. 0. 2.]
  [0. 0. 1. 3.]
  [0. 0. 0. 1.]]]
)
>>> ts.translate((1, 1, 1), alpha=[2, 3])
Transform(
    [[[1. 0. 0. 2.]
  [0. 1. 0. 2.]
  [0. 0. 1. 2.]
  [0. 0. 0. 1.]]

 [[1. 0. 0. 3.]
  [0. 1. 0. 3.]
  [0. 0. 1. 3.]
  [0. 0. 0. 1.]]]
)