Pylinalg API reference

pylinalg

Linear algebra utilities for Python.

pylinalg.aabb_to_sphere(aabb, /, *, out=None, dtype=None) ndarray

A sphere that envelops an Axis-Aligned Bounding Box.

Parameters:
  • aabb (ndarray, [2, 3]) – The axis-aligned bounding box.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

sphere – A sphere (x, y, z, radius).

Return type:

ndarray, [4]

pylinalg.aabb_transform(aabb, matrix, /, *, out=None, dtype=None) ndarray

Apply an affine transformation to an axis-aligned bounding box.

Parameters:
  • aabb (ndarray, [2, 3]) – The axis-aligned bounding box.

  • homogeneous_matrix ([4, 4]) – The homogeneous transformation to apply.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

aabb – The transformed axis-aligned bounding box.

Return type:

ndarray, [2, 3]

Notes

This function preserves the alignment axes of the bounding box. This means the returned bounding box has the same alignment axes as the input bounding box, but contains the transformed object. In other words, the box will grow or shrink depending on how the contained object is transformed, but its alignment axis stay the same.

pylinalg.mat_combine(matrices, /, *, out=None, dtype=None) ndarray

Combine a list of affine matrices by multiplying them.

Note that by matrix multiplication rules, the output matrix will applied the given transformations in reverse order. For example, passing a scaling, rotation and translation matrix (in that order), will lead to a combined transformation matrix that applies translation first, then rotation and finally scaling.

Parameters:
  • matrices (list of ndarray, [4, 4]) – List of affine matrices to combine.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Combined transformation matrix.

Return type:

ndarray, [4, 4]

pylinalg.mat_compose(translation, rotation, scaling, /, *, out=None, dtype=None) ndarray

Compose a transformation matrix given a translation vector, a quaternion and a scaling vector.

Parameters:
  • translation (number or ndarray, [3]) – translation vector

  • rotation (ndarray, [4]) – quaternion

  • scaling (number or ndarray, [3]) – scaling factor(s)

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Transformation matrix

Return type:

ndarray, [4, 4]

pylinalg.mat_decompose(matrix, /, *, scaling_signs=None, dtype=None, out=None) Tuple[ndarray, ndarray, ndarray]

Decompose a transformation matrix into a translation vector, a quaternion and a scaling vector.

Parameters:
  • matrix (ndarray, [4, 4]) – transformation matrix

  • scaling_signs (ndarray, [3], optional) – scaling factor signs. If you wish to preserve the original scaling factors through a compose-decompose roundtrip, you should provide the original scaling factors here, or alternatively just the signs.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

  • translation (ndarray, [3]) – translation vector

  • rotation (ndarray, [4]) – quaternion

  • scaling (ndarray, [3]) – scaling factors

pylinalg.mat_decompose_translation(homogeneous_matrix, /, *, out=None, dtype=None) ndarray

Position component of a homogeneous matrix.

Parameters:
  • homogeneous_matrix (ndarray, [4, 4]) – The matrix of which the position/translation component will be extracted.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

position – The position/translation component.

Return type:

ndarray, [3]

pylinalg.mat_from_axis_angle(axis, angle, /, *, out=None, dtype=None) ndarray

Make a rotation matrix given a rotation axis and an angle (in radians).

Parameters:
  • axis (ndarray, [3]) – The rotation axis.

  • angle (number) – The angle (in radians) to rotate about the axis.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Rotation matrix.

Return type:

ndarray, [4, 4]

pylinalg.mat_from_euler(angles, /, *, order='xyz', out=None, dtype=None) ndarray

Make a matrix given euler angles (in radians) per axis.

Parameters:
  • angles (ndarray, [3]) – The euler angles.

  • order (string, optional) – The rotation order as a string. Can include ‘X’, ‘Y’, ‘Z’ for intrinsic rotation (uppercase) or ‘x’, ‘y’, ‘z’ for extrinsic rotation (lowercase). Default is “xyz”.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Rotation matrix.

