transform#

Transformation functions for 3D graphics.

euler_matrix(ai, aj, ak, *[, axes])

Return homogeneous rotation matrix from Euler angles and axis sequence.

sphere2cart(r, theta, phi)

Convert spherical coordinates to Cartesian coordinates.

cart2sphere(x, y, z)

Convert Cartesian coordinates to spherical coordinates.

translate(translation, *[, actor, ...])

Create a transformation matrix for translation.

rotate(quat, *[, actor, transfomation_type])

Create a transformation matrix for rotation using quaternion.

scale(scales, *[, actor, transfomation_type])

Create a transformation matrix for scaling.

apply_transformation(vertices, transformation)

Apply transformation matrix to vertices.

transform_from_matrix(matrix)

Decompose transformation matrix into components.

euler_matrix#

fury.transform.euler_matrix(ai, aj, ak, *, axes='sxyz')[source]#

Return homogeneous rotation matrix from Euler angles and axis sequence.

Parameters:
  • ai (float) – First Euler angle in radians.

  • aj (float) – Second Euler angle in radians.

  • ak (float) – Third Euler angle in radians.

  • axes (str or tuple, optional) – One of 24 axis sequences as string or encoded tuple. Default is ‘sxyz’.

Returns:

Homogeneous rotation matrix.

Return type:

ndarray (4, 4)

Notes

Code modified from the work of Christoph Gohlke: http://www.lfd.uci.edu/~gohlke/code/transformations.py.html

Examples

>>> import numpy
>>> R = euler_matrix(1, 2, 3, axes='syxz')
>>> numpy.allclose(numpy.sum(R[0]), -1.34786452)
True
>>> R = euler_matrix(1, 2, 3, axes=(0, 1, 0, 1))
>>> numpy.allclose(numpy.sum(R[0]), -0.383436184)
True
>>> ai, aj, ak = (4.0*math.pi) * (numpy.random.random(3) - 0.5)
>>> for axes in _AXES2TUPLE.keys():
...    _ = euler_matrix(ai, aj, ak, axes=axes)
>>> for axes in _TUPLE2AXES.keys():
...    _ = euler_matrix(ai, aj, ak, axes=axes)

sphere2cart#

fury.transform.sphere2cart(r, theta, phi)[source]#

Convert spherical coordinates to Cartesian coordinates.

Parameters:
  • r (array_like) – Radius.

  • theta (array_like) – Inclination or polar angle.

  • phi (array_like) – Azimuth angle.

Returns:

  • x (array) – X coordinate(s) in Cartesian space.

  • y (array) – Y coordinate(s) in Cartesian space.

  • z (array) – Z coordinate(s) in Cartesian space.

Notes

Imagine a sphere with center (0,0,0). Orient it with the z axis running south-north, the y axis running west-east and the x axis from posterior to anterior. theta (the inclination angle) is the angle to rotate from the z-axis (the zenith) around the y-axis, towards the x axis. Thus the rotation is counter-clockwise from the point of view of positive y. phi (azimuth) gives the angle of rotation around the z-axis towards the y axis. The rotation is counter-clockwise from the point of view of positive z.

Equivalently, given a point P on the sphere, with coordinates x, y, z, theta is the angle between P and the z-axis, and phi is the angle between the projection of P onto the XY plane, and the X axis.

Geographical nomenclature designates theta as ‘co-latitude’, and phi as ‘longitude’.

See these pages for more details: * http://en.wikipedia.org/wiki/Spherical_coordinate_system * http://mathworld.wolfram.com/SphericalCoordinates.html

We have deliberately named this function sphere2cart rather than sph2cart to distinguish it from the Matlab function of that name, which uses a different convention.

cart2sphere#

fury.transform.cart2sphere(x, y, z)[source]#

Convert Cartesian coordinates to spherical coordinates.

Parameters:
  • x (array_like) – X coordinate in Cartesian space.

  • y (array_like) – Y coordinate in Cartesian space.

  • z (array_like) – Z coordinate in Cartesian space.

Returns:

  • r (array) – Radius.

  • theta (array) – Inclination (polar) angle in range [0, π].

  • phi (array) – Azimuth angle in range [-π, π].

Notes

Uses the same convention as sphere2cart. The inclination angle theta is in range [0, π] and the azimuth angle phi is in range [-π, π].

$0lethetamathrm{(theta)}lepi$ and $-pilephimathrm{(phi)}lepi$

See sphere2cart for detailed description of the coordinate convention.

