transforms3d

repository·main·Indexed 19 days ago

https://github.com/matthew-brett/transforms3d

A Python library for 3D coordinate transformations, providing tools for rotations, translations, zooms, shears, and reflections. It supports converting between various representations, including 3x3 rotation matrices, Euler angles, quaternions, axis-angle, and 4x4 affine matrices. The library includes specialized submodules such as taitbryan, affines, quaternions, euler, axangles, reflections, shears, and zooms, and provides utilities for the composition and decomposition of affine matrices.

Tokens
12.5K
Snippets
56
Records
66
Agent score
68%

What's inside transforms3d

  1. Overview of Transforms3d capabilities

    main

    Transforms3d is a library for converting between various geometric transformations. It provides tools for:

    • Composition: Combining rotations, zooms, shears, and translations into an affine matrix.
    • Decomposition: Breaking down an affine matrix into its constituent rotations, zooms, shears, and translations.
    • Rotation Conversions: Converting between different rotation representations, specifically:
      • 3x3 Rotation matrices
      • Euler angles
      • Quaternions
  2. Understand 3D transformation representations

    main

    The transforms3d library works with several mathematical representations of 3D transformations. Understanding these is key to choosing the right API calls for your use case:

    • Affine Matrix: A 4x4 matrix used to implement affine transformations in homogenous coordinates. An affine transformation consists of a linear transformation (like rotation, scaling, or shear) followed by a translation.
    • Rotation Matrix: A square, orthogonal matrix used for rotations. It has a determinant of 1 and its transpose is equal to its inverse ($R^T = R^{-1}$).
    • Quaternion: A 4-value representation ($w, x, y, z$) where $w$ is the real part and $x, y, z$ is the vector part. Quaternions are preferred for rapid interpolation of rotations and avoid the problem of Gimbal lock.
    • Axis-Angle: A rotation defined by a unit vector axis $\boldsymbol{\hat{u}}$ and an angle $\theta$.
    • Rotation Vector: A compact representation of axis-angle where the direction of the vector is the axis and its Euclidean norm is the angle $\theta$.
    • Euler Angles: A sequence of three rotations around specific axes. Note that these can lead to Gimbal lock.
    • Shear Matrix: A square matrix used to perform shearing transformations.
  3. Understand transforms3d naming conventions for mathematical objects

    main

    The transforms3d library uses specific abbreviations in its API to represent common 3D transformation concepts and their underlying data structures. When calling functions or inspecting outputs, recognize these abbreviations to understand the expected shapes and mathematical representations:

    Matrices

    • aff: A 4x4 affine matrix used for operating on homogeneous coordinates. Expected shapes are (4,) or (4, N).
    • mat: A 3x3 transformation matrix used for operating on non-homogeneous coordinate vectors. Expected shapes are (3,) or (3, N). A rotation matrix is a common example of a mat.

    Rotations

    • euler: Euler angles, represented as a sequence of three scalars defining rotations about specific axes.
    • axangle: Axis-angle representation, consisting of an axis (vector) and an angle (scalar).
    • quat: A quaternion, represented as a shape (4,) array.

    Other Transformations

    • rfnorm: Reflection in a plane defined by a normal (vector) and an optional point (vector).
    • zfdir: Zooms encoded by a factor (scalar) and a direction (vector).
    • striu: Shears encoded by a vector representing the triangular portion above the diagonal of an $N \times N$ array (used for ND transformations).
    • sadn: Shears encoded by an angle (scalar), a direction (vector), and a normal (vector, with an optional point vector).
  4. Mathematical definitions used in transforms3d

    main

    The following mathematical concepts are fundamental to the library's operations:

    • Euclidean Norm (L2 Norm): The length of a vector $\mathbf{x}$, calculated as $\sqrt{x_1^2 + \cdots + x_n^2}$.
    • Unit Vector: A vector with a Euclidean norm of 1. Also referred to as a normalized vector.
    • Linear Transformation: A transformation that preserves lines (e.g., rotation, scaling, and shear).
    • Homogenous Coordinates: A coordinate system used to represent affine transformations (including translation) as matrix multiplications.
    • Reflection: A transformation that creates a mirror image of an object across a plane defined by a point and a normal vector.
  5. Understand the matrix multiplication convention for transforms

    main

    The transforms3d library follows a specific convention for applying transformations to coordinates.

    1. Matrix Application: Transformation matrices are applied to the left of the coordinate vectors.
    2. Vector Storage: Coordinates are stored as column vectors.
    3. Point Matrices: When transforming a set of $N$ points, they are organized into a single matrix $P$ where each column represents a point $(x, y, z)$.

    To transform a matrix of points $P$ using a $3\times3$ transformation matrix $M$, the operation is:

    $v^\prime = M \cdot P$

    This means the resulting matrix $v^\prime$ contains the transformed coordinates in its columns.

       P = \left( \begin{matrix} 
         x^1, x^2, \ldots, x^N \\
         y^1, y^2, \ldots, y^N \\
         z^1, z^2, \ldots, z^N \\
         \end{matrix} \right)
    
       v^\prime = M \cdot P
  6. Understanding Gimbal Lock in Euler Angles

    main

    Gimbal lock is a deficiency in Euler angle rotation sequences where two of the three rotation axes align, causing a loss of one degree of freedom. In a sequence like $x ightarrow y ightarrow z$, if the $y$ axis rotation reaches $\pm\pi/2$ radians, the $x$ and $z$ axes become indistinguishable. This means any rotation around the $z$ axis has the exact same effect as an additional rotation around the $x$ axis.

    For the common 'sxyz' (intrinsic $x, y, z$) convention, gimbal lock occurs when $\cos(\beta) = 0$, where $\beta$ is the angle of rotation around the $y$ axis.

  7. Build the transforms3d documentation

    main

    The documentation for transforms3d is built using Sphinx. To build the documentation locally, you must have Sphinx v1.0 or greater installed.

    Follow these steps:

    1. Install the necessary documentation dependencies using the requirements file located in the parent directory.
    2. Use the provided Makefile to generate the HTML documentation.
    # Install dependencies
    pip install -r ../doc-requirements.txt
    
    # Build HTML documentation
    make html
  8. Verify installation and run tests in a virtualenv

    main

    Before releasing, ensure the package installs correctly by performing a clean installation into a fresh virtual environment and running the test suite.

    1. Create and activate a virtual environment.
    2. Install testing dependencies (pytest, wheel).
    3. Clean the repository of build artifacts.
    4. Install the package using setup.py.
    5. Run pytest with --doctest-modules to verify functionality and documentation examples.
    mkvirtualenv transforms3d-test
    pip install pytest wheel
    git clean -fxd
    python setup.py install
    mkdir for_test
    cd for_test
    pytest --doctest-modules transforms3d
  9. Handling Gimbal Lock in Code

    main

    When gimbal lock occurs, multiple combinations of Euler angles can produce the same rotation matrix. For example, in an 'sxyz' sequence where the $y$ angle is $-\pi/2$, you can achieve the same transformation by adding a value to the $x$ angle and subtracting it from the $z$ angle, or by combining them into a single $x$ rotation and setting $z$ to 0.

    To avoid the mathematical ambiguity of gimbal lock, you can:

    1. Change the rotation order: Use a different sequence (e.g., 'sxzy' instead of 'sxyz') so the axes do not align in the same way.
    2. Recalculate angles: Use mat2euler to find a new set of Euler angles that represent the same rotation matrix without the lock-induced ambiguity.
    import numpy as np
    from transforms3d.euler import euler2mat, mat2euler
    
    # Example of gimbal lock scenario
    x_angle = -0.2
    y_angle = -np.pi / 2
    z_angle = -0.2
    
    # This matrix is subject to gimbal lock
    R = euler2mat(x_angle, y_angle, z_angle, 'sxyz')
    
    # You can achieve the same R by combining x and z:
    R_dash = euler2mat(x_angle + z_angle, y_angle, 0, 'sxyz')
    print(np.allclose(R, R_dash))  # Returns True
    
    # To get a 'clean' representation, convert the matrix back to Euler angles
    x_dash, y_dash, z_dash = mat2euler(R, 'sxyz')
    print(np.array((x_dash, y_dash, z_dash)))