Return type:

ndarray, [4, 4]

Notes

If you are familiar with TreeJS note that this function uses order to denote both the order in which rotations are applied and the order in which angles are provided in angles. I.e., mat_from_euler([np.pi, np.pi, 0], order="zyx") will first rotate 180° ccw (counter-clockwise) around the z-axis, then 180° ccw around the y-axis, and finally 0° around the x axis.

pylinalg.mat_from_quat(quaternion, /, *, out=None, dtype=None) ndarray

Make a rotation matrix given a quaternion.

Parameters:
  • quaternion (ndarray, [4]) – Quaternion.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

rotation matrix.

Return type:

ndarray, [4, 4]

pylinalg.mat_from_scale(factors, /, *, out=None, dtype=None) ndarray

Make a scaling matrix given scaling factors per axis, or a single uniform scaling factor.

Parameters:
  • factor (number or ndarray, [3]) – scaling factor(s)

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Scaling matrix.

Return type:

ndarray, [4, 4]

pylinalg.mat_from_translation(vector, /, *, out=None, dtype=None) ndarray

Make a translationmatrix given a translation vector.

Parameters:
  • vector (number or ndarray, [3]) – translation vector

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Translation matrix.

Return type:

ndarray, [4, 4]

pylinalg.mat_has_shear(matrix, epsilon=1e-07) bool

Check if a matrix has shear by checking the orthogonality of its basis vectors.

Parameters:
  • matrix (ndarray, [4, 4]) – The matrix to check.

  • epsilon (float, optional) – The floating point error margin. Default is 1e-7.

Returns:

out – True if the matrix has a shearing component, False otherwise.

Return type:

bool

pylinalg.mat_inverse(matrix, /, *, method='numpy', raise_err=False, dtype=None, out=None) ndarray

Compute the inverse of a matrix.

Parameters:
  • matrix (ndarray, [4, 4]) – The matrix to invert.

  • method (str, optional) – The method to use for inversion. The default is “numpy” when numpy version is 2.0.0 or newer, otherwise “python”.

  • raise_err (bool, optional) – Raise a ValueError if the matrix is singular. Default is False.

  • dtype (data-type, optional) – Overrides the data type of the result.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

Returns:

out – The inverse of the input matrix.

Return type:

ndarray, [4, 4]

Notes

The default method is “numpy” when numpy version >= 2.0.0, which uses the numpy.linalg.inv function. The alternative method is “python”, which uses a pure python implementation of the inversion algorithm. The python method is used to avoid a performance issue with numpy.linalg.inv on some platforms when numpy version < 2.0.0. See: https://github.com/pygfx/pygfx/issues/763 The python method is slower than the numpy method, but it is guaranteed to work.

When the matrix is singular, it will return a matrix filled with zeros, This is a common behavior in real-time graphics applications.

pylinalg.mat_look_at(eye, target, up_reference, /, *, out=None, dtype=None) ndarray

Rotation that aligns two vectors.

Given an entity at position eye looking at position target, this function computes a rotation matrix that makes the local frame “look at” the same direction, i.e., the matrix will rotate the local frame’s z-axes (forward) to point in direction target - eye.

This rotation matrix is not unique (yet), as the above doesn’t specify the desired rotation around the new z-axis. The rotation around this axis is controlled by up_reference, which indicates the direction of the y-axis (up) of a reference frame of choice expressed in local coordinates. The rotation around the new z-axis will then align up_reference, the new y-axis, and the new z-axis in the same plane.

In many cases, a natural choice for up_reference is the world frame’s y-axis, i.e., up_reference would be the world’s y-axis expressed in local coordinates. This can be thought of as “gravity pulling on the rotation” (opposite direction of world frame’s up) and will create a result with a level attitude.