translate#

fury.transform.translate(translation, *, actor=None, transfomation_type='relative')[source]#

Create a transformation matrix for translation.

Parameters:
  • translation (ndarray (3,)) – Translation vector in x, y and z directions.

  • actor (Actor, optional) – If provided, the transformation will be applied to this actor.

  • transfomation_type (str, optional) – Type of transformation to apply to the actor. Can be either relative or absolute. - The relative transformation multiplies the current transformation matrix with the new translation matrix. - The absolute transformation sets the actor’s transformation matrix to the new translation matrix.

Returns:

Homogeneous transformation matrix with translation parameters in the last column.

Return type:

ndarray (4, 4)

Examples

>>> import numpy as np; import fury
>>> tran = np.array([0.3, 0.2, 0.25])
>>> transform = fury.transform.translate(tran)
>>> transform
array([[1.  , 0.  , 0.  , 0.3 ],
       [0.  , 1.  , 0.  , 0.2 ],
       [0.  , 0.  , 1.  , 0.25],
       [0.  , 0.  , 0.  , 1.  ]])

rotate#

fury.transform.rotate(quat, *, actor=None, transfomation_type='relative')[source]#

Create a transformation matrix for rotation using quaternion.

Parameters:
  • quat (ndarray (4,)) – Rotation quaternion in form [x, y, z, w].

  • actor (Actor, optional) – If provided, the transformation will be applied to this actor.

  • transfomation_type (str, optional) – Type of transformation to apply to the actor. Can be either relative or absolute. - The relative transformation multiplies the current transformation matrix with the new rotation matrix. - The absolute transformation sets the actor’s transformation matrix to the new rotation matrix.

Returns:

Homogeneous transformation matrix for rotation.

Return type:

ndarray (4, 4)

Examples

>>> import numpy as np; import fury
>>> quat = np.array([0.259, 0.0, 0.0, 0.966])
>>> rotation = fury.transform.rotate(quat)
>>> rotation
array([[ 1.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.86586979, -0.50026944,  0.        ],
       [ 0.        ,  0.50026944,  0.86586979,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  1.        ]])

scale#

fury.transform.scale(scales, *, actor=None, transfomation_type='relative')[source]#

Create a transformation matrix for scaling.

Parameters:
  • scales (ndarray (3,)) – Scale factors for x, y and z directions.

  • actor (Actor, optional) – If provided, the transformation will be applied to this actor.

  • transfomation_type (str, optional) – Type of transformation to apply to the actor. Can be either relative or absolute. - The relative transformation multiplies the current transformation matrix with the new scaling matrix. - The absolute transformation sets the actor’s transformation matrix to the new scaling matrix.

Returns:

Homogeneous transformation matrix with scale factors along the diagonal.

Return type:

ndarray (4, 4)

Examples

>>> import numpy as np; import fury
>>> scales = np.array([2.0, 1.0, 0.5])
>>> transform = fury.transform.scale(scales)
>>> transform
array([[2. , 0. , 0. , 0. ],
       [0. , 1. , 0. , 0. ],
       [0. , 0. , 0.5, 0. ],
       [0. , 0. , 0. , 1. ]])

apply_transformation#

fury.transform.apply_transformation(vertices, transformation)[source]#

Apply transformation matrix to vertices.

Parameters:
  • vertices (ndarray (n, 3)) – Array of vertices to be transformed.

  • transformation (ndarray (4, 4)) – Homogeneous transformation matrix.

Returns:

Transformed vertices.

Return type:

ndarray (n, 3)

Notes

This function multiplies the transformation matrix with the vertices to transform them in 3D space. The vertices are converted to homogeneous coordinates before multiplication.

transform_from_matrix#

fury.transform.transform_from_matrix(matrix)[source]#

Decompose transformation matrix into components.

Parameters:

matrix (ndarray (4, 4)) – Homogeneous transformation matrix.

Returns:

  • translation (ndarray (3,)) – Translation vector (tx, ty, tz) extracted from the matrix.

  • rotation (ndarray (4,)) – Rotation parameters as [angle, rx, ry, rz] where angle is in degrees and rx, ry, rz represent the rotation vector direction.

  • scale (ndarray (3,)) – Scale factors (sx, sy, sz) extracted from the matrix.

Notes

The function extracts the translation directly from the last column of the matrix, calculates the scale by computing the norm of each of the first three columns, and then derives the rotation matrix after normalizing for scale.