Parameters:
  • eye (ndarray, [3]) – A vector indicating the direction that should be aligned.

  • target (ndarray, [3]) – A vector indicating the direction to align on.

  • up (ndarray, [3]) – The direction of the camera’s up axis.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

rotation_matrix – A homogeneous matrix describing the rotation.

Return type:

ndarray, [4, 4]

Notes

If the new z-axis (target - eye) aligns with the chosen up_reference then we can’t compute the angle of rotation around the new z-axis. In this case, we will default to a rotation of 0, which may result in surprising behavior for some use-cases. It is the user’s responsibility to ensure that these two directions don’t align.

pylinalg.mat_orthographic(left, right, top, bottom, near, far, /, *, depth_range=(-1, 1), out=None, dtype=None) ndarray

Create an orthographic projection matrix.

The result projects points from local space into NDC (normalized device coordinates). Elements inside the viewing frustum defind by left, right, top, bottom, near, far, are projected into the unit cube centered at the origin (default) or a cuboid (custom depth_range). The frustum is centered around the local frame’s origin.

Parameters:
  • left (ndarray, [1]) – Distance between the left frustum plane and the origin

  • right (ndarray, [1]) – Distance between the right frustum plane and the origin

  • top (ndarray, [1]) – Distance between the top frustum plane and the origin

  • bottom (ndarray, [1]) – Distance between the bottom frustum plane and the origin

  • near (ndarray, [1]) – Distance between the near frustum plane and the origin

  • far (ndarray, [1]) – Distance between the far frustum plane and the origin

  • depth_range (ndarray, [2]) – The interval along the z-axis in NDC that shall correspond to the region inside the viewing frustum.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

matrix – orthographic projection matrix

Return type:

ndarray, [4, 4]

Notes

The parameters to this function are given in a left-handed frame that is obtained by mirroring source’s Z-axis at the origin. In other words, if the returned matrix represents a camera’s projection matrix then this function’s parameters are given in a frame that is like the camera’s local frame except that it’s Z-axis is inverted. This means that positive values for near and far refer to a negative Z values in camera local.

pylinalg.mat_perspective(left, right, top, bottom, near, far, /, *, depth_range=(-1, 1), out=None, dtype=None) ndarray

Create a perspective projection matrix.

Parameters:
  • left (number) – distance between the left frustum plane and the origin

  • right (number) – distance between the right frustum plane and the origin

  • top (number) – distance between the top frustum plane and the origin

  • bottom (number) – distance between the bottom frustum plane and the origin

  • near (number) – distance between the near frustum plane and the origin

  • far (number) – distance between the far frustum plane and the origin

  • depth_range (Tuple[float, float]) – The interval along the z-axis in NDC that shall correspond to the region inside the viewing frustum.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

matrix – perspective projection matrix

Return type:

ndarray, [4, 4]

pylinalg.quat_from_axis_angle(axis, angle, /, *, out=None, dtype=None) ndarray

Quaternion from axis-angle pair.

Create a quaternion representing the rotation of an given angle about a given unit vector

Parameters:
  • axis (ndarray, [3]) – Unit vector

  • angle (number) – The angle (in radians) to rotate about axis

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Quaternion.

Return type:

ndarray, [4]

pylinalg.quat_from_euler(angles, /, *, order='xyz', out=None, dtype=None) ndarray

Create a quaternion from euler angles.

Parameters:
  • angles (ndarray, [3]) – A set of XYZ euler angles.

  • order (string, optional) – The rotation order as a string. Can include ‘X’, ‘Y’, ‘Z’ for intrinsic rotation (uppercase) or ‘x’, ‘y’, ‘z’ for extrinsic rotation (lowercase). Default is “xyz”.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

quaternion – The rotation expressed as a quaternion.

Return type:

ndarray, [4]

pylinalg.quat_from_mat(matrix, /, *, out=None, dtype=None) ndarray

Make a quaternion given a rotation matrix.

Parameters:
  • matrix (ndarray, [3]) – The rotation matrix.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Quaternion.

Return type:

ndarray, [4]

pylinalg.quat_from_vecs(source, target, /, *, out=None, dtype=None) ndarray

Rotate one vector onto another.

Create a quaternion that rotates source onto target.

Parameters:
  • source (ndarray, [3]) – The vector that should be rotated.

  • target (ndarray, [3]) – The vector that will be rotated onto.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Quaternion.

Return type:

ndarray, [4]

Notes

Among all the possible rotations that send source onto target this function always chooses the right-hand rotation around the origin. In cases where more than one right-hand rotation around the origin exists (source and target are parallel), an arbitrary one is returned.

While this function is intended to be used with unit vectors, it also works on non-unit vectors in which case the returned rotation will point source in the direction of target.

pylinalg.quat_inv(quaternion, /, *, out=None, dtype=None) ndarray

Inverse of a given quaternion

Parameters:
  • a (ndarray, [3]) – First unit vector

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Quaternion.

Return type:

ndarray, [4]

pylinalg.quat_mul(a, b, /, *, out=None, dtype=None) ndarray

Multiply two quaternions

Parameters:
  • a (ndarray, [4]) – Left-hand quaternion

  • b (ndarray, [4]) – Right-hand quaternion

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

Quaternion.

Return type:

ndarray, [4]

pylinalg.quat_to_axis_angle(quaternion, /, *, out=None, dtype=None) ndarray

Convert a quaternion to axis-angle representation.

Parameters:
  • quaternion (ndarray, [4]) – A quaternion describing the rotation.

  • out (Tuple[ndarray, ...], optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

  • axis (ndarray, [3]) – The axis around which the quaternion rotates in euclidean coordinates.

  • angle (ndarray, [1]) – The angle (in rad) by which the quaternion rotates.

Notes

To use out with a single quaternion you need to provide a ndarray of shape (1,) for angle.

pylinalg.quat_to_euler(quaternion, /, *, order='xyz', epsilon=1e-07, out=None, dtype=None) ndarray

Convert quaternions to Euler angles with specified rotation order.

Parameters:
  • quaternion (ndarray, [4]) – The quaternion to convert (in xyzw format).

  • order (str, optional) – The rotation order as a string. Can include ‘X’, ‘Y’, ‘Z’ for intrinsic rotation (uppercase) or ‘x’, ‘y’, ‘z’ for extrinsic rotation (lowercase). Default is “xyz”.

  • epsilon (float, optional) – The floating point error margin. Default is 1e-7.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

out – The Euler angles in the specified order.

Return type:

ndarray, [3]

References

This implementation is based on the method described in the following paper: Bernardes E, Viollet S (2022) Quaternion to Euler angles conversion: A direct, general and computationally efficient method. PLoS ONE 17(11): e0276302. https://doi.org/10.1371/journal.pone.0276302

pylinalg.vec_angle(vector_a, vector_b, /, *, out=None, dtype=None) ndarray

The angle between two vectors

Parameters:
  • vector_a (ndarray, [3]) – The first vector.

  • vector_b (ndarray, [3]) – The second vector.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

angle – The angle between both vectors.

Return type:

ndarray

pylinalg.vec_dist(vector_a, vector_b, /, *, out=None, dtype=None) ndarray

The distance between two vectors

Parameters:
  • vector_a (ndarray, [3]) – The first vector.

  • vector_b (ndarray, [3]) – The second vector.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

distance – The distance between both vectors.

Return type:

ndarray

pylinalg.vec_euclidean_to_spherical(euclidean, /, *, out=None, dtype=None) ndarray

Convert euclidean -> spherical coordinates

Parameters:
  • euclidean (ndarray, [3]) – A vector in euclidean coordinates.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

spherical – A vector in spherical coordinates (r, phi, theta).

Return type:

ndarray, [3]

pylinalg.vec_homogeneous(vectors, /, *, w=1, out=None, dtype=None) ndarray

Append homogeneous coordinates to vectors.

Parameters:
  • vectors (array_like, [..., 3]) – array of vectors

  • w (number, optional, default is 1) – the value for the homogeneous dimensionality. this affects the result of translation transforms. use 0 (vectors) if the translation component should not be applied, 1 (positions) otherwise.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

The list of vectors with appended homogeneous value.

Return type:

ndarray, […, 4]

pylinalg.vec_normalize(vectors, /, *, out=None, dtype=None) ndarray

Normalize an array of vectors.

Parameters:
  • vectors (array_like, [..., 3]) – array of vectors

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

array of normalized vectors.

Return type:

ndarray, […, 3]

pylinalg.vec_spherical_safe(vector, /, *, out=None, dtype=None) ndarray

Normalize sperhical coordinates.

Normalizes a vector of spherical coordinates to restrict phi to [0, pi) and theta to [0, 2pi).

Parameters:
  • vector (ndarray, [3]) – A vector in spherical coordinates.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

normalized_vector – A vector in spherical coordinates with restricted angle values.

Return type:

ndarray, [3]

pylinalg.vec_spherical_to_euclidean(spherical, /, *, out=None, dtype=None) ndarray

Convert spherical -> euclidean coordinates.

Parameters:
  • spherical (ndarray, [3]) – A vector in spherical coordinates (r, phi, theta). Phi and theta are measured in radians.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

euclidean – A vector in euclidean coordinates.

Return type:

ndarray, [3]

Notes

This implementation follows pygfx’s coordinate conventions. This means that the positive y-axis is the zenith reference and the positive z-axis is the azimuth reference. Angles are measured counter-clockwise.

pylinalg.vec_transform(vectors, matrix, /, *, w=1, projection=True, out=None, dtype=None) ndarray

Apply a transformation matrix to a vector.

Parameters:
  • vectors (ndarray, [3]) – Array of vectors

  • matrix (ndarray, [4, 4]) – Transformation matrix

  • w (ndarray, [1], optional) – The value of the scale component of the homogeneous coordinate. This affects the result of translation transforms. use 0 (vectors) if the translation component should not be applied, 1 (positions) otherwise.

  • projection (bool, optional) – If False, the matrix is assumed to be purely affine and the homogeneous component is not applied. Default is True.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

transformed vectors

Return type:

ndarray, [3]

pylinalg.vec_transform_quat(vector, quaternion, /, *, out=None, dtype=None) ndarray

Rotate a vector using a quaternion.

Parameters:
  • vector (ndarray, [3]) – The vector to be rotated.

  • quaternion (ndarray, [4]) – The quaternion to apply (in xyzw format).

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

rotated_vector – The rotated vector.

Return type:

ndarray, [3]

pylinalg.vec_unproject(vector, matrix, /, *, matrix_is_inv=False, depth=0, out=None, dtype=None) ndarray

Un-project a vector from 2D space to 3D space.

Find a vectorB in 3D euclidean space such that the projection matrix @ vectorB yields the provided vector (in 2D euclidean space). Since the solution to the above is a 1D subspace of 3D space (a line), depth is used to select a single vector within.

Parameters:
  • vector (ndarray, [2]) – The vector to be un-projected.

  • matrix (ndarray, [4, 4]) – The camera’s intrinsic matrix.

  • matrix_is_inv (bool, optional) – Default is False. If True, the provided matrix is assumed to be the inverse of the camera’s intrinsic matrix.

  • depth (number, optional) – The distance of the unprojected vector from the camera.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have length equal to the number of outputs.

  • dtype (data-type, optional) – Overrides the data type of the result.

Returns:

projected_vector – The unprojected vector in 3D space

Return type:

ndarray, [3]

Notes

The source frame of this operation is the XY-plane of the camera’s NDC frame and the target frame is the camera’s local frame.